From b89a7ee8b956c96a1dcee995ea840feddc5d4b27 Mon Sep 17 00:00:00 2001 From: Pierre Frisch Date: Thu, 22 Dec 2011 07:25:50 -0800 Subject: First commit of Ninja to ninja-internal Signed-off-by: Valerio Virgillito --- assets/shaders/Basic.vert.glsl | 26 ++++ assets/shaders/linearGradient.frag.glsl | 52 +++++++ assets/shaders/linearGradient.vert.glsl | 48 ++++++ assets/shaders/test_fshader.glsl | 159 +++++++++++++++++++ assets/shaders/test_fshader_full.glsl | 82 ++++++++++ assets/shaders/test_vshader.glsl | 82 ++++++++++ assets/shaders/ub_fshader.glsl | 263 ++++++++++++++++++++++++++++++++ assets/shaders/ub_vshader.glsl | 162 ++++++++++++++++++++ 8 files changed, 874 insertions(+) create mode 100644 assets/shaders/Basic.vert.glsl create mode 100644 assets/shaders/linearGradient.frag.glsl create mode 100644 assets/shaders/linearGradient.vert.glsl create mode 100644 assets/shaders/test_fshader.glsl create mode 100644 assets/shaders/test_fshader_full.glsl create mode 100644 assets/shaders/test_vshader.glsl create mode 100644 assets/shaders/ub_fshader.glsl create mode 100644 assets/shaders/ub_vshader.glsl (limited to 'assets/shaders') diff --git a/assets/shaders/Basic.vert.glsl b/assets/shaders/Basic.vert.glsl new file mode 100644 index 00000000..028786d1 --- /dev/null +++ b/assets/shaders/Basic.vert.glsl @@ -0,0 +1,26 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + + +#ifdef GL_ES +precision highp float; +#endif + + +// attributes +attribute vec3 a_pos; + + + +// matrix uniforms +uniform mat4 u_mvMatrix; +uniform mat4 u_projMatrix; +uniform mat4 u_worldMatrix; + +void main(void) +{ + gl_Position = u_projMatrix * u_mvMatrix * vec4(a_pos,1.0) ; +} \ No newline at end of file diff --git a/assets/shaders/linearGradient.frag.glsl b/assets/shaders/linearGradient.frag.glsl new file mode 100644 index 00000000..64cf56ff --- /dev/null +++ b/assets/shaders/linearGradient.frag.glsl @@ -0,0 +1,52 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + +#ifdef GL_ES +precision highp float; +#endif + + +uniform vec4 u_color1; +uniform vec4 u_color2; +uniform vec4 u_color3; +uniform vec4 u_color4; +uniform float u_colorStop1; +uniform float u_colorStop2; +uniform float u_colorStop3; +uniform float u_colorStop4; +uniform vec2 u_cos_sin_angle; +//uniform int u_colorCount; // currently using 4 + +varying vec2 v_uv; + + +void main(void) +{ + float t = dot(v_uv, u_cos_sin_angle); + + vec4 color; + if (t < u_colorStop1) + color = u_color1; + else if (t < u_colorStop2) + { + float tLocal = (t - u_colorStop1)/(u_colorStop2 - u_colorStop1); + color = mix(u_color1,u_color2,tLocal); + } + else if (t < u_colorStop3) + { + float tLocal = (t - u_colorStop2)/(u_colorStop3 - u_colorStop2); + color = mix(u_color2,u_color3,tLocal); + } + else if (t < u_colorStop4) + { + float tLocal = (t - u_colorStop3)/(u_colorStop4 - u_colorStop3); + color = mix(u_color3,u_color4,tLocal); + } + else + color = u_color4; + + gl_FragColor =color; +} \ No newline at end of file diff --git a/assets/shaders/linearGradient.vert.glsl b/assets/shaders/linearGradient.vert.glsl new file mode 100644 index 00000000..aac9cbee --- /dev/null +++ b/assets/shaders/linearGradient.vert.glsl @@ -0,0 +1,48 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + + +#ifdef GL_ES +precision highp float; +#endif + + +// attributes +attribute vec3 vert; +attribute vec3 normal; +attribute vec2 texcoord; + +//uniform mat4 u_shadowLightWorld; +//uniform mat4 u_shadowBiasMatrix; +//uniform mat4 u_vShadowLight; +//uniform vec3 u_lightPos; + +// matrix uniforms +uniform mat4 u_mvMatrix; +uniform vec3 u_eye; +uniform mat4 u_normalMatrix; +uniform mat4 u_projMatrix; +uniform mat4 u_worldMatrix; + +uniform vec4 u_color1; +uniform vec4 u_color2; +uniform vec4 u_color3; +uniform vec4 u_color4; +uniform float u_colorStop1; +uniform float u_colorStop2; +uniform float u_colorStop3; +uniform float u_colorStop4; +uniform vec2 u_cos_sin_angle; +//uniform int u_colorCount; // currently using 4 + +varying vec2 v_uv; + + +void main(void) +{ + gl_Position = u_projMatrix * u_mvMatrix * vec4(vert,1.0) ; + v_uv = texcoord; +} diff --git a/assets/shaders/test_fshader.glsl b/assets/shaders/test_fshader.glsl new file mode 100644 index 00000000..3a0af39f --- /dev/null +++ b/assets/shaders/test_fshader.glsl @@ -0,0 +1,159 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + + +#ifdef GL_ES +precision highp float; +#endif + +// lighting uniforms +uniform vec3 u_light0Pos; +uniform vec4 u_light0Diff; +uniform vec4 u_light0Amb; + +// diffuse map +uniform sampler2D colMap; + +// environment map +uniform sampler2D envMap; + +// normal map +uniform sampler2D normalMap; + +// glow map +uniform sampler2D glowMap; + +// glow map +uniform sampler2D depthMap; + +//material uniforms +uniform vec4 u_matAmbient; +uniform vec4 u_matDiffuse; +uniform vec4 u_matSpecular; +uniform float u_matShininess; +uniform vec4 u_matEmission; +uniform float u_renderGlow; + +// varyings +varying vec4 vNormal; // w = texcoord.x +varying vec4 vECPos; // w = texcoord.y +varying vec3 vEyePos; +varying vec4 vShadowCoord; +varying vec2 vEnvTexCoord; +varying float vDiffuseIntensity; + +#ifdef PC + +void main() +{ + vec4 rgba_depth = texture2D(depthMap, vShadowCoord.xy/vShadowCoord.w, -32.0); + const vec4 bit_shift = vec4(1.0/(256.0*256.0*256.0), 1.0/(256.0*256.0), 1.0/256.0, 1.0); + float dist = vShadowCoord.w/200.0; + float d = dot(rgba_depth, bit_shift); + float shadowCoef = (dist > d + 0.00779) ? (0.6) : (1.0); + + vec4 colMapTexel = vec4(0); + if (u_renderGlow <= 0.5) { + colMapTexel = vec4(texture2D(colMap, vec2(vNormal.w, vECPos.w)).rgb, 1.0); + } else { + colMapTexel = vec4(texture2D(glowMap, vec2(vNormal.w, vECPos.w)).rgb, 1.0); + } + + // normal mapping + vec3 normal = normalize(vNormal.xyz); + vec3 mapNormal = texture2D(normalMap, vec2(vNormal.w, vECPos.w)).xyz * 2.0 - 1.0; + mapNormal = normalize(mapNormal.x*vec3(normal.z, 0.0, -normal.x) + vec3(0.0, mapNormal.y, 0.0) + mapNormal.z*normal); + + // create envmap coordinates + vec3 r = reflect( normalize(vec3(vECPos.xyz - vEyePos.xyz)), mapNormal); + float m = 2.0 * sqrt( r.x*r.x + r.y*r.y + r.z*r.z ); + + // calculate environment map texel + vec4 envMapTexel = vec4(texture2D(envMap, vec2(r.x/m + 0.5, r.y/m + 0.5)).rgb, 0.0); + + // lighting + vec3 lightDirection = u_light0Pos - vECPos.xyz; + float lightDist = length(lightDirection); + lightDirection /= lightDist; + + float attenuation = clamp(1.0 - lightDist * 0.01, 0.0, 1.0); + + vec3 halfVec = normalize(lightDirection + vEyePos); + + float diffuseIntensity = max(0.0, dot(mapNormal, lightDirection)); + float specularModifier = max(0.0, dot(mapNormal, halfVec)); + + float pf; + if(diffuseIntensity == 0.0) + pf = 0.0; + else + pf = pow(specularModifier, 76.0); + + vec4 ambient = u_matAmbient * u_light0Amb; + + vec4 diffuse = u_matDiffuse * (colMapTexel + envMapTexel)*shadowCoef; + + if (u_renderGlow <= 0.5) { + diffuse *= u_light0Diff; + } + + vec4 specular = 2.0 * pf * envMapTexel; + + //gl_FragColor = vec4(dist, dist, dist, 1.0); + gl_FragColor = ((colMapTexel*(ambient + diffuse)) + specular); +} + +#endif + +#ifdef DEVICE + +void main() +{ + vec4 colMapTexel = vec4(texture2D(colMap, vec2(vNormal.w, vECPos.w)).rgb, 1.0); + +// // normal mapping + vec3 normal = normalize(vNormal.xyz); +// vec3 mapNormal = texture2D(normalMap, vec2(vNormal.w, vECPos.w)).xyz * 2.0 - 1.0; +// mapNormal = normalize(mapNormal.x*vec3(normal.z, 0.0, -normal.x) + vec3(0.0, mapNormal.y, 0.0) + mapNormal.z*normal); +// +// // create envmap coordinates +// vec3 r = reflect( (vec3(vECPos.xyz - vEyePos.xyz)), mapNormal); +// float m = 2.0 * length(r); + + // calculate environment map texel + //vec4 envMapTexel = vec4(texture2D(envMap, vec2(r.x/m + 0.5, r.y/m + 0.5)).rgb, 0.0); + vec4 envMapTexel = vec4(texture2D(envMap, vEnvTexCoord).rgb, 0.0); + + // lighting + //vec3 lightDirection = normalize(u_light0Pos - vECPos.xyz); +// float lightDist = length(lightDirection); +// lightDirection /= lightDist; + + + //vec3 halfVec = normalize(lightDirection + vEyePos); + + //float diffuseIntensity = max(0.0, dot(normal, lightDirection)); + // float specularModifier = max(0.0, dot(mapNormal, halfVec)); + + // float pf; + //if(diffuseIntensity == 0.0) + //pf = 0.0; + //else + //pf = pow(specularModifier, 76.0); + + // vec4 ambient = u_matAmbient * u_light0Amb; + + vec4 diffuse = u_matDiffuse*(colMapTexel + envMapTexel)*(vDiffuseIntensity + u_matAmbient); + + //diffuse *= u_light0Diff * diffuseIntensity; + + //vec4 specular = envMapTexel; + + //gl_FragColor = vec4(dist, dist, dist, 1.0); + gl_FragColor = diffuse;//(colMapTexel*(ambient + diffuse)) + specular; +} + +#endif \ No newline at end of file diff --git a/assets/shaders/test_fshader_full.glsl b/assets/shaders/test_fshader_full.glsl new file mode 100644 index 00000000..ef721b39 --- /dev/null +++ b/assets/shaders/test_fshader_full.glsl @@ -0,0 +1,82 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + + +#ifdef GL_ES +precision highp float; +#endif + +// lighting uniforms +uniform vec3 u_lightPos; +uniform vec4 u_lightDiff; +uniform vec4 u_lightAmb; + +// diffuse map +uniform sampler2D colMap; + +// environment map +uniform sampler2D envMap; + +// normal map +uniform sampler2D normalMap; + +//material uniforms +uniform vec4 u_matAmbient; +uniform vec4 u_matDiffuse; +uniform vec4 u_matSpecular; +uniform float u_matShininess; +uniform vec4 u_matEmission; +uniform float u_renderGlow; + +// varyings +varying vec4 vNormal; // w = texcoord.x +varying vec4 vECPos; // w = texcoord.y +varying vec3 vEyePos; + +void main() +{ + vec4 colMapTexel = vec4(texture2D(colMap, vec2(vNormal.w, vECPos.w)).rgb, 1.0); + + // normal mapping + vec3 normal = normalize(vNormal.xyz); + vec3 mapNormal = texture2D(normalMap, vec2(vNormal.w, vECPos.w)).xyz * 2.0 - 1.0; + mapNormal = normalize(mapNormal.x*vec3(normal.z, 0.0, -normal.x) + vec3(0.0, mapNormal.y, 0.0) + mapNormal.z*normal); + + // create envmap coordinates + vec3 r = reflect( normalize(vec3(vECPos.xyz - vEyePos.xyz)), mapNormal); + float m = 2.0 * sqrt( r.x*r.x + r.y*r.y + r.z*r.z ); + + // calculate environment map texel + vec4 envMapTexel = vec4(texture2D(envMap, vec2(r.x/m + 0.5, r.y/m + 0.5)).rgb, 0.0); + + // lighting + vec3 lightDirection = u_lightPos - vECPos.xyz; + float lightDist = length(lightDirection); + lightDirection /= lightDist; + + float attenuation = clamp(1.0 - lightDist * 0.01, 0.0, 1.0); + + vec3 halfVec = normalize(lightDirection + vEyePos); + + float diffuseIntensity = max(0.0, dot(mapNormal, lightDirection)); + float specularModifier = max(0.0, dot(mapNormal, halfVec)); + + float pf; + if(diffuseIntensity == 0.0) + pf = 0.0; + else + pf = pow(specularModifier, 76.0); + + vec4 ambient = u_matAmbient * u_lightAmb; + vec4 diffuse = u_matDiffuse * (colMapTexel + envMapTexel); + + if (u_renderGlow <= 0.5) + diffuse *= u_lightDiff * diffuseIntensity * attenuation; + + vec4 specular = 2.0 * pf * envMapTexel; + + gl_FragColor = (colMapTexel*(ambient + diffuse)) + specular + vec4(0.0,0.0,0.0,1.0); +} diff --git a/assets/shaders/test_vshader.glsl b/assets/shaders/test_vshader.glsl new file mode 100644 index 00000000..a02d9720 --- /dev/null +++ b/assets/shaders/test_vshader.glsl @@ -0,0 +1,82 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + + +attribute vec3 vert; +attribute vec3 normal; +attribute vec2 texcoord; + +// matrix uniforms +uniform mat4 u_mvMatrix; +uniform vec3 u_eye; +uniform mat4 u_normalMatrix; +uniform mat4 u_projMatrix; +uniform mat4 u_worldMatrix; + +uniform mat4 u_shadowLightWorld; +uniform mat4 u_shadowBiasMatrix; +uniform mat4 u_vShadowLight; +uniform vec3 u_lightPos; + +// varyings +varying vec4 vNormal; // w = texcoord.x +varying vec4 vECPos; // w = texcoord.y +varying vec3 vEyePos; +varying vec4 vShadowCoord; +varying vec2 vEnvTexCoord; +varying float vDiffuseIntensity; + +#ifdef PC +void main() +{ + vNormal.w = texcoord.x; + vECPos.w = texcoord.y; + vEyePos = u_eye; + +// position normals and vert + vECPos.xyz = (u_mvMatrix*vec4(vert, 1.0)).xyz; + vNormal.xyz = (u_normalMatrix*vec4(normal, 0.0)).xyz; + +// pass along the geo + gl_Position = u_projMatrix * vec4(vECPos.xyz, 1.0); + + mat4 shadowMat = u_shadowBiasMatrix*u_projMatrix*u_vShadowLight*u_worldMatrix; + vShadowCoord = shadowMat * vec4(vert, 1.0); +} +#endif + +#ifdef DEVICE + +void main() +{ + vNormal.w = texcoord.x; + vECPos.w = texcoord.y; + vEyePos = u_eye; + +// position normals and vert + vECPos.xyz = (u_mvMatrix*vec4(vert, 1.0)).xyz; + vNormal.xyz = (u_normalMatrix*vec4(normal, 0.0)).xyz; + +// pass along the geo + gl_Position = u_projMatrix * vec4(vECPos.xyz, 1.0); + + //mat4 shadowMat = u_shadowBiasMatrix*u_projMatrix*u_vShadowLight*u_worldMatrix; + //vShadowCoord = shadowMat * vec4(vert, 1.0); + + // normal mapping + vec3 normal = normalize(vNormal.xyz); + + // create envmap coordinates + vec3 r = reflect( vec3(vECPos.xyz - u_eye.xyz), normal); + float m = 2.0 * length(r); + vEnvTexCoord = vec2(r.x/m + 0.5, r.y/m + 0.5); + + vec3 lightDirection = normalize(u_lightPos - vECPos.xyz); + vDiffuseIntensity = max(0.0, dot(normal, lightDirection)); + +} + +#endif diff --git a/assets/shaders/ub_fshader.glsl b/assets/shaders/ub_fshader.glsl new file mode 100644 index 00000000..19a35b7d --- /dev/null +++ b/assets/shaders/ub_fshader.glsl @@ -0,0 +1,263 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + +// defines +#if defined( GL_ES ) + precision highp float; +#endif + +uniform mat4 u_viewMatrix; + +#if defined( MATERIAL ) + uniform vec4 u_ambientColor; + uniform vec4 u_diffuseColor; + uniform vec4 u_specularColor; + uniform float u_specularPower; +#endif + +#if defined( LIGHTING ) + varying vec3 v_normal; + #if defined( LIGHT_0 ) +// uniform int u_light0Type; + uniform vec3 u_light0Pos; + uniform vec3 u_light0Dir; + uniform vec3 u_light0Atten; + uniform vec2 u_light0Spot; + uniform vec4 u_light0Color; + uniform vec4 u_light0Specular; + varying vec3 v_light0Dir; + varying vec3 v_light0SpotDir; + #endif + + #if defined( LIGHT_1 ) +// uniform int u_light1Type; + uniform vec3 u_light1Pos; + uniform vec3 u_light1Dir; + uniform vec3 u_light1Atten; + uniform vec2 u_light1Spot; + uniform vec4 u_light1Color; + uniform vec4 u_light1Specular; + varying vec3 v_light1Dir; + varying vec3 v_light1SpotDir; + #endif + + #if defined( LIGHT_2 ) +// uniform int u_light2Type; + uniform vec3 u_light2Pos; + uniform vec3 u_light2Dir; + uniform vec3 u_light2Atten; + uniform vec2 u_light2Spot; + uniform vec4 u_light2Color; + uniform vec4 u_light2Specular; + varying vec3 v_light2Dir; + varying vec3 v_light2SpotDir; + #endif + + #if defined( LIGHT_3 ) +// uniform int u_light3Type; + uniform vec3 u_light3Pos; + uniform vec3 u_light3Dir; + uniform vec3 u_light3Atten; + uniform vec2 u_light3Spot; + uniform vec4 u_light3Color; + uniform vec4 u_light3Specular; + varying vec3 v_light3Dir; + varying vec3 v_light3SpotDir; + #endif +#endif + +#if defined( ENVIRONMENT_MAP ) + uniform float u_envReflection; +#endif + +uniform vec3 u_eye; + +uniform sampler2D s_diffuseMap; +uniform sampler2D s_normalMap; +uniform sampler2D s_envMap; +uniform sampler2D s_specMap; + +varying vec3 v_mvPos; +varying vec3 v_eyeDir; +varying vec2 v_texcoord; + +void main() { + // these are the four principle color elements making up the final fragment equation. + vec4 a = vec4(0.0,0.0,0.0,0.0); // ambient contribution + vec4 d = vec4(0.0,0.0,0.0,0.0); // diffuse contribution + vec4 s = vec4(0.0,0.0,0.0,0.0); // specular contribution + vec4 l = vec4(0.0,0.0,0.0,0.0); // lighting contribution + +#if defined( MATERIAL ) + a += u_ambientColor; + d += u_diffuseColor; +#endif + +#if defined( DIFFUSE_MAP ) + d *= texture2D(s_diffuseMap, v_texcoord); +#endif + +#if ( defined( LIGHTING ) || defined( ENVIRONMENT_MAPPING ) ) + vec3 normal = normalize( v_normal ); +#endif + +#if defined( LIGHTING ) + #if defined( NORMAL_MAP ) + vec4 normalMap = texture2D(s_normalMap, v_texcoord); + normalMap = vec4( (normalMap.xyz * 2.0 - 1.0), 0.0 ); + normal = normalize(normalMap.x*vec3(normal.z, 0.0, normal.x) + vec3(0.0, normalMap.y, 0.0) + normalMap.z*normal); + #endif // NORMAL_MAP + + #if defined( LIGHT_0 ) + { + // diffuse lighting + float ldist = length( v_light0Dir.xyz ); + vec3 ldir = v_light0Dir.xyz / ldist; + + float atten = 1.0; + + #if ( LIGHT_0 > 0 ) + atten = 1.0 / ( u_light0Atten.x + u_light0Atten.y * ldist + u_light0Atten.z * ( ldist * ldist ) ); + #if (LIGHT_0 == 2) + float spotAngle = dot( ldir, normalize( v_light0SpotDir ) ); + float spotAtten = 0.0; + if ( spotAngle > u_light0Spot.y ) { + spotAtten = min(1.0, max( 0.0, ( spotAngle - u_light0Spot.y ) / ( u_light0Spot.x - u_light0Spot.y ) ) ); + } + atten *= spotAtten; + #endif + #endif + + float ndotl = max( 0.0, dot( normal, ldir ) ); + l += ndotl * atten * u_light0Color; + + #if defined( LIGHT_0_SPECULAR ) + // specular contribution + vec3 halfAngleVec = normalize( normalize( v_light0Dir.xyz ) + vec3(0.0,0.0,1.0) ); + float ndoth = max( 0.0, dot( normal, halfAngleVec ) ); + s += atten * pow( ndoth, u_specularPower ) * (u_specularColor * u_light0Specular); + #endif + } + #endif // LIGHT_0 + + #if defined( LIGHT_1 ) + { + // diffuse lighting + float ldist = length( v_light1Dir.xyz ); + vec3 ldir = v_light1Dir.xyz / ldist; + + float atten = 1.0; + + #if ( LIGHT_1 > 0 ) + atten = 1.0 / ( u_light1Atten.x + u_light1Atten.y * ldist + u_light1Atten.z * ( ldist * ldist ) ); + #if (LIGHT_1 == 2) + float spotAngle = dot( ldir, normalize( v_light1SpotDir ) ); + float spotAtten = 0.0; + if ( spotAngle > u_light1Spot.y ) { + spotAtten = min(1.0, max( 0.0, ( spotAngle - u_light1Spot.y ) / ( u_light1Spot.x - u_light1Spot.y ) ) ); + } + atten *= spotAtten; + #endif + #endif + + float ndotl = max( 0.0, dot( normal, ldir ) ); + l += ndotl * atten * u_light1Color; + + #if defined( LIGHT_1_SPECULAR ) + // specular contribution + vec3 halfAngleVec = normalize( normalize( v_light1Dir.xyz ) + vec3(0.0,0.0,1.0) ); + float ndoth = max( 0.0, dot( normal, halfAngleVec ) ); + s += atten * pow( ndoth, u_specularPower ) * (u_specularColor * u_light1Specular); + #endif + } + #endif // LIGHT_1 + + #if defined( LIGHT_2 ) + { + // diffuse lighting + float ldist = length( v_light2Dir.xyz ); + vec3 ldir = v_light2Dir.xyz / ldist; + + float atten = 1.0; + + #if ( LIGHT_2 > 0 ) + atten = 1.0 / ( u_light2Atten.x + u_light2Atten.y * ldist + u_light2Atten.z * ( ldist * ldist ) ); + #if (LIGHT_2 == 2) + float spotAngle = dot( ldir, normalize( v_light2SpotDir ) ); + float spotAtten = 0.0; + if ( spotAngle > u_light2Spot.y ) { + spotAtten = min(1.0, max( 0.0, ( spotAngle - u_light2Spot.y ) / ( u_light2Spot.x - u_light2Spot.y ) ) ); + } + atten *= spotAtten; + #endif + #endif + + float ndotl = max( 0.0, dot( normal, ldir ) ); + l += ndotl * atten * u_light2Color; + + #if defined( LIGHT_2_SPECULAR ) + // specular contribution + vec3 halfAngleVec = normalize( normalize( v_light2Dir.xyz ) + vec3(0.0,0.0,1.0) ); + float ndoth = max( 0.0, dot( normal, halfAngleVec ) ); + s += atten * pow( ndoth, u_specularPower ) * (u_specularColor * u_light2Specular); + #endif + } + #endif // LIGHT_2 + + #if defined( LIGHT_3 ) + { + // diffuse lighting + float ldist = length( v_light3Dir.xyz ); + vec3 ldir = v_light3Dir.xyz / ldist; + + float atten = 1.0; + + #if ( LIGHT_3 > 0 ) + atten = 1.0 / ( u_light3Atten.x + u_light3Atten.y * ldist + u_light3Atten.z * ( ldist * ldist ) ); + #if (LIGHT_3 == 2) + float spotAngle = dot( ldir, normalize( v_light3SpotDir ) ); + float spotAtten = 0.0; + if ( spotAngle > u_light3Spot.y ) { + spotAtten = min(1.0, max( 0.0, ( spotAngle - u_light3Spot.y ) / ( u_light3Spot.x - u_light3Spot.y ) ) ); + } + atten *= spotAtten; + #endif + #endif + + float ndotl = max( 0.0, dot( normal, ldir ) ); + l += ndotl * atten * u_light3Color; + + #if defined( LIGHT_3_SPECULAR ) + // specular contribution + vec3 halfAngleVec = normalize( normalize( v_light3Dir.xyz ) + vec3(0.0,0.0,1.0) ); + float ndoth = max( 0.0, dot( normal, halfAngleVec ) ); + s += atten * pow( ndoth, u_specularPower ) * (u_specularColor * u_light3Specular); + #endif + } + #endif // LIGHT_3 +#endif // LIGHTING + +#if defined( SPECULAR ) && defined( SPECULAR_MAP ) + vec4 specMapColor = texture2D(s_specMap, v_texcoord); + s *= specMapColor; +#endif + +#if defined( ENVIRONMENT_MAP ) + vec3 r = reflect( normalize( vec3(0.0,0.0,1.0) ), normal ); + float m = 2.0 * length(r); + vec4 envMapColor = texture2D(s_envMap, vec2(r.x/m + 0.5, r.y/m + 0.5)) * u_envReflection; + + #if defined( GLOSS_MAP ) + // this is an option to modulate the alpha channel of the specular map with the environment + // map (i.e. - gloss mapping). + envMapColor *= specMapColor.a; + #endif + + s += envMapColor; +#endif + + gl_FragColor = ( a + l ) * d + s; +} \ No newline at end of file diff --git a/assets/shaders/ub_vshader.glsl b/assets/shaders/ub_vshader.glsl new file mode 100644 index 00000000..ff0c09b4 --- /dev/null +++ b/assets/shaders/ub_vshader.glsl @@ -0,0 +1,162 @@ +/* +This file contains proprietary software owned by Motorola Mobility, Inc.
+No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
+(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. +
*/ + +// attributes +attribute vec3 a_pos; +attribute vec3 a_normal; +attribute vec2 a_texcoord; + +// uniforms +uniform mat4 u_worldMatrix; +uniform mat4 u_viewMatrix; +uniform mat4 u_projMatrix; +uniform mat4 u_mvMatrix; +uniform mat4 u_normalMatrix; +uniform mat4 u_uvMatrix; +uniform vec3 u_eye; + +// attributes +#if defined( MATERIAL ) + uniform vec4 u_ambientColor; + uniform vec4 u_diffuseColor; + uniform vec4 u_specularColor; + uniform float u_specularPower; +#endif + +#if defined( LIGHTING ) + varying vec3 v_normal; + #if defined( LIGHT_0 ) + uniform int u_light0Type; + uniform vec3 u_light0Pos; + uniform vec3 u_light0Dir; + uniform vec3 u_light0Atten; + uniform vec2 u_light0Spot; + uniform vec4 u_light0Color; + uniform vec4 u_light0Specular; + varying vec3 v_light0Dir; + varying vec3 v_light0SpotDir; + #endif + + #if defined( LIGHT_1 ) + uniform int u_light1Type; + uniform vec3 u_light1Pos; + uniform vec3 u_light1Dir; + uniform vec3 u_light1Atten; + uniform vec2 u_light1Spot; + uniform vec3 u_light1Color; + uniform vec4 u_light1Specular; + varying vec3 v_light1Dir; + varying vec3 v_light1SpotDir; + #endif + + #if defined( LIGHT_2 ) + uniform int u_light2Type; // 0 - dir, 1 - point, 2 - spot + uniform vec3 u_light2Pos; + uniform vec3 u_light2Dir; + uniform vec3 u_light2Atten; + uniform vec2 u_light2Spot; // 0 - spot light cutoff angle, 1 - spot light exponent + uniform vec3 u_light2Color; + uniform vec4 u_light2Specular; + varying vec3 v_light2Dir; + varying vec3 v_light2SpotDir; + #endif + + #if defined( LIGHT_3 ) + uniform int u_light3Type; + uniform vec3 u_light3Pos; + uniform vec3 u_light3Dir; + uniform vec3 u_light3Atten; + uniform vec2 u_light3Spot; + uniform vec3 u_light3Color; + uniform vec4 u_light3Specular; + varying vec3 v_light3Dir; + varying vec3 v_light3SpotDir; + #endif +#endif + +#if defined( ENVIRONMENT_MAP ) + uniform float u_envReflection; +#endif + +varying vec3 v_mvPos; +varying vec3 v_eyeDir; +varying vec2 v_texcoord; + +#if defined( PC ) +void main() { + // position normals and vert + v_mvPos = ( u_mvMatrix * vec4( a_pos, 1.0 ) ).xyz; + + v_texcoord = (u_uvMatrix * vec4(a_texcoord, 0, 1.0)).xy; +#if ( defined( LIGHTING ) || defined( ENVIRONMENT_MAP ) ) + v_normal = ( u_normalMatrix * vec4( a_normal, 0.0 ) ).xyz; +#endif + +#if defined( LIGHTING ) + v_eyeDir = vec3(-u_viewMatrix[3][0], -u_viewMatrix[3][1], -u_viewMatrix[3][2]); + #if defined( LIGHT_0 ) + { + vec3 lpos = ( u_viewMatrix * vec4( u_light0Pos, 1.0 ) ).xyz; + + #if ( LIGHT_0 == 0 ) + v_light0Dir = ( u_viewMatrix * vec4( -u_light0Dir, 0.0 ) ).xyz; + #else + v_light0Dir = lpos - v_mvPos; + #if ( LIGHT_0 == 2 ) + v_light0SpotDir = (u_viewMatrix * vec4( -u_light0Dir, 0.0 )).xyz; + #endif // ( LIGHT_0 == 2 ) + #endif // ( LIGHT_0 == 0 ) + } + #endif // defined( LIGHT_0 ) + #if defined( LIGHT_1 ) + { + vec3 lpos = ( u_viewMatrix * vec4( u_light1Pos, 1.0 ) ).xyz; + + #if ( LIGHT_1 == 0 ) + v_light1Dir = ( u_viewMatrix * vec4( -u_light1Dir, 0.0 ) ).xyz; + #else + v_light1Dir = lpos - v_mvPos; + #if ( LIGHT_1 == 2 ) + v_light1SpotDir = (u_viewMatrix * vec4( -u_light1Dir, 0.0 )).xyz; + #endif // ( LIGHT_1 == 2 ) + #endif // ( LIGHT_1 == 0 ) + } + #endif // defined( LIGHT_1 ) + #if defined( LIGHT_2 ) + { + vec3 lpos = ( u_viewMatrix * vec4( u_light2Pos, 1.0 ) ).xyz; + + #if ( LIGHT_2 == 0 ) + v_light2Dir = ( u_viewMatrix * vec4( -u_light2Dir, 0.0 ) ).xyz; + #else + v_light2Dir = lpos - v_mvPos; + #if ( LIGHT_2 == 2 ) + v_light2SpotDir = (u_viewMatrix * vec4( -u_light2Dir, 0.0 )).xyz; + #endif // ( LIGHT_2 == 2 ) + #endif // ( LIGHT_2 == 0 ) + } + #endif // defined( LIGHT_2 ) + #if defined( LIGHT_3 ) + { + vec3 lpos = ( u_viewMatrix * vec4( u_light3Pos, 1.0 ) ).xyz; + + #if ( LIGHT_3 == 0 ) + v_light3Dir = ( u_viewMatrix * vec4( -u_light3Dir, 0.0 ) ).xyz; + #else + v_light3Dir = lpos - v_mvPos; + #if ( LIGHT_3 == 2 ) + v_light3SpotDir = (u_viewMatrix * vec4( -u_light3Dir, 0.0 )).xyz; + #endif // ( LIGHT_3 == 2 ) + #endif // ( LIGHT_3 == 0 ) + } + #endif // defined( LIGHT_3 ) +#endif // defined( LIGHTING ) + + // pass along the geo + gl_Position = u_projMatrix * vec4(v_mvPos, 1.0); +} +#endif + -- cgit v1.2.3