Difference between revisions of "Shader.new"

From GiderosMobile
Line 10: Line 10:
  
 
The ‘Shader.new()’ constructor takes five arguments:
 
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 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
 
*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. See description below
+
*a set of numerical flags or 0 if none
 
*an array of uniforms/constants descriptors
 
*an array of uniforms/constants descriptors
 
*an array of attributes descriptors
 
*an array of attributes descriptors
Line 24: Line 24:
  
 
=== Example ===
 
=== Example ===
'''A blur shader'''
+
'''A wave shader'''
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight 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,
+
local shaderwave=Shader.new("openglshader/vShaderWave","openglshader/fShaderWave", 0,
 
{
 
{
{name="vMatrix", type=Shader.CMATRIX, sys=Shader.SYS_WVP, vertex=true},
+
{name="g_MVPMatrix", type=Shader.CMATRIX, sys=Shader.SYS_WVP, vertex=true},
{name="fColor", type=Shader.CFLOAT4, sys=Shader.SYS_COLOR, vertex=false},
+
{name="g_Color", type=Shader.CFLOAT4, mult=1, sys=Shader.SYS_COLOR},
{name="fTexture", type=Shader.CTEXTURE, vertex=false},
+
{name="g_Texture", type=Shader.CTEXTURE, mult=1, vertex=false},
{name="fTexelSize", type=Shader.CFLOAT4, vertex=false},
+
{name="time", type=Shader.CFLOAT, mult=1, vertex=false}
{name="fRad", type=Shader.CINT, vertex=false},
 
 
},
 
},
 
{
 
{
{name="vVertex", type=Shader.DFLOAT, mult=3, slot=0, offset=0},
+
{name="POSITION0", type=Shader.DFLOAT, mult=3, slot=0, offset=0},
{name="vColor", type=Shader.DUBYTE, mult=4, slot=1, offset=0},
+
{name="vColor", type=Shader.DUBYTE, mult=0, slot=1, offset=0},
{name="vTexCoord", type=Shader.DFLOAT, mult=2, slot=2, offset=0},
+
{name="TEXCOORD0", type=Shader.DFLOAT, mult=2, slot=2, offset=0}
 
}
 
}
);
+
)
  
shader:setConstant("fRad", Shader.CINT, 1, 0) -- initial blur level
+
application:setBackgroundColor(0x333333)
shader:setConstant("fTexelSize", Shader.CFLOAT4, 1, {1/texw, 1/texh, 0, 0}) -- initial texel size
 
  
sprite:setShader(shader)
+
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)
 +
</syntaxhighlight>
 +
'''The vertex shader''' ''"assets/openglshader/vShaderWave.glsl"''
 +
<syntaxhighlight lang="c++">
 +
attribute vec4 POSITION0;
 +
attribute vec2 TEXCOORD0;
 +
uniform mat4 g_MVPMatrix;
 +
varying vec2 texCoord;
 +
 
 +
void main()
 +
{
 +
gl_Position = g_MVPMatrix * POSITION0;
 +
texCoord = TEXCOORD0;
 +
}
 +
</syntaxhighlight>
 +
'''The fragment shader''' ''"assets/openglshader/fShaderWave.glsl"''
 +
<syntaxhighlight lang="c++">
 +
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);
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
{{Shader}}
 
{{Shader}}

Revision as of 00:28, 4 November 2023

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);
}