Game Development Reference
In-Depth Information
Implementing the fragment shader code
In the fragment shader, we add three variables: varying ( vTextureCoord ) to receive
the texture coordinates from the vertex shader, uniform ( hasTexture ) to see whether
the texture was associated with a primitive, and sampler ( uSampler ) which holds the
reference to the then active texture. The important thing to note is that we perform
our complete computation of ambient, diffuse, and specular color, but when we
decide to have a texture, we do not use the material diffuse color in our computation
of the iColor object ( +uDirectionalColor* directionalLightWeighting+ ). If
we do not have a texture, we use the material diffuse color ( +uDirectionalColor
*materialDiffuseColor * directionalLightWeighting+ ). We do not use the
diffuse color of the object if the object has a texture associated with it.
Lastly, we calculate the color of the fragment ( gl_FragColor ) by multiplying iColor
with the texture color obtained from the sampler object at the vTextureCoord
coordinate using the texture2DESSL function, as shown in the following code:
<script id="shader-fs" type="x-shader/x-fragment">
...
uniform sampler2D uSampler;
varying highp vec2 vTextureCoord;
uniform bool hasTexture;
void main(void) {
...
if(hasTexture){
vec3iColor = (uAmbientColor*materialAmbientColor)+ (uDirectionalColor*
directionalLightWeighting)+(uSpecularColor*materialSpecularColor*spec
ular);
gl_FragColor = vec4(iColor, 1.0)*texture2D(uSampler,
vec2(vTextureCoord.s, vTextureCoord.t));
}
else{
vec3iColor = (uAmbientColor*materialAmbientColor)+(uDirectionalColor
*materialDiffuseColor * directionalLightWeighting)+(uSpecularColor*mat
erialSpecularColor*specular);
gl_FragColor = vec4(iColor, 1.0);
}
}
</script>
 
Search WWH ::




Custom Search