Created
March 24, 2021 05:26
-
-
Save PranavSK/6d196e9e486aae9cf867459781d0ff4d 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
| shader_type spatial; | |
| render_mode cull_front, unshaded; | |
| // Set this value as 2 * outline_width_in_pixels * (1 / screen_size.xy) | |
| //uniform vec2 scaled_outline_width = vec2(0.0039, 0.0069); | |
| uniform float outline_width = 2.0; | |
| uniform vec4 outline_color: hint_color; | |
| void vertex () { | |
| // Move to clip space before applying the vertex growth. | |
| // This should counteract any object scaling and the outlines should | |
| // be of same thickness. | |
| vec4 position = PROJECTION_MATRIX * MODELVIEW_MATRIX * vec4(VERTEX, 1.0); | |
| vec3 clip_normal = (PROJECTION_MATRIX * MODELVIEW_MATRIX * vec4(NORMAL, 0.0)).xyz; | |
| // In clip space xy corresponds to screen positions. | |
| // By normalizing only xy we ensure the outline takes up exaclty | |
| // `outline_width` of clip space. | |
| // By extruding only in xy we can reduce the variations of the outline | |
| // thickness at grazing angles. | |
| // Eliminate foreshortening by counteracting perspective division. | |
| //position.xy += normalize(clip_normal.xy) * scaled_outline_width * position.w; | |
| position.xy += normalize(clip_normal.xy) / VIEWPORT_SIZE * outline_width * position.w; | |
| POSITION = position; | |
| } | |
| void fragment() { | |
| ALBEDO = outline_color.xyz; | |
| if(outline_color.a < 1.0) { | |
| ALPHA = outline_color.a; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment