The issue with the shadows not following the stretched geometry is due to the shadow pass not applying the same scaling transformations as the regular pass. To fix this, we can modify the mesh instance's transform in a script to include the scaling and translation. This ensures that both the regular and shadow passes use the same transformed positions.
Here's the solution:
- Modify the mesh instance's transform in a script to include the scaling and translation.
- Use the original shader for the regular pass, but remove the scaling transformations from the vertex shader since they are now handled by the script.
Here's the script to modify the mesh instance's transform:
extends MeshInstance3D
func _ready():
var shader_material = material_override as ShaderMaterial
if shader_material:
var pixel_scale = shader_material.get_shader_param("pixel_scale")
var pixel_size = shader_material.get_shader_param("pixel_size")
var subpixel = shader_material.get_shader_param("subpixel")
var pixel_clamp = shader_material.get_shader_param("pixel_clamp")
# Apply the scaling to the transform
var scale = pixel_scale
var translation = transform.origin
if pixel_clamp:
translation.x = round(translation.x / (pixel_scale.x * pixel_size)) * (pixel_scale.x * pixel_size)
translation.y = round(translation.y / (pixel_scale.y * pixel_size)) * (pixel_scale.y * pixel_size)
translation.z = round(translation.z / (pixel_scale.z * pixel_size)) * (pixel_scale.z * pixel_size)
translation += subpixel * pixel_scale * pixel_size
transform.origin = translation
transform.basis = transform.basis.scaled(scale)
And here's the modified shader code that doesn't apply the scaling transformation:
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;
POSITION = PROJECTION_MATRIX * VIEW_MATRIX * MODEL_MATRIX * vec4(VERTEX, 1.0);
}
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;
}