Snippets

Joseph Chow Per-Vertex BRDF Phong shader applied to Three.js

Created by Joseph Chow last modified
1
2
3
4
5
6
precision mediump float;
varying vec4 forFragColor;

void main() {
  gl_FragColor = forFragColor;
}
//original source : http://www.mathematik.uni-marburg.de/~thormae/lectures/graphics1/code/WebGLShaderLightMat/ShaderLightMat.html

uniform int mode;

varying vec4 forFragColor;
//uniform vec3 lightPos;
const vec3 lightPos = vec3(1.0, 1.0, 1.0);
const vec3 diffuseColor = vec3(0.5, 0.0, 0.0);
const vec3 specColor = vec3(1.0, 1.0, 1.0);

void main(){
  gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);

  // all following gemetric computations are performed in the
  // camera coordinate system (aka eye coordinates)
  vec3 cnormal = vec3(normalMatrix * vec4(normal, 0.0).xyz);
  vec4 vertPos4 = modelViewMatrix * vec4(position, 1.0);
  vec3 vertPos = vec3(vertPos4) / vertPos4.w;
  vec3 lightDir = normalize(lightPos - vertPos);
  vec3 reflectDir = reflect(-lightDir, cnormal);
  vec3 viewDir = normalize(-vertPos);

  float lambertian = max(dot(lightDir,cnormal), 0.0);
  float specular = 0.0;

  if(lambertian > 0.0) {
    float specAngle = max(dot(reflectDir, viewDir), 0.0);
    specular = pow(specAngle, 4.0);

    // the exponent controls the shininess (try mode 2)
    //if(mode == 2)  specular = pow(specAngle, 16.0);
    specular = pow(specAngle, 16.0);

    // according to the rendering equation we would need to multiply
    // with the the "lambertian", but this has little visual effect
    if(mode == 3) specular *= lambertian;
    // switch to mode 4 to turn off the specular component
    if(mode == 4) specular *= 0.0;
  }
  forFragColor = vec4(lambertian*diffuseColor + specular*specColor, 1.0);
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.