Grass rendering

From Nebulavenus/forge-gpu · View interactive version

Grass rendering is an AI agent skill from the GitHub repository Nebulavenus/forge-gpu. Add instanced grass rendering with segmented blades, wind animation, LOD density rings, and terrain LOD with geomorphing to an SDL GPU project

How to use this skill

Skill file

Add grass rendering to an SDL GPU project using forge_scene.h. Implements

instanced segmented grass blades with wind animation, density-based LOD rings,

and tile-based terrain LOD with vertex shader geomorphing.

When to use

Key API calls

Correct order

  1. Generate procedural heightmap via fBm noise
  2. Upload heightmap as R32_FLOAT GPU texture
  3. Create tile meshes at multiple LOD resolutions (both triangle and line IBs)
  4. Create grass blade meshes at multiple segment counts
  5. Create pipelines: terrain fill/line/shadow + grass fill/line/shadow
  6. Per frame:

a. Select terrain tile LODs based on camera distance

b. Place grass instances with distance/frustum/slope culling

c. Upload instance buffers via deferred upload

d. Shadow pass: draw terrain tiles + near grass into shadow map

e. Main pass: set left viewport → draw terrain + grass filled;

set right viewport → draw terrain + grass wireframe

f. Reset viewport for UI

Grass blade geometry

Each blade is a vertical segmented strip with 2*(N+1) vertices and 2*N

triangles. The height_t attribute (0 at base, 1 at tip) controls wind

weighting and tapering.

for (int i = 0; i <= segments; i++) {
    float t = (float)i / (float)segments;
    float half_w = 0.5f * (1.0f - t * GRASS_BLADE_TIP_TAPER);
    verts[2*i+0] = (GrassVertex){ {-half_w, t, 0}, t };
    verts[2*i+1] = (GrassVertex){ { half_w, t, 0}, t };
}

Per-instance data

typedef struct {
    float position[3];   /* world-space base position */
    float rotation;      /* Y-axis rotation (radians) */
    float scale[2];      /* width, height             */
    float color[3];      /* RGB tint                  */
    float _pad;          /* alignment to 40 bytes     */
} GrassInstance;

Wind animation (vertex shader)

float wind_weight = height_t * height_t;  /* quadratic: tip bends most */
float phase = time * wind_speed + world.x * 0.7 + world.z * 0.3;
float wind_offset = sin(phase) * wind_strength * wind_weight;

Terrain geomorphing (vertex shader)

float2 snapped_uv = round(global_uv / coarse_cell_size) * coarse_cell_size;
float2 morphed_uv = lerp(global_uv, snapped_uv, morph_factor);
float morphed_x = lerp(world_x, snapped_x, morph_factor);
float h = height_tex.SampleLevel(smp, morphed_uv, 0).r;

Common mistakes

screen width, so the projection matrix must use half_w / height aspect

LINELIST pipeline with a separate line index buffer (each triangle edge as

two indices)

wind animation as the main shader, or shadows will not match blade positions

cause visible popping; morph_factor must ramp 0→1 in the last 20% of each

LOD distance band

viewport and scissor to full screen before forge_scene_end_main_pass

Reference

See Lesson 50 — Grass Rendering

for the full implementation.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.