/** * Point lights for the raycaster (CPU). * Walls/floor/ceiling: Lambertian N·L + distance falloff; walls also grid LOS. * Paperdolls: view-space normals from .ppda (RGB = n*0.5+0.5, view=+Z) with L * transformed into the chosen yaw view; Half-Lambert so side lights still * reach front-facing atlas normals, plus a slight geometric X tilt from u. * * Floor/ceiling skip grid LOS: tile Bresenham casts hard vertical shadow * seams in screen space; distance falloff is enough there. Walls keep LOS * with the sample nudged into open space so the hit cell is not a blocker. */ 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); }, /** * Outward normal of the visible wall face. * Raycaster `dir` = travel direction of the casting ray when it hit: * "west" → ray went east, visible face is the west face → normal (-1,0,0) * "east" → ray went west, visible face is the east face → normal (+1,0,0) * "north" → ray went north (−Y), visible face is the south face → (+Y) * "south" → ray went south (+Y), visible face is the north face → (−Y) */ 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 LOS (Bresenham). Skips start+end tiles. Optional corner check so * diagonal steps cannot slip through a wall corner unnoticed. */ 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); if (tx0 === tx1 && ty0 === ty1) { return true; } 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 (steps < maxSteps) { steps++; var e2 = 2 * err; var movedX = false, movedY = false; if (e2 > -dy) { err -= dy; x += sx; movedX = true; } if (e2 < dx) { err += dx; y += sy; movedY = true; } if (x === tx1 && y === ty1) { return true; } if (x < 0 || y < 0 || x >= mapW || y >= mapH) { return false; } var id = tiles[x + y * mapW]; if (id === 1 || id === 3) { return false; } // Diagonal: also block if cutting a corner through a solid. if (movedX && movedY) { var a = tiles[(x - sx) + y * mapW]; var b = tiles[x + (y - sy) * mapW]; if ((a === 1 || a === 3) && (b === 1 || b === 3)) { return false; } } } return true; }, /** * RGB multipliers. los: {tiles,width,height,cellSize} or null. * For walls pass sample already nudged into open space along the outward normal. */ 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 }; }, /** * World direction (toward light) → paperdoll view space for yawDeg. * Yaw 0: camera on character +Z (front); view +Z toward camera, +Y up, * +X = camera right = character right at yaw 0. */ worldDirToView: function (dirX, dirY, dirZ, yawDeg, facingDeg) { var face = (facingDeg || 0) * Math.PI / 180; var yaw = (yawDeg || 0) * Math.PI / 180; var frontX = Math.cos(face); var frontY = Math.sin(face); var rightX = -Math.sin(face); var rightY = Math.cos(face); // Camera offset in character 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 = toward camera (in XY), viewY = up, viewX = cross(viewY, viewZ) var viewXx = -camY; var viewXy = camX; var vlen = Math.sqrt(viewXx * viewXx + viewXy * viewXy); if (vlen > 1e-8) { viewXx /= vlen; viewXy /= vlen; } return this.normalize3( dirX * viewXx + dirY * viewXy, dirZ, dirX * camX + dirY * camY ); }, /** * Half-Lambert wrap so front-facing atlas normals still receive side light; * geoTiltX nudges the normal toward the billboard side (u 0..1 → -1..+1). */ shadePaperdollPixel: function (albedoR, albedoG, albedoB, nr, ng, nb, pixelWorld, lights, ambient, yawDeg, facingDeg, geoTiltX) { ambient = ambient == null ? this.DEFAULT_AMBIENT : ambient; var n = this.decodeNormalRgb(nr, ng, nb); if (geoTiltX) { n = this.normalize3(n.x + geoTiltX * 0.85, n.y, n.z + 0.15); } var r = ambient * albedoR; var g = ambient * albedoG; var b = ambient * albedoB; if (!lights || !lights.length) { 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 raw = n.x * lv.x + n.y * lv.y + n.z * lv.z; // Half-Lambert: side lights still reach front-facing .ppda normals. var ndotl = raw * 0.5 + 0.5; if (ndotl <= 0.02) { 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)) }; }, legacyFaceShade: function (dir) { switch (dir) { case "east": return 0.9; case "west": return 0.8; case "south": return 0.7; default: return 1.0; } }, /** Max |Δ| between consecutive column means in a row range (for seam tests). */ maxColumnJump: function (getColumnMean, width) { var best = 0; var prev = getColumnMean(0); for (var x = 1; x < width; x++) { var v = getColumnMean(x); var d = Math.abs(v - prev); if (d > best) { best = d; } prev = v; } return best; } }; if (typeof module !== "undefined" && module.exports) { module.exports = Lighting; }