// Toon lighting, shared by toon.gdshader (ToonLook's converted materials) and the enemy shaders // (thief.gdshader, ufo.gdshader): diffuse light cut into bands, a hard specular spot, and a rim // light. The Godot 3 toon shader ported to Godot 4: source_color hints, ATTENUATION is a float, // PI is built in, and LIGHT_COLOR already carries the PI that the "/ PI" divides back out. Omni // and spot lights fade their rim with distance (a directional light's rim stays whole), and the // specular and rim powers clamp their base so back faces and grazing angles can't go negative. uniform bool clamp_diffuse_to_max = false; uniform int cuts : hint_range(1, 8) = 3; uniform float wrap : hint_range(-2.0f, 2.0f) = 0.0f; uniform float steepness : hint_range(1.0f, 8.0f) = 1.0f; uniform bool use_attenuation = true; uniform bool use_specular = true; uniform float specular_strength : hint_range(0.0f, 1.0f) = 1.0f; uniform float specular_shininess : hint_range(0.0f, 32.0f) = 16.0f; uniform sampler2D specular_map : source_color; uniform bool use_rim = true; uniform float rim_width : hint_range(0.0f, 16.0f) = 8.0f; uniform vec4 rim_color : source_color = vec4(1.0f); uniform bool use_ramp = false; uniform sampler2D ramp : source_color; uniform bool use_borders = false; uniform float border_width = 0.01f; float split_specular(float specular) { return step(0.5f, specular); } void light() { // Attenuation. float attenuation = 1.0f; if (use_attenuation) { attenuation = ATTENUATION; } // Diffuse lighting. float NdotL = dot(NORMAL, LIGHT); float diffuse_amount = NdotL + (attenuation - 1.0) + wrap; diffuse_amount *= steepness; float cuts_inv = 1.0f / float(cuts); float diffuse_stepped = clamp(diffuse_amount + mod(1.0f - diffuse_amount, cuts_inv), 0.0f, 1.0f); // Calculate borders. float border = 0.0f; if (use_borders) { float corr_border_width = length(cross(NORMAL, LIGHT)) * border_width * steepness; border = step(diffuse_stepped - corr_border_width, diffuse_amount) - step(1.0 - corr_border_width, diffuse_amount); } // Apply diffuse result to different styles. vec3 diffuse = ALBEDO.rgb * LIGHT_COLOR / PI; if (use_ramp) { diffuse *= texture(ramp, vec2(diffuse_stepped * (1.0f - border), 0.0f)).rgb; } else { diffuse *= diffuse_stepped * (1.0f - border); } if (clamp_diffuse_to_max) { // Clamp diffuse to max for multiple light sources. DIFFUSE_LIGHT = max(DIFFUSE_LIGHT, diffuse); } else { DIFFUSE_LIGHT += diffuse; } // Specular lighting. if (use_specular) { vec3 H = normalize(LIGHT + VIEW); float NdotH = max(dot(NORMAL, H), 0.0f); float specular_amount = pow(NdotH, specular_shininess * specular_shininess) * texture(specular_map, UV).r * attenuation; specular_amount = split_specular(specular_amount); SPECULAR_LIGHT += specular_strength * specular_amount * LIGHT_COLOR; } // Simple rim lighting. if (use_rim) { float NdotV = dot(NORMAL, VIEW); float rim_light = pow(clamp(1.0 - NdotV, 0.0f, 1.0f), rim_width); float rim_fade = LIGHT_IS_DIRECTIONAL ? 1.0f : attenuation; DIFFUSE_LIGHT += rim_light * rim_fade * rim_color.rgb * rim_color.a * LIGHT_COLOR / PI; } }