Difference between revisions of "Pixel:setTexture"

From GiderosMobile
Line 15: Line 15:
 
'''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/>
 
'''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/>
 
'''matrix''': (Matrix) an optional transform for the texture '''optional'''<br/>
 +
 +
=== Example ===
 +
'''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>
  
 
{{Pixel}}
 
{{Pixel}}

Revision as of 08:15, 9 November 2023

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 conjonction 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

Example

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)