Pixel:setTexture: Difference between revisions
From GiderosMobile
mNo edit summary |
|||
| (11 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
'''Available since:''' Gideros 2016.12<br/> | |||
''' | '''Class:''' [[Pixel]]<br/> | ||
''' | |||
=== | === Description === | ||
Associates a texture to the Pixel. | |||
< | <syntaxhighlight lang="lua"> | ||
Pixel:setTexture(texture,slot,matrix) | |||
</ | </syntaxhighlight> | ||
=== | |||
'''texture''': (TextureBase or | '''Please note: slot is only useful in conjunction with Shader''' | ||
'''slot''': (number) | |||
'''matrix''': (Matrix) | === Parameters === | ||
'''texture''': (TextureBase or TextureRegion) the texture to associate to the pixel, or nil to deassociate<br/> | |||
'''slot''': (number) the texture slot that the texture should be associated to. Leave empty or set to 0 for main texture, or if you don't use a specific shader '''optional'''<br/> | |||
'''matrix''': (Matrix) an optional transform for the texture '''optional'''<br/> | |||
=== Examples === | |||
'''2 textures masked in a Pixel''' | |||
<syntaxhighlight lang="lua"> | |||
--!NEEDS:luashader/luashader.lua | |||
application:setBackgroundColor(0x909090) | |||
function vshaderpixslot(vVertex,vColor,vTexCoord) : Shader | |||
local vertex = hF4(vVertex,0.0,1.0) | |||
fTexCoord = vTexCoord | |||
return vMatrix*vertex | |||
end | |||
function fshaderpixslot() : Shader | |||
local frag = texture2D(fTexture, fTexCoord) | |||
local frag2 = texture2D(fTexture2, fTexCoord) | |||
return frag * frag2 -- mask | |||
end | |||
local shaderpixslot=Shader.lua(vshaderpixslot,fshaderpixslot,0, | |||
{ | |||
{name="vMatrix", type=Shader.CMATRIX, sys=Shader.SYS_WVP, vertex=true}, | |||
{name="fTexture", type=Shader.CTEXTURE, vertex=false}, | |||
{name="fTexture2", type=Shader.CTEXTURE, vertex=false}, | |||
}, | |||
{ | |||
{name="vVertex", type=Shader.DFLOAT, mult=2, slot=0, offset=0}, | |||
{name="vColor", type=Shader.DUBYTE, mult=4, slot=1, offset=0}, | |||
{name="vTexCoord", type=Shader.DFLOAT, mult=2, slot=2, offset=0}, | |||
}, | |||
{ | |||
{name="fTexCoord", type=Shader.CFLOAT2}, | |||
} | |||
) | |||
local tex=Texture.new("gfx/test.png") | |||
local tex2=Texture.new("gfx/arrow_0001.png") | |||
local texw, texh = tex:getWidth(), tex:getHeight() | |||
local px = Pixel.new(nil, texw, texh) | |||
px:setTexture(tex, 0) | |||
px:setTexture(tex2, 1) | |||
px:setShader(shaderpixslot) | |||
stage:addChild(px) | |||
</syntaxhighlight> | |||
'''Change Pixel Texture slots on mouse hover''' | |||
<syntaxhighlight lang="lua"> | |||
--!NEEDS:luashader/luashader.lua | |||
-- bg | |||
application:setBackgroundColor(0x00007f) | |||
-- shader | |||
function vshaderpixelslot(vVertex, vColor, vTexCoord) : Shader | |||
local vertex = hF4(vVertex, 0.0, 1.0) | |||
fTexCoord = vTexCoord | |||
return vMatrix*vertex | |||
end | |||
function fshaderpixelslot() : Shader | |||
local frag = texture2D(fTexture, fTexCoord) | |||
local frag2 = texture2D(fTexture2, fTexCoord) | |||
if slot == 0 then return frag | |||
else return frag2 | |||
end | |||
end | |||
local shaderpixelslot=Shader.lua(vshaderpixelslot, fshaderpixelslot, 0, | |||
{ | |||
{name="vMatrix", type=Shader.CMATRIX, sys=Shader.SYS_WVP, vertex=true}, | |||
{name="fTexture", type=Shader.CTEXTURE, vertex=false}, | |||
{name="fTexture2", type=Shader.CTEXTURE, vertex=false}, | |||
{name="slot", type=Shader.CINT, vertex=false}, | |||
}, | |||
{ | |||
{name="vVertex", type=Shader.DFLOAT, mult=2, slot=0, offset=0}, | |||
{name="vColor", type=Shader.DUBYTE, mult=4, slot=1, offset=0}, | |||
{name="vTexCoord", type=Shader.DFLOAT, mult=2, slot=2, offset=0}, | |||
}, | |||
{ | |||
{name="fTexCoord", type=Shader.CFLOAT2}, | |||
} | |||
) | |||
-- textures | |||
local texbtnup=Texture.new("gfx/ui/btn_01_up.png") | |||
local texbtndown=Texture.new("gfx/ui/btn_01_down.png") | |||
-- objects | |||
local px = Pixel.new(texbtnup) | |||
-- params | |||
px:setTexture(texbtndown, 1) -- texture, slot | |||
px:setShader(shaderpixelslot) | |||
-- position | |||
px:setPosition(8*4, 8*16) | |||
-- order | |||
stage:addChild(px) | |||
-- listeners | |||
local onenter, moving, onexit | |||
px:addEventListener(Event.MOUSE_HOVER, function(ev) | |||
if px:hitTestPoint(ev.x, ev.y, true) then -- enter | |||
-- execute code only once * | |||
onenter = not onenter | |||
if not onenter then moving = true end | |||
if not moving then | |||
-- execute action only once | |||
px:setShaderConstant("slot", Shader.CINT, 1, 1) -- button Down texture | |||
else | |||
onexit = true | |||
end | |||
ev:stopPropagation() | |||
else -- exit | |||
onenter = false | |||
moving = false | |||
if onexit then | |||
-- execute exit action only once | |||
onexit = false | |||
px:setShaderConstant("slot", Shader.CINT, 1, 0) -- button Up texture | |||
end | |||
end | |||
end) | |||
</syntaxhighlight> | |||
{{Pixel}} | |||
Latest revision as of 00:03, 23 March 2025
Available since: Gideros 2016.12
Class: Pixel
Description
Associates a texture to the Pixel.
Pixel:setTexture(texture,slot,matrix)
Please note: slot is only useful in conjunction with Shader
Parameters
texture: (TextureBase or TextureRegion) the texture to associate to the pixel, or nil to deassociate
slot: (number) the texture slot that the texture should be associated to. Leave empty or set to 0 for main texture, or if you don't use a specific shader optional
matrix: (Matrix) an optional transform for the texture optional
Examples
2 textures masked in a Pixel
--!NEEDS:luashader/luashader.lua
application:setBackgroundColor(0x909090)
function vshaderpixslot(vVertex,vColor,vTexCoord) : Shader
local vertex = hF4(vVertex,0.0,1.0)
fTexCoord = vTexCoord
return vMatrix*vertex
end
function fshaderpixslot() : Shader
local frag = texture2D(fTexture, fTexCoord)
local frag2 = texture2D(fTexture2, fTexCoord)
return frag * frag2 -- mask
end
local shaderpixslot=Shader.lua(vshaderpixslot,fshaderpixslot,0,
{
{name="vMatrix", type=Shader.CMATRIX, sys=Shader.SYS_WVP, vertex=true},
{name="fTexture", type=Shader.CTEXTURE, vertex=false},
{name="fTexture2", type=Shader.CTEXTURE, vertex=false},
},
{
{name="vVertex", type=Shader.DFLOAT, mult=2, slot=0, offset=0},
{name="vColor", type=Shader.DUBYTE, mult=4, slot=1, offset=0},
{name="vTexCoord", type=Shader.DFLOAT, mult=2, slot=2, offset=0},
},
{
{name="fTexCoord", type=Shader.CFLOAT2},
}
)
local tex=Texture.new("gfx/test.png")
local tex2=Texture.new("gfx/arrow_0001.png")
local texw, texh = tex:getWidth(), tex:getHeight()
local px = Pixel.new(nil, texw, texh)
px:setTexture(tex, 0)
px:setTexture(tex2, 1)
px:setShader(shaderpixslot)
stage:addChild(px)
Change Pixel Texture slots on mouse hover
--!NEEDS:luashader/luashader.lua
-- bg
application:setBackgroundColor(0x00007f)
-- shader
function vshaderpixelslot(vVertex, vColor, vTexCoord) : Shader
local vertex = hF4(vVertex, 0.0, 1.0)
fTexCoord = vTexCoord
return vMatrix*vertex
end
function fshaderpixelslot() : Shader
local frag = texture2D(fTexture, fTexCoord)
local frag2 = texture2D(fTexture2, fTexCoord)
if slot == 0 then return frag
else return frag2
end
end
local shaderpixelslot=Shader.lua(vshaderpixelslot, fshaderpixelslot, 0,
{
{name="vMatrix", type=Shader.CMATRIX, sys=Shader.SYS_WVP, vertex=true},
{name="fTexture", type=Shader.CTEXTURE, vertex=false},
{name="fTexture2", type=Shader.CTEXTURE, vertex=false},
{name="slot", type=Shader.CINT, vertex=false},
},
{
{name="vVertex", type=Shader.DFLOAT, mult=2, slot=0, offset=0},
{name="vColor", type=Shader.DUBYTE, mult=4, slot=1, offset=0},
{name="vTexCoord", type=Shader.DFLOAT, mult=2, slot=2, offset=0},
},
{
{name="fTexCoord", type=Shader.CFLOAT2},
}
)
-- textures
local texbtnup=Texture.new("gfx/ui/btn_01_up.png")
local texbtndown=Texture.new("gfx/ui/btn_01_down.png")
-- objects
local px = Pixel.new(texbtnup)
-- params
px:setTexture(texbtndown, 1) -- texture, slot
px:setShader(shaderpixelslot)
-- position
px:setPosition(8*4, 8*16)
-- order
stage:addChild(px)
-- listeners
local onenter, moving, onexit
px:addEventListener(Event.MOUSE_HOVER, function(ev)
if px:hitTestPoint(ev.x, ev.y, true) then -- enter
-- execute code only once *
onenter = not onenter
if not onenter then moving = true end
if not moving then
-- execute action only once
px:setShaderConstant("slot", Shader.CINT, 1, 1) -- button Down texture
else
onexit = true
end
ev:stopPropagation()
else -- exit
onenter = false
moving = false
if onexit then
-- execute exit action only once
onexit = false
px:setShaderConstant("slot", Shader.CINT, 1, 0) -- button Up texture
end
end
end)