Only to show how complicated it sometimes get for a cross platform language
And the mess tgc have to make it work .
This is only wath i learned so far about opengl es shaders and wath varies on desktop and mobile device.
My current shader is this and runs at 415 fps on my pc and 13-14 fps on my slow 800 mhz zte skate that dont play 3d games from goggle play to well.
This shader also shows how you could make android/iphone not use some fx and computers show all the glory.
This is my latest pixel frag shader that gives the same display on mobile device and pc but runs better then the old on mobile.
// Check if its android/iphone and not desktop open gl to make it cross platform
// things like lowp,mediump and highp only works on mobile version
// normalize is very costly on iphone and android and only used when its really neaded
#ifdef GL_ES
precision lowp float;
uniform lowp vec4 agk_ObjColor;
uniform lowp vec4 agk_PLightPos;
uniform lowp vec4 agk_PLightColor;
uniform lowp vec4 agk_PLightPos2;
uniform lowp vec4 agk_PLightColor2;
uniform lowp vec3 agk_DLightDir;
uniform lowp vec4 agk_DLightColor;
varying lowp vec2 uvVarying;
varying lowp vec3 normalVarying;
varying lowp vec4 posVarying;
uniform sampler2D texture0;
uniform sampler2D texture1;
void main()
{
lowp vec2 onePixel = vec2(1.0 / 480.0, 1.0 / 480.0);
vec4 bump;
bump.rgb = vec3(0.5);
bump -= texture2D(texture1, uvVarying.xy - onePixel) * 8.0;
bump += texture2D(texture1, uvVarying.xy + onePixel) * 8.0;
bump.rgb = clamp(vec3((bump.r + bump.g + bump.b) / 8.0),0.22,1.0);
lowp vec3 dir = agk_PLightPos.xyz - posVarying.xyz;
lowp float atten = dot(dir,dir);
atten = clamp(agk_PLightPos.w/atten,0.01,1.0);
lowp float intensity = dot(normalize(dir),normalVarying);
intensity = clamp(intensity,0.5,1.0);
lowp float distance = clamp(length(dir),74.0,10000.0)/640.0;
lowp vec3 dir2 = agk_PLightPos2.xyz - posVarying.xyz;
lowp float atten2 = dot(dir2,dir2);
atten2 = clamp(agk_PLightPos2.w/atten2,0.01,1.0);
lowp float intensity2 = dot(normalize(dir2),normalVarying);
intensity2 = clamp(intensity2,0.01,1.0);
lowp vec3 color = clamp(clamp(agk_PLightColor.rgb * intensity * atten,0.01,4.0) + clamp(agk_PLightColor2.rgb * intensity2 * atten2,0.01,1.6)*bump.rgb,0.01,0.5);
gl_FragColor = (texture2D(texture0, uvVarying.xy ) * vec4(( color + clamp(dot(-agk_DLightDir,normalVarying)*agk_DLightColor.rgb,0.0,0.2) ),1) * agk_ObjColor)/distance;
}
#else
uniform vec4 agk_ObjColor;
uniform vec4 agk_PLightPos;
uniform vec4 agk_PLightColor;
uniform vec4 agk_PLightPos2;
uniform vec4 agk_PLightColor2;
uniform vec3 agk_DLightDir;
uniform vec4 agk_DLightColor;
varying vec2 uvVarying;
varying vec3 normalVarying;
varying vec4 posVarying;
uniform sampler2D texture0;
uniform sampler2D texture1;
void main()
{
vec2 onePixel = vec2(1.0 / 480.0, 1.0 / 480.0);
vec4 bump;
bump.rgb = vec3(0.5);
bump -= texture2D(texture1, uvVarying.xy - onePixel) * 8.0;
bump += texture2D(texture1, uvVarying.xy + onePixel) * 8.0;
bump.rgb = clamp(vec3((bump.r + bump.g + bump.b) / 8.0),0.22,1.0);
vec3 dir = agk_PLightPos.xyz - posVarying.xyz;
vec3 norm = normalize(normalVarying);
float atten = dot(dir,dir);
atten = clamp(agk_PLightPos.w/atten,0.01,1.0);
float intensity = dot(normalize(dir),norm);
intensity = clamp(intensity,0.5,1.0);
float distance = clamp(length(dir),74.0,10000.0)/640.0;
vec3 dir2 = agk_PLightPos2.xyz - posVarying.xyz;
float atten2 = dot(dir2,dir2);
atten2 = clamp(agk_PLightPos2.w/atten2,0.01,1.0);
float intensity2 = dot(normalize(dir2),norm);
intensity2 = clamp(intensity2,0.01,1.0);
vec3 color = clamp(clamp(agk_PLightColor.rgb * intensity * atten,0.01,4.0) + clamp(agk_PLightColor2.rgb * intensity2 * atten2,0.01,1.6)*bump.rgb,0.01,0.5);
gl_FragColor = (texture2D(texture0, uvVarying.xy ) * vec4(( color + clamp(dot(-agk_DLightDir,norm)*agk_DLightColor.rgb,0.0,0.2) ),1) * agk_ObjColor)/distance;
}
#endif
Here is the vertex shader that goes with it.
attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
varying vec2 uvVarying;
varying vec3 normalVarying;
varying vec4 posVarying;
uniform vec4 uvBounds0;
uniform mat4 agk_World;
uniform mat4 agk_ViewProj;
uniform mat3 agk_WorldNormal;
uniform vec2 tiling;
void main()
{
posVarying = agk_World * vec4(position,1);
normalVarying = agk_WorldNormal * normal;
uvVarying = uv * (uvBounds0.xy+tiling) + uvBounds0.zw;
gl_Position = agk_ViewProj * posVarying;
}
If anyone else could test this with the template i posted before and let me know of your device and framerate?
Iam going to finetune the pc shader later on as it dont nead all clamping like mobile devices.
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