| 1 | /** |
| 2 | * Fullscreen WebGL raycast view on frameworkv3. |
| 3 | * Wall strips: one source texel column per screen column, NEAREST, tinted for shading. |
| 4 | */ |
| 5 | class 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 | // Same horizon/pitch as FloorCast + NPC feet. |
| 187 | var yCenter = (h / 2) + (-Player.pitch * h * 0.25); |
| 188 | |
| 189 | var tint = [light, light, light]; |
| 190 | var htmlImage = null; |
| 191 | var texW = 1; |
| 192 | |
| 193 | // Z-buffer for NPC occlusion (per screen pixel column). |
| 194 | var x0 = Math.max(0, Math.floor(xLine)); |
| 195 | var x1 = Math.min(wallDepth.length, Math.ceil(xLine + widthLine)); |
| 196 | for (var xz = x0; xz < x1; xz++) |
| 197 | { |
| 198 | if (dist < wallDepth[xz]) |
| 199 | { wallDepth[xz] = dist; } |
| 200 | } |
| 201 | |
| 202 | if (rayHit.tile.Image && rayHit.tile.Image.g_image) |
| 203 | { |
| 204 | htmlImage = rayHit.tile.Image.g_image; |
| 205 | texW = htmlImage.width || 1; |
| 206 | var uv = textureColumnUv(rayHit.tileX, texW); |
| 207 | glc.drawSprite({ |
| 208 | texture: glc.getTexture(htmlImage), |
| 209 | x: xLine + widthLine / 2, |
| 210 | y: yCenter, |
| 211 | w: widthLine, |
| 212 | h: wallH, |
| 213 | rotationDeg: 0, |
| 214 | alpha: 1, |
| 215 | uvRect: { u0: uv.u0, v0: uv.v0, u1: uv.u1, v1: uv.v1 }, |
| 216 | shader: this.tintShader, |
| 217 | uniforms: { u_tint: tint } |
| 218 | }); |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | var c = rayHit.tile.Color || { R: 255, G: 255, B: 255 }; |
| 223 | tint = [(c.R / 255) * light, (c.G / 255) * light, (c.B / 255) * light]; |
| 224 | glc.drawSprite({ |
| 225 | texture: this.solidTexture, |
| 226 | x: xLine + widthLine / 2, |
| 227 | y: yCenter, |
| 228 | w: widthLine, |
| 229 | h: wallH, |
| 230 | rotationDeg: 0, |
| 231 | alpha: 1, |
| 232 | uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 }, |
| 233 | shader: this.tintShader, |
| 234 | uniforms: { u_tint: tint } |
| 235 | }); |
| 236 | } |
| 237 | } |
| 238 | return wallDepth; |
| 239 | } |
| 240 | |
| 241 | drawGun () |
| 242 | { |
| 243 | const images = this.game && this.game.images; |
| 244 | if (!images) |
| 245 | { return; } |
| 246 | var gunImg = null; |
| 247 | switch (Player.currentGun) |
| 248 | { |
| 249 | case 0: gunImg = images.shotgun; break; |
| 250 | case 1: gunImg = images.machinegun; break; |
| 251 | case 2: gunImg = images.pulserifle; break; |
| 252 | } |
| 253 | if (!gunImg || !gunImg.g_image) |
| 254 | { return; } |
| 255 | |
| 256 | var p = this.real_size.y / 1080; |
| 257 | var gw = p * gunImg.g_image.width; |
| 258 | var gh = p * gunImg.g_image.height; |
| 259 | var gx = this.real_size.x * Player.gunX + gw / 2; |
| 260 | var gy = this.real_size.y * Player.gunY + gh / 2; |
| 261 | |
| 262 | gunImg.draw( |
| 263 | this, |
| 264 | gx, |
| 265 | gy, |
| 266 | gw, |
| 267 | gh, |
| 268 | 0, |
| 269 | false, |
| 270 | false, |
| 271 | 1, |
| 272 | 0 |
| 273 | ); |
| 274 | } |
| 275 | } |