Difference between revisions of "Writing Lua Shaders"

From GiderosMobile
(Created page with "__NOTOC__ == LUA Shaders == === Description === === Creating a Shader === Typical Lua Vertex and Fragment shader code: <syntaxhighlight lang="lua"> function vertex(vVertex,vCo...")
 
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
== LUA Shaders ==
+
'''Parent:''' [[Shaders]]<br/>
=== Description ===
+
 
 +
== Lua Shaders ==
 
=== Creating a Shader ===
 
=== Creating a Shader ===
 
Typical Lua Vertex and Fragment shader code:
 
Typical Lua Vertex and Fragment shader code:

Revision as of 21:14, 5 November 2023

Parent: Shaders

Lua Shaders

Creating a Shader

Typical Lua Vertex and Fragment shader code:

function vertex(vVertex,vColor,vTexCoord)
	local vertex = hF4(vVertex,0.0,1.0)
	fTexCoord=vTexCoord
	return vMatrix*vertex
end

function fragment()
	local frag=lF4(fColor)*texture2D(fTexture, fTexCoord)
	local coef=lF3(0.2125, 0.7154, 0.0721)
	local gray=dot(frag.rgb,coef)
	frag.rgb=lF3(gray,gray,gray)
	if (frag.a==0.0) then discard() end
	return frag
end

Changing Uniforms/Constants

In order to change the value of a uniform from Lua, use the setConstant function, it takes three arguments:

  • the uniform name
  • the type of data to set (one of the Shader.Cxxx constants)
  • the actual data to set, either as a table or as multiple arguments

Associating a Shader to a Sprite

The Sprite API has a new call to deal with that: Sprite:setShader(shader) tells Gideros to use the specified shader for rendering the sprite. Setting back the shader to nil actually revert to the default shader.

The API allows the creation of Shader objects from within Lua. The 'Shader.new()' constructor takes five arguments:

With Lua Shaders, it is assumed that the code is within the vertex and fragment parameter functions.

Lua Shader Functions

(Please note that these functions may or may not be available, the Lua shader is still under development)

Restrictions

Don't rely on anything external, basically no globals except for attributes, uniforms and varying.

  • No Do..While
  • No Repeat..Until
  • No tables. The only allowed form of table lookup is for vector component swizzling and access to uniform arrays
  • No strings, userdata, etc. Basically only numbers are supported
  • Mind your types! Although lua is not a typed language, shaders need types
  • Always check on all platforms: Gideros will translate your lua code to GLSL,HLSL or MSL code, but some platforms are more strict than others about what you can write, or how many arguments a common function is supposed to take

Lua Shaders Pebbles and Demos

Rain drops demo - by Hgy29