| 1 | /** |
| 2 | * Browser/runtime atlas set: loads PNG diffuse/normal/specular + u16 depth, |
| 3 | * extracts cells, composes body+gear on CPU (cell is 32×48 — cheap). |
| 4 | */ |
| 5 | var PpdaAtlasRuntime = |
| 6 | { |
| 7 | _canvas: null, |
| 8 | _ctx: null, |
| 9 | |
| 10 | _ensureCanvas: function () |
| 11 | { |
| 12 | if (typeof document === "undefined") |
| 13 | { return null; } |
| 14 | if (!this._canvas) |
| 15 | { |
| 16 | this._canvas = document.createElement("canvas"); |
| 17 | this._ctx = this._canvas.getContext("2d", { willReadFrequently: true }); |
| 18 | } |
| 19 | return this._ctx; |
| 20 | }, |
| 21 | |
| 22 | loadImage: function (url) |
| 23 | { |
| 24 | return new Promise(function (resolve, reject) |
| 25 | { |
| 26 | var img = new Image(); |
| 27 | img.onload = function () |
| 28 | { resolve(img); }; |
| 29 | img.onerror = function () |
| 30 | { reject(new Error("image load failed: " + url)); }; |
| 31 | img.src = url; |
| 32 | }); |
| 33 | }, |
| 34 | |
| 35 | loadU16Depth: function (url, expectW, expectH) |
| 36 | { |
| 37 | return fetch(url).then(function (r) |
| 38 | { |
| 39 | if (!r.ok) |
| 40 | { throw new Error("depth fetch " + r.status + " " + url); } |
| 41 | return r.arrayBuffer(); |
| 42 | }).then(function (buf) |
| 43 | { |
| 44 | var need = expectW * expectH * 4; |
| 45 | if (buf.byteLength < need) |
| 46 | { throw new Error("depth u16 te kort: " + buf.byteLength + " < " + need); } |
| 47 | return new Uint16Array(buf.slice(0, need)); |
| 48 | }); |
| 49 | }, |
| 50 | |
| 51 | /** |
| 52 | * @returns {Promise<{ppda, images, depthU16, baseUrl}>} |
| 53 | */ |
| 54 | loadSet: function (baseUrl, ppdaFile) |
| 55 | { |
| 56 | var self = this; |
| 57 | baseUrl = baseUrl.replace(/\/?$/, "/"); |
| 58 | return fetch(baseUrl + ppdaFile).then(function (r) |
| 59 | { |
| 60 | if (!r.ok) |
| 61 | { throw new Error("ppda fetch " + r.status); } |
| 62 | return r.text(); |
| 63 | }).then(function (text) |
| 64 | { |
| 65 | var ppda = PpdaLoader.parse(text); |
| 66 | var maps = ppda.maps; |
| 67 | var jobs = [ |
| 68 | self.loadImage(baseUrl + maps.diffuse.file), |
| 69 | maps.normal ? self.loadImage(baseUrl + maps.normal.file) : Promise.resolve(null), |
| 70 | maps.specular ? self.loadImage(baseUrl + maps.specular.file) : Promise.resolve(null) |
| 71 | ]; |
| 72 | var depthFile = maps.depth.file; |
| 73 | var dw = maps.depth.width || maps.diffuse.width; |
| 74 | var dh = maps.depth.height || maps.diffuse.height; |
| 75 | if (maps.depth.storage === "u16le_ga" || /\.u16$/i.test(depthFile)) |
| 76 | { jobs.push(self.loadU16Depth(baseUrl + depthFile, dw, dh)); } |
| 77 | else |
| 78 | { throw new Error("browser-loader verwacht depth als .u16 (storage=u16le_ga); got " + depthFile); } |
| 79 | |
| 80 | return Promise.all(jobs).then(function (parts) |
| 81 | { |
| 82 | return { |
| 83 | ppda: ppda, |
| 84 | baseUrl: baseUrl, |
| 85 | diffuseImg: parts[0], |
| 86 | normalImg: parts[1], |
| 87 | specularImg: parts[2], |
| 88 | depthU16: parts[3], |
| 89 | depthW: dw, |
| 90 | depthH: dh |
| 91 | }; |
| 92 | }); |
| 93 | }); |
| 94 | }, |
| 95 | |
| 96 | extractRgbaCell: function (img, view) |
| 97 | { |
| 98 | var ctx = this._ensureCanvas(); |
| 99 | if (!ctx) |
| 100 | { throw new Error("geen canvas voor cell extract"); } |
| 101 | this._canvas.width = view.w; |
| 102 | this._canvas.height = view.h; |
| 103 | ctx.clearRect(0, 0, view.w, view.h); |
| 104 | ctx.drawImage(img, view.x, view.y, view.w, view.h, 0, 0, view.w, view.h); |
| 105 | return ctx.getImageData(0, 0, view.w, view.h).data; |
| 106 | }, |
| 107 | |
| 108 | extractDepthCell: function (depthU16, atlasW, view) |
| 109 | { |
| 110 | var n = view.w * view.h; |
| 111 | var gray = new Uint16Array(n); |
| 112 | var alpha = new Uint16Array(n); |
| 113 | for (var y = 0; y < view.h; y++) |
| 114 | { |
| 115 | for (var x = 0; x < view.w; x++) |
| 116 | { |
| 117 | var si = ((view.y + y) * atlasW + (view.x + x)) * 2; |
| 118 | var di = y * view.w + x; |
| 119 | gray[di] = depthU16[si]; |
| 120 | alpha[di] = depthU16[si + 1]; |
| 121 | } |
| 122 | } |
| 123 | return { depthGray: gray, depthAlpha: alpha }; |
| 124 | }, |
| 125 | |
| 126 | layerFromSet: function (set, clipToken, frameIndex, pitch, yaw) |
| 127 | { |
| 128 | var clip = PpdaViewFrame.findClip(set.ppda, clipToken); |
| 129 | if (!clip) |
| 130 | { throw new Error("clip niet gevonden: " + clipToken); } |
| 131 | var frame = clip.frames[frameIndex]; |
| 132 | if (!frame) |
| 133 | { throw new Error("frame " + frameIndex + " ontbreekt"); } |
| 134 | var view = PpdaViewFrame.findView(frame, pitch, yaw); |
| 135 | if (!view) |
| 136 | { throw new Error("view p" + pitch + " y" + yaw + " ontbreekt"); } |
| 137 | if (view.empty) |
| 138 | { |
| 139 | var emptyN = view.w * view.h; |
| 140 | return { |
| 141 | width: view.w, |
| 142 | height: view.h, |
| 143 | diffuse: new Uint8ClampedArray(emptyN * 4), |
| 144 | normal: new Uint8ClampedArray(emptyN * 4), |
| 145 | specular: new Uint8ClampedArray(emptyN * 4), |
| 146 | depthGray: new Uint16Array(emptyN), |
| 147 | depthAlpha: new Uint16Array(emptyN), |
| 148 | view: view |
| 149 | }; |
| 150 | } |
| 151 | var diffuse = this.extractRgbaCell(set.diffuseImg, view); |
| 152 | var normal = set.normalImg ? this.extractRgbaCell(set.normalImg, view) : null; |
| 153 | var specular = set.specularImg ? this.extractRgbaCell(set.specularImg, view) : null; |
| 154 | var depth = this.extractDepthCell(set.depthU16, set.depthW, view); |
| 155 | return { |
| 156 | width: view.w, |
| 157 | height: view.h, |
| 158 | diffuse: diffuse, |
| 159 | normal: normal, |
| 160 | specular: specular, |
| 161 | depthGray: depth.depthGray, |
| 162 | depthAlpha: depth.depthAlpha, |
| 163 | view: view |
| 164 | }; |
| 165 | }, |
| 166 | |
| 167 | composeView: function (bodySet, gearSet, clipToken, frameIndex, pitch, yaw) |
| 168 | { |
| 169 | var body = this.layerFromSet(bodySet, clipToken, frameIndex, pitch, yaw); |
| 170 | var layers = [body]; |
| 171 | if (gearSet) |
| 172 | { |
| 173 | PpdaLoader.assertCompatible(bodySet.ppda, gearSet.ppda); |
| 174 | layers.push(this.layerFromSet(gearSet, clipToken, frameIndex, pitch, yaw)); |
| 175 | } |
| 176 | var composed = PpdaCompositor.compose(layers); |
| 177 | composed.view = body.view; |
| 178 | composed.lit = PpdaCompositor.relightDiffuse(composed, 1); |
| 179 | return composed; |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | if (typeof module !== "undefined" && module.exports) |
| 184 | { module.exports = PpdaAtlasRuntime; } |