RenderTexture 橡皮檫 電筒效果

參考文章:http://zengrong.net/post/2067.htm點擊打開鏈接

1.準備資源

背景圖 background.jpg


遮蓋圖 HelloWorld.png


橡皮檫圖片 test1.png 中間透明的圖。  BlendFunc爲 cc.blendFunc(GL_ZERO, GL_SRC_ALPHA)

如果是實心外透明  BlendFunc爲 cc.blendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA)

這裏最主要還是對glBlendFunc的知識的運用,如果不想知道原理可以無視。



2. code

	local background = cc.Sprite:create("background.jpg")
		:pos(display.cx,display.cy)
		:addTo(self)
	background:scale(display.height / background:getContentSize().height)

	local dirt = cc.Sprite:create("HelloWorld.png")
		:pos(display.cx,display.cy)

	dirt:scale(display.width / dirt:getContentSize().width)
	dirt:retain();

	local eraser = cc.Sprite:create("test1.png")
		:pos(display.cx, display.cy)
		:scale(10)

	eraser:retain()
	eraser:setBlendFunc(cc.blendFunc(GL_ZERO, GL_SRC_ALPHA));

	local renderTexture = cc.RenderTexture:create(display.width,display.height)
		:pos(display.cx,display.cy)
		:addTo(self)

	renderTexture:begin();
	dirt:visit();
	renderTexture:endToLua();	--todo


	self:onTouch(function(event) 
		eraser:pos(event.x, event.y)
		renderTexture:begin();
		-- dirt:visit(); --電筒效果
		eraser:visit();
		renderTexture:endToLua();	--todo
		return true;
	end):enableTouch(true)


3.效果圖 電筒 打開onTouch裏的dirt:visit 這個註釋

4.效果圖 橡皮檫 註釋onTouch 裏的dirt:visit 


發佈了54 篇原創文章 · 獲贊 56 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章