/** * Point lights for the raycaster (CPU). * Walls/floor/ceiling: Lambertian N·L + distance falloff + optional grid LOS. * Paperdolls: same lights, L transformed into the .ppda view space (view=+Z) * so baked normals respond correctly (see Paperdoll compositor.rs relight). * * CPU (not a custom WebGL shader): frameworkv3 is sprite/WebGL1 without a * shared G-buffer; floor is already an ImageData upload, walls are 1px * columns, NPCs are a CPU z-buffer overlay — lighting fits those paths with * no extra GPU pipeline. Grid LOS is O(steps) per sample and capped by * light range; cheaper than raymarched shadows and enough for tile maps. */ var Lighting = { DEFAULT_AMBIENT: 0.38, MAX_LIGHTS: 32, normalize3: function (x, y, z) { var len = Math.sqrt(x * x + y * y + z * z); if (len < 1e-8) { return { x: 0, y: 0, z: 1 }; } return { x: x / len, y: y / len, z: z / len }; }, decodeNormalRgb: function (r, g, b) { return this.normalize3( r / 255 * 2 - 1, g / 255 * 2 - 1, b / 255 * 2 - 1 ); }, /** intensity * saturate(1 - dist/range)^falloff */ attenuation: function (dist, range, falloff) { if (range <= 0) { return 0; } var t = 1 - dist / range; if (t <= 0) { return 0; } var f = falloff == null ? 2 : falloff; if (f === 1) { return t; } if (f === 2) { return t * t; } return Math.pow(t, f); }, faceNormal: function (dir) { switch (dir) { case "north": return { x: 0, y: -1, z: 0 }; case "south": return { x: 0, y: 1, z: 0 }; case "east": return { x: 1, y: 0, z: 0 }; case "west": return { x: -1, y: 0, z: 0 }; case "floor": return { x: 0, y: 0, z: 1 }; case "ceiling": return { x: 0, y: 0, z: -1 }; default: return { x: 0, y: 0, z: 1 }; } }, /** * Grid line-of-sight on the wall map (Bresenham). Endpoints are the light * and sample tiles; a solid wall strictly between blocks the ray. */ hasLineOfSight: function (x0, y0, x1, y1, tiles, mapW, mapH, cellSize) { cellSize = cellSize || 32; var tx0 = Math.floor(x0 / cellSize); var ty0 = Math.floor(y0 / cellSize); var tx1 = Math.floor(x1 / cellSize); var ty1 = Math.floor(y1 / cellSize); var dx = Math.abs(tx1 - tx0); var dy = Math.abs(ty1 - ty0); var sx = tx0 < tx1 ? 1 : -1; var sy = ty0 < ty1 ? 1 : -1; var err = dx - dy; var x = tx0; var y = ty0; var steps = 0; var maxSteps = dx + dy + 2; while (!(x === tx1 && y === ty1) && steps < maxSteps) { steps++; var e2 = 2 * err; if (e2 > -dy) { err -= dy; x += sx; } if (e2 < dx) { err += dx; y += sy; } if (x === tx1 && y === ty1) { break; } if (x < 0 || y < 0 || x >= mapW || y >= mapH) { return false; } var id = tiles[x + y * mapW]; if (id === 1 || id === 3) { return false; } } return true; }, /** * RGB multipliers (can exceed 1 before clamp by caller). * @param {{x,y,z}} pos world * @param {{x,y,z}} normal unit * @param {Array} lights * @param {number} ambient * @param {object|null} los { tiles, width, height, cellSize } or null to skip */ shadeRgb: function (pos, normal, lights, ambient, los) { ambient = ambient == null ? this.DEFAULT_AMBIENT : ambient; var r = ambient; var g = ambient; var b = ambient; if (!lights || !lights.length) { return { r: r, g: g, b: b }; } var n = Math.min(lights.length, this.MAX_LIGHTS); for (var i = 0; i < n; i++) { var L = lights[i]; var lx = L.x, ly = L.y, lz = L.z != null ? L.z : 16; var dx = lx - pos.x; var dy = ly - pos.y; var dz = lz - pos.z; var dist = Math.sqrt(dx * dx + dy * dy + dz * dz); var att = this.attenuation(dist, L.range || 64, L.falloff); if (att <= 0.001) { continue; } if (los && !this.hasLineOfSight(lx, ly, pos.x, pos.y, los.tiles, los.width, los.height, los.cellSize)) { continue; } var ld = this.normalize3(dx, dy, dz); var ndotl = normal.x * ld.x + normal.y * ld.y + normal.z * ld.z; if (ndotl <= 0) { continue; } var inten = (L.intensity == null ? 1 : L.intensity) * att * ndotl; var cr = L.color ? L.color[0] : 1; var cg = L.color ? L.color[1] : 1; var cb = L.color ? L.color[2] : 1; r += inten * cr; g += inten * cg; b += inten * cb; } return { r: r, g: g, b: b }; }, /** * Transform a world-space direction (toward light) into paperdoll view space * for the chosen yaw (degrees). View +Z = toward capture camera (= front at yaw 0). * Character +Z (front) aligns with facingDeg in the XY plane; +Y = world up (Z). */ worldDirToView: function (dirX, dirY, dirZ, yawDeg, facingDeg) { var face = (facingDeg || 0) * Math.PI / 180; var yaw = (yawDeg || 0) * Math.PI / 180; // Character basis in world (XY) + up Z var frontX = Math.cos(face); var frontY = Math.sin(face); var rightX = -Math.sin(face); var rightY = Math.cos(face); // Camera direction from character (char space): yaw 0 → +Z front var camCX = Math.sin(yaw); var camCZ = Math.cos(yaw); var camX = rightX * camCX + frontX * camCZ; var camY = rightY * camCX + frontY * camCZ; // viewZ = camDir (toward camera), viewY = up, viewX = viewY × viewZ var viewZx = camX, viewZy = camY, viewZz = 0; var viewYx = 0, viewYy = 0, viewYz = 1; // cross(up, viewZ) for right-handed viewX? Standard: viewX = normalize(cross(viewY?)) // viewX = cross(viewY, viewZ) with viewY=up → (-viewZy, viewZx, 0) wait: // cross((0,0,1), (camX,camY,0)) = (-camY, camX, 0) var viewXx = -camY; var viewXy = camX; var viewXz = 0; var vlen = Math.sqrt(viewXx * viewXx + viewXy * viewXy); if (vlen > 1e-8) { viewXx /= vlen; viewXy /= vlen; } return this.normalize3( dirX * viewXx + dirY * viewXy + dirZ * viewXz, dirX * viewYx + dirY * viewYy + dirZ * viewYz, dirX * viewZx + dirY * viewZy + dirZ * viewZz ); }, /** * Relight one paperdoll pixel (albedo RGB + view-space normal) with point lights. * pixelWorld ≈ NPC base + height from atlas row; depth shifts along view cam. */ shadePaperdollPixel: function (albedoR, albedoG, albedoB, nr, ng, nb, pixelWorld, lights, ambient, yawDeg, facingDeg) { ambient = ambient == null ? this.DEFAULT_AMBIENT : ambient; var n = this.decodeNormalRgb(nr, ng, nb); var r = ambient * albedoR; var g = ambient * albedoG; var b = ambient * albedoB; if (!lights || !lights.length) { // Match old preview-ish fill when no lamps: slight Lambert from default view light var nd = Math.max(0, n.x * 0.3 + n.y * 0.2 + n.z * 0.9); var shade = Math.min(1, 0.35 + 0.65 * nd); return { r: Math.min(255, Math.round(albedoR * shade)), g: Math.min(255, Math.round(albedoG * shade)), b: Math.min(255, Math.round(albedoB * shade)) }; } var count = Math.min(lights.length, this.MAX_LIGHTS); for (var i = 0; i < count; i++) { var L = lights[i]; var lx = L.x, ly = L.y, lz = L.z != null ? L.z : 16; var dx = lx - pixelWorld.x; var dy = ly - pixelWorld.y; var dz = lz - pixelWorld.z; var dist = Math.sqrt(dx * dx + dy * dy + dz * dz); var att = this.attenuation(dist, L.range || 64, L.falloff); if (att <= 0.001) { continue; } var lv = this.worldDirToView(dx, dy, dz, yawDeg, facingDeg); var ndotl = n.x * lv.x + n.y * lv.y + n.z * lv.z; if (ndotl <= 0) { continue; } var inten = (L.intensity == null ? 1 : L.intensity) * att * ndotl; var cr = L.color ? L.color[0] : 1; var cg = L.color ? L.color[1] : 1; var cb = L.color ? L.color[2] : 1; r += albedoR * inten * cr; g += albedoG * inten * cg; b += albedoB * inten * cb; } return { r: Math.min(255, Math.round(r)), g: Math.min(255, Math.round(g)), b: Math.min(255, Math.round(b)) }; }, /** Legacy wall face shade (no lamps) — keep look of pre-light builds. */ legacyFaceShade: function (dir) { switch (dir) { case "east": return 0.9; case "west": return 0.8; case "south": return 0.7; default: return 1.0; } } }; if (typeof module !== "undefined" && module.exports) { module.exports = Lighting; }