BlueGit

timfa raytracer

Files

raytracer / src / raycastScene.js

raycastScene.js

raycastScene.js 6.7 KiB Download Raw
1/**
2 * Fullscreen WebGL raycast view on frameworkv3.
3 * Wall strips: one source texel column per screen column, NEAREST, tinted for shading.
4 */
5class RaycastScene extends Scene
6{
7 constructor (width, height)
8 {
9 super(0, 0, width, height, Scene.DisplayModes.absolute);
10 this.backgroundColor = { r: 0, g: 0, b: 0 };
11 this.tintShader = null;
12 this.solidTexture = null;
13 this.gunImages = null;
14 this._lookBound = false;
15 this.floorTexMap = null;
16 this._floorCanvas = null;
17 this._floorCtx = null;
18 this._floorTex = null;
19 }
20
21 onLoad ()
22 {
23 const gl = this.glContext.gl;
24 this.glContext.setDefaultTextureFilter(gl.NEAREST, gl.NEAREST);
25
26 this.tintShader = new Shader(Shader.DEFAULT_VERT, [
27 "precision mediump float;",
28 "uniform sampler2D u_texture;",
29 "uniform float u_alpha;",
30 "uniform vec3 u_tint;",
31 "varying vec2 v_texCoord;",
32 "void main() {",
33 " vec4 color = texture2D(u_texture, v_texCoord);",
34 " gl_FragColor = vec4(color.rgb * u_tint, color.a * u_alpha);",
35 "}"
36 ].join("\n"));
37
38 // 1x1 white for solid (untextured) walls.
39 const c = document.createElement("canvas");
40 c.width = 1;
41 c.height = 1;
42 const ctx = c.getContext("2d");
43 ctx.fillStyle = "#ffffff";
44 ctx.fillRect(0, 0, 1, 1);
45 this.solidTexture = this.glContext.getTexture(c);
46
47 this.bindPointerLock();
48 }
49
50 bindPointerLock ()
51 {
52 if (this._lookBound)
53 { return; }
54 this._lookBound = true;
55 const canvas = this.cv_canvas;
56 canvas.addEventListener("click", function ()
57 {
58 if (canvas.requestPointerLock)
59 { canvas.requestPointerLock(); }
60 });
61 document.addEventListener("mousemove", function (e)
62 {
63 if (document.pointerLockElement === canvas)
64 { Player.applyLookDelta(e.movementX || 0, e.movementY || 0); }
65 });
66 }
67
68 update (deltaTime)
69 {
70 super.update(deltaTime);
71 Player.update(deltaTime);
72 if (typeof NpcSystem !== "undefined")
73 { NpcSystem.update(deltaTime); }
74 if (Player.horizontalResolution > this.real_size.x)
75 { Player.horizontalResolution = this.real_size.x; }
76 }
77
78 draw ()
79 {
80 if (this.destroyed || !this.glContext)
81 { return; }
82
83 this.g_fixSize();
84 const glc = this.glContext;
85 glc.endOffscreenPass();
86 glc.clear(0, 0, 0, 1);
87
88 this.drawFloorCeiling();
89 const wallDepth = this.drawColumns();
90 if (typeof NpcSystem !== "undefined" && NpcSystem.ready)
91 { NpcSystem.draw(this, Player, wallDepth); }
92 this.drawGun();
93 }
94
95
96 setFloorTextures (texMap)
97 {
98 this.floorTexMap = texMap;
99 }
100
101 drawFloorCeiling ()
102 {
103 if (!this.floorTexMap || !World.level || !World.level.floors)
104 { return; }
105 const w = Math.max(1, Math.round(this.real_size.x));
106 const h = Math.max(1, Math.round(this.real_size.y));
107 if (!this._floorCanvas || this._floorCanvas.width !== w || this._floorCanvas.height !== h)
108 {
109 this._floorCanvas = document.createElement("canvas");
110 this._floorCanvas.width = w;
111 this._floorCanvas.height = h;
112 this._floorCtx = this._floorCanvas.getContext("2d");
113 this._floorTex = null;
114 }
115 const img = this._floorCtx.createImageData(w, h);
116 FloorCast.render(img, w, h, Player, World.level, this.floorTexMap, World.size);
117 this._floorCtx.putImageData(img, 0, 0);
118 const glc = this.glContext;
119 const gl = glc.gl;
120 if (this._floorTex)
121 {
122 gl.bindTexture(gl.TEXTURE_2D, this._floorTex);
123 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this._floorCanvas);
124 }
125 else
126 {
127 glc.setDefaultTextureFilter(gl.NEAREST, gl.NEAREST);
128 this._floorTex = glc.getTexture(this._floorCanvas);
129 }
130 glc.drawSprite({
131 texture: this._floorTex,
132 x: w / 2,
133 y: h / 2,
134 w: w,
135 h: h,
136 rotationDeg: 0,
137 alpha: 1,
138 uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 }
139 });
140 }
141
142 faceLight (dir)
143 {
144 switch (dir)
145 {
146 case "east": return 0.9;
147 case "west": return 0.8;
148 case "south": return 0.7;
149 default: return 1.0;
150 }
151 }
152
153 drawColumns ()
154 {
155 const glc = this.glContext;
156 const w = this.real_size.x;
157 const h = this.real_size.y;
158 const cols = Math.max(1, Math.floor(Player.horizontalResolution));
159 const rad = Math.PI / (cols * 2);
160 const widthLine = w / cols;
161 const wallDepth = new Float32Array(Math.max(1, Math.round(w)));
162 for (var zi = 0; zi < wallDepth.length; zi++)
163 { wallDepth[zi] = 1e9; }
164
165 for (var i = 0; i < cols; i++)
166 {
167 var offsetP = -0.5 + (i / cols);
168 var offset = offsetP * cols;
169 var rotationalOffset = offset * rad;
170 var rot = Player.rotation + rotationalOffset;
171 if (rot < 0) { rot += Math.PI * 2; }
172 if (rot > Math.PI * 2) { rot -= Math.PI * 2; }
173
174 var rayHit = World.rayCast(Player.x, Player.y, rot);
175 if (!rayHit || !rayHit.tile)
176 { continue; }
177
178 var light = this.faceLight(rayHit.dir);
179 var deltaAngle = Player.rotation - rot;
180 var dist = rayHit.distance * Math.cos(deltaAngle);
181 if (dist < 0.0001)
182 { dist = 0.0001; }
183
184 var xLine = i * widthLine;
185 var wallH = (World.size * h) / dist;
186 var yCenter = (h / 2) + (-Player.pitch * (World.size * h - wallH) * 0.005);
187
188 var tint = [light, light, light];
189 var htmlImage = null;
190 var texW = 1;
191
192 // Z-buffer for NPC occlusion (per screen pixel column).
193 var x0 = Math.max(0, Math.floor(xLine));
194 var x1 = Math.min(wallDepth.length, Math.ceil(xLine + widthLine));
195 for (var xz = x0; xz < x1; xz++)
196 {
197 if (dist < wallDepth[xz])
198 { wallDepth[xz] = dist; }
199 }
200
201 if (rayHit.tile.Image && rayHit.tile.Image.g_image)
202 {
203 htmlImage = rayHit.tile.Image.g_image;
204 texW = htmlImage.width || 1;
205 var uv = textureColumnUv(rayHit.tileX, texW);
206 glc.drawSprite({
207 texture: glc.getTexture(htmlImage),
208 x: xLine + widthLine / 2,
209 y: yCenter,
210 w: widthLine,
211 h: wallH,
212 rotationDeg: 0,
213 alpha: 1,
214 uvRect: { u0: uv.u0, v0: uv.v0, u1: uv.u1, v1: uv.v1 },
215 shader: this.tintShader,
216 uniforms: { u_tint: tint }
217 });
218 }
219 else
220 {
221 var c = rayHit.tile.Color || { R: 255, G: 255, B: 255 };
222 tint = [(c.R / 255) * light, (c.G / 255) * light, (c.B / 255) * light];
223 glc.drawSprite({
224 texture: this.solidTexture,
225 x: xLine + widthLine / 2,
226 y: yCenter,
227 w: widthLine,
228 h: wallH,
229 rotationDeg: 0,
230 alpha: 1,
231 uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 },
232 shader: this.tintShader,
233 uniforms: { u_tint: tint }
234 });
235 }
236 }
237 return wallDepth;
238 }
239
240 drawGun ()
241 {
242 const images = this.game && this.game.images;
243 if (!images)
244 { return; }
245 var gunImg = null;
246 switch (Player.currentGun)
247 {
248 case 0: gunImg = images.shotgun; break;
249 case 1: gunImg = images.machinegun; break;
250 case 2: gunImg = images.pulserifle; break;
251 }
252 if (!gunImg || !gunImg.g_image)
253 { return; }
254
255 var p = this.real_size.y / 1080;
256 var gw = p * gunImg.g_image.width;
257 var gh = p * gunImg.g_image.height;
258 var gx = this.real_size.x * Player.gunX + gw / 2;
259 var gy = this.real_size.y * Player.gunY + gh / 2;
260
261 gunImg.draw(
262 this,
263 gx,
264 gy,
265 gw,
266 gh,
267 0,
268 false,
269 false,
270 1,
271 0
272 );
273 }
274}