Created
August 13, 2015 07:01
-
-
Save hamaluik/6639622f1e8071331512 to your computer and use it in GitHub Desktop.
Flatshading GLSL Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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