| 1 | /** |
| 2 | * Paperdoll NPC billboards. |
| 3 | * Compose body+gear on CPU when view/frame changes (cell ~32×48). |
| 4 | * Project with per-pixel depth into an offscreen RGBA buffer + z-buffer, |
| 5 | * then one WebGL textured quad — avoids thousands of drawSprite calls. |
| 6 | */ |
| 7 | var NpcSystem = |
| 8 | { |
| 9 | npcs: [], |
| 10 | bodySet: null, |
| 11 | gearSet: null, |
| 12 | ready: false, |
| 13 | cache: {}, |
| 14 | worldScale: 32, |
| 15 | _overlay: null, |
| 16 | _overlayCtx: null, |
| 17 | _overlayImg: null, |
| 18 | _zbuf: null, |
| 19 | _zbufW: 0, |
| 20 | _zbufH: 0, |
| 21 | |
| 22 | initEmpty: function () |
| 23 | { |
| 24 | this.npcs = []; |
| 25 | this.cache = {}; |
| 26 | this.ready = false; |
| 27 | }, |
| 28 | |
| 29 | addNpc: function (opts) |
| 30 | { |
| 31 | this.npcs.push({ |
| 32 | x: opts.x, |
| 33 | y: opts.y, |
| 34 | facingDeg: opts.facingDeg || 0, |
| 35 | clip: opts.clip || "walk", |
| 36 | animating: opts.animating !== false, |
| 37 | time: opts.time || 0, |
| 38 | label: opts.label || "npc" |
| 39 | }); |
| 40 | }, |
| 41 | |
| 42 | load: function (bodyBase, bodyPpda, gearBase, gearPpda) |
| 43 | { |
| 44 | var self = this; |
| 45 | return PpdaAtlasRuntime.loadSet(bodyBase, bodyPpda).then(function (body) |
| 46 | { |
| 47 | self.bodySet = body; |
| 48 | if (!gearBase) |
| 49 | { |
| 50 | self.ready = true; |
| 51 | return body; |
| 52 | } |
| 53 | return PpdaAtlasRuntime.loadSet(gearBase, gearPpda).then(function (gear) |
| 54 | { |
| 55 | PpdaLoader.assertCompatible(body.ppda, gear.ppda); |
| 56 | self.gearSet = gear; |
| 57 | self.ready = true; |
| 58 | return body; |
| 59 | }); |
| 60 | }); |
| 61 | }, |
| 62 | |
| 63 | update: function (dt) |
| 64 | { |
| 65 | for (var i = 0; i < this.npcs.length; i++) |
| 66 | { |
| 67 | if (this.npcs[i].animating) |
| 68 | { this.npcs[i].time += dt; } |
| 69 | } |
| 70 | }, |
| 71 | |
| 72 | composedFor: function (npc, pitch, yaw) |
| 73 | { |
| 74 | if (!this.bodySet) |
| 75 | { return null; } |
| 76 | var clip = PpdaViewFrame.findClip(this.bodySet.ppda, npc.clip); |
| 77 | var frameCount = clip ? clip.frame_count : 1; |
| 78 | var frame = PpdaViewFrame.frameIndex(npc.time, this.bodySet.ppda.fps, frameCount); |
| 79 | var key = npc.clip + "|" + frame + "|" + pitch + "|" + yaw; |
| 80 | if (this.cache[key]) |
| 81 | { return this.cache[key]; } |
| 82 | var composed = PpdaAtlasRuntime.composeView( |
| 83 | this.bodySet, |
| 84 | this.gearSet, |
| 85 | npc.clip, |
| 86 | frame, |
| 87 | pitch, |
| 88 | yaw |
| 89 | ); |
| 90 | this.cache[key] = composed; |
| 91 | var keys = Object.keys(this.cache); |
| 92 | if (keys.length > 96) |
| 93 | { delete this.cache[keys[0]]; } |
| 94 | return composed; |
| 95 | }, |
| 96 | |
| 97 | _ensureOverlay: function (w, h) |
| 98 | { |
| 99 | if (!this._overlay || this._overlay.width !== w || this._overlay.height !== h) |
| 100 | { |
| 101 | this._overlay = document.createElement("canvas"); |
| 102 | this._overlay.width = w; |
| 103 | this._overlay.height = h; |
| 104 | this._overlayCtx = this._overlay.getContext("2d"); |
| 105 | this._overlayImg = null; |
| 106 | this._overlayTex = null; |
| 107 | this._zbuf = new Float32Array(w * h); |
| 108 | this._zbufW = w; |
| 109 | this._zbufH = h; |
| 110 | } |
| 111 | }, |
| 112 | |
| 113 | /** |
| 114 | * @param {Float32Array} wallDepthPerCol length = screen width |
| 115 | */ |
| 116 | draw: function (scene, player, wallDepthPerCol) |
| 117 | { |
| 118 | if (!this.ready || !this.bodySet) |
| 119 | { return; } |
| 120 | |
| 121 | var w = Math.round(scene.real_size.x); |
| 122 | var h = Math.round(scene.real_size.y); |
| 123 | this._ensureOverlay(w, h); |
| 124 | var ctx = this._overlayCtx; |
| 125 | ctx.clearRect(0, 0, w, h); |
| 126 | var img = ctx.createImageData(w, h); |
| 127 | var data = img.data; |
| 128 | var zbuf = this._zbuf; |
| 129 | var i; |
| 130 | for (i = 0; i < w * h; i++) |
| 131 | { |
| 132 | var col = i % w; |
| 133 | zbuf[i] = wallDepthPerCol[col]; |
| 134 | } |
| 135 | |
| 136 | var ppda = this.bodySet.ppda; |
| 137 | var heightM = (ppda.height_milli || 2600) / 1000; |
| 138 | var camDist = ppda.camera_distance || 10; |
| 139 | var near = ppda.depth_near; |
| 140 | var far = ppda.depth_far; |
| 141 | var metersToWorld = this.worldScale; |
| 142 | var tint = scene.npcLight != null ? scene.npcLight : 1; |
| 143 | |
| 144 | var list = this.npcs.slice().sort(function (a, b) |
| 145 | { |
| 146 | var da = (a.x - player.x) * (a.x - player.x) + (a.y - player.y) * (a.y - player.y); |
| 147 | var db = (b.x - player.x) * (b.x - player.x) + (b.y - player.y) * (b.y - player.y); |
| 148 | return db - da; |
| 149 | }); |
| 150 | |
| 151 | for (var ni = 0; ni < list.length; ni++) |
| 152 | { |
| 153 | var npc = list[ni]; |
| 154 | var dx = npc.x - player.x; |
| 155 | var dy = npc.y - player.y; |
| 156 | var dist = Math.sqrt(dx * dx + dy * dy); |
| 157 | if (dist < 0.5 || dist > 800) |
| 158 | { continue; } |
| 159 | |
| 160 | var angleTo = Math.atan2(dy, dx); |
| 161 | var delta = angleTo - player.rotation; |
| 162 | while (delta > Math.PI) { delta -= Math.PI * 2; } |
| 163 | while (delta < -Math.PI) { delta += Math.PI * 2; } |
| 164 | if (Math.abs(delta) > Math.PI * 0.55) |
| 165 | { continue; } |
| 166 | |
| 167 | var relYaw = PpdaViewFrame.relativeYawDeg(player.x, player.y, npc.x, npc.y, npc.facingDeg); |
| 168 | var yaw = PpdaViewFrame.pickYaw(relYaw, ppda.yaws); |
| 169 | var pitch = Math.abs(player.pitch) < 0.25 |
| 170 | ? PpdaViewFrame.nearestInList(0, ppda.pitches) |
| 171 | : PpdaViewFrame.pickPitch(player.pitch, ppda.pitches); |
| 172 | |
| 173 | var clipInfo = PpdaViewFrame.findClip(ppda, npc.clip); |
| 174 | var frame = PpdaViewFrame.frameIndex( |
| 175 | npc.time, ppda.fps, clipInfo ? clipInfo.frame_count : 1 |
| 176 | ); |
| 177 | var composed = this.composedFor(npc, pitch, yaw); |
| 178 | if (!composed) |
| 179 | { continue; } |
| 180 | |
| 181 | var spriteH = (World.size * heightM) * (h / dist) * 0.5; |
| 182 | var aspect = composed.width / Math.max(1, composed.height); |
| 183 | var spriteW = spriteH * aspect; |
| 184 | var screenX = (w / 2) + (delta / (Math.PI / 3)) * (w / 2); |
| 185 | // Horizon + pitch (same mid as FloorCast). Visual feet (= last opaque |
| 186 | // atlas row) sit on the floor plane at this distance. |
| 187 | var mid = h / 2 + (-player.pitch * h * 0.25); |
| 188 | var floorY = mid + (World.size * 0.5 * h) / dist; |
| 189 | var left = screenX - spriteW / 2; |
| 190 | // Feet from BODY atlas only (gear can hang below and would lift the sprite). |
| 191 | var feetRow = composed.height - 1; |
| 192 | var feetH = composed.height; |
| 193 | var feetW = composed.width; |
| 194 | var feetAlpha = composed.lit; |
| 195 | try |
| 196 | { |
| 197 | var bodyLayer = PpdaAtlasRuntime.layerFromSet( |
| 198 | this.bodySet, npc.clip, frame, pitch, yaw |
| 199 | ); |
| 200 | feetAlpha = bodyLayer.diffuse; |
| 201 | feetW = bodyLayer.width; |
| 202 | feetH = bodyLayer.height; |
| 203 | feetRow = feetH - 1; |
| 204 | } |
| 205 | catch (e) { /* fallback: composed */ } |
| 206 | for (var fy = feetH - 1; fy >= 0; fy--) |
| 207 | { |
| 208 | var rowHit = false; |
| 209 | for (var fx = 0; fx < feetW; fx++) |
| 210 | { |
| 211 | if (feetAlpha[(fy * feetW + fx) * 4 + 3] > 10) |
| 212 | { rowHit = true; break; } |
| 213 | } |
| 214 | if (rowHit) |
| 215 | { feetRow = fy; break; } |
| 216 | } |
| 217 | var top = floorY - ((feetRow + 0.5) / feetH) * spriteH; |
| 218 | var cols = Math.max(1, Math.ceil(spriteW)); |
| 219 | |
| 220 | for (var c = 0; c < cols; c++) |
| 221 | { |
| 222 | var sx = Math.floor(left + c); |
| 223 | if (sx < 0 || sx >= w) |
| 224 | { continue; } |
| 225 | var u = (c + 0.5) / cols; |
| 226 | var texX = Math.min(composed.width - 1, Math.max(0, Math.floor(u * composed.width))); |
| 227 | var y0 = Math.floor(top); |
| 228 | var y1 = Math.ceil(top + spriteH); |
| 229 | |
| 230 | for (var sy = y0; sy < y1; sy++) |
| 231 | { |
| 232 | if (sy < 0 || sy >= h) |
| 233 | { continue; } |
| 234 | var v = (sy - top + 0.5) / spriteH; |
| 235 | if (v < 0 || v >= 1) |
| 236 | { continue; } |
| 237 | var texY = Math.min(composed.height - 1, Math.max(0, Math.floor(v * composed.height))); |
| 238 | var pi = texY * composed.width + texX; |
| 239 | var a = composed.lit[pi * 4 + 3]; |
| 240 | if (!a || composed.depthAlpha[pi] === 0) |
| 241 | { continue; } |
| 242 | |
| 243 | var depthM = near + (composed.depthGray[pi] / 65535) * (far - near); |
| 244 | var pixelDist = dist + (depthM - camDist) * metersToWorld; |
| 245 | if (pixelDist < 0.05) |
| 246 | { pixelDist = 0.05; } |
| 247 | |
| 248 | var zi = sy * w + sx; |
| 249 | if (pixelDist >= zbuf[zi]) |
| 250 | { continue; } |
| 251 | zbuf[zi] = pixelDist; |
| 252 | |
| 253 | var o = zi * 4; |
| 254 | data[o] = Math.min(255, Math.round(composed.lit[pi * 4] * tint)); |
| 255 | data[o + 1] = Math.min(255, Math.round(composed.lit[pi * 4 + 1] * tint)); |
| 256 | data[o + 2] = Math.min(255, Math.round(composed.lit[pi * 4 + 2] * tint)); |
| 257 | data[o + 3] = a; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | ctx.putImageData(img, 0, 0); |
| 263 | var glc = scene.glContext; |
| 264 | // Re-upload every frame (content changes). |
| 265 | if (this._overlayTex) |
| 266 | { |
| 267 | var gl = glc.gl; |
| 268 | gl.bindTexture(gl.TEXTURE_2D, this._overlayTex); |
| 269 | gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this._overlay); |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | var prevMin = glc.textureMinFilter; |
| 274 | var prevMag = glc.textureMagFilter; |
| 275 | glc.setDefaultTextureFilter(glc.gl.NEAREST, glc.gl.NEAREST); |
| 276 | this._overlayTex = glc.getTexture(this._overlay); |
| 277 | glc.textureMinFilter = prevMin; |
| 278 | glc.textureMagFilter = prevMag; |
| 279 | } |
| 280 | |
| 281 | glc.drawSprite({ |
| 282 | texture: this._overlayTex, |
| 283 | x: w / 2, |
| 284 | y: h / 2, |
| 285 | w: w, |
| 286 | h: h, |
| 287 | rotationDeg: 0, |
| 288 | alpha: 1, |
| 289 | uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 } |
| 290 | }); |
| 291 | } |
| 292 | }; |