Difference between revisions of "Sprite:setStencilOperation"
From GiderosMobile
|  (added example) | |||
| Line 1: | Line 1: | ||
| __NOTOC__ | __NOTOC__ | ||
| + | |||
| <languages /> | <languages /> | ||
| − | '''<translate>Available since</translate>:''' Gideros 2017.6<br/> | + | |
| − | '''<translate>Class</translate>:''' [[Special:MyLanguage/Sprite|Sprite]]<br/> | + | '''<translate>Available since</translate>:''' Gideros 2017.6 | 
| + | <br/> | ||
| + | |||
| + | '''<translate>Class</translate>:''' [[Special:MyLanguage/Sprite|Sprite]] | ||
| + | <br/> | ||
| + | |||
| === <translate>Description</translate> === | === <translate>Description</translate> === | ||
| − | <translate>Allows to set a stencil operation to be used when drawing this sprite.<br /> | + | <translate> | 
| − |      The table can contain the following fields: | + | Allows to set a stencil operation to be used when drawing this sprite. | 
| + | <br/> | ||
| + |      The table can contain the following fields: | ||
|      <ul> |      <ul> | ||
|      <li><b>stencilClear</b>: (boolean) whether the stencil should be cleared beforehand.</li> |      <li><b>stencilClear</b>: (boolean) whether the stencil should be cleared beforehand.</li> | ||
| Line 15: | Line 23: | ||
|      <li><b>depthFail</b>: (integer) the stencil operation when depth test failed.</li> |      <li><b>depthFail</b>: (integer) the stencil operation when depth test failed.</li> | ||
|      <li><b>depthPass</b>: (integer) the stencil operation when depth test has passed.</li> |      <li><b>depthPass</b>: (integer) the stencil operation when depth test has passed.</li> | ||
| − |      </ul><br /> | + |      </ul> | 
| − | + | <br /> | |
| + | Stencil function and operations code are defined in Sprite.STENCIL_xxx fields and correspond to the GL_xxx relevant values in OpenGL stencil documentation. | ||
| + | </translate> | ||
| + | |||
| <source lang="lua"> | <source lang="lua"> | ||
| − | + | Sprite:setStencilOperation(op) | |
| </source> | </source> | ||
| + | |||
| === <translate>Parameters</translate> === | === <translate>Parameters</translate> === | ||
| − | '''op''': (table) <translate>A table containing the stencil operation settings, or nil to disable stencil</translate> <br/> | + | '''op''': (table) <translate>A table containing the stencil operation settings, or nil to disable stencil</translate> | 
| + | |||
| + | === <translate>Examples</translate> === | ||
| + | '''Example''': | ||
| + | |||
| + | <source lang="lua"> | ||
| + | -- STENCILOPERATION @hgy29 | ||
| + | local bg = Bitmap.new(Texture.new("gfx/vip/background.png")) | ||
| + | local ticket = Bitmap.new(Texture.new("gfx/vip/ticket.png")) | ||
| + | local text = Bitmap.new(Texture.new("gfx/vip/text.png")) | ||
| + | local fx1 = Bitmap.new(Texture.new("gfx/vip/fx1.png")) | ||
| + | local fx2 = Bitmap.new(Texture.new("gfx/vip/fx2.png")) | ||
| + | |||
| + | stage:addChild(bg) bg:setScale(0.5) | ||
| + | stage:addChild(ticket) ticket:setPosition(80,20) ticket:setScale(0.5) | ||
| + | ticket:addChild(fx1) fx1:setX(-100) | ||
| + | fx1:addChild(fx2) fx2:setX(-100) | ||
| + | ticket:addChild(text) | ||
| + | |||
| + | -- Use auto layout on ticket to center text automatically | ||
| + | ticket:setLayoutParameters{ rowWeights = {1}, columnWeights = {1} } | ||
| + | text:setLayoutConstraints{} | ||
| + | |||
| + | -- Use stencil for masking glow: | ||
| + | -- 1) Clear stencil and set it to '1' for every pixel drawn on 'ticket' | ||
| + | ticket:setStencilOperation{ | ||
| + | 	stencilClear = true, stenciMask = 1, stencilWriteMask = 1, stencilRef = 1, | ||
| + | 	stencilFunc = Sprite.STENCIL_ALWAYS, depthPass = Sprite.STENCIL_REPLACE | ||
| + | } | ||
| + | --2) Only draw fx1/fx2 if stencil isn't 0 | ||
| + | fx1:setStencilOperation{ | ||
| + | 	stencilClear = false, stencilMask = 1, stencilRef = 0, | ||
| + | 	stencilFunc = Sprite.STENCIL_NOTEQUAL | ||
| + | } | ||
| + | |||
| + | local dir = 1 | ||
| + | stage:addEventListener(Event.ENTER_FRAME, function() | ||
| + | 	if fx1:getX() > 450 then | ||
| + | 		dir = -1 | ||
| + | 	elseif fx1:getX() < -100 then | ||
| + | 		dir = 1 | ||
| + | 	end | ||
| + | 	fx1:setX(fx1:getX() + dir*5) | ||
| + | end) | ||
| + | </source> | ||
| + | <br/> | ||
| + | <br/> | ||
Revision as of 09:02, 16 September 2019
Available since: Gideros 2017.6
Class: Sprite
Description
Allows to set a stencil operation to be used when drawing this sprite.
The table can contain the following fields:
- stencilClear: (boolean) whether the stencil should be cleared beforehand.
- stencilMask: (integer) the mask value used in stencil operations.
- stencilWriteMask: (integer) the mask value used when writing to stencil.
- stencilRef: (integer) the reference value used in stencil operations.
- stencilFunc: (integer) the stencil function to use.
- stencilFail: (integer) the stencil operation when stencil test failed.
- depthFail: (integer) the stencil operation when depth test failed.
- depthPass: (integer) the stencil operation when depth test has passed.
Stencil function and operations code are defined in Sprite.STENCIL_xxx fields and correspond to the GL_xxx relevant values in OpenGL stencil documentation.
Sprite:setStencilOperation(op)
Parameters
op: (table) A table containing the stencil operation settings, or nil to disable stencil
Examples
Example:
-- STENCILOPERATION @hgy29
local bg = Bitmap.new(Texture.new("gfx/vip/background.png"))
local ticket = Bitmap.new(Texture.new("gfx/vip/ticket.png"))
local text = Bitmap.new(Texture.new("gfx/vip/text.png"))
local fx1 = Bitmap.new(Texture.new("gfx/vip/fx1.png"))
local fx2 = Bitmap.new(Texture.new("gfx/vip/fx2.png"))
 
stage:addChild(bg) bg:setScale(0.5)
stage:addChild(ticket) ticket:setPosition(80,20) ticket:setScale(0.5)
ticket:addChild(fx1) fx1:setX(-100)
fx1:addChild(fx2) fx2:setX(-100)
ticket:addChild(text)
 
-- Use auto layout on ticket to center text automatically
ticket:setLayoutParameters{ rowWeights = {1}, columnWeights = {1} }
text:setLayoutConstraints{}
-- Use stencil for masking glow:
-- 1) Clear stencil and set it to '1' for every pixel drawn on 'ticket'
ticket:setStencilOperation{
	stencilClear = true, stenciMask = 1, stencilWriteMask = 1, stencilRef = 1,
	stencilFunc = Sprite.STENCIL_ALWAYS, depthPass = Sprite.STENCIL_REPLACE
}
--2) Only draw fx1/fx2 if stencil isn't 0
fx1:setStencilOperation{
	stencilClear = false, stencilMask = 1, stencilRef = 0,
	stencilFunc = Sprite.STENCIL_NOTEQUAL
}
 
local dir = 1
stage:addEventListener(Event.ENTER_FRAME, function()
	if fx1:getX() > 450 then
		dir = -1
	elseif fx1:getX() < -100 then
		dir = 1
	end
	fx1:setX(fx1:getX() + dir*5)
end)
