Samsung Galxy s3 or other mali 400 gpu chippset devices.
If you have problems with agk shaders read this.
http://forum.thegamecreators.com/?m=forum_view&t=206452&b=41
Nivida tegra 2 devices demands that you pass in your own values from agk thru the vertex shader first and then to the fragment/pixel shader with an varying.
This is if you do your own fog shader etc.
And want to pass in an min and max value to the fragment shader.
-------------------------------------------------------------------
Great online resource with open glshaders.
http://www.iquilezles.org/apps/shadertoy/index.html
-------------------------------------------------------------------
If you get slow performance with your shader on mobiles test this.
This code part should be at the top of your pixel shader.
#ifdef GL_ES
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
#endif
Now do you wonder wath this code does ?
It simply first checks if the device is an mobile device.
then does it check if the device supports high precicion calculations or not.
And if it dosent so does it set it to medium precision.
I only tested this on some parts of my segments on an test map and i got an increase in 10 fps by doing this.
-------------------------------------------------------------------
-------------------------------------------------------------------
And the point light shader with 108 beta 3 do you also nead to activate the directional light.
The shaders under here have not bin tested on android!!!!
Working on some kind of bump fx.
vertex shader for the pixel shaders with
bump map and tiling.
attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
varying vec2 uvVarying;
varying vec3 normalVarying;
varying vec3 posVarying;
uniform vec4 uvBounds0;
uniform mat4 agk_World;
uniform mat4 agk_ViewProj;
uniform mat3 agk_WorldNormal;
uniform vec2 tiling;
void main()
{
vec4 pos = agk_World * vec4(position,1);
gl_Position = agk_ViewProj * pos;
vec3 norm = agk_WorldNormal * normal;
posVarying = pos.xyz;
normalVarying = norm;
uvVarying = uv * (uvBounds0.xy+tiling) + uvBounds0.zw;
}
Pixel frag. Here is the one with an bump map.
uniform vec4 agk_PLightPos;
uniform vec4 agk_PLightColor;
uniform vec3 agk_DLightDir;
uniform vec4 agk_DLightColor;
uniform vec4 agk_ObjColor;
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform float ambientPower;
uniform float ambientMin;
uniform float ambientMax;
uniform vec3 defaultLight;
varying vec2 uvVarying;
varying vec3 normalVarying;
varying vec3 posVarying;
void main()
{
// the code from the embosse we use to get an bump fx
vec2 onePixel = vec2(1.0 / 480.0, 1.0 / 480.0);
vec2 texCoord = uvVarying;
vec4 bump;
bump.rgb = vec3(0.5);
bump -= texture2D(texture1, texCoord - onePixel) * 16.0;
bump += texture2D(texture1, texCoord + onePixel) * 16.0;
bump.rgb = vec3((bump.r + bump.g + bump.b) / 8.0);
vec3 dir = agk_PLightPos.xyz - posVarying;
vec3 norm = normalize(normalVarying);
float distance = length(dir)/ambientPower;
float atten = dot(dir,dir);
atten = clamp(agk_PLightPos.w/atten,0.0,1.0);
float intensity = dot(normalize(dir),norm);
intensity = clamp(intensity,0.0,1.0);
vec3 color = (agk_PLightColor.rgb+bump.rgb) * intensity * atten;
color = color + clamp(dot(-agk_DLightDir,norm)*agk_DLightColor.rgb,0.0,0.2);
gl_FragColor = clamp((texture2D(texture0, uvVarying ) * vec4(color,1) * agk_ObjColor * defaultLight ) / distance , ambientMin , ambientMax );
}
This shader have 1 directional light,1 point light and ambient lighting.
Here is it used for a dungeon.
The shader it self.
Pixel/frag shader default lighting,ambient,directional and point light.
uniform vec4 agk_PLightPos;
uniform vec4 agk_PLightColor;
uniform vec3 agk_DLightDir;
uniform vec4 agk_DLightColor;
uniform vec4 agk_ObjColor;
uniform sampler2D texture0;
uniform float ambientPower;
uniform float ambientMin;
uniform float ambientMax;
uniform vec3 defaultLight;
varying vec2 uvVarying;
varying vec3 normalVarying;
varying vec3 posVarying;
void main()
{
vec3 dir = agk_PLightPos.xyz - posVarying;
vec3 norm = normalize(normalVarying);
float distance = length(dir)/ambientPower;
float atten = dot(dir,dir);
atten = clamp(agk_PLightPos.w/atten,0.0,1.0);
float intensity = dot(normalize(dir),norm);
intensity = clamp(intensity,0.0,1.0);
vec3 color = agk_PLightColor.rgb * intensity * atten;
color = color + clamp(dot(-agk_DLightDir,norm)*agk_DLightColor.rgb,0.0,0.2);
gl_FragColor = clamp((texture2D(texture0, uvVarying) * vec4(color,1) * agk_ObjColor * defaultLight ) / distance , ambientMin , ambientMax );
}
Reference card. (kronos group)
http://www.khronos.org/opengles/sdk/docs/reference_cards/OpenGL-ES-2_0-Reference-card.pdf
Your agk commands if you want to convert shaders to agk.
The shader constants supported by AppGameKit are
agk_World - object world matrix
agk_WorldNormal - object world matrix for normals, only necessary when an object has a non-uniform scale applied, otherwise it is the same as agk_World
agk_View - the camera view matrix
agk_Proj - the 3D projection matrix
agk_Ortho - the 2D orthographic projection matrix used by sprites
agk_ViewProj - combined view and projection matrices
agk_WorldViewProj - combined world, view, and projection matrices
agk_WorldOrtho - combined world and ortho matrices for sprites
texture0 to texture7 - texture stages 0-7 applied to the current object (pixel shader only)
uvBounds0 to uvBounds7 - UV adjustments for the textures applied above
agk_ObjColor - the current color set for this object
agk_PLightPos - the position of the first point light
agk_PLightColor - the color of the first point light
agk_DLightDir - the direction of the first directional light
agk_DLightColor - the color of the first directional light
The vertex attributes supported by the AppGameKit primitives and OBJ models are
"position"
"normal"
"uv"
But the AGO format allows you to add as many vertex attributes as you want and to name them whatever you like.
The shaders i create while iam trying to learn agk shaders.
This is base shaders you can base your own shaders on.
They are simply only coloring your objects with the object color.
Vertex color.
// combined model/view/projection matrix
uniform mat4 agk_WorldViewProj;
// Per-vertex position information
attribute vec4 position;
// vertex color
uniform vec4 agk_ObjColor;
// Only used to send dat to the pixel/fragment shader
varying vec4 vertex_color;
void main()
{
vertex_color = agk_ObjColor;
gl_Position = agk_WorldViewProj * position;
}
Pixel color.
// gets this from the vertex shader
varying vec4 vertex_color;
void main()
{
gl_FragColor = vertex_color;
}
Here is my
vertex shader with tiling that works with all of the previous pixel frag shaders.
You set the tiling like this in agk.
SetShaderConstantByName( box, "tiling",1.0 ,0.5,0, 0 )
The order are x/y.
This do not work on atlas images!
setimage wrap u and wrap v to 1 on the image you use.
attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
varying vec2 uvVarying;
varying vec3 normalVarying;
varying vec3 posVarying;
uniform vec4 uvBounds0;
uniform mat4 agk_World;
uniform mat4 agk_ViewProj;
uniform mat3 agk_WorldNormal;
uniform vec2 tiling;
void main()
{
vec4 pos = agk_World * vec4(position,1);
gl_Position = agk_ViewProj * pos;
vec3 norm = agk_WorldNormal * normal;
posVarying = pos.xyz;
normalVarying = norm;
uvVarying = uv * (uvBounds0.xy+tiling) + uvBounds0.zw;
}
vertex shader texture.
// The attribute keyword tells the compiler that this is an
// input variable that comes with each vertex data structure.
// Types vec2 and vec4 declare that the data is a vector of
// floats; vec can have 2-4 components.
attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
// When you need some external variables passed to the
// shader from your source code, you need to declare them
// as uniform. Type mat4 declares that the data is a
// 4×4 matrix of floats.
uniform mat4 agk_WorldViewProj;
// This is object texture 0
uniform vec4 uvBounds0;
// The vertex shader needs to send some data to the
// pixel shader. To notate variables that you pass from
// a vertex shader to a pixel shader, you use the
// varying keyword.
varying vec2 uv0Varying;
void main()
{
// Vertex shaders need to fill the built-in variable
// gl_Position with the transformed vertex position.
gl_Position = agk_WorldViewProj * vec4(position,1);
// This passes the input coordinates to the pixel shader
// unchanged by assigning them to the varying variable.
uv0Varying = uv * uvBounds0.xy + uvBounds0.zw;
}
Clean version.
attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
uniform mat4 agk_WorldViewProj;
uniform vec4 uvBounds0;
varying vec2 uv0Varying;
void main()
{
gl_Position = agk_WorldViewProj * vec4(position,1);
uv0Varying = uv * uvBounds0.xy + uvBounds0.zw;
}
pixel shader texture.
// highp and lowp. They define the importance of the quality
// of calculations/data storage. The higher precision means
// more precise data types will be used, and the calculations
// will be slower.
#ifdef GL_ES
precision highp float;
#endif
// constant values sent through from code.
uniform sampler2D texture0;
// Anything that the vertex shader passes as output needs to
// be defined here as input. The vertex shader is passing the
// texture coordinate, so it is defined again here.
varying vec2 uv0Varying;
void main()
{
// gl_FragColor is the built-in variable that needs to
// be filled with the final color of the pixel. This gets
// the color from the uniform texture and uses the
// texCoords from the vertex shader to determine which
// pixel should be used.
gl_FragColor = texture2D(texture0, uv0Varying);
}
Clean version.
#ifdef GL_ES
precision highp float;
#endif
uniform sampler2D texture0;
varying vec2 uv0Varying;
void main()
{
gl_FragColor = texture2D(texture0, uv0Varying);
}
Pixel shader ramp colors.
// highp and lowp. They define the importance of the quality
// of calculations/data storage. The higher precision means
// more precise data types will be used, and the calculations
// will be slower.
#ifdef GL_ES
precision highp float;
#endif
// constant values sent through from code.
uniform sampler2D texture0;
// This is the color ramp image that is white to black.
// grey scales.
uniform sampler2D texture1;
// Anything that the vertex shader passes as output needs to
// be defined here as input. The vertex shader is passing the
// texture coordinate, so it is defined again here.
varying vec2 uv0Varying;
void main()
{
// Get the real color from the texture.
vec3 normalColor = texture2D(texture0, uv0Varying).rgb;
// Use each real color channel value as an address to
// get the modified color from your ramp texture.
float rampedR = texture2D(texture1, vec2(normalColor.r, 0)).r;
float rampedG = texture2D(texture1, vec2(normalColor.g, 0)).g;
float rampedB = texture2D(texture1, vec2(normalColor.b, 0)).b;
// gl_FragColor is the built-in variable that needs to
// be filled with the final color of the pixel. This gets
// the color from the uniform texture and uses the
// texCoords from the vertex shader to determine which
// pixel should be used.
// Create the fragment color (the final color of your pixel)
// based on the ramped values for color and make it fully
// opaque (the 1 at the end).
gl_FragColor = vec4(rampedR, rampedG, rampedB, 1);
}
Clean version.
#ifdef GL_ES
precision highp float;
#endif
uniform sampler2D texture0;
uniform sampler2D texture1;
varying vec2 uv0Varying;
void main()
{
vec3 normalColor = texture2D(texture0, uv0Varying).rgb;
float rampedR = texture2D(texture1, vec2(normalColor.r, 0)).r;
float rampedG = texture2D(texture1, vec2(normalColor.g, 0)).g;
float rampedB = texture2D(texture1, vec2(normalColor.b, 0)).b;
gl_FragColor = vec4(rampedR, rampedG, rampedB, 1);
}
Pixel shader embosse.
// highp and lowp. They define the importance of the quality
// of calculations/data storage. The higher precision means
// more precise data types will be used, and the calculations
// will be slower.
#ifdef GL_ES
precision highp float;
#endif
// constant values sent through from code.
uniform sampler2D texture0;
// Use this command in agk tier 1 to set the embosse scale
// SetShaderConstantByName( shaderID, "Scalus", set size here , 0, 0, 0 )
uniform float Scalus;
// Anything that the vertex shader passes as output needs to
// be defined here as input. The vertex shader is passing the
// texture coordinate, so it is defined again here.
varying vec2 uv0Varying;
void main()
{
// Define the size for one pixel of your texture in
// texCoords space (normalized 0, – 1).
// higher values gives smoother apperance
vec2 onePixel = vec2(1.0 / Scalus, 1.0 / Scalus);
// copy the texture coords
vec2 texCoord = uv0Varying;
// Start by defining the base color, which will be half the
// power of white. Then subtract the color of one pixel
// diagonal movement, and add the opposite diagonal
// displacement. This will make pixels that are on different
// color borders stand out, and the pixels of similar
// color will be pretty close to the base color.
vec4 color;
color.rgb = vec3(0.5);
color -= texture2D(texture0, texCoord - onePixel) * 5.0;
color += texture2D(texture0, texCoord + onePixel) * 5.0;
// You average the color columns so that you end up with
// a grayscale image that focuses on color differences in
// the original texture.
color.rgb = vec3((color.r + color.g + color.b) / 3.0);
gl_FragColor = vec4(color.rgb, 1);
}
Clean version.
#ifdef GL_ES
precision highp float;
#endif
uniform sampler2D texture0;
uniform float Scalus;
varying vec2 uv0Varying;
void main()
{
vec2 onePixel = vec2(1.0 / Scalus, 1.0 / Scalus);
vec2 texCoord = uv0Varying;
vec4 color;
color.rgb = vec3(0.5);
color -= texture2D(texture0, texCoord - onePixel) * 5.0;
color += texture2D(texture0, texCoord + onePixel) * 5.0;
color.rgb = vec3((color.r + color.g + color.b) / 3.0);
gl_FragColor = vec4(color.rgb, 1);
}
pixel shader wavy grass.
// highp and lowp. They define the importance of the quality
// of calculations/data storage. The higher precision means
// more precise data types will be used, and the calculations
// will be slower.
#ifdef GL_ES
precision highp float;
#endif
// constant values sent through from code.
uniform sampler2D texture0;
// Use this command in agk tier 1 to set the timer
// SetShaderConstantByName( shaderID, "name", set time here , 0, 0, 0 )
// set the agk command inside the loop and use timer() as a value
uniform float time;
// Anything that the vertex shader passes as output needs to
// be defined here as input. The vertex shader is passing the
// texture coordinate, so it is defined again here.
varying vec2 uv0Varying;
// These explains it self
// I where to lazy to make them agk internal settings
const float speed = 2.0;
const float bendFactor = 0.2;
void main()
{
// Calculate the texture offset based on the height.
// The texture starts from 0 at the top, so you need to
// invert it.
float height = 1.0 - uv0Varying.y;
// You don’t want the height value to influence the bending
// linearly, as you want the bottom of the texture to remain
// still. The bend should increase with the height of the
// grass, so a perfect fit here is an exponential function
// based on height.
float offset = pow(height, 2.5);
// multiply by sin since it gives us nice bending
offset *= (sin(time * speed) * bendFactor);
// Add your offset to the texCoord.x value. The fract
// function makes sure that when you get values that
// are < 0 or > 1, the texture repeats. Then use your
// offset color as the final fragment color.
vec3 normalColor = texture2D(texture0, fract(vec2(uv0Varying.x + offset, uv0Varying.y))).rgb;
gl_FragColor = vec4(normalColor, 1);
}
Clean version.
#ifdef GL_ES
precision highp float;
#endif
uniform sampler2D texture0;
uniform float time;
varying vec2 uv0Varying;
const float speed = 2.0;
const float bendFactor = 0.2;
void main()
{
float height = 1.0 - uv0Varying.y;
float offset = pow(height, 2.5);
offset *= (sin(time * speed) * bendFactor);
vec3 normalColor = texture2D(texture0, fract(vec2(uv0Varying.x + offset, uv0Varying.y))).rgb;
gl_FragColor = vec4(normalColor, 1);
}
Template shader (No texture, 1 dir light,1 point light)
vertex.
attribute vec3 position;
attribute vec3 normal;
varying vec3 normalVarying;
varying vec3 posVarying;
uniform mat4 agk_World;
uniform mat4 agk_ViewProj;
uniform mat3 agk_WorldNormal;
void main()
{
vec4 pos = agk_World * vec4(position,1);
gl_Position = agk_ViewProj * pos;
vec3 norm = agk_WorldNormal * normal;
posVarying = pos.xyz;
normalVarying = norm;
}
pixel.
varying vec3 normalVarying;
varying vec3 posVarying;
uniform vec4 agk_PLightPos;
uniform vec4 agk_PLightColor;
uniform vec3 agk_DLightDir;
uniform vec4 agk_DLightColor;
uniform vec4 agk_ObjColor;
void main()
{
vec3 dir = agk_PLightPos.xyz - posVarying;
vec3 norm = normalize(normalVarying);
float atten = dot(dir,dir);
atten = clamp(agk_PLightPos.w/atten,0.0,1.0);
float intensity = dot(normalize(dir),norm);
intensity = clamp(intensity,0.0,1.0);
vec3 color = agk_PLightColor.rgb * intensity * atten;
color = color + clamp(dot(-agk_DLightDir,norm)*agk_DLightColor.rgb,0.2,1.0);
gl_FragColor = vec4(color,1) * agk_ObjColor;
}
Android 2.3 , ZTE Skate , 480x800 , 800 mhz , Samsung Galaxy Y , 240x320 , 832 mhz , Sony ericson arc 480x854 , 1 ghz
Android 4.0 , Dmtech 3g 9738B , 1024x768 , 9.7 inches , 1.2 ghz