-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlighting.ts
More file actions
181 lines (159 loc) · 6.1 KB
/
Copy pathlighting.ts
File metadata and controls
181 lines (159 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { Robot } from '../game/robot';
import { Camera } from './camera';
import { World } from '../world/world';
import { VoxelType } from '../world/types';
import type { Cell } from './renderer';
const MAX_LIGHT_RADIUS = 60;
const AMBIENT_RADIUS = 10;
const NUM_RAYS = 180;
export class Lighting {
cols: number;
rows: number;
lightMap: Float32Array;
// 1 where a direct light ray reached the cell, 0 where it's only lit by
// the short "x-ray" bleed past an occluder. Used to keep bleed-lit walls
// visually distinct from collision-reachable ones.
directMap: Uint8Array;
constructor(cols: number, rows: number) {
this.cols = cols;
this.rows = rows - 6; // viewport height minus HUD
this.lightMap = new Float32Array(cols * this.rows);
this.directMap = new Uint8Array(cols * this.rows);
}
getLight(x: number, y: number): number {
if (x < 0 || x >= this.cols || y < 0 || y >= this.rows) return 0;
return this.lightMap[y * this.cols + x];
}
isDirect(x: number, y: number): boolean {
if (x < 0 || x >= this.cols || y < 0 || y >= this.rows) return false;
return this.directMap[y * this.cols + x] === 1;
}
compute(robot: Robot, camera: Camera, world: World) {
// Clear lightmap
this.lightMap.fill(0);
this.directMap.fill(0);
if (!robot.lightActive && !robot.engineOn) {
// Completely dark — tiny glow at robot position only
const cx = Math.floor(this.cols / 2);
const cy = Math.floor(this.rows / 2);
this.addLight(cx, cy, 0.15);
return;
}
const cx = Math.floor(this.cols / 2);
const cy = Math.floor(this.rows / 2);
const radius = robot.lightActive
? (robot.lightDim ? MAX_LIGHT_RADIUS * 0.7 : MAX_LIGHT_RADIUS)
: AMBIENT_RADIUS;
const intensity = robot.lightActive
? (robot.lightDim ? 0.65 : 1.0)
: 0.2;
// Cast rays in all directions for shadow casting
for (let i = 0; i < NUM_RAYS; i++) {
const angle = (i / NUM_RAYS) * Math.PI * 2;
const dx = Math.cos(angle);
const dy = Math.sin(angle);
this.castLightRay(cx, cy, dx, dy, radius, intensity, world, robot, camera);
}
// Small ambient glow at robot position always
for (let dy = -2; dy <= 2; dy++) {
for (let dx = -2; dx <= 2; dx++) {
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist <= 2) {
this.addLight(cx + dx, cy + dy, 0.1 * (1 - dist / 3));
}
}
}
}
private castLightRay(
ox: number, oy: number,
dx: number, dy: number,
maxDist: number, intensity: number,
world: World, robot: Robot, camera: Camera
) {
let x = ox;
let y = oy;
for (let d = 0; d < maxDist; d++) {
const ix = Math.floor(x);
const iy = Math.floor(y);
if (ix < 0 || ix >= this.cols || iy < 0 || iy >= this.rows) break;
const falloff = 1 - (d / maxDist);
const light = intensity * falloff * falloff; // quadratic falloff
this.addLight(ix, iy, light);
if (ix >= 0 && ix < this.cols && iy >= 0 && iy < this.rows) {
this.directMap[iy * this.cols + ix] = 1;
}
// Check if ray hits a wall (in screen space, we check the world)
const cosA = Math.cos(camera.angle);
const sinA = Math.sin(camera.angle);
const worldX = Math.floor(robot.x + (ix - this.cols / 2) * cosA);
const worldY = Math.floor(robot.y + (iy - this.rows / 2));
const worldZ = Math.floor(robot.z + (ix - this.cols / 2) * (-sinA));
const voxel = world.getVoxel(worldX, worldY, worldZ);
if (voxel !== VoxelType.EMPTY && voxel !== VoxelType.WATER && voxel !== VoxelType.FOG && d > 0) {
// Hit a wall — light stops, but bleed a faint "x-ray" into the next
// few view-plane cells along the ray so occluded walls (still the
// actual collision geometry) remain readable without exposing the
// whole level.
const BLEED_STEPS = 4;
const BLEED_INTENSITY = 0.12;
let bx = x + dx;
let by = y + dy;
for (let b = 0; b < BLEED_STEPS; b++) {
const bix = Math.floor(bx);
const biy = Math.floor(by);
if (bix < 0 || bix >= this.cols || biy < 0 || biy >= this.rows) break;
const bFalloff = 1 - b / BLEED_STEPS;
this.addLight(bix, biy, BLEED_INTENSITY * bFalloff * bFalloff);
bx += dx;
by += dy;
}
break;
}
x += dx;
y += dy;
}
}
private addLight(x: number, y: number, amount: number) {
if (x < 0 || x >= this.cols || y < 0 || y >= this.rows) return;
const idx = y * this.cols + x;
this.lightMap[idx] = Math.min(1, this.lightMap[idx] + amount);
}
// After renderWorld has populated the grid, fog cells broadcast a soft
// white glow out to neighboring screen cells so the pool illuminates the
// walls it touches. Sparse sampling (every STEPth fog cell) keeps cost
// bounded — the radius is generous, so adjacent samples overlap plenty.
addEmissiveFogGlow(grid: Cell[][], viewTop: number) {
const RADIUS = 4;
const INTENSITY = 0.7;
const STEP = 3;
const r2 = RADIUS * RADIUS;
const maxY = viewTop + this.rows;
const scale = STEP * STEP * 0.11;
for (let sy = viewTop; sy < maxY; sy += STEP) {
const row = grid[sy];
for (let sx = 0; sx < this.cols; sx += STEP) {
if (row[sx].material !== VoxelType.FOG) continue;
for (let dy = -RADIUS; dy <= RADIUS; dy++) {
const ty = sy - viewTop + dy;
if (ty < 0 || ty >= this.rows) continue;
const rowBase = ty * this.cols;
for (let dx = -RADIUS; dx <= RADIUS; dx++) {
const tx = sx + dx;
if (tx < 0 || tx >= this.cols) continue;
const d2 = dx * dx + dy * dy;
if (d2 > r2) continue;
// Quadratic falloff, no sqrt
const f = 1 - d2 / r2;
const light = INTENSITY * f * f * scale;
const idx = rowBase + tx;
const cur = this.lightMap[idx];
if (light > 0 && cur < 1) {
this.lightMap[idx] = Math.min(1, cur + light);
this.directMap[idx] = 1;
}
}
}
}
}
}
}