Shader.lua

From GiderosMobile
Revision as of 04:55, 6 November 2023 by MoKaLux (talk | contribs) (Created page with "__NOTOC__ '''Available since:''' Gideros 2020.7<br/> '''Class:''' Shader<br/> === Description === Creates a new Lua shader instance. <syntaxhighlight lang="lua"> Shader.l...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Available since: Gideros 2020.7
Class: Shader

Description

Creates a new Lua shader instance.

Shader.lua(vertexShader,fragmentShader,flags,uniformDescriptor,attributeDescriptor)
Requirements:
In order to use Lua Shaders you need to include luashader standard library in your projects
luashader standard library is available in your Gideros installation folder under Library

Parameters

vertexShader: (function) the vertex shader function
fragmentShader: (function) the fragment shader function
flags: (number) a set of numerical flags or 0 if none: Shader Flags
uniformDescriptor: (table) an array of uniforms/constants descriptors: Shader Uniform Descriptors
attributeDescriptor: (table) an array of attributes descriptors: Shader Attribute Descriptors

Example

A wave shader

function vswave(vVertex, vColor, vTexCoord) : Shader
	local vertex = hF4(vVertex, 0.0, 1.0)
	fTexCoord = vTexCoord
	return vMatrix * vertex
end
function fswave() : Shader
	local tc = hF2(fTexCoord.x + (1 + sin(fTexCoord.x * 10 + fTime * 2)) * 0.05, fTexCoord.y) * 0.9
	local frag = lF4(fColor) * texture2D(fTexture, tc)
	if (frag.a == 0.0) then discard() end
	return frag
end

local wave = Shader.lua(vswave, fswave, 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 = "fTextureInfo", type = Shader.CFLOAT4, sys = Shader.SYS_TEXTUREINFO, vertex = false},
	{name = "fTime", type = Shader.CFLOAT, sys = Shader.SYS_TIMER, vertex = false},
	},
	{
	{name = "vVertex", type = Shader.DFLOAT, mult = 2, 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},
	},
	{
	{name = "fTexCoord", type = Shader.CFLOAT2},
	}
)

-- bg color
application:setBackgroundColor(0x909090)
-- a texture and a bitmap
local bmp = Bitmap.new(Texture.new("gfx/test.png"))
-- position
bmp:setPosition(32*2, 32*4)
-- lua shader
bmp:setShader(wave)
-- order
stage:addChild(bmp)

-- loop
local timer = 0
stage:addEventListener(Event.ENTER_FRAME, function(e)
	timer += 0.018
	wave:setConstant("fTime", Shader.CFLOAT, 1, timer)
	bmp:setX(bmp:getX() + 1)
	if bmp:getX() > 400 then bmp:setX(-80) end
end)