Difference between revisions of "Shader"

From GiderosMobile
(mostly formatting :-))
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages />
 
 
<!-- GIDEROSOBJ:Shader -->
 
<!-- GIDEROSOBJ:Shader -->
'''<translate>Supported platforms</translate>:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]][[File:Platform html5.png]][[File:Platform winrt.png]][[File:Platform win32.png]]<br/>
+
'''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]][[File:Platform html5.png]][[File:Platform winrt.png]][[File:Platform win32.png]]<br/>
'''<translate>Available since</translate>:''' Gideros 2015.06.30<br/>
+
'''Available since:''' Gideros 2015.06.30<br/>
'''<translate>Inherits from</translate>:''' [[Special:MyLanguage/Object|Object]]<br/>
+
'''Inherits from:''' [[Object]]<br/>
  
=== <translate>Description</translate> ===
+
=== Description ===
<translate>Gideros internally uses five distinct shaders:
+
Gideros internally uses five distinct shaders:
*the ‘Basic’ shader handle shapes with a constant color
+
*the ‘'''Basic'''’ shader handles shapes with a constant color
*the ‘Color’ shader handle shapes with per-vertex colors (mostly used by Mesh sprite)
+
*the ‘'''Color'''’ shader handles shapes with per-vertex colors (mostly used by Mesh sprite)
*the ‘Texture’ shader handle textured shapes (Bitmaps)
+
*the ‘'''Texture'''’ shader handles textured shapes (Bitmaps)
*the ‘TextureColor’ shader handle textured and per-vertex colored shapes
+
*the ‘'''TextureColor'''’ shader handles textured and per-vertex colored shapes
*and the ‘Particle’ shader deals with Box2D particle systems
+
*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 [[Writing Shaders|straightforward]]: create a Shader object and assign it to one or several sprites.
+
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 [[Writing Shaders|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.</translate>
+
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 [[Lua Shaders|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.
+
You can also write your '''[[Lua Shaders|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 ===
=== <translate>Examples</translate> ===
+
'''A blur shader'''
 
<source lang="lua">
 
<source lang="lua">
 
--Shaders are in vShader.glsl and fShader.glsl files
 
--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},
 +
}
 +
);
  
local shader=Shader.new("vShader","fShader",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
{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)
 
sprite:setShader(shader)
Line 47: Line 46:
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Methods</translate> ===
+
=== Methods ===
[[Special:MyLanguage/Shader.new|Shader.new]] ''<translate>create new shader</translate>''<br/><!-- GIDEROSMTD:Shader.new(vertex shader,fragment shader,flags,uniform descriptor,attribute descriptor) create new shader -->
+
[[Shader.new]] ''creates a new shader''<br/><!--GIDEROSMTD:Shader.new(vertex shader,fragment shader,flags,uniform descriptor,attribute descriptor) creates a new shader-->
[[Special:MyLanguage/Shader:getEngineVersion|Shader:getEngineVersion]] ''<translate>get shader version</translate>''<br/><!-- GIDEROSMTD:Shader:getEngineVersion() get shader version -->
+
[[Shader:getEngineVersion]] ''gets the shader engine version''<br/><!--GIDEROSMTD:Shader:getEngineVersion() gets the shader engine version-->
[[Special:MyLanguage/Shader:getProperties|Shader:getProperties]] ''<translate>get graphics engine properties</translate>''<br/><!-- GIDEROSMTD:Shader:getProperties() get graphics engine properties -->
+
[[Shader:getProperties]] ''gets the graphics engine properties''<br/><!--GIDEROSMTD:Shader:getProperties() gets the graphics engine properties-->
[[Special:MyLanguage/Shader:getShaderLanguage|Shader:getShaderLanguage]] ''<translate>Get shader language</translate>''<br/><!-- GIDEROSMTD:Shader:getShaderLanguage() Get shader language -->
+
[[Shader:getShaderLanguage]] ''gets the shader language''<br/><!--GIDEROSMTD:Shader:getShaderLanguage() gets the shader language-->
[[Special:MyLanguage/Shader:setConstant|Shader:setConstant]] ''<translate>change the value of a uniform</translate>''<br/><!-- GIDEROSMTD:Shader:setConstant(uniform name,data type,mult,data) change the value of a uniform -->
+
[[Shader:setConstant]] ''changes the value of a uniform''<br/><!--GIDEROSMTD:Shader:setConstant(uniform name,data type,mult,data) changes the value of a uniform-->
  
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Events</translate> ===
+
=== Events ===
=== <translate>Constants</translate> ===
+
=== Constants ===
[[Special:MyLanguage/Shader.CFLOAT|Shader.CFLOAT]]<br/><!-- GIDEROSCST:Shader.CFLOAT 1-->
+
[[Shader.CFLOAT]]<br/><!--GIDEROSCST:Shader.CFLOAT 1-->
[[Special:MyLanguage/Shader.CFLOAT4|Shader.CFLOAT4]]<br/><!-- GIDEROSCST:Shader.CFLOAT4 2-->
+
[[Shader.CFLOAT4]]<br/><!--GIDEROSCST:Shader.CFLOAT4 2-->
[[Special:MyLanguage/Shader.CINT|Shader.CINT]]<br/><!-- GIDEROSCST:Shader.CINT 0-->
+
[[Shader.CINT]]<br/><!--GIDEROSCST:Shader.CINT 0-->
[[Special:MyLanguage/Shader.CMATRIX|Shader.CMATRIX]]<br/><!-- GIDEROSCST:Shader.CMATRIX 3-->
+
[[Shader.CMATRIX]]<br/><!--GIDEROSCST:Shader.CMATRIX 3-->
[[Special:MyLanguage/Shader.CTEXTURE|Shader.CTEXTURE]]<br/><!-- GIDEROSCST:Shader.CTEXTURE 4-->
+
[[Shader.CTEXTURE]]<br/><!--GIDEROSCST:Shader.CTEXTURE 4-->
[[Special:MyLanguage/Shader.DBYTE|Shader.DBYTE]]<br/><!-- GIDEROSCST:Shader.DBYTE 0-->
+
[[Shader.DBYTE]]<br/><!--GIDEROSCST:Shader.DBYTE 0-->
[[Special:MyLanguage/Shader.DFLOAT|Shader.DFLOAT]]<br/><!-- GIDEROSCST:Shader.DFLOAT 5-->
+
[[Shader.DFLOAT]]<br/><!--GIDEROSCST:Shader.DFLOAT 5-->
[[Special:MyLanguage/Shader.DINT|Shader.DINT]]<br/><!-- GIDEROSCST:Shader.DINT 4-->
+
[[Shader.DINT]]<br/><!--GIDEROSCST:Shader.DINT 4-->
[[Special:MyLanguage/Shader.DSHORT|Shader.DSHORT]]<br/><!-- GIDEROSCST:Shader.DSHORT 2-->
+
[[Shader.DSHORT]]<br/><!--GIDEROSCST:Shader.DSHORT 2-->
[[Special:MyLanguage/Shader.DUBYTE|Shader.DUBYTE]]<br/><!-- GIDEROSCST:Shader.DUBYTE 1-->
+
[[Shader.DUBYTE]]<br/><!--GIDEROSCST:Shader.DUBYTE 1-->
[[Special:MyLanguage/Shader.DUSHORT|Shader.DUSHORT]]<br/><!-- GIDEROSCST:Shader.DUSHORT 3-->
+
[[Shader.DUSHORT]]<br/><!--GIDEROSCST:Shader.DUSHORT 3-->
[[Special:MyLanguage/Shader.FLAG_FROM_CODE|Shader.FLAG_FROM_CODE]]<br/><!-- GIDEROSCST:Shader.FLAG_FROM_CODE 4-->
+
[[Shader.FLAG_FROM_CODE]]<br/><!--GIDEROSCST:Shader.FLAG_FROM_CODE 4-->
[[Special:MyLanguage/Shader.FLAG_NONE|Shader.FLAG_NONE]]<br/><!-- GIDEROSCST:Shader.FLAG_NONE 0-->
+
[[Shader.FLAG_NONE]]<br/><!--GIDEROSCST:Shader.FLAG_NONE 0-->
[[Special:MyLanguage/Shader.FLAG_NO_DEFAULT_HEADER|Shader.FLAG_NO_DEFAULT_HEADER]]<br/><!-- GIDEROSCST:Shader.FLAG_NO_DEFAULT_HEADER 1-->
+
[[Shader.FLAG_NO_DEFAULT_HEADER]]<br/><!--GIDEROSCST:Shader.FLAG_NO_DEFAULT_HEADER 1-->
[[Special:MyLanguage/Shader.SYS_COLOR|Shader.SYS_COLOR]]<br/><!-- GIDEROSCST:Shader.SYS_COLOR 2-->
+
[[Shader.SYS_COLOR]]<br/><!--GIDEROSCST:Shader.SYS_COLOR 2-->
[[Special:MyLanguage/Shader.SYS_NONE|Shader.SYS_NONE]]<br/><!-- GIDEROSCST:Shader.SYS_NONE 0-->
+
[[Shader.SYS_NONE]]<br/><!--GIDEROSCST:Shader.SYS_NONE 0-->
[[Special:MyLanguage/Shader.SYS_PARTICLESIZE|Shader.SYS_PARTICLESIZE]]<br/><!-- GIDEROSCST:Shader.SYS_PARTICLESIZE 6-->
+
[[Shader.SYS_PARTICLESIZE]]<br/><!--GIDEROSCST:Shader.SYS_PARTICLESIZE 6-->
[[Special:MyLanguage/Shader.SYS_TEXTUREINFO|Shader.SYS_TEXTUREINFO]]<br/><!-- GIDEROSCST:Shader.SYS_TEXTUREINFO 5-->
+
[[Shader.SYS_TEXTUREINFO]]<br/><!--GIDEROSCST:Shader.SYS_TEXTUREINFO 5-->
[[Special:MyLanguage/Shader.SYS_WIT|Shader.SYS_WIT]]<br/><!-- GIDEROSCST:Shader.SYS_WIT 3-->
+
[[Shader.SYS_WIT]]<br/><!--GIDEROSCST:Shader.SYS_WIT 3-->
[[Special:MyLanguage/Shader.SYS_WORLD|Shader.SYS_WORLD]]<br/><!-- GIDEROSCST:Shader.SYS_WORLD 4-->
+
[[Shader.SYS_WORLD]]<br/><!--GIDEROSCST:Shader.SYS_WORLD 4-->
[[Special:MyLanguage/Shader.SYS_WVP|Shader.SYS_WVP]]<br/><!-- GIDEROSCST:Shader.SYS_WVP 1-->
+
[[Shader.SYS_WVP]]<br/><!--GIDEROSCST:Shader.SYS_WVP 1-->
 
|}
 
|}
  
 
{{GIDEROS IMPORTANT LINKS}}
 
{{GIDEROS IMPORTANT LINKS}}

Revision as of 08:47, 5 December 2020

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:setConstant changes the value of a uniform

Events

Constants

Shader.CFLOAT
Shader.CFLOAT4
Shader.CINT
Shader.CMATRIX
Shader.CTEXTURE
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
Shader.SYS_NONE
Shader.SYS_PARTICLESIZE
Shader.SYS_TEXTUREINFO
Shader.SYS_WIT
Shader.SYS_WORLD
Shader.SYS_WVP