Single light textureless phong shading complete.
This commit is contained in:
@@ -2,9 +2,50 @@
|
|||||||
precision mediump float;
|
precision mediump float;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Camera world space position.
|
||||||
|
uniform vec3 u_cameraPos;
|
||||||
|
|
||||||
|
// Light source position
|
||||||
|
uniform vec4 u_lightPos;
|
||||||
|
|
||||||
|
// Diffuse light color.
|
||||||
|
uniform vec4 u_lightDiffuse;
|
||||||
|
|
||||||
|
// Ambient light color.
|
||||||
|
uniform vec4 u_ambient;
|
||||||
|
|
||||||
|
// Shininess.
|
||||||
|
uniform float u_shiny;
|
||||||
|
|
||||||
|
// Fragment position.
|
||||||
|
varying vec4 v_position;
|
||||||
|
|
||||||
|
// Fragment normal.
|
||||||
|
varying vec3 v_normal;
|
||||||
|
|
||||||
// Fragment color received from the vertex shader.
|
// Fragment color received from the vertex shader.
|
||||||
varying vec4 v_color;
|
varying vec4 v_color;
|
||||||
|
|
||||||
void main(){
|
void main(){
|
||||||
gl_FragColor = v_color;
|
vec3 normal = normalize(v_normal);
|
||||||
|
vec3 lightVector = normalize(u_lightPos.xyz - v_position.xyz);
|
||||||
|
vec3 eyeVector = normalize(u_cameraPos.xyz - v_position.xyz);
|
||||||
|
vec3 reflectedVector = normalize(-reflect(lightVector, normal));
|
||||||
|
|
||||||
|
// Ambient Term.
|
||||||
|
vec4 ambient = u_ambient;
|
||||||
|
|
||||||
|
// Diffuse Term.
|
||||||
|
vec4 diffuse = u_lightDiffuse * max(dot(normal, lightVector), 0.0);
|
||||||
|
diffuse = clamp(diffuse, 0.0, 1.0);
|
||||||
|
|
||||||
|
// Specular Term:
|
||||||
|
vec4 specular = vec4(1.0) * pow(max(dot(reflectedVector, eyeVector), 0.0), 0.3 * u_shiny);
|
||||||
|
specular = clamp(specular, 0.0, 1.0);
|
||||||
|
|
||||||
|
// Aggregate light color.
|
||||||
|
vec4 lightColor = vec4(ambient.rgb + diffuse.rgb + specular.rgb, 1.0);
|
||||||
|
|
||||||
|
// Final color.
|
||||||
|
gl_FragColor = clamp(lightColor * v_color, 0.0, 1.0);
|
||||||
}
|
}
|
||||||
|
@@ -1,16 +1,31 @@
|
|||||||
// Model-view matrix.
|
// Model-view matrix.
|
||||||
uniform mat4 u_projTrans;
|
uniform mat4 u_projTrans;
|
||||||
|
|
||||||
|
// Normal matrix.
|
||||||
|
uniform mat4 u_normalMat;
|
||||||
|
|
||||||
// Vertex position in world coordinates.
|
// Vertex position in world coordinates.
|
||||||
attribute vec4 a_position;
|
attribute vec4 a_position;
|
||||||
|
|
||||||
|
// Vertex normal.
|
||||||
|
attribute vec4 a_normal;
|
||||||
|
|
||||||
// Vertex color.
|
// Vertex color.
|
||||||
attribute vec4 a_color;
|
attribute vec4 a_color;
|
||||||
|
|
||||||
|
// Vertex position to pass to the fragment shader.
|
||||||
|
varying vec4 v_position;
|
||||||
|
|
||||||
|
// Vertex normal to pass to the fragment shader.
|
||||||
|
varying vec3 v_normal;
|
||||||
|
|
||||||
// Vertex color to pass to the fragment shader.
|
// Vertex color to pass to the fragment shader.
|
||||||
varying vec4 v_color;
|
varying vec4 v_color;
|
||||||
|
|
||||||
void main(){
|
void main(){
|
||||||
|
v_position = u_projTrans * a_position;
|
||||||
v_color = a_color;
|
v_color = a_color;
|
||||||
gl_Position = u_projTrans * a_position;
|
v_normal = vec3(u_normalMat * a_normal);
|
||||||
|
|
||||||
|
gl_Position = v_position;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user