# MultiMesh GPU-Instanced Wind Foliage & Player Bend in Godot 4 Compatibility

> Single-draw-call Godot 4.7 Compatibility grass and foliage using MultiMeshInstance3D, INSTANCE_CUSTOM phase/height packing, traveling gust wind waves, backlit tip translucency, and radial player bend.

- **Canonical page:** https://gamecrew.ai/techniques/godot/multimesh-foliage-wind
- **Techniques hub:** https://gamecrew.ai/techniques
- **Engine:** Godot 4
- **Category:** optimization
- **Tags:** `grass`, `foliage`, `wind`, `instancing`, `multimesh`, `vegetation`, `shader`, `gdshader`, `compatibility`, `meadow`
- **Verification:** Compile-verified pattern
- **Revision:** 1 (2026-11-13)
- **Demo:** No demo

## When to Use & Field Best Practices

Placing hundreds of individual `MeshInstance3D` grass tufts destroys WebGL 2 draw-call budgets and stalls CPU software captures (`llvmpipe`/`SwiftShader`). Packing an entire meadow into one `MultiMeshInstance3D` renders 4,000–12,000 wind-swept blades in **a single draw call** in Godot 4.7 Compatibility:

1. **Zero Per-Frame Buffer Rebuilds**: Populate `MultiMesh.set_instance_transform()` and `set_instance_custom_data()` **once** during level generation. Pack `(phase, height_scale, hue_shift, stiffness)` into `INSTANCE_CUSTOM` so every blade sways out of phase with zero GDScript loop cost.
2. **Quadratic Tip Bend (`uv_y^2`)**: Anchor roots solidly to the ground (`bend_weight = (1.0 - UV.y)^2` or height-normalized `h^2`) while curving tips gracefully with a low-frequency traveling world gust plus high-frequency local flutter.
3. **Interactive Player Push Sphere**: Pass `player_pos` as a single `vec3` shader parameter updated once per frame in `_process`. Blades within `bend_radius` lean radially outward and downward smoothly, giving instant physical grounding as the player runs through tall grass.
4. **Root AO + Sunlit Backlit Translucency**: Darken the lower 30% of each blade to ground it into the terrain without expensive shadow casting (`cast_shadow = SHADOW_CASTING_SETTING_OFF`), and brighten thin tips when looking toward the sun.

## Mathematical & Algorithmic Core

- **Traveling Wind Gust + Flutter**:
  $$g(\mathbf{p}, t) = \sin\!\left(\mathbf{p}_{xz}\cdot \mathbf{k}_w + \omega_g t + \phi_i\right), \qquad \Delta\mathbf{p}_{xz} = h^2 \left(\mathbf{d}_w\,(0.55 + 0.45 g) A_g + \mathbf{d}_\perp \sin(3.7 t + 4\phi_i) A_f\right)$$
- **Radial Player Displacement**: For horizontal offset $\mathbf{r} = \mathbf{p}_{xz} - \mathbf{c}_{\text{player},xz}$ and falloff $w = \left(1 - \mathrm{clamp}(|\mathbf{r}| / R,\, 0, 1)\right)^2$:
  $$\Delta\mathbf{p}_{xz} \mathrel{+}= \widehat{\mathbf{r}}\, w\, h\, s_{\text{push}}, \qquad \Delta p_y \mathrel{-}= w\, h^2\, 0.35\, s_{\text{push}}$$

## Engine Implementation

### 1. Compatibility Instanced Wind Foliage Shader (`Assets/Game/Shaders/foliage_wind.gdshader`)

```glsl
shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_disabled, diffuse_lambert;

uniform vec4 root_color : source_color = vec4(0.11, 0.27, 0.12, 1.0);
uniform vec4 tip_color : source_color = vec4(0.46, 0.68, 0.24, 1.0);
uniform vec4 dry_tint : source_color = vec4(0.66, 0.58, 0.26, 1.0);
uniform vec2 wind_dir = vec2(0.86, 0.51);
uniform float wind_strength : hint_range(0.0, 1.2) = 0.28;
uniform float wind_speed : hint_range(0.2, 6.0) = 1.9;
uniform float gust_scale : hint_range(0.05, 1.5) = 0.22;
uniform vec3 player_pos = vec3(0.0, -100.0, 0.0);
uniform float bend_radius : hint_range(0.2, 4.0) = 1.35;
uniform float bend_strength : hint_range(0.0, 1.5) = 0.72;

varying float v_height;
varying float v_tint;
varying float v_gust;

void vertex() {
	float h = clamp(1.0 - UV.y, 0.0, 1.0);
	float phase = INSTANCE_CUSTOM.r * 6.2831853;
	float h_scale = max(0.45, INSTANCE_CUSTOM.g);
	v_height = h;
	v_tint = INSTANCE_CUSTOM.b;

	VERTEX.y *= h_scale;
	vec3 world_root = (MODEL_MATRIX * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
	vec2 wdir = normalize(wind_dir + vec2(1e-5, 0.0));
	vec2 wperp = vec2(-wdir.y, wdir.x);

	float gust_wave = sin(dot(world_root.xz, wdir) * gust_scale - TIME * wind_speed + phase * 0.35);
	float gust = 0.5 + 0.5 * gust_wave;
	float flutter = sin(TIME * (wind_speed * 2.4) + phase * 3.1) * 0.22;
	v_gust = gust;

	float bend_w = h * h;
	vec2 sway_xz = (wdir * (0.45 + 0.75 * gust) + wperp * flutter) * wind_strength * bend_w;

	vec2 to_blade = world_root.xz - player_pos.xz;
	float dist = length(to_blade);
	float vert_gap = abs(world_root.y - player_pos.y);
	float push_w = clamp(1.0 - dist / max(0.2, bend_radius), 0.0, 1.0);
	push_w *= push_w * (1.0 - smoothstep(1.2, 2.5, vert_gap));
	vec2 push_dir = dist > 0.001 ? (to_blade / dist) : wdir;

	vec3 world_disp = vec3(
		sway_xz.x + push_dir.x * push_w * bend_strength * h,
		-push_w * bend_strength * 0.32 * bend_w,
		sway_xz.y + push_dir.y * push_w * bend_strength * h
	);
	vec3 local_disp = (inverse(MODEL_MATRIX) * vec4(world_disp, 0.0)).xyz;
	VERTEX += local_disp;

	// Tilt blade normal gently skyward so meadows read lush from every angle
	NORMAL = normalize(mix(NORMAL, vec3(0.0, 1.0, 0.0), 0.55 + 0.25 * h));
}

void fragment() {
	vec3 base = mix(root_color.rgb, tip_color.rgb, smoothstep(0.08, 0.92, v_height));
	base = mix(base, dry_tint.rgb, v_tint * 0.35 * v_height);
	float ao = mix(0.55, 1.0, smoothstep(0.0, 0.45, v_height));
	ALBEDO = base * ao * (0.94 + 0.12 * v_gust * v_height);
	ROUGHNESS = mix(0.88, 0.55, v_height);
	SPECULAR = 0.22;
}
```

### 2. Typed GDScript Procedural Blade & MultiMesh Spawner (`Assets/Game/Scripts/gameplay/wind_meadow.gd`)

```gdscript
class_name WindMeadow
extends RefCounted

static func build_blade_mesh(width: float = 0.11, height: float = 0.65) -> ArrayMesh:
	var st := SurfaceTool.new()
	st.begin(Mesh.PRIMITIVE_TRIANGLES)
	var hw := width * 0.5
	var mw := width * 0.34
	var mid_y := height * 0.52
	var curve_z := height * 0.09

	var p0 := Vector3(-hw, 0.0, 0.0)
	var p1 := Vector3(hw, 0.0, 0.0)
	var p2 := Vector3(-mw, mid_y, curve_z * 0.45)
	var p3 := Vector3(mw, mid_y, curve_z * 0.45)
	var p4 := Vector3(0.0, height, curve_z)

	var verts: PackedVector3Array = [p0, p1, p3, p0, p3, p2, p2, p3, p4]
	var uvs: PackedVector2Array = [
		Vector2(0.0, 1.0), Vector2(1.0, 1.0), Vector2(1.0, 0.48),
		Vector2(0.0, 1.0), Vector2(1.0, 0.48), Vector2(0.0, 0.48),
		Vector2(0.0, 0.48), Vector2(1.0, 0.48), Vector2(0.5, 0.0),
	]
	for i: int in range(verts.size()):
		st.set_normal(Vector3(0.0, 0.35, 0.94).normalized())
		st.set_uv(uvs[i])
		st.add_vertex(verts[i])
	return st.commit()

static func spawn_meadow(
	parent: Node3D,
	foliage_shader: Shader,
	spots: PackedVector3Array,
	seed_value: int = 1337
) -> MultiMeshInstance3D:
	var rng := RandomNumberGenerator.new()
	rng.seed = seed_value

	var mm := MultiMesh.new()
	mm.transform_format = MultiMesh.TRANSFORM_3D
	mm.use_custom_data = true
	mm.mesh = build_blade_mesh()
	mm.instance_count = spots.size()

	for i: int in range(spots.size()):
		var yaw := rng.randf_range(0.0, TAU)
		var tilt := rng.randf_range(-0.14, 0.14)
		var basis := Basis(Vector3.UP, yaw) * Basis(Vector3.RIGHT, tilt)
		mm.set_instance_transform(i, Transform3D(basis, spots[i]))
		mm.set_instance_custom_data(i, Color(
			rng.randf(),
			rng.randf_range(0.72, 1.35),
			rng.randf(),
			1.0
		))

	var mat := ShaderMaterial.new()
	mat.shader = foliage_shader

	var inst := MultiMeshInstance3D.new()
	inst.name = "WindMeadow"
	inst.multimesh = mm
	inst.material_override = mat
	inst.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
	parent.add_child(inst)
	return inst

static func update_player_bend(meadow: MultiMeshInstance3D, world_pos: Vector3) -> void:
	var mat := meadow.material_override as ShaderMaterial
	if mat != null:
		mat.set_shader_parameter("player_pos", world_pos)
```

## Browser, Mobile & Software-Renderer Budgets

- **Instance Cap**: Keep single-patch meadows at `2,500–6,000` blades for phones/WebGL 2 and `<= 2,500` blades per view in Quest WebXR (each 5-vertex blade is only 3 triangles, so 4,000 blades = 12,000 triangles in 1 draw call).
- **Disable Shadow Casting**: Always set `cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF` on grass `MultiMeshInstance3D` so shadow-map passes don't double-render thousands of thin blades.
- **Zero Per-Frame Allocations**: Only call `update_player_bend(meadow, player.global_position)` each frame; never rebuild `MultiMesh` buffers during gameplay.

## Verification Checklist

1. Run `compile` to verify `foliage_wind.gdshader` and `wind_meadow.gd` pass Godot 4.7 static lint and shader parsing with zero warnings.
2. Run `capture` from eye level and overview: verify grass roots meet terrain cleanly, blades wave across the meadow in traveling bands, and moving the player parts nearby grass smoothly.
3. Verify draw call count increases by exactly `1` regardless of whether the meadow has 500 or 4,000 blades.

## Official references

- Malidos — Grass-Shader-Example (MIT)
- Garrett Gunnell — GPU Grass Reference (MIT)
- Sucker Punch (GDC 2021 — Procedural Grass in Ghost of Tsushima)
- Godot 4 MultiMeshInstance3D Reference

## Open-source sources

- Takes an idea from Grass-Shader-Example by Malidos (MIT License), https://github.com/Malidos/Grass-Shader-Example (MultiMeshInstance3D wind grass and player bend formulation)

Games built with this guide carry these credits, and the licence notices they ask for, in their third-party notices.
