-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathraycaster3d.js
365 lines (337 loc) · 9.89 KB
/
raycaster3d.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// @ts-check
// Start kaplay
kaplay();
// load assets
let bean;
let objSlices = [];
let wall;
let slices = [];
loadSprite("bean", "sprites/bean.png");
loadSprite("wall", "sprites/brick_wall.png");
onLoad(() => {
bean = getSprite("bean").data;
for (let i = 0; i < bean.width; i++) {
objSlices.push(
bean.frames[0].scale(
new Quad(i / bean.width, 0, 1 / bean.width, 1),
),
);
}
wall = getSprite("wall").data;
for (let i = 0; i < wall.width; i++) {
slices.push(
wall.frames[0].scale(
new Quad(i / wall.width, 0, 1 / wall.width, 1),
),
);
}
});
function rayCastGrid(origin, direction, gridPosHit, maxDistance = 64) {
const pos = origin;
const len = direction.len();
const dir = direction.scale(1 / len);
let t = 0;
let gridPos = vec2(Math.floor(origin.x), Math.floor(origin.y));
const step = vec2(dir.x > 0 ? 1 : -1, dir.y > 0 ? 1 : -1);
const tDelta = vec2(Math.abs(1 / dir.x), Math.abs(1 / dir.y));
let dist = vec2(
(step.x > 0) ? (gridPos.x + 1 - origin.x) : (origin.x - gridPos.x),
(step.y > 0) ? (gridPos.y + 1 - origin.y) : (origin.y - gridPos.y),
);
let tMax = vec2(
(tDelta.x < Infinity) ? tDelta.x * dist.x : Infinity,
(tDelta.y < Infinity) ? tDelta.y * dist.y : Infinity,
);
let steppedIndex = -1;
while (t <= maxDistance) {
const hit = gridPosHit(gridPos);
if (hit === true) {
return {
point: pos.add(dir.scale(t)),
normal: vec2(
steppedIndex === 0 ? -step.x : 0,
steppedIndex === 1 ? -step.y : 0,
),
t: t / len, // Since dir is normalized, t is len times too large
gridPos,
};
}
else if (hit) {
return hit;
}
if (tMax.x < tMax.y) {
gridPos.x += step.x;
t = tMax.x;
tMax.x += tDelta.x;
steppedIndex = 0;
}
else {
gridPos.y += step.y;
t = tMax.y;
tMax.y += tDelta.y;
steppedIndex = 1;
}
}
return null;
}
function raycastEdge(origin, direction, line) {
const a = origin;
const c = line.p1.add(line.pos);
const d = line.p2.add(line.pos);
const ab = direction;
const cd = d.sub(c);
let abxcd = ab.cross(cd);
// If parallel, no intersection
if (Math.abs(abxcd) < Number.EPSILON) {
return false;
}
const ac = c.sub(a);
const s = ac.cross(cd) / abxcd;
// s is the percentage of the position of the intersection on cd
if (s <= 0 || s >= 1) {
return false;
}
const t = ac.cross(ab) / abxcd;
// t is the percentage of the position of the intersection on ab
if (t <= 0 || t >= 1) {
return false;
}
const normal = cd.normal().unit();
if (direction.dot(normal) > 0) {
normal.x *= -1;
normal.y *= -1;
}
return {
point: a.add(ab.scale(s)),
normal: normal,
t: s,
s: t,
object: line,
};
}
function rayCastAsciiGrid(origin, direction, grid) {
origin = origin.scale(1 / 16);
direction = direction.scale(1 / 16);
const objects = [];
const hit = rayCastGrid(origin, direction, ({ x, y }) => {
if (y >= 0 && y < grid.length) {
const row = grid[y];
if (x >= 0 && x < row.length) {
if (row[x] === "&") {
const perp = direction.normal().unit();
const planeP1 = perp.scale(-0.2);
const planeP2 = perp.scale(0.2);
const objectHit = raycastEdge(origin, direction, {
pos: vec2(x + 0.5, y + 0.5),
p1: planeP1,
p2: planeP2,
});
if (objectHit) {
objects.push(objectHit);
}
}
return row[x] !== " " && row[x] !== "&";
}
}
}, direction.len());
if (hit) {
hit.point = hit.point.scale(16);
hit.object = { color: colors[grid[hit.gridPos.y][hit.gridPos.x]] };
hit.objects = objects;
}
return hit;
}
const colors = {
"#": RED,
"$": GREEN,
"%": BLUE,
"&": YELLOW,
};
const grid = [
"##################",
"# #",
"# $$$$$$$ $$$$$$ #",
"# $ $ #",
"# $ %% %%%%%%% $ #",
"# $ % % $ #",
"#&$&%%%%% %%%&$&#",
"# $ % $ #",
"# $ %%%%%%%%%% #",
"# $ $ #",
"# $$$$$$$ $$$$$$ #",
"# & #",
"##################",
];
const camera = add([
pos(7 * 16, 11 * 16 + 8),
rotate(0),
z(-1),
rect(8, 8),
anchor("center"),
area(),
opacity(0),
body(),
{
draw() {
pushTransform();
pushRotate(-this.angle);
drawCircle({
pos: vec2(),
radius: 4,
color: RED,
});
const dir = Vec2.fromAngle(this.angle);
const perp = dir.normal();
const planeP1 = this.pos.add(dir.scale(this.focalLength)).add(
perp.scale(this.fov),
).sub(this.pos);
const planeP2 = this.pos.add(dir.scale(this.focalLength)).sub(
perp.scale(this.fov),
).sub(this.pos);
drawLine({
p1: planeP1,
p2: planeP2,
width: 1,
color: RED,
});
pushTranslate(this.pos.scale(-1).add(300, 50));
drawRect({
width: 240,
height: 120,
color: rgb(100, 100, 100),
});
drawRect({
pos: vec2(0, 120),
width: 240,
height: 120,
color: rgb(128, 128, 128),
});
for (let x = 0; x <= 120; x++) {
let direction = lerp(planeP1, planeP2, x / 120).scale(6);
const hit = rayCastAsciiGrid(this.pos, direction, grid);
if (hit) {
const t = hit.t;
// Distance to attenuate light
const d = (1 - t)
* ((hit.normal.x + hit.normal.y) < 0 ? 0.5 : 1);
// Horizontal texture slice
let u = Math.abs(hit.normal.x) > Math.abs(hit.normal.y)
? hit.point.y
: hit.point.x;
u = (u % 16) / 16;
u = u - Math.floor(u);
// Height of the wall
const h = 240 / (t * direction.len() / 16);
drawUVQuad({
width: 2,
height: h,
pos: vec2(x * 2, 120 - h / 2),
tex: wall.tex,
quad: slices[Math.round(u * (wall.width - 1))],
color: BLACK.lerp(WHITE, d),
});
// If we hit any objects
if (hit.objects) {
hit.objects.reverse().forEach(o => {
const t = o.t;
// Wall and object height
const wh = 240 / (t * direction.len() / 16);
const oh = 140 / (t * direction.len() / 16);
// Slice to render
let u = o.s;
drawUVQuad({
width: 2,
height: oh,
pos: vec2(x * 2, 120 + wh / 2 - oh),
tex: bean.tex,
quad:
objSlices[Math.round(u * (bean.width - 1))],
color: BLACK.lerp(WHITE, u),
});
});
}
}
}
popTransform();
},
focalLength: 40,
fov: 10,
},
]);
addLevel(grid, {
pos: vec2(0, 0),
tileWidth: 16,
tileHeight: 16,
tiles: {
"#": () => [
rect(16, 16),
color(RED),
area(),
body({ isStatic: true }),
],
"$": () => [
rect(16, 16),
color(GREEN),
area(),
body({ isStatic: true }),
],
"%": () => [
rect(16, 16),
color(BLUE),
area(),
body({ isStatic: true }),
],
"&": () => [
pos(4, 4),
rect(8, 8),
color(YELLOW),
],
},
});
onKeyDown("up", () => {
camera.move(Vec2.fromAngle(camera.angle).scale(40));
});
onKeyDown("down", () => {
camera.move(Vec2.fromAngle(camera.angle).scale(-40));
});
onKeyDown("left", () => {
camera.angle -= 90 * dt();
});
onKeyDown("right", () => {
camera.angle += 90 * dt();
});
onKeyDown("f", () => {
camera.focalLength = Math.max(1, camera.focalLength - 10 * dt());
});
onKeyDown("g", () => {
camera.focalLength += 10 * dt();
});
onKeyDown("r", () => {
camera.fov = Math.max(1, camera.fov - 10 * dt());
});
onKeyDown("t", () => {
camera.fov += 10 * dt();
});
onKeyDown("p", () => {
debug.paused = !debug.paused;
});
let lastPos = vec2();
onTouchStart(pos => {
lastPos = pos;
});
onTouchMove(pos => {
const delta = pos.sub(lastPos);
if (delta.x < 0) {
camera.angle -= 90 * dt();
}
else if (delta.x > 0) {
camera.angle += 90 * dt();
}
if (delta.y < 0) {
camera.move(Vec2.fromAngle(camera.angle).scale(40));
}
else if (delta.y > 0) {
camera.move(Vec2.fromAngle(camera.angle).scale(-40));
}
lastPos = pos;
});