2 precision mediump float;
4 uniform mediump mat4 uProjection;
5 uniform mediump mat4 uModelView;
6 uniform mediump mat4 uMvpMatrix;
8 uniform bool uTextureMapped;
9 uniform mediump vec4 uCustomTextureCoords;
10 attribute mediump vec2 aTexCoord;
11 varying mediump vec2 vTexCoord;
13 uniform mat3 uModelViewIT;
14 attribute mediump vec3 aNormal;
15 varying mediump vec3 vNormal;
17 attribute mediump vec3 aPosition;
18 varying mediump vec4 vVertex;
20 #define MAX_BONES_PER_MESH 12
23 uniform int uBoneCount;
24 uniform mediump mat4 uBoneMatrices[MAX_BONES_PER_MESH];
25 uniform mediump mat3 uBoneMatricesIT[MAX_BONES_PER_MESH];
26 attribute mediump vec4 aBoneWeights;
27 attribute mediump vec4 aBoneIndices;
32 mediump vec4 vertexPosition = vec4(aPosition, 1.0);
33 mediump float lightIntensity;
38 mediump vec4 boneWeights = aBoneWeights;
39 mediump ivec4 boneIndices = ivec4(aBoneIndices);
40 mediump vec3 vertexNormal;
42 // re-calculate the final weight
43 boneWeights.w = 1.0 - dot(boneWeights.xyz, vec3(1.0, 1.0, 1.0));
45 vec4 bonePos = (uBoneMatrices[boneIndices.x] * vertexPosition) * boneWeights.x;
46 bonePos += (uBoneMatrices[boneIndices.y] * vertexPosition) * boneWeights.y;
47 bonePos += (uBoneMatrices[boneIndices.z] * vertexPosition) * boneWeights.z;
48 bonePos += (uBoneMatrices[boneIndices.w] * vertexPosition) * boneWeights.w;
50 vertexNormal = (uBoneMatricesIT[boneIndices.x] * aNormal) * boneWeights.x;
51 vertexNormal += (uBoneMatricesIT[boneIndices.y] * aNormal) * boneWeights.y;
52 vertexNormal += (uBoneMatricesIT[boneIndices.z] * aNormal) * boneWeights.z;
53 vertexNormal += (uBoneMatricesIT[boneIndices.w] * aNormal) * boneWeights.w;
54 vertexNormal = normalize(vertexNormal);
56 vertexPosition = uProjection * bonePos;
58 vNormal = vertexNormal;
63 vertexPosition = uMvpMatrix * vec4(aPosition, 1.0);
64 vVertex = uModelView * vec4(aPosition, 1.0);
65 vNormal = mat3(uModelViewIT) * aNormal;
69 gl_Position = vertexPosition;
71 mediump vec2 start = uCustomTextureCoords.xy;
72 mediump vec2 scale = uCustomTextureCoords.zw;
73 vTexCoord = vec2(start.x + aTexCoord.x * scale.x, start.y + aTexCoord.y * scale.y);
79 precision mediump float;
83 mediump float mOpacity;
84 mediump float mShininess;
91 uniform sampler2D sTexture;
92 uniform sampler2D sOpacityTexture;
93 uniform sampler2D sNormalMapTexture;
94 uniform sampler2D sEffect;
95 varying vec2 vTexCoord;
97 uniform Material uMaterial;
99 uniform lowp vec4 uColor;
100 varying highp vec4 vVertex;
101 varying highp vec3 vNormal;
106 int mType; // 0=AMBIENT,1=DIRECTIONAL,2=SPOT,3=POINT
107 highp vec2 mFallOff; // x,y = falloff start, falloff end
108 mediump vec2 mSpotAngle; // x,y = inner cone and outer cone
109 mediump vec3 mLightPos; // position
110 mediump vec3 mLightDir; // directional (for direction/spot lights)
111 lowp vec3 mAmbient; // ambient component of the light's color
112 lowp vec3 mDiffuse; // diffuse component of the light's color
113 lowp vec3 mSpecular; // specular component of the light's color
116 uniform int uNumberOfLights;
117 uniform Light uLight0;
119 lowp vec3 lightColor;
120 lowp vec3 specularColor;
122 void calculateLight(Light light)
124 // Ensure that the varying vertex position doesn't lose precision
125 highp vec3 lightVector = light.mLightPos - vVertex.xyz;
126 vec3 N = normalize(vNormal);
127 vec3 L = normalize(lightVector);
128 // TODO: for directional light, should use mLightDir for light direction not lightVector
129 float NdotL = dot(N, L);
131 vec3 color = light.mAmbient * uMaterial.mAmbient.rgb;
132 color += light.mDiffuse * uMaterial.mDiffuse.rgb * abs(NdotL);
135 highp float attenuation = 1.0; // requires highp
136 if (light.mType >= 2)
138 attenuation -= smoothstep(light.mFallOff.x, light.mFallOff.y, length(lightVector));
143 // add color to cumulative light total. TODO: don't attenuate directional light
144 lightColor += color * attenuation;
146 if (light.mType != 0 && NdotL > 0.0 && light.mType != 0)
148 // Specular highlight
149 vec3 E = normalize(vVertex.xyz);
150 vec3 R = reflect(L, N);
151 float specular = pow(max(dot(R, E), 0.0), uMaterial.mShininess);
152 specularColor += uMaterial.mSpecular.rgb * light.mSpecular * specular * attenuation;
159 // sample the texture for the initial color
160 vec4 fragColor = texture2D(sTexture, vTexCoord);
164 // apply lighting and material properties
165 specularColor = vec3(0.0);
166 lightColor = vec3(0.0);
168 // @TODO conditionally compile different shaders for different number of lights
169 if( uNumberOfLights > 0 )
171 calculateLight(uLight0);
174 fragColor.rgb *= lightColor;
175 fragColor.rgb += specularColor;
179 // apply material properties
180 fragColor.rgb *= (uMaterial.mAmbient + uMaterial.mDiffuse).rgb;
184 // apply material alpha/opacity to alpha channel
185 fragColor.a *= uMaterial.mOpacity * uMaterial.mDiffuse.a;
187 // and finally, apply Actor color
190 // Next line useful for visualizing the normals
191 // fragColor = vec4(normalize(vNormal), 1.0);
192 gl_FragColor = fragColor;