# 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.

- **Canonical page:** https://gamecrew.ai/techniques/godot/planar-reflections-glass-refraction
- **Techniques hub:** https://gamecrew.ai/techniques
- **Engine:** Godot 4
- **Category:** rendering
- **Tags:** `glass`, `refraction`, `dispersion`, `crystal`, `mirror`, `planar`, `reflection`, `shader`, `gdshader`, `compatibility`
- **Verification:** Compile-verified pattern
- **Revision:** 1 (2026-11-13)
- **Demo:** 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:

1. **Chromatic Glass & Crystal Refraction (`chromatic_glass.gdshader`)**:
   - Declare `uniform sampler2D screen_tex : hint_screen_texture, filter_linear_mipmap;` (never Godot 3's removed `SCREEN_TEXTURE`) and sample R, G, and B at slightly staggered normal-refracted `SCREEN_UV` offsets with `textureLod(..., 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 in `screen_tex`.
2. **Half-Res `SubViewport` Planar Mirror (`planar_mirror.gdshader` + `planar_mirror_rig.gd`)**:
   - Mirror the active `Camera3D`'s world position and basis vertically across a horizontal floor plane `y = plane_y`, render into a `256×256` or `384×384` `SubViewport` (`render_target_update_mode = UPDATE_ALWAYS`), and mask out the mirror mesh itself using `cull_mask` layer 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 $\mathbf{n}_v$ and dispersion $\delta$:
  $$\mathbf{u}_c = \mathbf{u}_{\text{screen}} + \mathbf{n}_v.xy \cdot s_{\text{refract}} \cdot (1 + c\,\delta), \qquad c \in \{-1, 0, +1\}$$
- **Horizontal Plane Reflection** across $y = y_0$: $R_y = \mathrm{diag}(1, -1, 1)$, $\mathbf{p}' = (p_x,\, 2y_0 - p_y,\, p_z)$.

## Engine Implementation

### 1. Chromatic Frosted Glass & Crystal Shader (`Assets/Game/Shaders/chromatic_glass.gdshader`)

```glsl
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`)

```glsl
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`)

```gdscript
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_DISABLED` and `res = 256` (`512` max on desktop) so WebGL 2 planar reflections stay under `0.5 ms`.
- **Single-Pass Chromatic Glass**: `chromatic_glass.gdshader` uses `hint_screen_texture` with zero extra viewports — safe even in Quest WebXR and mobile browsers.
- **Avoid Self-Reflection Loops**: Assigning `mirror_mesh.layers = MIRROR_LAYER_BIT` and clearing that bit on `mirror_cam.cull_mask` prevents feedback artifacts.

## Verification Checklist

1. Run `compile` to verify both `.gdshader` files and `planar_mirror_rig.gd` compile with zero Godot 4.7 lint warnings.
2. Call `mirror_rig.sync_camera(active_camera)` in `_process` and inspect `Captures/*.png` to verify inverted reflections align with pillars and characters on the mirror floor.
3. Place a crystal/glass mesh using `chromatic_glass.gdshader` in 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), https://github.com/GarrettGunnell/AcerolaFX-Godot (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.
