溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Quick-cocos(3.2) 將對象(包括子對象)變灰的方法

發(fā)布時間:2020-07-10 18:08:31 來源:網(wǎng)絡(luò) 閱讀:1574 作者:evanstone 欄目:游戲開發(fā)

先上效果圖:

Quick-cocos(3.2) 將對象(包括子對象)變灰的方法


借鑒了[Cocos2d-x 讓精靈圖像變灰的方法]。


但這個方法在Quick-Cocos3.2下不能完美實現(xiàn)變灰效果-變灰了的對象的位置會跳到屏幕右上角。

百思不得其解,搜一下有沒有人發(fā)現(xiàn)這個問題,果然有:

[關(guān)于Sprite的setShaderProgram后坐標(biāo)改變的問題]

發(fā)現(xiàn)4樓的仁兄的回復(fù)有亮點:

[如何在Cocos2d-x 3.0中使用opengl shader?]

點進(jìn)去一看,內(nèi)容是這樣的:

“坐標(biāo)變化的解決了,將附件gray.vsh 中的CC_MVPMatrix 改為 CC_PMatrix 即可 ”


我估計應(yīng)該是位置轉(zhuǎn)換的矩陣問題吧,gray.vsh是什么下面會說到。


經(jīng)過分析,發(fā)現(xiàn)原因在addGray方法([Cocos2d-x 讓精靈圖像變灰的方法])的27行:

pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);


ccPositionTextureColor_vert是什么呢?它存放在cocos/renderer下,名為ccShader_PositionTextureColor.vert。它的作用是……以在下的理解,是一個shader方法(ccShader),關(guān)于位置、材質(zhì)與顏色的(PositionTextureColor)且是針對頂點的(.vert)。

它的內(nèi)容是:

const char* ccPositionTextureColor_vert = STRINGIFY(
attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;

\n#ifdef GL_ES\n
varying lowp vec4 v_fragmentColor;
varying mediump vec2 v_texCoord;
\n#else\n
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
\n#endif\n

void main()
{
    gl_Position = CC_MVPMatrix * a_position;
    v_fragmentColor = a_color;
    v_texCoord = a_texCoord;
}
);


咦,發(fā)現(xiàn)有一個熟悉的面孔-“CC_MVPMatrix”。其實上面說到的gray.vsh的內(nèi)容就是ccShader_PositionTextureColor.vert大括號括住的部分。那我將ccShader_PositionTextureColor.vert的CC_MVPMatrix改為CC_PMatrix是否就能解決灰化后對象的位置問題呢?答案是否定的,這樣改會影響其他對象(例如文本)的定位。

那我再定義一個GLchar傳到pProgram->initWithVertexShaderByteArray的第一個參數(shù)不就得咯?

const GLchar* pszVertSource = 
	"attribute vec4 a_position; \n \
	attribute vec2 a_texCoord; \n \
	attribute vec4 a_color; \n \
	\n#ifdef GL_ES\n \n \
	varying lowp vec4 v_fragmentColor; \n \
	varying mediump vec2 v_texCoord; \n \
	\n#else\n \n \
	varying vec4 v_fragmentColor; \n \
	varying vec2 v_texCoord; \n \
	\n#endif\n \n \
	void main() \n \
	{ \n \
		gl_Position = CC_PMatrix * a_position; \n \
		v_fragmentColor = a_color; \n \
		v_texCoord = a_texCoord; \n \
	}";
  
pProgram->initWithVertexShaderByteArray(pszVertSource, pszFragSource);


經(jīng)實踐證實是可行的。其實有個更簡單的方法,就是:

pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_noMVP_vert, pszFragSource);


原來cocos/renderer下有個文件叫ccShader_PositionTextureColor_noMVP.vert。




最后附上實現(xiàn)灰化功能的全部代碼。


C++部分,將此方法導(dǎo)出給lua用:

void setGray(Node *node)
{
  USING_NS_CC;
  do
  {
  	const GLchar* pszFragSource =
			"#ifdef GL_ES \n \
			precision mediump float; \n \
			#endif \n \
			uniform sampler2D u_texture; \n \
			varying vec2 v_texCoord; \n \
			varying vec4 v_fragmentColor; \n \
			void main(void) \n \
			{ \n \
			// Convert to greyscale using NTSC weightings \n \
			vec4 col = texture2D(u_texture, v_texCoord); \n \
			float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114)); \n \
			gl_FragColor = vec4(grey, grey, grey, col.a); \n \
			}";
      
  	GLProgram* pProgram = new GLProgram();
  	pProgram->initWithByteArrays(ccPositionTextureColor_noMVP_vert, pszFragSource);
  	node->setGLProgram();
  	CHECK_GL_ERROR_DEBUG();
  }while(0);

}


lua部分:

--不進(jìn)行灰化的對象特有的方法
DisplayUtil.LIST_DONT_GRAY = {
	"getSprite", 	--ProgressTimer
	"setString", 	--Label
}

--判斷能否灰化
function DisplayUtil.canGray(node)
	for i,v in ipairs(DisplayUtil.LIST_DONT_GRAY) do
		if node[v] then
			return false
		end
	end
	return true
end

--灰化對象
function DisplayUtil.setGray(node, v)
	if type(node) ~= "userdata" then
		printError("node must be a userdata")
		return
	end
	if v == nil then
		v = true
	end
	if not node.__isGray__ then
		node.__isGray__ = false
	end
	if v == node.__isGray__ then
		return
	end
	if v then
		if DisplayUtil.canGray(node) then
    	--調(diào)用C++的setGray方法
			setGray(tolua.cast(node, "cocos2d::Node"))
			--
			-- local glProgram = node:getGLProgram()
			-- node:setGLProgram(glProgram)
			-- node:getGLProgram():bindAttribLocation(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION)
			-- node:getGLProgram():bindAttribLocation(cc.ATTRIBUTE_NAME_COLOR, cc.VERTEX_ATTRIB_COLOR)
			-- node:getGLProgram():bindAttribLocation(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS)

			--不知道為什么下面2行一定要寫
			node:getGLProgram():link()
			node:getGLProgram():updateUniforms()
		end
		--children
		local children = node:getChildren()
		if children and table.nums(children) > 0 then
			--遍歷子對象設(shè)置
			for i,v in ipairs(children) do
				if DisplayUtil.canGray(v) then
					DisplayUtil.setGray(v)
				end
			end
		end
	else
		DisplayUtil.removeGray(node)
	end
	node.__isGray__ = v
end



--取消灰化
function DisplayUtil.removeGray(node)
	if type(node) ~= "userdata" then
		printError("node must be a userdata")
		return
	end
	if not node.__isGray__ then
		return
	end
	if DisplayUtil.canGray(node) then
		local glProgram = cc.GLProgramCache:getInstance():getGLProgram(
			"ShaderPositionTextureColor_noMVP")
		node:setGLProgram(glProgram)
		-- glProgram:bindAttribLocation(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION)
		-- glProgram:bindAttribLocation(cc.ATTRIBUTE_NAME_COLOR, cc.VERTEX_ATTRIB_COLOR)
		-- glProgram:bindAttribLocation(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS)

		--不知道為什么下面2行不能寫,寫了會出問題
		-- glProgram:link()
		-- glProgram:updateUniforms()
	end
	--children
	local children = node:getChildren()
	if children and table.nums(children) > 0 then
		--遍歷子對象設(shè)置
		for i,v in ipairs(children) do
			if DisplayUtil.canGray(v) then
				DisplayUtil.removeGray(v)
			end
		end
	end
	node.__isGray__ = false
end


怎么使用不用我多說了吧。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI