Created
November 17, 2019 16:52
-
-
Save johans2/22e2fe3f7b21a1a0614e580aada2b904 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#version 330 | |
in vec2 fragTexCoord; | |
in vec3 fragNormal; | |
in vec3 fragWorldPos; | |
uniform vec3 color; | |
uniform float specPower; | |
uniform sampler2D cellRampDiffuse; | |
uniform sampler2D cellRampSpecular; | |
uniform mat4 modelMatrix; | |
uniform vec3 cameraWorldPos; | |
out vec4 outputColor; | |
void main() { | |
// Calculate normal in world coordinates | |
mat3 worldMatrix = transpose(inverse(mat3(modelMatrix))); | |
vec3 normal = normalize(worldMatrix * fragNormal); | |
// Calculate diffuse light | |
vec4 indirectDiffuse = vec4(0.2,0.2,0.2,1); | |
vec3 lightColor = vec3(1,1,1) * 0.6; | |
vec4 lightDir = vec4(0.5,1.2,1.5,1); | |
float nDotL = dot(normal, normalize(lightDir.xyz)); | |
float cellLight = clamp(texture(cellRampDiffuse, vec2(nDotL,0)).r,0,1); | |
vec3 directDiffuse = lightColor * cellLight; | |
vec4 diffuse = indirectDiffuse + vec4(directDiffuse,1); | |
// Calculate specular highlight | |
vec3 viewDir = normalize(fragWorldPos - cameraWorldPos); | |
vec3 halfDir = normalize(lightDir.xyz + viewDir); | |
float specAngle = max(dot(halfDir, normal), 0.0); | |
float specular = pow(specAngle,specPower); | |
float cellSpecular = clamp(texture(cellRampSpecular, vec2(specular,0)).r,0,1); | |
outputColor = vec4(color,1) * diffuse + vec4(lightColor,1) * cellSpecular; | |
} |
This file contains hidden or 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
#version 330 | |
uniform mat4 modelMatrix; | |
uniform mat4 viewMatrix; | |
uniform mat4 projMatrix; | |
uniform mat4 MVP; | |
in vec3 vert; | |
in vec2 vertTexCoord; | |
in vec3 normal; | |
out vec2 fragTexCoord; | |
out vec3 fragNormal; | |
out vec3 fragWorldPos; | |
void main() { | |
fragTexCoord = vertTexCoord; | |
fragNormal = normal; | |
fragWorldPos = (modelMatrix * vec4(vert,1)).xyz; | |
gl_Position = MVP * vec4(vert, 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment