Shader.new

From GiderosMobile
Revision as of 23:28, 3 November 2023 by MoKaLux (talk | contribs)

Available since: Gideros 2015.06.30
Class: Shader

Description

Creates a new shader instance.

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

The ‘Shader.new()’ constructor takes five arguments:

  • the path and name for the vertex shader without its extension. 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
  • the path and name for the fragment shader without its extension. Same remark as above applies too
  • a set of numerical flags or 0 if none
  • an array of uniforms/constants descriptors
  • an array of attributes descriptors

Parameters

vertexShader: (string) the path and name for the vertex shader without its extension
fragmentShader: (string) the path and name for the fragment shader without its extension
flags: (number) a set of numerical flags or 0 if none
uniformDescriptor: (table) an array of uniforms/constants descriptors
attributeDescriptor: (table) an array of attributes 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 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 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);
}