Shader.new

From GiderosMobile
Revision as of 23:57, 4 November 2023 by MoKaLux (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Available since: Gideros 2015.06.30
Class: Shader

Description

Creates a new shader instance.

Shader.new(vertexShader,fragmentShader,flags,uniformDescriptor,attributeDescriptor)

The paths and names for the vertexShader and the fragmentShader are without extensions. Gideros will search the assets for a file with the supplied name, automatically adding the extension relevant for the target platform: .glsl for OpenGL, .cso or .hlsl for DirectX and .mtl for Metal.

The Flag Shader.FLAG_FROM_CODE can be used to let Gideros know that the string is actually Shader code, not contained within a file.

Parameters

vertexShader: (string) the path and name for the vertex shader (no extension) or code within a string
fragmentShader: (string) the path and name for the fragment shader (no extension) or code within a string
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

--Shaders are in vShader.glsl and fShader.glsl files
local shaderwave=Shader.new("openglshader/vShaderWave","openglshader/fShaderWave", 0,
	{
	{name="g_MVPMatrix", type=Shader.CMATRIX, sys=Shader.SYS_WVP, vertex=true},
	{name="g_Color", type=Shader.CFLOAT4, mult=1, sys=Shader.SYS_COLOR},
	{name="g_Texture", type=Shader.CTEXTURE, mult=1, vertex=false},
	{name="time", type=Shader.CFLOAT, mult=1, vertex=false}
	},
	{
	{name="POSITION0", type=Shader.DFLOAT, mult=3, slot=0, offset=0},
	{name="vColor", type=Shader.DUBYTE, mult=0, slot=1, offset=0},
	{name="TEXCOORD0", type=Shader.DFLOAT, mult=2, slot=2, offset=0}
	}
)

application:setBackgroundColor(0x333333)

local bitmap = Bitmap.new(Texture.new("gfx/test.png"))
local bitmap2 = Bitmap.new(Texture.new("gfx/arrow_0001.png"))
-- position
bitmap:setPosition(32*1, 32*4)
bitmap2:setPosition(32*6, 32*1)
-- shaders
bitmap:setShader(shaderwave)
bitmap2:setShader(shaderwave)
-- order
stage:addChild(bitmap)
stage:addChild(bitmap2)

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

The wave vertex shader "assets/openglshader/vShaderWave.glsl"

attribute vec4 POSITION0;
attribute vec2 TEXCOORD0;
uniform mat4 g_MVPMatrix;
varying vec2 texCoord;

void main()
{
	gl_Position = g_MVPMatrix * POSITION0;
	texCoord = TEXCOORD0;
}

The wave fragment shader "assets/openglshader/fShaderWave.glsl"

uniform lowp sampler2D g_Texture;
uniform lowp vec4 g_Color;
uniform highp float time;
varying highp vec2 texCoord;

void main()
{
	highp vec2 tc = texCoord.xy;
//	highp float dist = cos(tc.x * 24.0 - time * 4.0) * 0.02;
	highp float dist = cos(tc.x * 2.0 - time * 8.0) * 0.02;
	highp vec2 uv = tc + dist;
	gl_FragColor = g_Color * texture2D(g_Texture, uv);
}