BlueGit

timfa raytracer

Files

raytracer / src / raycastScene.js

raycastScene.js

raycastScene.js 4.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 }
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 (Player.horizontalResolution > this.real_size.x)
69 { Player.horizontalResolution = this.real_size.x; }
70 }
71
72 draw ()
73 {
74 if (this.destroyed || !this.glContext)
75 { return; }
76
77 this.g_fixSize();
78 const glc = this.glContext;
79 glc.endOffscreenPass();
80 glc.clear(0, 0, 0, 1);
81
82 this.drawColumns();
83 this.drawGun();
84 }
85
86 faceLight (dir)
87 {
88 switch (dir)
89 {
90 case "east": return 0.9;
91 case "west": return 0.8;
92 case "south": return 0.7;
93 default: return 1.0;
94 }
95 }
96
97 drawColumns ()
98 {
99 const glc = this.glContext;
100 const w = this.real_size.x;
101 const h = this.real_size.y;
102 const cols = Math.max(1, Math.floor(Player.horizontalResolution));
103 const rad = Math.PI / (cols * 2);
104 const widthLine = w / cols;
105
106 for (var i = 0; i < cols; i++)
107 {
108 var offsetP = -0.5 + (i / cols);
109 var offset = offsetP * cols;
110 var rotationalOffset = offset * rad;
111 var rot = Player.rotation + rotationalOffset;
112 if (rot < 0) { rot += Math.PI * 2; }
113 if (rot > Math.PI * 2) { rot -= Math.PI * 2; }
114
115 var rayHit = World.rayCast(Player.x, Player.y, rot);
116 if (!rayHit || !rayHit.tile)
117 { continue; }
118
119 var light = this.faceLight(rayHit.dir);
120 var deltaAngle = Player.rotation - rot;
121 var dist = rayHit.distance * Math.cos(deltaAngle);
122 if (dist < 0.0001)
123 { dist = 0.0001; }
124
125 var xLine = i * widthLine;
126 var wallH = (World.size * h) / dist;
127 var yCenter = (h / 2) + (-Player.pitch * (World.size * h - wallH) * 0.005);
128
129 var tint = [light, light, light];
130 var htmlImage = null;
131 var texW = 1;
132
133 if (rayHit.tile.Image && rayHit.tile.Image.g_image)
134 {
135 htmlImage = rayHit.tile.Image.g_image;
136 texW = htmlImage.width || 1;
137 var uv = textureColumnUv(rayHit.tileX, texW);
138 glc.drawSprite({
139 texture: glc.getTexture(htmlImage),
140 x: xLine + widthLine / 2,
141 y: yCenter,
142 w: widthLine,
143 h: wallH,
144 rotationDeg: 0,
145 alpha: 1,
146 uvRect: { u0: uv.u0, v0: uv.v0, u1: uv.u1, v1: uv.v1 },
147 shader: this.tintShader,
148 uniforms: { u_tint: tint }
149 });
150 }
151 else
152 {
153 var c = rayHit.tile.Color || { R: 255, G: 255, B: 255 };
154 tint = [(c.R / 255) * light, (c.G / 255) * light, (c.B / 255) * light];
155 glc.drawSprite({
156 texture: this.solidTexture,
157 x: xLine + widthLine / 2,
158 y: yCenter,
159 w: widthLine,
160 h: wallH,
161 rotationDeg: 0,
162 alpha: 1,
163 uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 },
164 shader: this.tintShader,
165 uniforms: { u_tint: tint }
166 });
167 }
168 }
169 }
170
171 drawGun ()
172 {
173 const images = this.game && this.game.images;
174 if (!images)
175 { return; }
176 var gunImg = null;
177 switch (Player.currentGun)
178 {
179 case 0: gunImg = images.shotgun; break;
180 case 1: gunImg = images.machinegun; break;
181 case 2: gunImg = images.pulserifle; break;
182 }
183 if (!gunImg || !gunImg.g_image)
184 { return; }
185
186 var p = this.real_size.y / 1080;
187 var gw = p * gunImg.g_image.width;
188 var gh = p * gunImg.g_image.height;
189 var gx = this.real_size.x * Player.gunX + gw / 2;
190 var gy = this.real_size.y * Player.gunY + gh / 2;
191
192 gunImg.draw(
193 this,
194 gx,
195 gy,
196 gw,
197 gh,
198 0,
199 false,
200 false,
201 1,
202 0
203 );
204 }
205}