The following Godot 4 shader has a bug that shadows are distorted as the shadows don't…
- User
The following Godot 4 shader has a bug that shadows are distorted as the shadows don't follow the stretched geometry based on pixel_scale. Specifically the shadow is at the wrong position, as if the mesh wasn't scaled in the shadow pass.
shader_type spatial; render_mode blend_mix, depth_draw_opaque, cull_back, diffuse_lambert, specular_disabled; uniform sampler2D texture_albedo : source_color, filter_nearest, repeat_enable; uniform float alpha_scissor_threshold : hint_range(0.0, 1.0, 0.001); uniform ivec2 albedo_texture_size; uniform float point_size : hint_range(0.1, 128.0, 0.1); uniform float roughness : hint_range(0.0, 1.0); uniform sampler2D texture_metallic : hint_default_white, filter_nearest, repeat_enable; uniform vec4 metallic_texture_channel; uniform sampler2D texture_roughness : hint_roughness_r, filter_nearest, repeat_enable; uniform float specular : hint_range(0.0, 1.0, 0.01); uniform float metallic : hint_range(0.0, 1.0, 0.01); uniform sampler2D texture_normal : hint_roughness_normal, filter_nearest, repeat_enable; uniform float normal_scale : hint_range(-16.0, 16.0); uniform float rim : hint_range(0.0, 1.0, 0.01); uniform float rim_tint : hint_range(0.0, 1.0, 0.01); uniform sampler2D texture_rim : hint_default_white, filter_nearest, repeat_enable; instance uniform vec4 tint : source_color = vec4(1.0,1.0,1.0,0.0); instance uniform vec4 outline : source_color = vec4(1.0,1.0,1.0,0.0); instance uniform vec3 uv1_scale = vec3(1.0,1.0,1.0); instance uniform vec3 uv1_offset = vec3(0.0,0.0,0.0); uniform float pixel_size = 0.01; uniform vec3 pixel_scale = vec3(1.0, 1.0, 1.0); uniform bool pixel_clamp = true; uniform vec3 subpixel = vec3(0.0, 0.0, 0.0); uniform vec4 grid : source_color = vec4(1.0,1.0,1.0,0.0); uniform float grid_width : hint_range(0, 1) = 0.0; float clamp_to_pixel(float v, float pixel) { return round(v / pixel) * pixel; } mat4 clamp_translation_to_pixel(mat4 matrix) { if (pixel_clamp) { matrix[3].x = clamp_to_pixel(matrix[3].x, pixel_scale.x * pixel_size); matrix[3].y = clamp_to_pixel(matrix[3].y, pixel_scale.y * pixel_size); matrix[3].z = clamp_to_pixel(matrix[3].z, pixel_scale.z * pixel_size); } return matrix; } mat4 local_rotate_matrix(mat4 matrix) { matrix[3].x = 0.0; matrix[3].y = 0.0; matrix[3].z = 0.0; return matrix; } void vertex() { UV = UV * uv1_scale.xy + uv1_offset.xy; vec3 positon = clamp_translation_to_pixel(MODEL_MATRIX)[3].xyz + subpixel * pixel_scale * pixel_size; vec4 rotated = local_rotate_matrix(MODEL_MATRIX) * vec4(VERTEX, 1.0); vec4 sqrt2scaled_world = vec4(rotated.x * pixel_scale.x + positon.x, rotated.y * pixel_scale.y + positon.y, rotated.z * pixel_scale.z + positon.z, 1.0); POSITION = PROJECTION_MATRIX * VIEW_MATRIX * sqrt2scaled_world; } vec4 voxel_grid(vec2 uv,) { vec2 pos = uv - vec2(0.5); float line_thickness = grid_width / 2.0; float alpha = 1.0 - step(abs(pos.x), (0.5-line_thickness)) * step(abs(pos.y), (0.5-line_thickness)); return vec4(grid.rgb, grid.a*alpha); } void fragment() { vec2 base_uv = UV; vec4 albedo_tex = texture(texture_albedo, base_uv); vec4 voxel_grid = voxel_grid(UV2); vec3 albedo_voxel = mix(albedo_tex.rgb, voxel_grid.rgb, voxel_grid.a); vec3 albedo_tint = mix(albedo_voxel.rgb, tint.rgb, tint.a); vec3 albedo_outline = mix(albedo_tint.rgb, outline.rgb, COLOR.r * outline.a); ALBEDO = albedo_outline; float metallic_tex = dot(texture(texture_metallic, base_uv), metallic_texture_channel); METALLIC = metallic_tex * metallic; SPECULAR = specular; vec4 roughness_texture_channel = vec4(1.0, 0.0, 0.0, 0.0); float roughness_tex = dot(texture(texture_roughness, base_uv), roughness_texture_channel); ROUGHNESS = roughness_tex * roughness; // Normal Map: Enabled NORMAL_MAP = texture(texture_normal, base_uv).rgb; NORMAL_MAP_DEPTH = normal_scale; ALPHA *= albedo_tex.a; ALPHA_SCISSOR_THRESHOLD = alpha_scissor_threshold; // Rim: Enabled vec2 rim_tex = texture(texture_rim, base_uv).xy; RIM = rim * rim_tex.x; RIM_TINT = rim_tint * rim_tex.y; } Assistant
You're absolutely right — the issue you're describing stems from how the mesh is transformed in the vertex shader, particularly during shadow rendering. The problem lies in that your
vertex()function applies a custom transformation (pixel_scale) to the geometry, but this transformation is not taken into account when shadows are computed by Godot's lighting/shadow passes.🔍 Problem Summary
In Godot 4, for lighting and shadow calculations:
- The engine uses a simplified version of the vertex shader (typically without uniforms or logic unless explicitly included).
- If transformations are applied in
vertex()using complex logic involving uniforms likepixel_scale, they must be explicitly handled in bothvertex()andlight()functions, especially if shadows are involved.
Your current
vertex()function applies a scale via:rotated.x * pixel_scale.x + ...But this scaling isn't visible during shadow map rendering because the shadow pass doesn't use your full
vertex()logic unless told otherwise.
✅ Solution: Add
light()Function to Handle Shadow TransformationsTo fix distorted shadows, you need to duplicate the transformation logic in the
light()function, which runs during shadow mapping and lighting passes.Add this to your shader:
void light() { // Reapply the same transformation logic used in vertex() vec3 position = clamp_translation_to_pixel(MODEL_MATRIX)[3].xyz + subpixel * pixel_scale * pixel_size; vec4 rotated = local_rotate_matrix(MODEL_MATRIX) * vec4(VERTEX, 1.0); vec4 scaled_world = vec4( rotated.x * pixel_scale.x + position.x, rotated.y * pixel_scale.y + position.y, rotated.z * pixel_scale.z + position.z, 1.0 ); // Set built-in shadow coordinates based on scaled geometry SHADOW_WORLD_VERTEX = scaled_world.xyz; }This tells Godot how to compute world-space positions during lighting/shadow passes, ensuring that shadows align with the stretched/scaled geometry.
🧠 Additional Notes
-
Why does this happen?
- Godot separates base rendering from lighting/shadowing. The
vertex()function is only run for the base draw pass. - For shadows and lights, it calls
light()instead (if present), or falls back to simpler internal logic.
- Godot separates base rendering from lighting/shadowing. The
-
Why does the shadow look "wrong"?
- Because the actual mesh is stretched by
pixel_scale, but the shadow caster is treated as unscaled unless explicitly told otherwise.
- Because the actual mesh is stretched by
-
Is there another way?
- You could apply scaling through the mesh or node transform rather than the shader, but if you're doing pixel-aligned effects, manipulating in the shader is often necessary.
✅ Final Recommendation
Always include the same or equivalent transformation logic in both:
vertex()→ for main renderinglight()→ for lighting and shadows
This ensures visual consistency across all render passes, including shadow casting.
Let me know if you'd like help optimizing this further!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.