Shader:setConstant: Difference between revisions

From GiderosMobile
Created page with "__NOTOC__ '''Available since:''' Gideros 2015.06.30<br/> === Description === To change the value of a uniform from lua <source lang="lua"> = Shader:setConstant(uniform namedat..."
 
No edit summary
 
(12 intermediate revisions by 3 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
'''Available since:''' Gideros 2015.06.30<br/>
'''Available since:''' Gideros 2015.06.30<br/>
'''Class:''' [[Shader]]<br/>
=== Description ===
=== Description ===
To change the value of a uniform from lua
Changes the value of a uniform from Lua code.
<source lang="lua">
<syntaxhighlight lang="lua">
= Shader:setConstant(uniform namedata typemultdata,)
Shader:setConstant(uniformName,dataType,mult,data)
</source>
</syntaxhighlight>
'''uniform name:''' (string) The uniform name to change ''''''<br/>
 
'''data type:''' (int) The type if data to set (one of the Shader.Cxxx constants) ''''''<br/>
=== Parameters ===
'''mult:''' (number) number of elements of the given type to set ''''''<br/>
'''uniformName''': (string) the uniform name to change<br/>
'''data:''' (varies) And the actual data to set, either as a table or as multiple arguments ''''''<br/>
'''dataType''': (int) the type of data to set (one of the Shader.Cxxx constants)<br/>
'''mult''': (number) number of elements of the given type to set<br/>
'''data''': (varies) and the actual data to set, either as a table or as multiple arguments<br/>
 
=== Examples ===
'''setConstant in a loop'''
<syntaxhighlight lang="lua">
-- loop
local timer = 0
stage:addEventListener(Event.ENTER_FRAME, function(e)
timer += 0.018
shaderwave:setConstant("time", Shader.CFLOAT, 1, timer)
bitmap:setX(bitmap:getX() + 1)
if bitmap:getX() > 400 then bitmap:setX(-80) end
end)
</syntaxhighlight>
 
'''An outline shader setConstant 44 activated'''
<syntaxhighlight lang="lua">
spriteOutline = {}
 
spriteOutline.VS_GL = [[
 
uniform highp mat4 vMatrix;
 
attribute highp vec3 vVertex;
attribute mediump vec2 vTexCoord;
 
varying mediump vec2 fTexCoord;
 
void main() {
vec4 vertex = vec4(vVertex, 1.0);
gl_Position = vMatrix*vertex;
fTexCoord = vTexCoord;
}
]]
 
spriteOutline.FS_GL = [[
 
#ifdef GL_ES
precision highp float;
#endif
 
const float offseta = 0.010;
 
uniform lowp vec4 fColor;
uniform mediump vec4 fTexSize;
uniform lowp sampler2D fTexture;
 
uniform lowp int fSwitch;
 
varying mediump vec2 fTexCoord;
 
vec4 getSample(float x, float y, float offset, float num)
{
vec4 result = vec4(0);
float size = 360.0 / num;
 
for (float a; a <= 360.0; a += size)
{
vec2 coord = vec2(cos(a) * offset + x, sin(a) * offset + y);
 
result += texture2D(fTexture, coord);
}
 
return result;
}
 
void main()
{
vec4 col = texture2D(fTexture, fTexCoord);
 
float nums = 32.0;
 
if (fSwitch == 44) // fSwitch can be changed in code, 44 is some random value for documentation purposes
{
vec4 nb = getSample(fTexCoord.x, fTexCoord.y, 0.020, nums);
 
if (col.a > 0. && nb.a < nums)
{
col.a = nb.a / nums;
col.r = 0.0;
col.g = 0.0;
col.b = 0.0;
}
}
 
gl_FragColor = col;
}
]]
 
spriteOutline.Shader=Shader.new(spriteOutline.VS_GL, spriteOutline.FS_GL, Shader.FLAG_FROM_CODE,
{
{name="vMatrix",type=Shader.CMATRIX,sys=Shader.SYS_WVP,vertex=true},
{name="fColor",type=Shader.CFLOAT4,sys=Shader.SYS_COLOR,vertex=false},
{name="fTexture",type=Shader.CTEXTURE,vertex=false},
{name="fSwitch",type=Shader.CINT,vertex=false},
},
{
{name="vVertex",type=Shader.DFLOAT,mult=3,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},
}
)
 
local sprite = Bitmap.new(Texture.new("gfx/test.png"))
spriteOutline.Shader:setConstant("fSwitch", Shader.CINT, 1, 44) -- set fSwitch value to be 44
sprite:setShader(spriteOutline.Shader)
stage:addChild(sprite)
</syntaxhighlight>
 
{{Shader}}

Latest revision as of 03:08, 6 November 2023

Available since: Gideros 2015.06.30
Class: Shader

Description

Changes the value of a uniform from Lua code.

Shader:setConstant(uniformName,dataType,mult,data)

Parameters

uniformName: (string) the uniform name to change
dataType: (int) the type of data to set (one of the Shader.Cxxx constants)
mult: (number) number of elements of the given type to set
data: (varies) and the actual data to set, either as a table or as multiple arguments

Examples

setConstant in a loop

-- loop
local timer = 0
stage:addEventListener(Event.ENTER_FRAME, function(e)
	timer += 0.018
	shaderwave:setConstant("time", Shader.CFLOAT, 1, timer)
	bitmap:setX(bitmap:getX() + 1)
	if bitmap:getX() > 400 then bitmap:setX(-80) end
end)

An outline shader setConstant 44 activated

spriteOutline = {}

spriteOutline.VS_GL = [[

	uniform highp mat4 vMatrix;

	attribute highp vec3 vVertex;
	attribute mediump vec2 vTexCoord;

	varying mediump vec2 fTexCoord;

	void main() {
		vec4 vertex = vec4(vVertex, 1.0);
		gl_Position = vMatrix*vertex;
		fTexCoord = vTexCoord;
	}
]]

spriteOutline.FS_GL = [[

	#ifdef GL_ES
		precision highp float;
	#endif

	const float offseta = 0.010;

	uniform lowp vec4 fColor;
	uniform mediump vec4 fTexSize;
	uniform lowp sampler2D fTexture;

	uniform lowp int fSwitch;

	varying mediump vec2 fTexCoord;

	vec4 getSample(float x, float y, float offset, float num)
	{
		vec4 result = vec4(0);
		float size = 360.0 / num;

		for (float a; a <= 360.0; a += size)
		{
			vec2 coord = vec2(cos(a) * offset + x, sin(a) * offset + y);

			result += texture2D(fTexture, coord);
		}

		return result;
	}

	void main()
	{
		vec4 col = texture2D(fTexture, fTexCoord);

		float nums = 32.0;

		if (fSwitch == 44) // fSwitch can be changed in code, 44 is some random value for documentation purposes
		{
			vec4 nb = getSample(fTexCoord.x, fTexCoord.y, 0.020, nums);

			if (col.a > 0. && nb.a < nums)
			{
				col.a = nb.a / nums;
				col.r = 0.0;
				col.g = 0.0;
				col.b = 0.0;
			}
		}

		gl_FragColor = col;
	}
]]

spriteOutline.Shader=Shader.new(spriteOutline.VS_GL, spriteOutline.FS_GL, Shader.FLAG_FROM_CODE,
	{
	{name="vMatrix",type=Shader.CMATRIX,sys=Shader.SYS_WVP,vertex=true},
	{name="fColor",type=Shader.CFLOAT4,sys=Shader.SYS_COLOR,vertex=false},
	{name="fTexture",type=Shader.CTEXTURE,vertex=false},
	{name="fSwitch",type=Shader.CINT,vertex=false},
	},
	{
	{name="vVertex",type=Shader.DFLOAT,mult=3,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},
	}
)

local sprite = Bitmap.new(Texture.new("gfx/test.png"))
spriteOutline.Shader:setConstant("fSwitch", Shader.CINT, 1, 44) -- set fSwitch value to be 44
sprite:setShader(spriteOutline.Shader)
stage:addChild(sprite)