Difference between revisions of "Shader Constants"

From GiderosMobile
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
'''Available since:''' Gideros 2015.06.30<br/>
 
'''Available since:''' Gideros 2015.06.30<br/>
'''Defined by:''' '''Shader'''<br/>
+
'''Defined by:''' [[Shader]]<br/>
  
 
=== Constants ===
 
=== Constants ===
Line 46: Line 46:
 
'''Shader.SYS_WORLD''': This Uniform of type CMATRIX will hold the World transform matrix, 4<br/>
 
'''Shader.SYS_WORLD''': This Uniform of type CMATRIX will hold the World transform matrix, 4<br/>
 
'''Shader.SYS_WVP''': This Uniform of type CMATRIX will hold the combined World/View/Projection matrix, 1<br/>
 
'''Shader.SYS_WVP''': This Uniform of type CMATRIX will hold the combined World/View/Projection matrix, 1<br/>
 +
 +
=== Example ===
 +
'''Shader.FLAG_FROM_CODE'''
 +
<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)
 +
sprite:setShaderConstant("fSwitch", Shader.CINT, 1, 44) -- set fSwitch value to be 44
 +
stage:addChild(sprite)
 +
</syntaxhighlight>
  
 
{{Shader}}
 
{{Shader}}

Latest revision as of 04:01, 6 November 2023

Available since: Gideros 2015.06.30
Defined by: Shader

Constants

Shader.CFLOAT: Uniform descriptor CFLOAT data type, a float value, 1
Shader.CFLOAT2: Uniform descriptor CFLOAT2 data type, a vector of two floats, 2
Shader.CFLOAT3: Uniform descriptor CFLOAT3 data type, a vector of three floats, 3
Shader.CFLOAT4: Uniform descriptor CFLOAT4 data type , a vector of four floats 4
Shader.CINT: Uniform descriptor CINT data type, an integer value, 0
Shader.CMATRIX: Uniform descriptor CMATRIX data type , a 4x4 float matrix, 5
Shader.CTEXTURE: Uniform descriptor CTEXTURE data type, a texture, 6
Shader.DBYTE: Attributes descriptor DBYTE data type, 0
Shader.DFLOAT: Attributes descriptor DFLOAT data type , 5
Shader.DINT: Attributes descriptor DINT data type, 4
Shader.DSHORT: Attributes descriptor DSHORT data type, 2
Shader.DUBYTE: Attributes descriptor DUBYTE data type, 1
Shader.DUSHORT: Attributes descriptor DUSHORT data type, 3
Shader.FLAG_FROM_CODE: Shader loaded from code, 4
Shader.FLAG_NONE: No flags, 0
Shader.FLAG_NO_DEFAULT_HEADER: Suppress the automatic addition of a default header for GLSL programs, 1
Shader.SHADER_PROGRAM_UNSPECIFIED: Indeterminate program, 0
Shader.SHADER_PROGRAM_BASIC: Render a plain color shape, 1
Shader.SHADER_PROGRAM_COLOR: Render a colored shape, 2
Shader.SHADER_PROGRAM_TEXTURE: Render a textured shape, 3
Shader.SHADER_PROGRAM_TEXTUREALPHA: Render shape with an alpha texture, 4
Shader.SHADER_PROGRAM_TEXTURECOLOR: Render a colored and textured shape, 5
Shader.SHADER_PROGRAM_TEXTUREALPHACOLOR: Render a colored shape with an alpha texture, 6
Shader.SHADER_PROGRAM_PARTICLE: Used by liquidfun particles, 7
Shader.SHADER_PROGRAM_PARTICLES: Used by Particles, 8
Shader.SHADER_PROGRAM_PATHFILLCURVE: Used by Path2D to fill a curve, 9
Shader.SHADER_PROGRAM_PATHSTROKECURVE: Used by Path2D to draw a curve, 10
Shader.SHADER_PROGRAM_PATHSTROKELINE: Used by Path2D to draw a segment, 11
Shader.SHADER_VARIANT_TEXTURED: A textured variant of a standard program, 1
Shader.SHADER_VARIANT_3D: A 3D variant of a standard program, 2
Shader.SYS_COLOR: This Uniform of type CFLOAT4 will hold the constant color to be used to draw the shape, 2
Shader.SYS_NONE: Not filled by Gideros, 0
Shader.SYS_PARTICLESIZE: This Uniform of type CFLOAT will hold the particle size to use in a particle shader, 6
Shader.SYS_PROJECTION: The projection matrix (CMATRIX), 10
Shader.SYS_TEXTUREINFO: This Uniform of type CFLOAT4 will hold information about the texture used in a particle shader, like real extent and texel size, 5
Shader.SYS_TIMER: Current time (CFLOAT), 9
Shader.SYS_VIEW: The view matrix (CMATRIX), 8
Shader.SYS_VP: Combined view/projection matrix (CMATRIX), 11
Shader.SYS_WIT: This Uniform of type CMATRIX will hold the World Inverse Transpose matrix, 7
Shader.SYS_WIT3: 3x3 World Inverse Transpose matrix, 3
Shader.SYS_WORLD: This Uniform of type CMATRIX will hold the World transform matrix, 4
Shader.SYS_WVP: This Uniform of type CMATRIX will hold the combined World/View/Projection matrix, 1

Example

Shader.FLAG_FROM_CODE

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