BlueGit

timfa raytracer

Files

raytracer / src / raycastScene.js

raycastScene.js

raycastScene.js 5.4 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 }
16
17 onLoad ()
18 {
19 const gl = this.glContext.gl;
20 this.glContext.setDefaultTextureFilter(gl.NEAREST, gl.NEAREST);
21
22 this.tintShader = new Shader(Shader.DEFAULT_VERT, [
23 "precision mediump float;",
24 "uniform sampler2D u_texture;",
25 "uniform float u_alpha;",
26 "uniform vec3 u_tint;",
27 "varying vec2 v_texCoord;",
28 "void main() {",
29 " vec4 color = texture2D(u_texture, v_texCoord);",
30 " gl_FragColor = vec4(color.rgb * u_tint, color.a * u_alpha);",
31 "}"
32 ].join("\n"));
33
34 // 1x1 white for solid (untextured) walls.
35 const c = document.createElement("canvas");
36 c.width = 1;
37 c.height = 1;
38 const ctx = c.getContext("2d");
39 ctx.fillStyle = "#ffffff";
40 ctx.fillRect(0, 0, 1, 1);
41 this.solidTexture = this.glContext.getTexture(c);
42
43 this.bindPointerLock();
44 }
45
46 bindPointerLock ()
47 {
48 if (this._lookBound)
49 { return; }
50 this._lookBound = true;
51 const canvas = this.cv_canvas;
52 canvas.addEventListener("click", function ()
53 {
54 if (canvas.requestPointerLock)
55 { canvas.requestPointerLock(); }
56 });
57 document.addEventListener("mousemove", function (e)
58 {
59 if (document.pointerLockElement === canvas)
60 { Player.applyLookDelta(e.movementX || 0, e.movementY || 0); }
61 });
62 }
63
64 update (deltaTime)
65 {
66 super.update(deltaTime);
67 Player.update(deltaTime);
68 if (typeof NpcSystem !== "undefined")
69 { NpcSystem.update(deltaTime); }
70 if (Player.horizontalResolution > this.real_size.x)
71 { Player.horizontalResolution = this.real_size.x; }
72 }
73
74 draw ()
75 {
76 if (this.destroyed || !this.glContext)
77 { return; }
78
79 this.g_fixSize();
80 const glc = this.glContext;
81 glc.endOffscreenPass();
82 glc.clear(0, 0, 0, 1);
83
84 const wallDepth = this.drawColumns();
85 if (typeof NpcSystem !== "undefined" && NpcSystem.ready)
86 { NpcSystem.draw(this, Player, wallDepth); }
87 this.drawGun();
88 }
89
90 faceLight (dir)
91 {
92 switch (dir)
93 {
94 case "east": return 0.9;
95 case "west": return 0.8;
96 case "south": return 0.7;
97 default: return 1.0;
98 }
99 }
100
101 drawColumns ()
102 {
103 const glc = this.glContext;
104 const w = this.real_size.x;
105 const h = this.real_size.y;
106 const cols = Math.max(1, Math.floor(Player.horizontalResolution));
107 const rad = Math.PI / (cols * 2);
108 const widthLine = w / cols;
109 const wallDepth = new Float32Array(Math.max(1, Math.round(w)));
110 for (var zi = 0; zi < wallDepth.length; zi++)
111 { wallDepth[zi] = 1e9; }
112
113 for (var i = 0; i < cols; i++)
114 {
115 var offsetP = -0.5 + (i / cols);
116 var offset = offsetP * cols;
117 var rotationalOffset = offset * rad;
118 var rot = Player.rotation + rotationalOffset;
119 if (rot < 0) { rot += Math.PI * 2; }
120 if (rot > Math.PI * 2) { rot -= Math.PI * 2; }
121
122 var rayHit = World.rayCast(Player.x, Player.y, rot);
123 if (!rayHit || !rayHit.tile)
124 { continue; }
125
126 var light = this.faceLight(rayHit.dir);
127 var deltaAngle = Player.rotation - rot;
128 var dist = rayHit.distance * Math.cos(deltaAngle);
129 if (dist < 0.0001)
130 { dist = 0.0001; }
131
132 var xLine = i * widthLine;
133 var wallH = (World.size * h) / dist;
134 var yCenter = (h / 2) + (-Player.pitch * (World.size * h - wallH) * 0.005);
135
136 var tint = [light, light, light];
137 var htmlImage = null;
138 var texW = 1;
139
140 // Z-buffer for NPC occlusion (per screen pixel column).
141 var x0 = Math.max(0, Math.floor(xLine));
142 var x1 = Math.min(wallDepth.length, Math.ceil(xLine + widthLine));
143 for (var xz = x0; xz < x1; xz++)
144 {
145 if (dist < wallDepth[xz])
146 { wallDepth[xz] = dist; }
147 }
148
149 if (rayHit.tile.Image && rayHit.tile.Image.g_image)
150 {
151 htmlImage = rayHit.tile.Image.g_image;
152 texW = htmlImage.width || 1;
153 var uv = textureColumnUv(rayHit.tileX, texW);
154 glc.drawSprite({
155 texture: glc.getTexture(htmlImage),
156 x: xLine + widthLine / 2,
157 y: yCenter,
158 w: widthLine,
159 h: wallH,
160 rotationDeg: 0,
161 alpha: 1,
162 uvRect: { u0: uv.u0, v0: uv.v0, u1: uv.u1, v1: uv.v1 },
163 shader: this.tintShader,
164 uniforms: { u_tint: tint }
165 });
166 }
167 else
168 {
169 var c = rayHit.tile.Color || { R: 255, G: 255, B: 255 };
170 tint = [(c.R / 255) * light, (c.G / 255) * light, (c.B / 255) * light];
171 glc.drawSprite({
172 texture: this.solidTexture,
173 x: xLine + widthLine / 2,
174 y: yCenter,
175 w: widthLine,
176 h: wallH,
177 rotationDeg: 0,
178 alpha: 1,
179 uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 },
180 shader: this.tintShader,
181 uniforms: { u_tint: tint }
182 });
183 }
184 }
185 return wallDepth;
186 }
187
188 drawGun ()
189 {
190 const images = this.game && this.game.images;
191 if (!images)
192 { return; }
193 var gunImg = null;
194 switch (Player.currentGun)
195 {
196 case 0: gunImg = images.shotgun; break;
197 case 1: gunImg = images.machinegun; break;
198 case 2: gunImg = images.pulserifle; break;
199 }
200 if (!gunImg || !gunImg.g_image)
201 { return; }
202
203 var p = this.real_size.y / 1080;
204 var gw = p * gunImg.g_image.width;
205 var gh = p * gunImg.g_image.height;
206 var gx = this.real_size.x * Player.gunX + gw / 2;
207 var gy = this.real_size.y * Player.gunY + gh / 2;
208
209 gunImg.draw(
210 this,
211 gx,
212 gy,
213 gw,
214 gh,
215 0,
216 false,
217 false,
218 1,
219 0
220 );
221 }
222}