Last active
April 23, 2019 11:25
-
-
Save haxpor/5ebe4a5d33e27f99ce9ede56b00739f3 to your computer and use it in GitHub Desktop.
Toon shading for model. Thanks for great tutorial https://bit.ly/2KZyseQ by Erik Roystan Ross
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
// toonshader, see https://roystan.net/articles/toon-shader.html | |
// but this time with input model file | |
#ifdef GL_ES | |
precision mediump float; | |
#endif | |
uniform vec2 u_resolution; | |
uniform vec2 u_tex0Resolution; | |
uniform vec3 u_lightColor; | |
uniform vec3 u_camera; | |
uniform vec3 u_light; | |
varying vec4 v_position; | |
varying vec4 v_color; | |
varying vec3 v_normal; | |
varying vec2 v_texcoord; | |
const vec3 sphere_color = vec3(1.0, 0.0, 1.0); | |
const vec3 ambient_color = vec3(0.4, 0.4, 0.4); | |
const vec4 specular_color = vec4(0.9, 0.9, 0.9, 1.0); | |
const float glossiness = 32.0; | |
const vec3 light_pos = vec3(2.0, 4.2, -4.0); | |
const vec3 rim_color = vec3(0.4, 0.4, 0.4); | |
const float rim_amount = 0.716; | |
const float rim_threshold = 0.1; | |
void main() | |
{ | |
// maintain aspect ratio to the height | |
vec2 uv = vec2(gl_FragCoord.x / u_resolution.x, gl_FragCoord.y / u_resolution.y); | |
uv -= 0.5; | |
uv /= vec2(u_resolution.y / u_resolution.x, 1.0); | |
vec3 dir = normalize(v_position.xyz - u_camera); | |
vec3 view_dir = -dir; | |
vec3 half_vector = normalize(-light_pos + view_dir); | |
float NdotH = dot(v_normal, half_vector); | |
float NdotL = dot(light_pos, v_normal); | |
float light_intensity = smoothstep(0.0, 0.01, NdotL); | |
float specular_intensity = pow(NdotH * light_intensity, glossiness * glossiness); | |
vec3 light = light_intensity * u_lightColor; | |
float rim_dot = 1.0 - dot(-dir, v_normal); | |
float rim_intensity = rim_dot * pow(NdotL, rim_threshold); | |
rim_intensity = smoothstep(rim_amount - 0.01, rim_amount + 0.01, rim_intensity); | |
vec3 rim = rim_intensity * rim_color; | |
vec4 color = v_color * vec4(sphere_color, 1.0) * vec4(ambient_color + light, 1.0) + specular_intensity + vec4(rim * light, 0.0); | |
// gamma correction | |
color.rgb = pow(color.rgb, vec3(0.4545454545)); | |
gl_FragColor = color; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment