Shader.new: Difference between revisions

From GiderosMobile
Created page with "__NOTOC__ '''Available since:''' Gideros 2015.06.30<br/> === Description === Create new shader instance. The ‘Shader.new()’ constructor takes five arguments: - The path a..."
 
No edit summary
 
(18 intermediate revisions by 3 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
'''Available since:''' Gideros 2015.06.30<br/>
'''Available since:''' Gideros 2015.06.30<br/>
'''Class:''' [[Shader]]<br/>
=== Description ===
=== Description ===
Create new shader instance.
Creates a new shader instance.
<syntaxhighlight lang="lua">
Shader.new(vertexShader,fragmentShader,flags,uniformDescriptor,attributeDescriptor)
</syntaxhighlight>
 
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<br/>
'''fragmentShader''': (string) the path and name for the fragment shader (''no extension'') or code within a string<br/>
'''flags''': (number) a set of numerical flags or 0 if none: '''[[Shader Flags]]'''<br/>
'''uniformDescriptor''': (table) an array of uniforms/constants descriptors: '''[[Shader Uniform Descriptors]]'''<br/>
'''attributeDescriptor''': (table) an array of attributes descriptors: '''[[Shader Attribute Descriptors]]'''<br/>
 
=== Example ===
'''A wave shader'''
<syntaxhighlight lang="lua">
--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)
</syntaxhighlight>
'''The wave 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 wave 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>


The ‘Shader.new()’ constructor takes five arguments:
{{Shader}}
- 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. See description below.
- An array of uniforms/constants descriptors
- An array of attributes descriptors
<source lang="lua">
= Shader.new(vertex shaderfragment shaderflagsuniform descriptorattribute descriptor,)
</source>
'''vertex shader:''' (string) The path and name for the vertex shader without its extension ''''''<br/>
'''fragment shader:''' (string) The path and name for the fragment shader without its extension ''''''<br/>
'''flags:''' (number) A set of numerical flags or 0 if none ''''''<br/>
'''uniform descriptor:''' (table) An array of uniforms/constants descriptors ''''''<br/>
'''attribute descriptor:''' (table) An array of attributes descriptors ''''''<br/>

Latest revision as of 22:57, 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 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);
}