@d5techs/3dgs-lib 1.4.52 → 1.4.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/3dgs-lib.cjs CHANGED
@@ -2098,122 +2098,211 @@ class SceneAidsRenderer {
2098
2098
  this.axesBindGroup = null;
2099
2099
  }
2100
2100
  }
2101
- const WGSL = (
2101
+ async function loadHdr(url) {
2102
+ const res = await fetch(url);
2103
+ if (!res.ok) throw new Error(`HdrLoader: fetch failed ${url} (${res.status})`);
2104
+ const buf = await res.arrayBuffer();
2105
+ return parseHdr(new Uint8Array(buf));
2106
+ }
2107
+ function parseHdr(bytes) {
2108
+ let pos = 0;
2109
+ const readLine = () => {
2110
+ let line = "";
2111
+ while (pos < bytes.length) {
2112
+ const c = bytes[pos++];
2113
+ if (c === 10) return line;
2114
+ line += String.fromCharCode(c);
2115
+ }
2116
+ return line;
2117
+ };
2118
+ const magic = readLine();
2119
+ if (!magic.startsWith("#?")) throw new Error("HdrLoader: not a Radiance HDR file");
2120
+ while (pos < bytes.length) {
2121
+ const line = readLine();
2122
+ if (line.length === 0) break;
2123
+ }
2124
+ const resLine = readLine();
2125
+ const m = resLine.match(/-Y\s+(\d+)\s+\+X\s+(\d+)/);
2126
+ if (!m) throw new Error("HdrLoader: unsupported resolution format: " + resLine);
2127
+ const height = parseInt(m[1], 10);
2128
+ const width = parseInt(m[2], 10);
2129
+ const rgbe = new Uint8Array(width * height * 4);
2130
+ for (let y = 0; y < height; y++) {
2131
+ const scanline = decodeScanline(bytes, pos, width);
2132
+ pos = scanline.newPos;
2133
+ rgbe.set(scanline.pixels, y * width * 4);
2134
+ }
2135
+ const data = new Float32Array(width * height * 4);
2136
+ for (let i = 0; i < width * height; i++) {
2137
+ const r = rgbe[i * 4];
2138
+ const g = rgbe[i * 4 + 1];
2139
+ const b = rgbe[i * 4 + 2];
2140
+ const e = rgbe[i * 4 + 3];
2141
+ if (e === 0) {
2142
+ data[i * 4] = 0;
2143
+ data[i * 4 + 1] = 0;
2144
+ data[i * 4 + 2] = 0;
2145
+ } else {
2146
+ const scale = Math.pow(2, e - 128 - 8);
2147
+ data[i * 4] = r * scale;
2148
+ data[i * 4 + 1] = g * scale;
2149
+ data[i * 4 + 2] = b * scale;
2150
+ }
2151
+ data[i * 4 + 3] = 1;
2152
+ }
2153
+ return { width, height, data };
2154
+ }
2155
+ function decodeScanline(bytes, pos, width) {
2156
+ const pixels = new Uint8Array(width * 4);
2157
+ if (width < 8 || width > 32767) {
2158
+ for (let i = 0; i < width * 4; i++) pixels[i] = bytes[pos++];
2159
+ return { pixels, newPos: pos };
2160
+ }
2161
+ const b0 = bytes[pos], b1 = bytes[pos + 1], b22 = bytes[pos + 2], b3 = bytes[pos + 3];
2162
+ if (b0 !== 2 || b1 !== 2 || (b22 & 128) !== 0) {
2163
+ for (let i = 0; i < width * 4; i++) pixels[i] = bytes[pos++];
2164
+ return { pixels, newPos: pos };
2165
+ }
2166
+ const lineWidth = b22 << 8 | b3;
2167
+ if (lineWidth !== width) throw new Error("HdrLoader: scanline width mismatch");
2168
+ pos += 4;
2169
+ for (let ch = 0; ch < 4; ch++) {
2170
+ let x = 0;
2171
+ while (x < width) {
2172
+ const code = bytes[pos++];
2173
+ if (code > 128) {
2174
+ const count = code - 128;
2175
+ const val = bytes[pos++];
2176
+ for (let i = 0; i < count; i++) pixels[(x + i) * 4 + ch] = val;
2177
+ x += count;
2178
+ } else {
2179
+ for (let i = 0; i < code; i++) pixels[(x + i) * 4 + ch] = bytes[pos++];
2180
+ x += code;
2181
+ }
2182
+ }
2183
+ }
2184
+ return { pixels, newPos: pos };
2185
+ }
2186
+ const f32Buf = new Float32Array(1);
2187
+ const u32Buf = new Uint32Array(f32Buf.buffer);
2188
+ function float32ToFloat16(val) {
2189
+ f32Buf[0] = val;
2190
+ const f = u32Buf[0];
2191
+ const sign = f >> 16 & 32768;
2192
+ const exp = (f >> 23 & 255) - 127 + 15;
2193
+ const frac = f >> 13 & 1023;
2194
+ if (exp <= 0) return sign;
2195
+ if (exp >= 31) return sign | 31744;
2196
+ return sign | exp << 10 | frac;
2197
+ }
2198
+ const CUBE_WGSL = (
2102
2199
  /* wgsl */
2103
2200
  `
2104
- struct Uniforms {
2105
- col0: vec4<f32>,
2106
- col1: vec4<f32>,
2107
- col2: vec4<f32>,
2108
- };
2109
-
2201
+ struct Uniforms { col0: vec4<f32>, col1: vec4<f32>, col2: vec4<f32> };
2110
2202
  @group(0) @binding(0) var<uniform> u: Uniforms;
2111
2203
  @group(0) @binding(1) var cubeSampler: sampler;
2112
2204
  @group(0) @binding(2) var cubeTexture: texture_cube<f32>;
2113
-
2114
- struct VSOut {
2115
- @builtin(position) position: vec4<f32>,
2116
- @location(0) dir: vec3<f32>,
2117
- };
2118
-
2119
- @vertex
2120
- fn vs(@builtin(vertex_index) vi: u32) -> VSOut {
2121
- let positions = array<vec2<f32>, 3>(
2122
- vec2<f32>(-1.0, -1.0),
2123
- vec2<f32>(3.0, -1.0),
2124
- vec2<f32>(-1.0, 3.0),
2125
- );
2126
- var out: VSOut;
2127
- let p = positions[vi];
2128
- out.position = vec4<f32>(p, 0.0, 1.0);
2129
-
2130
- let eyeDir = vec3<f32>(p.x * u.col0.w, p.y * u.col1.w, -1.0);
2131
-
2132
- out.dir = vec3<f32>(
2133
- dot(u.col0.xyz, eyeDir),
2134
- dot(u.col1.xyz, eyeDir),
2135
- dot(u.col2.xyz, eyeDir),
2136
- );
2137
- return out;
2205
+ struct V { @builtin(position) pos: vec4<f32>, @location(0) dir: vec3<f32> };
2206
+ @vertex fn vs(@builtin(vertex_index) vi: u32) -> V {
2207
+ let ps = array<vec2<f32>,3>(vec2(-1.,-1.),vec2(3.,-1.),vec2(-1.,3.));
2208
+ var o: V; let p = ps[vi]; o.pos = vec4(p, 0., 1.);
2209
+ let e = vec3(p.x*u.col0.w, p.y*u.col1.w, -1.);
2210
+ o.dir = vec3(dot(u.col0.xyz,e), dot(u.col1.xyz,e), dot(u.col2.xyz,e));
2211
+ return o;
2138
2212
  }
2139
-
2140
- @fragment
2141
- fn fs(input: VSOut) -> @location(0) vec4<f32> {
2142
- let color = textureSample(cubeTexture, cubeSampler, normalize(input.dir));
2143
- return vec4<f32>(color.rgb, 1.0);
2213
+ @fragment fn fs(i: V) -> @location(0) vec4<f32> {
2214
+ return vec4(textureSample(cubeTexture, cubeSampler, normalize(i.dir)).rgb, 1.);
2215
+ }`
2216
+ );
2217
+ const EQUIRECT_WGSL = (
2218
+ /* wgsl */
2219
+ `
2220
+ struct Uniforms { col0: vec4<f32>, col1: vec4<f32>, col2: vec4<f32> };
2221
+ @group(0) @binding(0) var<uniform> u: Uniforms;
2222
+ @group(0) @binding(1) var envSampler: sampler;
2223
+ @group(0) @binding(2) var envTexture: texture_2d<f32>;
2224
+ struct V { @builtin(position) pos: vec4<f32>, @location(0) dir: vec3<f32> };
2225
+ const PI: f32 = 3.14159265358979;
2226
+ @vertex fn vs(@builtin(vertex_index) vi: u32) -> V {
2227
+ let ps = array<vec2<f32>,3>(vec2(-1.,-1.),vec2(3.,-1.),vec2(-1.,3.));
2228
+ var o: V; let p = ps[vi]; o.pos = vec4(p, 0., 1.);
2229
+ let e = vec3(p.x*u.col0.w, p.y*u.col1.w, -1.);
2230
+ o.dir = vec3(dot(u.col0.xyz,e), dot(u.col1.xyz,e), dot(u.col2.xyz,e));
2231
+ return o;
2144
2232
  }
2145
- `
2233
+ @fragment fn fs(i: V) -> @location(0) vec4<f32> {
2234
+ let d = normalize(i.dir);
2235
+ let uv = vec2(atan2(d.z, d.x) / (2.*PI) + .5, asin(clamp(d.y,-1.,1.)) / PI + .5);
2236
+ let c = textureSample(envTexture, envSampler, vec2(uv.x, 1. - uv.y)).rgb;
2237
+ let mapped = c / (c + vec3(1.));
2238
+ let gamma = pow(mapped, vec3(1./2.2));
2239
+ return vec4(gamma, 1.);
2240
+ }`
2146
2241
  );
2147
2242
  class SkyboxRenderer {
2148
2243
  constructor(device, format, depthFormat) {
2149
2244
  __publicField(this, "device");
2150
- __publicField(this, "pipeline");
2151
2245
  __publicField(this, "uniformBuffer");
2152
2246
  __publicField(this, "sampler");
2153
- __publicField(this, "bindGroupLayout");
2154
2247
  __publicField(this, "uniformData", new Float32Array(12));
2155
- __publicField(this, "cubeTexture", null);
2156
- __publicField(this, "cubeBindGroup", null);
2248
+ __publicField(this, "cubePipeline");
2249
+ __publicField(this, "cubeBindGroupLayout");
2250
+ __publicField(this, "equirectPipeline");
2251
+ __publicField(this, "equirectBindGroupLayout");
2252
+ __publicField(this, "texture", null);
2253
+ __publicField(this, "bindGroup", null);
2157
2254
  __publicField(this, "frameReady", false);
2255
+ __publicField(this, "mode", "none");
2158
2256
  this.device = device;
2159
- const shaderModule = device.createShaderModule({ code: WGSL });
2160
- this.bindGroupLayout = device.createBindGroupLayout({
2257
+ const depthStencil = {
2258
+ format: depthFormat,
2259
+ depthWriteEnabled: false,
2260
+ depthCompare: "always"
2261
+ };
2262
+ this.cubeBindGroupLayout = device.createBindGroupLayout({
2161
2263
  entries: [
2162
- {
2163
- binding: 0,
2164
- visibility: GPUShaderStage.VERTEX,
2165
- buffer: { type: "uniform" }
2166
- },
2167
- {
2168
- binding: 1,
2169
- visibility: GPUShaderStage.FRAGMENT,
2170
- sampler: { type: "filtering" }
2171
- },
2172
- {
2173
- binding: 2,
2174
- visibility: GPUShaderStage.FRAGMENT,
2175
- texture: { sampleType: "float", viewDimension: "cube" }
2176
- }
2264
+ { binding: 0, visibility: GPUShaderStage.VERTEX, buffer: { type: "uniform" } },
2265
+ { binding: 1, visibility: GPUShaderStage.FRAGMENT, sampler: { type: "filtering" } },
2266
+ { binding: 2, visibility: GPUShaderStage.FRAGMENT, texture: { sampleType: "float", viewDimension: "cube" } }
2177
2267
  ]
2178
2268
  });
2179
- this.pipeline = device.createRenderPipeline({
2180
- layout: device.createPipelineLayout({
2181
- bindGroupLayouts: [this.bindGroupLayout]
2182
- }),
2183
- vertex: { module: shaderModule, entryPoint: "vs" },
2184
- fragment: {
2185
- module: shaderModule,
2186
- entryPoint: "fs",
2187
- targets: [{ format }]
2188
- },
2269
+ this.cubePipeline = device.createRenderPipeline({
2270
+ layout: device.createPipelineLayout({ bindGroupLayouts: [this.cubeBindGroupLayout] }),
2271
+ vertex: { module: device.createShaderModule({ code: CUBE_WGSL }), entryPoint: "vs" },
2272
+ fragment: { module: device.createShaderModule({ code: CUBE_WGSL }), entryPoint: "fs", targets: [{ format }] },
2189
2273
  primitive: { topology: "triangle-list", cullMode: "none" },
2190
- depthStencil: {
2191
- format: depthFormat,
2192
- depthWriteEnabled: false,
2193
- depthCompare: "always"
2194
- }
2274
+ depthStencil
2275
+ });
2276
+ this.equirectBindGroupLayout = device.createBindGroupLayout({
2277
+ entries: [
2278
+ { binding: 0, visibility: GPUShaderStage.VERTEX, buffer: { type: "uniform" } },
2279
+ { binding: 1, visibility: GPUShaderStage.FRAGMENT, sampler: { type: "filtering" } },
2280
+ { binding: 2, visibility: GPUShaderStage.FRAGMENT, texture: { sampleType: "float", viewDimension: "2d" } }
2281
+ ]
2282
+ });
2283
+ this.equirectPipeline = device.createRenderPipeline({
2284
+ layout: device.createPipelineLayout({ bindGroupLayouts: [this.equirectBindGroupLayout] }),
2285
+ vertex: { module: device.createShaderModule({ code: EQUIRECT_WGSL }), entryPoint: "vs" },
2286
+ fragment: { module: device.createShaderModule({ code: EQUIRECT_WGSL }), entryPoint: "fs", targets: [{ format }] },
2287
+ primitive: { topology: "triangle-list", cullMode: "none" },
2288
+ depthStencil
2195
2289
  });
2196
2290
  this.uniformBuffer = device.createBuffer({
2197
2291
  size: 48,
2198
2292
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
2199
2293
  });
2200
- this.sampler = device.createSampler({
2201
- magFilter: "linear",
2202
- minFilter: "linear"
2203
- });
2294
+ this.sampler = device.createSampler({ magFilter: "linear", minFilter: "linear" });
2204
2295
  }
2205
2296
  get isActive() {
2206
- return this.cubeTexture !== null && this.cubeBindGroup !== null;
2297
+ return this.mode !== "none" && this.texture !== null && this.bindGroup !== null;
2207
2298
  }
2299
+ // ---- cubemap ----
2208
2300
  async loadCubemap(faceUrls) {
2209
2301
  this.clear();
2210
2302
  const bitmaps = await Promise.all(
2211
2303
  faceUrls.map(async (url) => {
2212
2304
  const res = await fetch(url);
2213
- if (!res.ok)
2214
- throw new Error(
2215
- `SkyboxRenderer: failed to fetch ${url} (${res.status})`
2216
- );
2305
+ if (!res.ok) throw new Error(`SkyboxRenderer: failed to fetch ${url} (${res.status})`);
2217
2306
  return createImageBitmap(await res.blob());
2218
2307
  })
2219
2308
  );
@@ -2222,9 +2311,7 @@ class SkyboxRenderer {
2222
2311
  for (let i = 1; i < 6; i++) {
2223
2312
  if (bitmaps[i].width !== width || bitmaps[i].height !== height) {
2224
2313
  bitmaps.forEach((b) => b.close());
2225
- throw new Error(
2226
- "SkyboxRenderer: all cubemap faces must share the same dimensions"
2227
- );
2314
+ throw new Error("SkyboxRenderer: all cubemap faces must share the same dimensions");
2228
2315
  }
2229
2316
  }
2230
2317
  const tex = this.device.createTexture({
@@ -2241,16 +2328,49 @@ class SkyboxRenderer {
2241
2328
  );
2242
2329
  }
2243
2330
  bitmaps.forEach((b) => b.close());
2244
- this.cubeBindGroup = this.device.createBindGroup({
2245
- layout: this.bindGroupLayout,
2331
+ this.bindGroup = this.device.createBindGroup({
2332
+ layout: this.cubeBindGroupLayout,
2246
2333
  entries: [
2247
2334
  { binding: 0, resource: { buffer: this.uniformBuffer } },
2248
2335
  { binding: 1, resource: this.sampler },
2249
2336
  { binding: 2, resource: tex.createView({ dimension: "cube" }) }
2250
2337
  ]
2251
2338
  });
2252
- this.cubeTexture = tex;
2339
+ this.texture = tex;
2340
+ this.mode = "cubemap";
2253
2341
  }
2342
+ // ---- equirectangular HDR ----
2343
+ async loadEquirectangular(url) {
2344
+ this.clear();
2345
+ const hdr = await loadHdr(url);
2346
+ const pixelCount = hdr.width * hdr.height * 4;
2347
+ const f16 = new Uint16Array(pixelCount);
2348
+ for (let i = 0; i < pixelCount; i++) {
2349
+ f16[i] = float32ToFloat16(hdr.data[i]);
2350
+ }
2351
+ const tex = this.device.createTexture({
2352
+ size: [hdr.width, hdr.height],
2353
+ format: "rgba16float",
2354
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
2355
+ });
2356
+ this.device.queue.writeTexture(
2357
+ { texture: tex },
2358
+ f16.buffer,
2359
+ { bytesPerRow: hdr.width * 8, rowsPerImage: hdr.height },
2360
+ { width: hdr.width, height: hdr.height }
2361
+ );
2362
+ this.bindGroup = this.device.createBindGroup({
2363
+ layout: this.equirectBindGroupLayout,
2364
+ entries: [
2365
+ { binding: 0, resource: { buffer: this.uniformBuffer } },
2366
+ { binding: 1, resource: this.sampler },
2367
+ { binding: 2, resource: tex.createView() }
2368
+ ]
2369
+ });
2370
+ this.texture = tex;
2371
+ this.mode = "equirect";
2372
+ }
2373
+ // ---- common ----
2254
2374
  prepareFrame(viewMatrix, projectionMatrix) {
2255
2375
  if (!this.isActive) {
2256
2376
  this.frameReady = false;
@@ -2273,18 +2393,20 @@ class SkyboxRenderer {
2273
2393
  this.frameReady = true;
2274
2394
  }
2275
2395
  draw(pass) {
2276
- if (!this.frameReady || !this.cubeBindGroup) return;
2277
- pass.setPipeline(this.pipeline);
2278
- pass.setBindGroup(0, this.cubeBindGroup);
2396
+ if (!this.frameReady || !this.bindGroup) return;
2397
+ const pipeline = this.mode === "cubemap" ? this.cubePipeline : this.equirectPipeline;
2398
+ pass.setPipeline(pipeline);
2399
+ pass.setBindGroup(0, this.bindGroup);
2279
2400
  pass.draw(3);
2280
2401
  }
2281
2402
  clear() {
2282
- if (this.cubeTexture) {
2283
- this.cubeTexture.destroy();
2284
- this.cubeTexture = null;
2403
+ if (this.texture) {
2404
+ this.texture.destroy();
2405
+ this.texture = null;
2285
2406
  }
2286
- this.cubeBindGroup = null;
2407
+ this.bindGroup = null;
2287
2408
  this.frameReady = false;
2409
+ this.mode = "none";
2288
2410
  }
2289
2411
  destroy() {
2290
2412
  this.clear();
@@ -19090,6 +19212,16 @@ class App {
19090
19212
  faceUrls.negZ
19091
19213
  ]);
19092
19214
  }
19215
+ async setHdrBackground(url) {
19216
+ if (!this.skyboxRenderer) {
19217
+ this.skyboxRenderer = new SkyboxRenderer(
19218
+ this.renderer.device,
19219
+ this.renderer.format,
19220
+ "depth24plus"
19221
+ );
19222
+ }
19223
+ await this.skyboxRenderer.loadEquirectangular(url);
19224
+ }
19093
19225
  clearSkybox() {
19094
19226
  var _a2;
19095
19227
  (_a2 = this.skyboxRenderer) == null ? void 0 : _a2.clear();
@@ -19581,6 +19713,7 @@ exports.exportEditedPLY = exportEditedPLY;
19581
19713
  exports.getRecommendedDPR = getRecommendedDPR;
19582
19714
  exports.isMobileDevice = isMobileDevice;
19583
19715
  exports.isWebGPUSupported = isWebGPUSupported;
19716
+ exports.loadHdr = loadHdr;
19584
19717
  exports.loadPLY = loadPLY;
19585
19718
  exports.loadPLYMobile = loadPLYMobile;
19586
19719
  exports.loadSOG = loadSOG;