Shader

From GiderosMobile
Revision as of 08:19, 18 December 2020 by Hgy29 (talk | contribs) (→‎Constants)
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.pngPlatform html5.pngPlatform winrt.pngPlatform win32.png
Available since: Gideros 2015.06.30
Inherits from: Object

Description

Gideros internally uses five distinct shaders:

  • the ‘Basic’ shader handles shapes with a constant color
  • the ‘Color’ shader handles shapes with per-vertex colors (mostly used by Mesh sprite)
  • the ‘Texture’ shader handles textured shapes (Bitmaps)
  • the ‘TextureColor’ shader handles textured and per-vertex colored shapes
  • and the ‘Particle’ shader deals with Box2D particle systems

The shader API allows replacing the default shader used by Gideros with a custom one, on a sprite per sprite basis. As with most of Gideros API’s this one is straightforward: you create a Shader object and assign it to one or several sprites.

That said, since Gideros will use your shader as if it was the standard one, you will have to make sure that your custom shader is compatible with the standard one, which essentially means that it takes the same input parameters.

You can also write your shader code in Lua, it will then be automatically translated to the relevant shader language for the platform you are using, eg GLSL, HLSL or MTL. This is the recommended way.

Example

A blur shader

--Shaders are in vShader.glsl and fShader.glsl files
local shader = Shader.new("vShader", "fShader", 0,
	{
		{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="fTexelSize", type=Shader.CFLOAT4, vertex=false},
		{name="fRad", 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},
	}
);

shader:setConstant("fRad", Shader.CINT, 1, 0) -- initial blur level
shader:setConstant("fTexelSize", Shader.CFLOAT4, 1, {1/texw, 1/texh, 0, 0}) -- initial texel size

sprite:setShader(shader)

Methods

Shader.new creates a new shader
Shader.getEngineVersion gets the shader engine version
Shader.getProperties gets the graphics engine properties
Shader.getShaderLanguage gets the shader language
Shader:isValid check if this shader was compiled successfully
Shader:setConstant changes the value of a uniform

Events

Constants

Shader.CFLOAT a float value
Shader.CFLOAT2 a vector of two floats
Shader.CFLOAT3 a vector of three floats
Shader.CFLOAT4 a vector of four floats
Shader.CINT an integer value
Shader.CMATRIX a 4x4 float matrix
Shader.CTEXTURE a texture
Shader.DBYTE
Shader.DFLOAT
Shader.DINT
Shader.DSHORT
Shader.DUBYTE
Shader.DUSHORT
Shader.FLAG_FROM_CODE
Shader.FLAG_NONE
Shader.FLAG_NO_DEFAULT_HEADER
Shader.SYS_COLOR The current color (CFLOAT4)
Shader.SYS_NONE Not filled by Gideros
Shader.SYS_PARTICLESIZE The particle size
Shader.SYS_PROJECTION The projection matrix (CMATRIX)
Shader.SYS_TEXTUREINFO Real extent and texel size of the texture (CFLOAT4)
Shader.SYS_TIMER Current time (CFLOAT)
Shader.SYS_VIEW The view matrix (CMATRIX)
Shader.SYS_VP Combined view/projection matrix (CMATRIX)
Shader.SYS_WIT The World Inverse Transpose matrix (CMATRIX)
Shader.SYS_WIT3 3x3 World Inverse Transpose matrix
Shader.SYS_WORLD The world or matrix (CMATRIX)
Shader.SYS_WVP The combined world/view/projection matrix (CMATRIX)