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:
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;
}