Sprite:setShaderConstant

From GiderosMobile

Available since: Gideros 2017.4
Class: Sprite

Description

Changes the value of a uniform from lua.

Sprite:setShaderConstant(uniformName,dataType,mult,data,programType,programVariant)

Parameters

uniform name: (string) the uniform name to change
data type: (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
program type: (int) the type of program this constant applies to optional
program variant: (int) the variant of program this constant applies to optional

Example

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"))
sprite:setShader(spriteOutline.Shader)
sprite:setShaderConstant("fSwitch", Shader.CINT, 1, 44) -- set fSwitch value to be 44
stage:addChild(sprite)