Skip to content

Instantly share code, notes, and snippets.

@hamaluik
Created August 13, 2015 07:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamaluik/6639622f1e8071331512 to your computer and use it in GitHub Desktop.
Save hamaluik/6639622f1e8071331512 to your computer and use it in GitHub Desktop.
Flatshading GLSL Example
#extension GL_OES_standard_derivatives : enable
precision highp float;
precision highp int;
varying vec3 vViewPos;
vec3 normals_1_0(vec3 pos) {
vec3 fdx = dFdx(pos);
vec3 fdy = dFdy(pos);
return normalize(cross(fdx, fdy));
}
void main() {
vec3 lightDir = vec3(1.0, 0.0, 0.0);
vec3 normal = normals_1_0(vViewPos);
vec3 lightColor = vec3(0.9, 0.9, 0.8);
vec3 materialColor = vec3(0.8, 0.6, 0.6);
vec3 ambientColor = vec3(0.2, 0.2, 0.2);
float cosTheta = clamp(dot(normal, lightDir), 0.0, 1.0);
vec3 ambientLight = ambientColor * materialColor;
gl_FragColor.rgb = ambientLight + lightColor * materialColor * cosTheta;
gl_FragColor.a = 1.0;
}
precision highp float;
precision highp int;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
attribute vec3 position;
varying vec3 vViewPos;
void main() {
vec4 pos = vec4(position, 1.0);
vec4 mpos = modelViewMatrix * pos;
gl_Position = projectionMatrix * mpos;
vViewPos = -mpos.xyz;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment