From 3268556bd0f5792728c606ea2308332ab662e804 Mon Sep 17 00:00:00 2001 From: Erik Date: Fri, 10 Apr 2026 22:22:10 +0200 Subject: [PATCH] feat(app): directional lighting on terrain and static meshes (Phase 3a) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a hardcoded sun direction + ambient + Lambert diffuse to both terrain.frag and mesh.frag. Both vertex shaders now forward a world- space normal (computed as mat3(uModel) * aNormal) for the fragment shader to dot against the sun vector. Lighting model: final_rgb = texture_rgb * (AMBIENT + DIFFUSE * max(0, dot(N, SUN))) where AMBIENT=0.4, DIFFUSE=0.6, SUN=normalize(0.4,0.3,0.8). Building walls facing the sun light up, walls in shadow dim to ~40%. Scenery (trees, bushes, rocks) with real per-vertex normals from SWVertex shades naturally. Terrain currently uses flat UnitZ normals so every terrain fragment gets the same contribution — terrain will look a bit washed out compared to real AC until a Phase 3b pass computes per-vertex landblock normals from the heightmap. Non-uniform scale (from scenery's random scale baked into MeshRef PartTransform) would technically require the inverse-transpose for correct normals, but scenery uses uniform scale so mat3(uModel) is good enough. Flagging as a known Phase 3+ concern if nonuniform scale ever shows up. Build clean, runtime clean: 1133 entities hydrated, no shader compile errors, process runs through startup. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/AcDream.App/Rendering/Shaders/mesh.frag | 17 ++++++++++++++--- src/AcDream.App/Rendering/Shaders/mesh.vert | 6 ++++++ src/AcDream.App/Rendering/Shaders/terrain.frag | 17 ++++++++++++++++- src/AcDream.App/Rendering/Shaders/terrain.vert | 5 +++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/AcDream.App/Rendering/Shaders/mesh.frag b/src/AcDream.App/Rendering/Shaders/mesh.frag index 382895e..28503bb 100644 --- a/src/AcDream.App/Rendering/Shaders/mesh.frag +++ b/src/AcDream.App/Rendering/Shaders/mesh.frag @@ -1,14 +1,25 @@ #version 430 core in vec2 vTex; +in vec3 vWorldNormal; out vec4 fragColor; uniform sampler2D uDiffuse; +// Phase 3a: simple directional lighting. A single sun direction + ambient term +// gives scenery and building faces enough differentiation to read as 3D instead +// of looking like paper cutouts. Hardcoded for now; a later phase can route +// light parameters through uniforms driven by the game's time-of-day. +const vec3 SUN_DIR = normalize(vec3(0.4, 0.3, 0.8)); +const float AMBIENT = 0.4; +const float DIFFUSE = 0.6; + void main() { vec4 sampled = texture(uDiffuse, vTex); // Alpha cutout for doors, windows, vegetation, and other alpha-keyed textures. - // Without this, zero-alpha pixels in palette-indexed textures render as opaque - // rectangles where the transparent parts should be. if (sampled.a < 0.5) discard; - fragColor = sampled; + + vec3 N = normalize(vWorldNormal); + float ndotl = max(dot(N, SUN_DIR), 0.0); + float lighting = AMBIENT + DIFFUSE * ndotl; + fragColor = vec4(sampled.rgb * lighting, sampled.a); } diff --git a/src/AcDream.App/Rendering/Shaders/mesh.vert b/src/AcDream.App/Rendering/Shaders/mesh.vert index b288c04..509ee49 100644 --- a/src/AcDream.App/Rendering/Shaders/mesh.vert +++ b/src/AcDream.App/Rendering/Shaders/mesh.vert @@ -8,8 +8,14 @@ uniform mat4 uView; uniform mat4 uProjection; out vec2 vTex; +out vec3 vWorldNormal; void main() { vTex = aTex; + // Transform the mesh normal into world space. For uniform-scale transforms + // (the common case), the upper-left 3x3 of uModel is correct. Non-uniform + // scale would require the inverse transpose; we accept that as a Phase 3+ + // concern. + vWorldNormal = normalize(mat3(uModel) * aNormal); gl_Position = uProjection * uView * uModel * vec4(aPos, 1.0); } diff --git a/src/AcDream.App/Rendering/Shaders/terrain.frag b/src/AcDream.App/Rendering/Shaders/terrain.frag index 6e0e917..f3ebbe4 100644 --- a/src/AcDream.App/Rendering/Shaders/terrain.frag +++ b/src/AcDream.App/Rendering/Shaders/terrain.frag @@ -1,10 +1,25 @@ #version 430 core in vec2 vTex; in flat uint vLayer; +in vec3 vWorldNormal; out vec4 fragColor; uniform sampler2DArray uAtlas; +// Phase 3a: shared lighting model with mesh.frag. Terrain normals are currently +// flat UnitZ (Phase 2b set this when building LandblockMesh vertices) so every +// terrain fragment gets the same ndotl contribution — terrain will look a bit +// flatter than hill-shaded terrain, which is a Phase 3b concern (compute +// per-vertex normals from the 9x9 heightmap to get relief lighting). +const vec3 SUN_DIR = normalize(vec3(0.4, 0.3, 0.8)); +const float AMBIENT = 0.4; +const float DIFFUSE = 0.6; + void main() { - fragColor = texture(uAtlas, vec3(vTex, float(vLayer))); + vec4 sampled = texture(uAtlas, vec3(vTex, float(vLayer))); + + vec3 N = normalize(vWorldNormal); + float ndotl = max(dot(N, SUN_DIR), 0.0); + float lighting = AMBIENT + DIFFUSE * ndotl; + fragColor = vec4(sampled.rgb * lighting, sampled.a); } diff --git a/src/AcDream.App/Rendering/Shaders/terrain.vert b/src/AcDream.App/Rendering/Shaders/terrain.vert index a9443e2..c81d77c 100644 --- a/src/AcDream.App/Rendering/Shaders/terrain.vert +++ b/src/AcDream.App/Rendering/Shaders/terrain.vert @@ -10,9 +10,14 @@ uniform mat4 uProjection; out vec2 vTex; out flat uint vLayer; +out vec3 vWorldNormal; void main() { vTex = aTex; vLayer = aTerrainLayer; + // uModel for terrain is a pure translation so mat3(uModel) is identity + // and vWorldNormal == aNormal. Computing it the uniform way anyway so + // later world-rotated landblocks (if any) still work. + vWorldNormal = normalize(mat3(uModel) * aNormal); gl_Position = uProjection * uView * uModel * vec4(aPos, 1.0); }