Difference between revisions of "Sprite:setShaderConstant"

From GiderosMobile
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages />
+
'''Available since:''' Gideros 2017.4<br/>
'''<translate>Available since</translate>:''' Gideros 2017.4<br/>
+
'''Class:''' [[Sprite]]<br/>
'''<translate>Class</translate>:''' [[Special:MyLanguage/Sprite|Sprite]]<br/>
+
 
=== <translate>Description</translate> ===
+
=== Description ===
<translate>To change the value of a uniform from lua</translate>
+
Changes the value of a uniform from lua.
<source lang="lua">
+
<syntaxhighlight lang="lua">
Sprite:setShaderConstant(uniform name,data type,mult,data,program type,program variant)
+
Sprite:setShaderConstant(uniformName,dataType,mult,data,programType,programVariant)
</source>
+
</syntaxhighlight>
=== <translate>Parameters</translate> ===
+
 
'''uniform name''': (string) <translate>The uniform name to change</translate> <br/>
+
=== Parameters ===
'''data type''': (int) <translate>The type if data to set (one of the Shader.Cxxx constants)</translate> <br/>
+
'''uniform name''': (string) the uniform name to change<br/>
'''mult''': (number) <translate>number of elements of the given type to set</translate> <br/>
+
'''data type''': (int) the type of data to set (one of the Shader.Cxxx constants)<br/>
'''data''': (varies) <translate>And the actual data to set, either as a table or as multiple arguments</translate> <br/>
+
'''mult''': (number) number of elements of the given type to set<br/>
'''program type''': (int) '''optional''' The type of program this constant applies to <br/>
+
'''data''': (varies) and the actual data to set, either as a table or as multiple arguments<br/>
'''program variant''': (int) '''optional''' The variant of program this constant applies to <br/>
+
'''program type''': (int) the type of program this constant applies to '''optional'''<br/>
 +
'''program variant''': (int) the variant of program this constant applies to '''optional'''<br/>
 +
 
 +
=== Example ===
 +
<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"))
 +
sprite:setShader(spriteOutline.Shader)
 +
sprite:setShaderConstant("fSwitch", Shader.CINT, 1, 44) -- set fSwitch value to be 44
 +
stage:addChild(sprite)
 +
</syntaxhighlight>
 +
 
 
{{Sprite}}
 
{{Sprite}}

Latest revision as of 05:03, 6 November 2023

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)