Chromatic Glass Refraction & Planar Mirror Reflections in Godot 4 Compatibility
Godot 4.7 Compatibility chromatic glass refraction reading hint_screen_texture with RGB dispersion and mip blur, plus a half-res SubViewport mirrored Camera3D planar reflection rig for polished floors and lakes.
- glass
- refraction
- dispersion
- crystal
- mirror
- planar
- reflection
- shader
- gdshader
- compatibility
No demo.
When to Use & Field Best Practices
Godot 4's Compatibility renderer (WebGL 2) does not provide Vulkan SSR, so polished museum halls, ice caves, crystal lanterns, and calm temple mirrors look flat unless built with Compatibility-native techniques:
- Chromatic Glass & Crystal Refraction (
chromatic_glass.gdshader):
- Declare
uniform sampler2D screen_tex : hint_screen_texture, filter_linear_mipmap;(never Godot 3's removedSCREEN_TEXTURE) and sample R, G, and B at slightly staggered normal-refractedSCREEN_UVoffsets withtextureLod(..., blur_lod)for frosted or prismatic glass in zero extra render passes. - Use
render_mode blend_mix, depth_draw_opaque, cull_back, specular_schlick_ggx;and keep interior/background meshes opaque so they are already present inscreen_tex.
- Half-Res
SubViewportPlanar Mirror (planar_mirror.gdshader+planar_mirror_rig.gd):
- Mirror the active
Camera3D's world position and basis vertically across a horizontal floor planey = plane_y, render into a256×256or384×384SubViewport(render_target_update_mode = UPDATE_ALWAYS), and mask out the mirror mesh itself usingcull_masklayer bit 20 so it never reflects itself! - Flip
SCREEN_UV.x(or invert the mirrored basis cleanly) and blend by Schlick Fresnel so grazing reflections look mirror-crisp while steep downward angles show the tiled floor underneath.
Mathematical & Algorithmic Core
- Chromatic Screen-Space Offset: For view-space normal
n_vand dispersion δ: $u_c = u_screen + n_v.xy · s_refract · (1 + c\,δ), c ∈ \-1, 0, +1\$ - Horizontal Plane Reflection across
y = y_0:R_y = diag(1, -1, 1),p' = (p_x,\, 2y_0 - p_y,\, p_z).
Engine Implementation
1. Chromatic Frosted Glass & Crystal Shader (Assets/Game/Shaders/chromatic_glass.gdshader)
shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_back, specular_schlick_ggx;
uniform sampler2D screen_tex : hint_screen_texture, filter_linear_mipmap;
uniform vec4 tint_color : source_color = vec4(0.76, 0.92, 0.98, 1.0);
uniform float refract_strength : hint_range(0.0, 0.12) = 0.038;
uniform float dispersion : hint_range(0.0, 0.6) = 0.24;
uniform float frost_lod : hint_range(0.0, 4.0) = 1.2;
uniform float fresnel_power : hint_range(1.0, 6.0) = 3.2;
uniform vec4 rim_color : source_color = vec4(0.95, 0.98, 1.0, 1.0);
void fragment() {
vec2 offset = NORMAL.xy * refract_strength;
vec2 uv_r = clamp(SCREEN_UV + offset * (1.0 + dispersion), vec2(0.002), vec2(0.998));
vec2 uv_g = clamp(SCREEN_UV + offset, vec2(0.002), vec2(0.998));
vec2 uv_b = clamp(SCREEN_UV + offset * (1.0 - dispersion), vec2(0.002), vec2(0.998));
vec3 refracted = vec3(
textureLod(screen_tex, uv_r, frost_lod).r,
textureLod(screen_tex, uv_g, frost_lod).g,
textureLod(screen_tex, uv_b, frost_lod).b
);
float ndv = clamp(dot(NORMAL, VIEW), 0.0, 1.0);
float fresnel = pow(1.0 - ndv, fresnel_power);
vec3 glass_col = mix(refracted * tint_color.rgb, rim_color.rgb, fresnel * 0.68);
ALBEDO = glass_col;
EMISSION = glass_col * 0.45 + rim_color.rgb * (fresnel * 0.25);
ROUGHNESS = clamp(0.05 + frost_lod * 0.12, 0.04, 0.55);
SPECULAR = 0.85;
}
2. Planar Mirror Floor Shader (Assets/Game/Shaders/planar_mirror.gdshader)
shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_back, specular_schlick_ggx;
uniform sampler2D mirror_tex : filter_linear;
uniform vec4 floor_color : source_color = vec4(0.14, 0.17, 0.22, 1.0);
uniform float reflectivity : hint_range(0.0, 1.0) = 0.68;
uniform float fresnel_power : hint_range(1.0, 6.0) = 3.0;
uniform float distort_amount : hint_range(0.0, 0.03) = 0.004;
void fragment() {
vec2 wobble = vec2(
sin(UV.y * 40.0 + TIME * 1.4),
cos(UV.x * 40.0 - TIME * 1.1)
) * distort_amount;
vec2 suv = clamp(vec2(1.0 - SCREEN_UV.x, SCREEN_UV.y) + wobble, vec2(0.002), vec2(0.998));
vec3 refl = texture(mirror_tex, suv).rgb;
float ndv = clamp(dot(NORMAL, VIEW), 0.0, 1.0);
float fresnel = mix(0.28, 1.0, pow(1.0 - ndv, fresnel_power));
vec3 col = mix(floor_color.rgb, refl, clamp(reflectivity * fresnel, 0.0, 0.92));
ALBEDO = col;
EMISSION = refl * (reflectivity * fresnel * 0.35);
ROUGHNESS = 0.12;
SPECULAR = 0.75;
}
3. Typed GDScript Planar Mirror Rig (Assets/Game/Scripts/gameplay/planar_mirror_rig.gd)
class_name PlanarMirrorRig
extends RefCounted
const MIRROR_LAYER_BIT: int = 1 << 19
var viewport: SubViewport
var mirror_cam: Camera3D
var mirror_mesh: MeshInstance3D
var plane_y: float = 0.0
func setup(parent: Node3D, mirror_shader: Shader, size: Vector2, p_plane_y: float = 0.0, res: int = 256) -> MeshInstance3D:
plane_y = p_plane_y
viewport = SubViewport.new()
viewport.name = "PlanarMirrorViewport"
var clamped_res := clampi(res, 128, 512)
viewport.size = Vector2i(clamped_res, clamped_res)
viewport.render_target_update_mode = SubViewport.UPDATE_ALWAYS
viewport.msaa_3d = Viewport.MSAA_DISABLED
parent.add_child(viewport)
mirror_cam = Camera3D.new()
mirror_cam.name = "PlanarMirrorCamera"
mirror_cam.cull_mask = 0xFFFFF & (~MIRROR_LAYER_BIT)
viewport.add_child(mirror_cam)
var plane := PlaneMesh.new()
plane.size = size
var mat := ShaderMaterial.new()
mat.shader = mirror_shader
mat.set_shader_parameter("mirror_tex", viewport.get_texture())
mirror_mesh = MeshInstance3D.new()
mirror_mesh.name = "PlanarMirrorFloor"
mirror_mesh.mesh = plane
mirror_mesh.material_override = mat
mirror_mesh.position = Vector3(0.0, plane_y, 0.0)
mirror_mesh.layers = MIRROR_LAYER_BIT
mirror_mesh.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
parent.add_child(mirror_mesh)
return mirror_mesh
func sync_camera(main_cam: Camera3D) -> void:
if main_cam == null or mirror_cam == null:
return
var src_pos := main_cam.global_position
var src_fwd := -main_cam.global_transform.basis.z
var reflected_pos := Vector3(src_pos.x, 2.0 * plane_y - src_pos.y, src_pos.z)
var reflected_target := reflected_pos + Vector3(src_fwd.x, -src_fwd.y, src_fwd.z)
mirror_cam.fov = main_cam.fov
mirror_cam.near = main_cam.near
mirror_cam.far = main_cam.far
mirror_cam.global_position = reflected_pos
mirror_cam.look_at(reflected_target, Vector3.UP)
Browser, Mobile & Software-Renderer Budgets
- Disable SubViewport MSAA: Keep
viewport.msaa_3d = Viewport.MSAA_DISABLEDandres = 256(512max on desktop) so WebGL 2 planar reflections stay under0.5 ms. - Single-Pass Chromatic Glass:
chromatic_glass.gdshaderuseshint_screen_texturewith zero extra viewports — safe even in Quest WebXR and mobile browsers. - Avoid Self-Reflection Loops: Assigning
mirror_mesh.layers = MIRROR_LAYER_BITand clearing that bit onmirror_cam.cull_maskprevents feedback artifacts.
Verification Checklist
- Run
compileto verify both.gdshaderfiles andplanar_mirror_rig.gdcompile with zero Godot 4.7 lint warnings. - Call
mirror_rig.sync_camera(active_camera)in_processand inspectCaptures/*.pngto verify inverted reflections align with pillars and characters on the mirror floor. - Place a crystal/glass mesh using
chromatic_glass.gdshaderin front of high-contrast scenery and confirm subtle RGB chromatic separation along edges.
Official references
- Siesta-Games — Planar Reflections Reference (MIT)
- jiaozi158 — Screen-Space Refraction Reference (MIT)
- pmndrs — Chromatic Transmission Reference (MIT)
- Godot 4 Spatial Shader hint_screen_texture Reference
Open-source sources
- Takes an idea from AcerolaFX-Godot by Garrett Gunnell (MIT License): Compatibility-safe screen-texture refraction and reflection workflow
Games built with this guide carry these credits, and the licence notices they ask for, in their third-party notices.
More Godot 4 games
Games made with GameCrew on the same engine. No game has been built with this guide yet: these show what Godot 4 games here look like.

Fernlight
Raise a carved whistle to your lips in a fog-drowned forest, coax a glowing bird onto your open hand, and point to send it off to light the dark.
More Godot 4 techniques
Rain Puddles, Wetness & Interactive Snow Trails in Godot 4 Compatibility →
Dynamic wet surfaces with procedural puddle masks, porosity darkening, and animated raindrop ripples alongside an R8 ImageTexture persistent snow/sand trail displacement shader in Godot 4.7 Compatibility.
Stylized Water, Gerstner Waves & Depth Shoreline Foam in Godot 4 Compatibility →
Godot 4.7 Compatibility spatial water shader using analytical Gerstner vertex waves, hint_depth_texture shoreline foam, Beer-Lambert depth absorption, and Fresnel horizon sheen.
Cel-Shading, Shadow Ramps & Width-Compensated Outlines in Godot 4 Compatibility →
Godot 4.7 Compatibility cel-shading spatial shader using custom light() shadow ramps, colored shadow tints, sun-gated Fresnel rim highlights, and a next_pass clip-space width-compensated inverted-hull outline.
CharacterBody3D Slopes, Snap & Coyote Jump in Godot 4 →
Crisp Godot 4 CharacterBody3D locomotion combining floor_snap_length, apex hang gravity, jump-cut, coyote time, and zero-arg move_and_slide.
Use verified techniques inside a real game
Describe your game idea for free or connect an MCP assistant to search and contribute techniques.