Game Development Reference
In-Depth Information
The fragment shader
In the following code, the
uPositionalColor1
and
uPositionalColor2
uniforms are the diffuse colors of the positional light. The
uLightRayLamp1
and
uLightRayLamp2
uniforms are the varying passed from the vertex shader. The
directionalLightWeighting1
float data type
is a Lambert term calculated from the
normalized light ray vector. The final diffuse light intensity (
iDiffuse
) is calculated
after adding the diffuse intensity of the positional light to the diffuse intensity from
other lights. This is shown in the following code:
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
varying vec3 transformedNormal;
varying vec3 vertexPos;
uniform vec3 uAmbientColor;
uniform vec3 uLightingDirection;
uniform vec3 uDirectionalColor;
uniform vec3 uSpecularColor;
uniform vec3 uPositionalColor1;
uniform vec3 uPositionalColor2;
uniform vec3 materialDiffuseColor;
uniform vec3 materialAmbientColor;
uniform vec3 materialSpecularColor;
varying vec3 uLightRayLamp1;
varying vec3 uLightRayLamp2;
void main(void) {
vec3 normal=normalize(transformedNormal);
vec3 eyeVector=normalize(-vertexPos);
vec3 lightDirection = normalize(uLightingDirection);
vec3 lightDirection1 = normalize(uLightRayLamp1);
vec3 lightDirection2 = normalize(uLightRayLamp2);
vec3 iAmbient=uAmbientColor*materialAmbientColor;
vec3 iDiffuse=vec3(0.0,0.0,0.0);
vec3 iSpecular=vec3(0.0,0.0,0.0);
float specular = 0.0;
float directionalLightWeighting = max(dot(normal, -
lightDirection), 0.0);
iDiffuse+=uDirectionalColor *materialDiffuseColor *
directionalLightWeighting;
float directionalLightWeighting1 = max(dot(normal, -
lightDirection1), 0.0);
iDiffuse+=uPositionalColor1 *materialDiffuseColor *
directionalLightWeighting1;
float directionalLightWeighting2 = max(dot(normal, -
lightDirection2), 0.0);