Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Simple cel shader with support for painted shadow maps in Godot 4
- // Original shader by Xavier Coelho-Kostolny https://www.xavierck.com/
- // Feel free to copy, modify, and share as you like.
- // Credit is appreciated but not necessary.
- // Spatial shaders are used for 3D scenes
- shader_type spatial;
- // Drawing the model on the screen as opaque and turning off backface culling
- render_mode blend_mix,depth_draw_opaque,cull_disabled;
- // Base color on top of which the base_texture is multiplied
- // Displayed if no base_texture is assigned
- // Defaults to white
- uniform vec4 base_color: source_color;
- // Base color texture with no lighting info painted in
- uniform sampler2D base_texture: source_color;
- // Base shadow texture
- // Defaults to flat white
- uniform sampler2D shadow_texture: hint_default_white;
- // Adjustable parameters visible in the shader editor
- // group_uniforms defines a named section of the shader editor
- group_uniforms ToonShadingProperties;
- // Width of the lighting terminator
- uniform float terminator_width : hint_range(0, 1) = 0.01;
- // Push the light terminator around the model between the range -1.0 to +1.0
- uniform float terminator_push : hint_range(-1.0, 1.0) = -0.3;
- // Push the painted shadow terminator in the same way as above
- uniform float painted_shadow_push : hint_range(-1.0, 1.0) = 0.25;
- // Alpha cutout threshold. Anything above this value will be opaque.
- uniform float alpha_cutoff : hint_range(0, 1) = 0.5;
- // End of the adjustable parameter section
- group_uniforms;
- // Pixel shader to give us the base colors and alpha
- void fragment() {
- // Store the base_texture in a variable so we can use it in other areas
- vec4 albedo_tex = texture(base_texture, UV);
- // Multiply the base_color and base_texture
- ALBEDO = base_color.rgb * texture(base_texture, UV).rgb;
- // Multiply the base_color and base_texture alpha channels so you can get alpha cutout
- ALPHA = base_color.a * albedo_tex.a;
- // Set the threshold of the alpha cutout
- ALPHA_SCISSOR_THRESHOLD = alpha_cutoff;
- }
- // The lighting calculations go in here
- void light() {
- // Base position of the light terminator
- // Starts at 0.5 for a larger lit area
- float terminator_max = 0.5;
- // Width of the light terminator
- // Smoothstep between min and max with the transition width being terminator_width
- float terminator_min = terminator_max - terminator_width;
- // Grab the painted Shadow Texture and store it in a vec4 variable
- vec4 shadows = texture(shadow_texture, UV);
- // Calculate lighting based on normal and light direction, then clamp it between 0-1
- // Doesn't include shadows, just the directional lighting
- float base_lighting = clamp(dot(NORMAL, LIGHT), 0.0, 1.0);
- // Lerp the shadow texture
- float lighting_mix = mix(shadows.r, 1.0, base_lighting);
- // Sharpen the shadow texture terminator so there aren't smooth gradients on painted shadows
- // Add in painted_shadow_push so you can adjust the painted light terminator separately from the regular lighting
- float lighting_mix_sharpen = smoothstep(
- terminator_min + painted_shadow_push,
- terminator_max + painted_shadow_push,
- lighting_mix
- );
- // Multiply the shadows cast from the light onto the base lighting so the model self-shadows
- float attenuated_base = base_lighting * ATTENUATION;
- // Smoothstep to make a sharp light terminator
- // Use terminator_push to force the terminator further along the surface
- float sharp_terminator = smoothstep(
- terminator_min + terminator_push,
- terminator_max + terminator_push,
- attenuated_base);
- // Get the lowest values from the base lighting and the painted shadows
- float combined_lighting = min(lighting_mix_sharpen, sharp_terminator);
- // Replace the standard diffuse lighting calculation and multiply it with the color of the light
- DIFFUSE_LIGHT += combined_lighting * LIGHT_COLOR;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement