Implement Source engine lightcache system for entity lighting #30
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Implement the full Source engine lightcache pipeline for entity lighting, matching how the engine combines ambient cubes with world lights (point lights, spotlights, sun) including occlusion traces.
Current state
We have a partial implementation:
onBeforeRendercallbacks for per-draw-call lightingBut we're missing the core of what Source does — the world lights system.
How Source does it (from SDK 2013 source)
Entity lighting = ambient cube + up to 4 local lights, computed per-entity:
dworldlight_t[])emit_skylight)locallight[]entries with full per-vertex shadingDWL_FLAGS_INAMBIENTCUBEare skipped (already baked into leaf ambient)Shader pipeline (from
common_vs_fxc.h)Each local light has: position, direction, color, attenuation params, spotlight cone angles.
Requirements
1. Parse world lights from BSP
Parse lump 15 (
LUMP_WORLDLIGHTS) or 54 (LUMP_WORLDLIGHTS_HDR):emittype_t: 0=surface, 1=point, 2=spotlight, 3=skylight, 4=quakelight, 5=skyambient
2. Implement lightcache per entity
For each entity position:
emit_skylight: check leaf sky flag (existing), full contribution if visibleemit_point:intensity / (const + linear*dist + quad*dist²), with radius cutoffemit_spotlight: same + angular attenuation viastopdot/stopdot2emit_surface: same as spotlight with 90° coneemit_skyambient(already in leaf ambient cube)DWL_FLAGS_INAMBIENTCUBEwhen leaf ambient is used3. Update shader to handle local lights
Add to the uber-shader:
uniform vec3 localLightPos[4]uniform vec3 localLightColor[4]uniform vec3 localLightDir[4](for spotlights/directional)uniform vec4 localLightAtten[4](const, linear, quad, type)uniform vec4 localLightSpot[4](stopdot, stopdot2, exponent, 0)uniform int numLocalLightsPer-vertex or per-fragment light evaluation:
4. Remove hardcoded sun handling
The sun (
emit_skylight) should be just another world light that goes through the same pipeline. Remove the separatesunDirection/sunColor/sunOcclusionuniforms for the ambient cube path — the sun becomes one of the 4 local lights (or gets folded into the ambient cube if it's not in the top 4).Context
client/src/bsp/parser.jsclient/src/bsp/leaf-lighting.jsclient/src/bsp/material.jsclient/src/scene.js/home/kit/Develop/source-sdk-2013/src/public/bspfile.h/home/kit/Develop/source-sdk-2013/src/materialsystem/stdshaders/common_vs_fxc.hFixed in
fa5cb22. Three bugs were causing incorrect BSP lighting:ColorRGBExp32 decode —
TexLightToLinearisc * 2^exp(no normalization). Our decode had a spurious/255, making all lightmap values 255x too small.Bumpmapped lightmap stride — Faces with
SURF_BUMPLIGHTstore 4 lightmap layers per style (flat + 3 directional). We assumed 1 layer per style, so 1032 bumpmapped faces with style 33 were reading style 0's bump data instead of the actual sun contribution.Gamma correction — Linear lightmap values multiplied directly against sRGB base textures crushed dark areas. Now gamma-encode lighting (
pow(light, 1/2.2)) before texture multiplication. Applied to lightmap, ambient cube, and local light paths.Also added: