@viggle/splat-engine-three 0.3.0 → 0.5.0

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.
@@ -5867,6 +5867,7 @@ var _screenSizeCull = 0;
5867
5867
  var _minOpacity = 0.05;
5868
5868
  var _pruneRatio = 0;
5869
5869
  var _pruneVoxelSize = 0.04;
5870
+ var _depthWriteAlpha = 0;
5870
5871
  var clamp = (v, lo, hi) => v < lo ? lo : v > hi ? hi : v;
5871
5872
  var warnedAboutCap = false;
5872
5873
  var SplatQuality = {
@@ -6018,6 +6019,54 @@ var SplatQuality = {
6018
6019
  * application wants - a character deliberately drawn over the scene.
6019
6020
  */
6020
6021
  depthOcclusion: true,
6022
+ /**
6023
+ * Alpha at which a splat writes depth, so that a splat object drawn later
6024
+ * is hidden behind the solid parts of one drawn earlier. `0` (the default)
6025
+ * writes no depth: the behaviour before this setting existed.
6026
+ *
6027
+ * Every splat object is sorted and drawn on its own, so where two of them
6028
+ * overlap - a weapon in a hand, two fighters in a clinch, a character
6029
+ * behind a prop - whichever draws second paints over the other across the
6030
+ * whole overlap, whatever is really in front. The PlayCanvas build sorts
6031
+ * every splat on a layer together and does not have the problem. With this
6032
+ * on, each splat object follows its colour draw with a depth-only draw of
6033
+ * its solid parts: the fragments of single splats whose alpha (the
6034
+ * gaussian's falloff times the splat's opacity) reaches this value. An
6035
+ * object drawn after it depth-tests against that, so its parts behind the
6036
+ * first object's solid parts are hidden; its parts in front paint over it,
6037
+ * as they should. An object never tests against its own depth, so one that
6038
+ * overlaps nothing draws exactly as it did.
6039
+ *
6040
+ * `0.1`-`0.2` came closest to the PlayCanvas build in the A/B suite's
6041
+ * overlap cases (a staff through a hand). Lower, the faint edges of the
6042
+ * first object hide the second; higher, too little of a character is solid
6043
+ * enough to hide anything.
6044
+ *
6045
+ * What it costs:
6046
+ * - A second, depth-only draw of every splat object. Its quads are cut to
6047
+ * where a splat is above this alpha, so the fragments are few; the vertex
6048
+ * work, which under WebGL includes the skinning, is paid twice.
6049
+ * - Where the first object is faint but above this alpha, it hides the
6050
+ * second outright instead of letting it show through: a harder edge
6051
+ * where the two overlap.
6052
+ * - Where the first object is fainter than this alpha, the second still
6053
+ * paints over it, as before.
6054
+ * - The alpha is taken after {@link Splat.setOpacity}, so an object fading
6055
+ * out keeps hiding what is behind it at its cores until its opacity
6056
+ * falls below this value. Opt a fading object out with its own
6057
+ * {@link Splat.depthWriteAlpha} for the fade.
6058
+ * - Splats now hide whatever draws after them in depth: transparent meshes
6059
+ * and particles behind a character, and post-processing that reads the
6060
+ * depth buffer sees them.
6061
+ *
6062
+ * Read per frame. A splat's own {@link Splat.depthWriteAlpha} overrides it.
6063
+ */
6064
+ get depthWriteAlpha() {
6065
+ return _depthWriteAlpha;
6066
+ },
6067
+ set depthWriteAlpha(value) {
6068
+ _depthWriteAlpha = clamp(value, 0, 1);
6069
+ },
6021
6070
  /**
6022
6071
  * Collect {@link SplatQuality.timings}. Off by default.
6023
6072
  *
@@ -6065,10 +6114,14 @@ var SplatQuality = {
6065
6114
  SplatQuality.pruneRatio = 0;
6066
6115
  SplatQuality.pruneVoxelSize = 0.04;
6067
6116
  SplatQuality.depthOcclusion = true;
6117
+ SplatQuality.depthWriteAlpha = 0;
6068
6118
  SplatQuality.passTimings = false;
6069
6119
  resetTimings();
6070
6120
  }
6071
6121
  };
6122
+ function effectiveDepthWriteAlpha(splatAlpha) {
6123
+ return splatAlpha ?? SplatQuality.depthWriteAlpha;
6124
+ }
6072
6125
  function effectiveMinPixelSize(splatMinPixelSize) {
6073
6126
  const global = SplatQuality.screenSizeCull;
6074
6127
  return global > splatMinPixelSize ? global : splatMinPixelSize;
@@ -6348,6 +6401,7 @@ import {
6348
6401
  InstancedBufferAttribute,
6349
6402
  InstancedBufferGeometry,
6350
6403
  LinearTransfer,
6404
+ NoBlending,
6351
6405
  OneFactor,
6352
6406
  OneMinusSrcAlphaFactor,
6353
6407
  Quaternion as Quaternion2,
@@ -6989,6 +7043,10 @@ uniform float uMinPixelSize;
6989
7043
  // expressions below reduce, bit for bit, to what they were before the cutoff
6990
7044
  // was a setting at all. See SplatQuality.quadAlphaCutoff.
6991
7045
  uniform float uAlphaCutoffScale;
7046
+ #ifdef DEPTH_ONLY
7047
+ // 1 / SplatQuality.depthWriteAlpha: the depth write sizes its quads to that alpha.
7048
+ uniform float uDepthCutoffScale;
7049
+ #endif
6992
7050
  // World rotation (xyzw) and scale of the splat object.
6993
7051
  uniform vec4 uModelRotation;
6994
7052
  uniform vec3 uModelScale;
@@ -7216,7 +7274,11 @@ void main(void) {
7216
7274
 
7217
7275
  // A splat whose peak alpha is already under the cutoff cannot put a
7218
7276
  // fragment on screen: its own quad would be empty.
7277
+ #ifdef DEPTH_ONLY
7278
+ float alphaOverCutoff = clr.a * uDepthCutoffScale;
7279
+ #else
7219
7280
  float alphaOverCutoff = clr.a * uAlphaCutoffScale;
7281
+ #endif
7220
7282
  if (alphaOverCutoff <= 1.0) {
7221
7283
  gl_Position = discardVec;
7222
7284
  return;
@@ -7248,6 +7310,11 @@ in mediump vec4 gaussianColor;
7248
7310
 
7249
7311
  layout(location = 0) out highp vec4 fragColor;
7250
7312
 
7313
+ #ifdef DEPTH_ONLY
7314
+ // SplatQuality.depthWriteAlpha: the alpha at which a fragment writes depth.
7315
+ uniform float uDepthAlpha;
7316
+ #endif
7317
+
7251
7318
  const float EXP4 = exp(-4.0);
7252
7319
  const float INV_EXP4 = 1.0 / (1.0 - EXP4);
7253
7320
 
@@ -7262,11 +7329,18 @@ void main(void) {
7262
7329
  discard;
7263
7330
  }
7264
7331
  mediump float alpha = normExp(A) * gaussianColor.a;
7332
+ #ifdef DEPTH_ONLY
7333
+ if (alpha < uDepthAlpha) {
7334
+ discard;
7335
+ }
7336
+ fragColor = vec4(0.0);
7337
+ #else
7265
7338
  if (alpha < 1.0 / 255.0) {
7266
7339
  discard;
7267
7340
  }
7268
7341
  // Premultiplied.
7269
7342
  fragColor = vec4(gaussianColor.rgb * alpha, alpha);
7343
+ #endif
7270
7344
  }
7271
7345
  `
7272
7346
  );
@@ -7291,6 +7365,7 @@ var RELIGHT_UNIFORMS = [
7291
7365
  var WebGLSplatImpl = class {
7292
7366
  geometry;
7293
7367
  material;
7368
+ depthMaterial;
7294
7369
  /** The depth sort runs on the CPU, so it needs CPU-side centres. */
7295
7370
  needsCpuCenters = true;
7296
7371
  _host;
@@ -7375,8 +7450,25 @@ var WebGLSplatImpl = class {
7375
7450
  blendSrcAlpha: OneFactor,
7376
7451
  blendDstAlpha: OneMinusSrcAlphaFactor
7377
7452
  });
7453
+ material.uniforms.uDepthCutoffScale = { value: 0 };
7454
+ material.uniforms.uDepthAlpha = { value: 2 };
7455
+ const depth = new RawShaderMaterial({
7456
+ name: "SplatDepthWrite",
7457
+ glslVersion: GLSL3,
7458
+ vertexShader: material.vertexShader,
7459
+ fragmentShader: SPLAT_FRAGMENT_GLSL,
7460
+ uniforms: material.uniforms,
7461
+ side: DoubleSide,
7462
+ // Drawn in the transparent list, straight after the colour draw.
7463
+ transparent: true,
7464
+ depthTest: true,
7465
+ depthWrite: true,
7466
+ colorWrite: false,
7467
+ blending: NoBlending
7468
+ });
7378
7469
  this.geometry = geometry;
7379
7470
  this.material = material;
7471
+ this.depthMaterial = depth;
7380
7472
  this._host = host;
7381
7473
  this._splat = splat;
7382
7474
  this._engine = engine;
@@ -7433,6 +7525,10 @@ var WebGLSplatImpl = class {
7433
7525
  this.syncRelight();
7434
7526
  this.material.defines = defines;
7435
7527
  this.material.needsUpdate = true;
7528
+ const depth = this.depthMaterial;
7529
+ depth.vertexShader = this.material.vertexShader;
7530
+ depth.defines = { ...defines, DEPTH_ONLY: "" };
7531
+ depth.needsUpdate = true;
7436
7532
  }
7437
7533
  /** Point the relight uniforms at the splat's current binding. */
7438
7534
  syncRelight() {
@@ -7505,7 +7601,11 @@ var WebGLSplatImpl = class {
7505
7601
  u.splatGroupRots.value = splat.groupRotsTexture ?? null;
7506
7602
  if (this.material.depthTest !== SplatQuality.depthOcclusion) {
7507
7603
  this.material.depthTest = SplatQuality.depthOcclusion;
7604
+ this.depthMaterial.depthTest = SplatQuality.depthOcclusion;
7508
7605
  }
7606
+ const depthAlpha = effectiveDepthWriteAlpha(splat.depthWriteAlpha);
7607
+ u.uDepthCutoffScale.value = depthAlpha > 0 ? 1 / depthAlpha : 0;
7608
+ u.uDepthAlpha.value = depthAlpha > 0 ? depthAlpha : 2;
7509
7609
  this._engine.syncLighting();
7510
7610
  if (!SplatQuality.passTimings) {
7511
7611
  this._sortFor(camera);
@@ -7574,6 +7674,7 @@ var WebGLSplatImpl = class {
7574
7674
  this._asyncSorter?.dispose();
7575
7675
  this.geometry.dispose();
7576
7676
  this.material.dispose();
7677
+ this.depthMaterial.dispose();
7577
7678
  }
7578
7679
  };
7579
7680
 
@@ -7624,6 +7725,8 @@ var SplatMesh = class extends Mesh {
7624
7725
  _engine;
7625
7726
  _asyncSort;
7626
7727
  _impl = null;
7728
+ /** Set by the first of this frame's two draws, which prepared it, for the second. */
7729
+ _prepared = false;
7627
7730
  /**
7628
7731
  * @param asyncSort - See {@link SplatBackend.createImpl}.
7629
7732
  * @internal Construct a {@link Splat} instead; it creates its own mesh.
@@ -7640,6 +7743,9 @@ var SplatMesh = class extends Mesh {
7640
7743
  _attach(backend) {
7641
7744
  const impl = backend.createImpl(this, this.splat, this._engine, this._asyncSort);
7642
7745
  this._impl = impl;
7746
+ const quad = impl.geometry.index.count;
7747
+ impl.geometry.addGroup(0, quad, 0);
7748
+ impl.geometry.addGroup(0, quad, 1);
7643
7749
  this.geometry = impl.geometry;
7644
7750
  this.material = impl.material;
7645
7751
  this.frustumCulled = true;
@@ -7670,13 +7776,35 @@ var SplatMesh = class extends Mesh {
7670
7776
  syncRelight() {
7671
7777
  this._impl?.syncRelight();
7672
7778
  }
7673
- onBeforeRender(renderer, _scene, camera) {
7674
- if (!this._impl) {
7779
+ onBeforeRender(renderer, _scene, camera, _geometry, material) {
7780
+ const impl = this._impl;
7781
+ if (!impl) {
7675
7782
  const backend = backendFor(renderer);
7676
7783
  if (backend) this._attach(backend);
7677
7784
  return;
7678
7785
  }
7679
- this._impl.beforeRender(renderer, camera);
7786
+ if (this._prepared) {
7787
+ this._prepared = false;
7788
+ } else {
7789
+ impl.beforeRender(renderer, camera);
7790
+ this._prepared = Array.isArray(this.material);
7791
+ }
7792
+ if (material !== impl.depthMaterial) this._syncDepthWrite(impl);
7793
+ }
7794
+ /**
7795
+ * Draw the depth group from the next frame on when this splat writes
7796
+ * depth, and only the colour group when it does not. The render list for
7797
+ * this frame is already made.
7798
+ *
7799
+ * Both groups are transparent, so they draw in the transparent list one
7800
+ * after the other: three.js sorts by render order, then depth, then object,
7801
+ * and keeps a group's order within that. The depth this object writes is
7802
+ * there for the splat objects drawn after it, never for its own colour.
7803
+ */
7804
+ _syncDepthWrite(impl) {
7805
+ const on = effectiveDepthWriteAlpha(this.splat.depthWriteAlpha) > 0;
7806
+ if (on === Array.isArray(this.material)) return;
7807
+ this.material = on ? [impl.material, impl.depthMaterial] : impl.material;
7680
7808
  }
7681
7809
  /**
7682
7810
  * A second mesh drawing the same splat data, at the same transform. The
@@ -7803,6 +7931,12 @@ var Splat = class _Splat {
7803
7931
  localBounds = new Box3();
7804
7932
  /** Discard splats smaller than this many pixels on screen. `0` draws everything. */
7805
7933
  minPixelSize = 0;
7934
+ /**
7935
+ * This splat's own {@link SplatQuality.depthWriteAlpha}: the alpha above which
7936
+ * it writes depth. `null` (the default) follows the global setting; `0`
7937
+ * never writes depth, whatever the global one says.
7938
+ */
7939
+ depthWriteAlpha = null;
7806
7940
  _activeSplatCount;
7807
7941
  _importanceOrder = null;
7808
7942
  /** `_importanceOrder` inverted: each splat's place in it. @internal */
@@ -7882,6 +8016,7 @@ var Splat = class _Splat {
7882
8016
  copy.grade.copy(this.grade);
7883
8017
  copy.effects.x = this.effects.x;
7884
8018
  copy.minPixelSize = this.minPixelSize;
8019
+ copy.depthWriteAlpha = this.depthWriteAlpha;
7885
8020
  copy.antiAlias = this.antiAlias;
7886
8021
  return copy;
7887
8022
  }
@@ -8614,6 +8749,214 @@ var _band = new Float32Array(4);
8614
8749
  var _sketch = new Float32Array(4);
8615
8750
  var _tint = new Float32Array(4);
8616
8751
 
8752
+ // src/loader/environment/compressed-ply.ts
8753
+ var SH_C02 = 0.28209479177387814;
8754
+ var CHUNK_SPLATS = 256;
8755
+ var CHUNK_RANGE = [
8756
+ "min_x",
8757
+ "min_y",
8758
+ "min_z",
8759
+ "max_x",
8760
+ "max_y",
8761
+ "max_z",
8762
+ "min_scale_x",
8763
+ "min_scale_y",
8764
+ "min_scale_z",
8765
+ "max_scale_x",
8766
+ "max_scale_y",
8767
+ "max_scale_z"
8768
+ ];
8769
+ var CHUNK_COLOR = ["min_r", "min_g", "min_b", "max_r", "max_g", "max_b"];
8770
+ var PACKED = ["packed_position", "packed_rotation", "packed_scale", "packed_color"];
8771
+ var TYPE_SIZE = {
8772
+ char: 1,
8773
+ int8: 1,
8774
+ uchar: 1,
8775
+ uint8: 1,
8776
+ short: 2,
8777
+ int16: 2,
8778
+ ushort: 2,
8779
+ uint16: 2,
8780
+ int: 4,
8781
+ int32: 4,
8782
+ uint: 4,
8783
+ uint32: 4,
8784
+ float: 4,
8785
+ float32: 4,
8786
+ double: 8,
8787
+ float64: 8
8788
+ };
8789
+ function parseElements(headerLines) {
8790
+ const elements = [];
8791
+ for (const raw of headerLines) {
8792
+ const tokens = raw.trim().split(/\s+/);
8793
+ if (tokens[0] === "element") {
8794
+ elements.push({ name: tokens[1], count: Number.parseInt(tokens[2], 10), props: [], stride: 0 });
8795
+ } else if (tokens[0] === "property") {
8796
+ const el = elements[elements.length - 1];
8797
+ if (!el) throw new Error("[parseCompressedPly] property declared before any element");
8798
+ if (tokens[1] === "list") throw new Error("[parseCompressedPly] list properties are not supported");
8799
+ const size = TYPE_SIZE[tokens[1]];
8800
+ if (!size) throw new Error(`[parseCompressedPly] unknown property type '${tokens[1]}'`);
8801
+ el.props.push({ name: tokens[2], type: tokens[1], offset: el.stride });
8802
+ el.stride += size;
8803
+ }
8804
+ }
8805
+ return elements;
8806
+ }
8807
+ function isCompressedPlyHeader(header) {
8808
+ const elements = parseElementsSafe(header.split("\n"));
8809
+ if (!elements) return false;
8810
+ const vertex = elements.find((e) => e.name === "vertex");
8811
+ return elements.some((e) => e.name === "chunk") && !!vertex && PACKED.every((n) => vertex.props.some((p) => p.name === n));
8812
+ }
8813
+ function parseElementsSafe(lines) {
8814
+ try {
8815
+ return parseElements(lines);
8816
+ } catch {
8817
+ return null;
8818
+ }
8819
+ }
8820
+ var unorm = (value, bits) => {
8821
+ const t = (1 << bits) - 1;
8822
+ return (value & t) / t;
8823
+ };
8824
+ var lerp = (a, b, t) => a * (1 - t) + b * t;
8825
+ function parseCompressedPly(file, headerText, dataStart) {
8826
+ const lines = headerText.split("\n");
8827
+ const format = lines.map((l) => l.trim().split(/\s+/)).find((t) => t[0] === "format");
8828
+ if (!format || format[1] !== "binary_little_endian") {
8829
+ throw new Error(`[parseCompressedPly] unsupported PLY format '${format?.[1]}'; expected binary_little_endian`);
8830
+ }
8831
+ const elements = parseElements(lines);
8832
+ const offsets = /* @__PURE__ */ new Map();
8833
+ let at = dataStart;
8834
+ for (const el of elements) {
8835
+ if (!Number.isFinite(el.count) || el.count < 0) {
8836
+ throw new Error(`[parseCompressedPly] element '${el.name}' count is not a non-negative integer`);
8837
+ }
8838
+ offsets.set(el.name, at);
8839
+ at += el.count * el.stride;
8840
+ }
8841
+ if (at > file.byteLength) {
8842
+ throw new Error(
8843
+ `[parseCompressedPly] truncated: header declares ${at - dataStart} B of data, but only ${file.byteLength - dataStart} B follow it`
8844
+ );
8845
+ }
8846
+ const chunk = elements.find((e) => e.name === "chunk");
8847
+ const vertex = elements.find((e) => e.name === "vertex");
8848
+ const numSplats = vertex.count;
8849
+ if (chunk.count < Math.ceil(numSplats / CHUNK_SPLATS)) {
8850
+ throw new Error(
8851
+ `[parseCompressedPly] ${numSplats} splats need ${Math.ceil(numSplats / CHUNK_SPLATS)} chunks, but the file has ${chunk.count}`
8852
+ );
8853
+ }
8854
+ const column = (el, name, type, required) => {
8855
+ const p = el.props.find((q) => q.name === name);
8856
+ if (!p) {
8857
+ if (required) throw new Error(`[parseCompressedPly] PLY is missing the required ${el.name} property '${name}'`);
8858
+ return -1;
8859
+ }
8860
+ const ok = type === "float" ? p.type === "float" || p.type === "float32" : p.type === "uint" || p.type === "uint32";
8861
+ if (!ok) throw new Error(`[parseCompressedPly] ${el.name} property '${name}' is ${p.type}; expected ${type}`);
8862
+ return p.offset;
8863
+ };
8864
+ const view = new DataView(file);
8865
+ const rangeCols = CHUNK_RANGE.map((n) => column(chunk, n, "float", true));
8866
+ const colorCols = CHUNK_COLOR.map((n) => column(chunk, n, "float", false));
8867
+ const hasColorRange = colorCols.every((c) => c >= 0);
8868
+ const chunkCols = hasColorRange ? [...rangeCols, ...colorCols] : rangeCols;
8869
+ const chunkSize = chunkCols.length;
8870
+ const chunkData = new Float32Array(chunk.count * chunkSize);
8871
+ const chunkBase = offsets.get("chunk");
8872
+ for (let c = 0; c < chunk.count; c++) {
8873
+ const row = chunkBase + c * chunk.stride;
8874
+ for (let k = 0; k < chunkSize; k++) chunkData[c * chunkSize + k] = view.getFloat32(row + chunkCols[k], true);
8875
+ }
8876
+ const [posCol, rotCol, scaleCol, colorCol] = PACKED.map((n) => column(vertex, n, "uint", true));
8877
+ const vertexBase = offsets.get("vertex");
8878
+ const out = {
8879
+ x: new Float32Array(numSplats),
8880
+ y: new Float32Array(numSplats),
8881
+ z: new Float32Array(numSplats),
8882
+ scale_0: new Float32Array(numSplats),
8883
+ scale_1: new Float32Array(numSplats),
8884
+ scale_2: new Float32Array(numSplats),
8885
+ rot_0: new Float32Array(numSplats),
8886
+ rot_1: new Float32Array(numSplats),
8887
+ rot_2: new Float32Array(numSplats),
8888
+ rot_3: new Float32Array(numSplats),
8889
+ f_dc_0: new Float32Array(numSplats),
8890
+ f_dc_1: new Float32Array(numSplats),
8891
+ f_dc_2: new Float32Array(numSplats),
8892
+ opacity: new Float32Array(numSplats)
8893
+ };
8894
+ const SQRT2 = Math.SQRT2;
8895
+ for (let i = 0; i < numSplats; i++) {
8896
+ const row = vertexBase + i * vertex.stride;
8897
+ const ci = Math.floor(i / CHUNK_SPLATS) * chunkSize;
8898
+ const pos = view.getUint32(row + posCol, true);
8899
+ out.x[i] = lerp(chunkData[ci], chunkData[ci + 3], unorm(pos >>> 21, 11));
8900
+ out.y[i] = lerp(chunkData[ci + 1], chunkData[ci + 4], unorm(pos >>> 11, 10));
8901
+ out.z[i] = lerp(chunkData[ci + 2], chunkData[ci + 5], unorm(pos, 11));
8902
+ const rot = view.getUint32(row + rotCol, true);
8903
+ const a = (unorm(rot >>> 20, 10) - 0.5) * SQRT2;
8904
+ const b = (unorm(rot >>> 10, 10) - 0.5) * SQRT2;
8905
+ const c = (unorm(rot, 10) - 0.5) * SQRT2;
8906
+ const m = Math.sqrt(Math.max(0, 1 - (a * a + b * b + c * c)));
8907
+ let qw, qx, qy, qz;
8908
+ switch (rot >>> 30) {
8909
+ case 0:
8910
+ qw = m;
8911
+ qx = a;
8912
+ qy = b;
8913
+ qz = c;
8914
+ break;
8915
+ case 1:
8916
+ qw = a;
8917
+ qx = m;
8918
+ qy = b;
8919
+ qz = c;
8920
+ break;
8921
+ case 2:
8922
+ qw = a;
8923
+ qx = b;
8924
+ qy = m;
8925
+ qz = c;
8926
+ break;
8927
+ default:
8928
+ qw = a;
8929
+ qx = b;
8930
+ qy = c;
8931
+ qz = m;
8932
+ break;
8933
+ }
8934
+ out.rot_0[i] = qw;
8935
+ out.rot_1[i] = qx;
8936
+ out.rot_2[i] = qy;
8937
+ out.rot_3[i] = qz;
8938
+ const scale = view.getUint32(row + scaleCol, true);
8939
+ out.scale_0[i] = lerp(chunkData[ci + 6], chunkData[ci + 9], unorm(scale >>> 21, 11));
8940
+ out.scale_1[i] = lerp(chunkData[ci + 7], chunkData[ci + 10], unorm(scale >>> 11, 10));
8941
+ out.scale_2[i] = lerp(chunkData[ci + 8], chunkData[ci + 11], unorm(scale, 11));
8942
+ const col = view.getUint32(row + colorCol, true);
8943
+ let r = unorm(col >>> 24, 8);
8944
+ let g = unorm(col >>> 16, 8);
8945
+ let bl = unorm(col >>> 8, 8);
8946
+ const alpha = unorm(col, 8);
8947
+ if (hasColorRange) {
8948
+ r = lerp(chunkData[ci + 12], chunkData[ci + 15], r);
8949
+ g = lerp(chunkData[ci + 13], chunkData[ci + 16], g);
8950
+ bl = lerp(chunkData[ci + 14], chunkData[ci + 17], bl);
8951
+ }
8952
+ out.f_dc_0[i] = (r - 0.5) / SH_C02;
8953
+ out.f_dc_1[i] = (g - 0.5) / SH_C02;
8954
+ out.f_dc_2[i] = (bl - 0.5) / SH_C02;
8955
+ out.opacity[i] = alpha <= 0 ? -40 : alpha >= 1 ? 40 : -Math.log(1 / alpha - 1);
8956
+ }
8957
+ return { numSplats, splats: out };
8958
+ }
8959
+
8617
8960
  // src/loader/environment/ply.ts
8618
8961
  var HEADER_END = "end_header\n";
8619
8962
  var MAX_HEADER_BYTES = 64 * 1024;
@@ -8659,6 +9002,9 @@ function parseGaussianPly(file) {
8659
9002
  throw new Error('[parseGaussianPly] not a PLY file: missing "ply" magic or "end_header"');
8660
9003
  }
8661
9004
  const dataStart = end + HEADER_END.length;
9005
+ if (isCompressedPlyHeader(headerText.slice(0, end))) {
9006
+ return parseCompressedPly(file, headerText.slice(0, end), dataStart);
9007
+ }
8662
9008
  let numSplats = 0;
8663
9009
  let stride = 0;
8664
9010
  let inVertex = false;
@@ -9044,7 +9390,7 @@ function packSplatsInWorker(numSplats, s) {
9044
9390
  const e = f >> 23 & 511;
9045
9391
  return baseTable[e] + ((f & 8388607) >> shiftTable[e]);
9046
9392
  };
9047
- const SH_C05 = 0.28209479177387814;
9393
+ const SH_C06 = 0.28209479177387814;
9048
9394
  const width2 = Math.max(1, Math.ceil(Math.sqrt(numSplats)));
9049
9395
  const height = Math.max(1, Math.ceil(numSplats / width2));
9050
9396
  const texels = width2 * height;
@@ -9057,9 +9403,9 @@ function packSplatsInWorker(numSplats, s) {
9057
9403
  let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
9058
9404
  for (let i = 0; i < numSplats; i++) {
9059
9405
  const o = i * 4;
9060
- color[o] = toHalf(s.f_dc_0[i] * SH_C05 + 0.5);
9061
- color[o + 1] = toHalf(s.f_dc_1[i] * SH_C05 + 0.5);
9062
- color[o + 2] = toHalf(s.f_dc_2[i] * SH_C05 + 0.5);
9406
+ color[o] = toHalf(s.f_dc_0[i] * SH_C06 + 0.5);
9407
+ color[o + 1] = toHalf(s.f_dc_1[i] * SH_C06 + 0.5);
9408
+ color[o + 2] = toHalf(s.f_dc_2[i] * SH_C06 + 0.5);
9063
9409
  color[o + 3] = toHalf(1 / (1 + Math.exp(-s.opacity[i])));
9064
9410
  const x = s.x[i], y = s.y[i], z = s.z[i];
9065
9411
  transformAFloat[o] = x;
@@ -9148,7 +9494,7 @@ async function readImagePixels(files) {
9148
9494
  function decodeSogPixels(meta, px) {
9149
9495
  const n = sogCount(meta);
9150
9496
  const v2 = meta.version === 2;
9151
- const lerp = (a, b, t) => a * (1 - t) + b * t;
9497
+ const lerp2 = (a, b, t) => a * (1 - t) + b * t;
9152
9498
  const book = (codebook) => {
9153
9499
  const out2 = (codebook ?? []).slice();
9154
9500
  if (codebook?.[0] === null) out2[0] = out2[1] + (out2[1] - out2[255]) / 255;
@@ -9156,12 +9502,12 @@ function decodeSogPixels(meta, px) {
9156
9502
  };
9157
9503
  const scaleBook = book(meta.scales.codebook);
9158
9504
  const colorBook = book(meta.sh0.codebook);
9159
- const SH_C05 = 0.28209479177387814;
9505
+ const SH_C06 = 0.28209479177387814;
9160
9506
  const positionTable = (axis) => {
9161
9507
  const out2 = new Float32Array(65536);
9162
9508
  const min = meta.means.mins[axis], max = meta.means.maxs[axis];
9163
9509
  for (let v = 0; v < 65536; v++) {
9164
- const t = lerp(min, max, v / 65535);
9510
+ const t = lerp2(min, max, v / 65535);
9165
9511
  out2[v] = Math.sign(t) * (Math.exp(Math.abs(t)) - 1);
9166
9512
  }
9167
9513
  return out2;
@@ -9171,14 +9517,14 @@ function decodeSogPixels(meta, px) {
9171
9517
  const colorOf = new Float32Array(768);
9172
9518
  for (let c = 0; c < 3; c++) {
9173
9519
  for (let b = 0; b < 256; b++) {
9174
- scaleOf[c * 256 + b] = v2 ? scaleBook[b] : lerp(meta.scales.mins[c], meta.scales.maxs[c], b / 255);
9175
- const color = 0.5 + (v2 ? colorBook[b] : lerp(meta.sh0.mins[c], meta.sh0.maxs[c], b / 255)) * SH_C05;
9176
- colorOf[c * 256 + b] = (color - 0.5) / SH_C05;
9520
+ scaleOf[c * 256 + b] = v2 ? scaleBook[b] : lerp2(meta.scales.mins[c], meta.scales.maxs[c], b / 255);
9521
+ const color = 0.5 + (v2 ? colorBook[b] : lerp2(meta.sh0.mins[c], meta.sh0.maxs[c], b / 255)) * SH_C06;
9522
+ colorOf[c * 256 + b] = (color - 0.5) / SH_C06;
9177
9523
  }
9178
9524
  }
9179
9525
  const opacityOf = new Float32Array(256);
9180
9526
  for (let b = 0; b < 256; b++) {
9181
- const a = v2 ? b / 255 : 1 / (1 + Math.exp(-lerp(meta.sh0.mins[3], meta.sh0.maxs[3], b / 255)));
9527
+ const a = v2 ? b / 255 : 1 / (1 + Math.exp(-lerp2(meta.sh0.mins[3], meta.sh0.maxs[3], b / 255)));
9182
9528
  opacityOf[b] = a <= 0 ? -40 : a >= 1 ? 40 : -Math.log(1 / a - 1);
9183
9529
  }
9184
9530
  const out = {};
@@ -9498,7 +9844,6 @@ function fileNameOf(url) {
9498
9844
  }
9499
9845
  function splatFormatOf(filename) {
9500
9846
  const lower = filename.toLowerCase();
9501
- if (lower.endsWith(".compressed.ply")) return null;
9502
9847
  if (lower.endsWith(".ply")) return "ply";
9503
9848
  if (lower.endsWith(".spz")) return "spz";
9504
9849
  if (lower.endsWith(".sog")) return "sog";
@@ -9576,7 +9921,7 @@ var CharacterBundleError = class extends Error {
9576
9921
  // src/loader/character/legacy/interleaved-splat.ts
9577
9922
  var CHARACTER_BUNDLE_MAGIC = "VFCHARB1";
9578
9923
  var FIRST_PLANAR_VERSION = 3;
9579
- var SH_C02 = 0.28209479177387814;
9924
+ var SH_C03 = 0.28209479177387814;
9580
9925
  var _f16Buf = new ArrayBuffer(4);
9581
9926
  var _f16U32 = new Uint32Array(_f16Buf);
9582
9927
  var _f16F32 = new Float32Array(_f16Buf);
@@ -9686,9 +10031,9 @@ function parseBinaryGsplat(bundleBuffer) {
9686
10031
  const r = splatData.getUint8(offset + 0);
9687
10032
  const g = splatData.getUint8(offset + 1);
9688
10033
  const b = splatData.getUint8(offset + 2);
9689
- storage_f_dc_0[i] = (r / 255 - 0.5) / SH_C02;
9690
- storage_f_dc_1[i] = (g / 255 - 0.5) / SH_C02;
9691
- storage_f_dc_2[i] = (b / 255 - 0.5) / SH_C02;
10034
+ storage_f_dc_0[i] = (r / 255 - 0.5) / SH_C03;
10035
+ storage_f_dc_1[i] = (g / 255 - 0.5) / SH_C03;
10036
+ storage_f_dc_2[i] = (b / 255 - 0.5) / SH_C03;
9692
10037
  offset += 4;
9693
10038
  const opacity = f16ToF32(splatData.getUint16(offset + 0, true));
9694
10039
  const clampedOpacity = Math.max(1e-4, Math.min(0.9999, opacity));
@@ -9720,9 +10065,9 @@ function parseBinaryGsplat(bundleBuffer) {
9720
10065
  const r = splatData.getUint8(offset + 0);
9721
10066
  const g = splatData.getUint8(offset + 1);
9722
10067
  const b = splatData.getUint8(offset + 2);
9723
- storage_f_dc_0[i] = (r / 255 - 0.5) / SH_C02;
9724
- storage_f_dc_1[i] = (g / 255 - 0.5) / SH_C02;
9725
- storage_f_dc_2[i] = (b / 255 - 0.5) / SH_C02;
10068
+ storage_f_dc_0[i] = (r / 255 - 0.5) / SH_C03;
10069
+ storage_f_dc_1[i] = (g / 255 - 0.5) / SH_C03;
10070
+ storage_f_dc_2[i] = (b / 255 - 0.5) / SH_C03;
9726
10071
  offset += 4;
9727
10072
  const opacity = splatData.getFloat32(offset + 0, true);
9728
10073
  const clampedOpacity = Math.max(1e-4, Math.min(0.9999, opacity));
@@ -9838,7 +10183,7 @@ function parseBinaryGsplat(bundleBuffer) {
9838
10183
  }
9839
10184
 
9840
10185
  // src/loader/character/legacy/planar-splat.ts
9841
- var SH_C03 = 0.28209479177387814;
10186
+ var SH_C04 = 0.28209479177387814;
9842
10187
  var OPACITY_CLAMP_LO = 1e-4;
9843
10188
  var OPACITY_CLAMP_HI = 0.9999;
9844
10189
  var MIN_SCALE = 1e-8;
@@ -10046,7 +10391,7 @@ async function parsePlanarSplats(buffer) {
10046
10391
  for (let c = 0; c < 3; c++) {
10047
10392
  const src = readU8(colorPlane, c);
10048
10393
  const out = dcOut[c];
10049
- for (let i = 0; i < numSplats; i++) out[i] = (src[i] / 255 - 0.5) / SH_C03;
10394
+ for (let i = 0; i < numSplats; i++) out[i] = (src[i] / 255 - 0.5) / SH_C04;
10050
10395
  }
10051
10396
  if (isQ23) {
10052
10397
  const lo = header.logitOpacity.min;
@@ -10204,7 +10549,7 @@ async function decodeLegacyBundle(file) {
10204
10549
  }
10205
10550
 
10206
10551
  // src/loader/character/v1/decode.ts
10207
- var SH_C04 = 0.28209479177387814;
10552
+ var SH_C05 = 0.28209479177387814;
10208
10553
  var POSITION_LEVELS = 65536;
10209
10554
  var BYTE_LEVELS = 256;
10210
10555
  var BYTES_PER_ELEMENT2 = { f32: 4, u16: 2, u8: 1 };
@@ -10479,7 +10824,7 @@ async function decodeV1(file, payloadOffset, payloadLength) {
10479
10824
  for (let c = 0; c < 3; c++) {
10480
10825
  const src = readU8(p, c);
10481
10826
  const dst = dcOut[c];
10482
- for (let i = 0; i < numSplats; i++) dst[i] = (src[i] / 255 - 0.5) / SH_C04;
10827
+ for (let i = 0; i < numSplats; i++) dst[i] = (src[i] / 255 - 0.5) / SH_C05;
10483
10828
  }
10484
10829
  }
10485
10830
  let boneIndices;
@@ -11417,7 +11762,9 @@ var Character = class _Character {
11417
11762
  * animates in lockstep) but on `layerIds`. With the body occluded by real
11418
11763
  * objects, this copy is what shows through obstacles: give it a see-through
11419
11764
  * look with its own {@link Splat.setOpacity} or {@link Splat.setColorModifier}
11420
- * (the PlayCanvas build's `applyOverlaySplatStyle` is not ported).
11765
+ * (the PlayCanvas build's `applyOverlaySplatStyle` is not ported). It
11766
+ * writes no depth under {@link SplatQuality.depthWriteAlpha}, so it never
11767
+ * hides what it is meant to show through.
11421
11768
  * Synchronous: reuses the decoded data.
11422
11769
  */
11423
11770
  addOverlaySplat(layerIds) {
@@ -11428,6 +11775,7 @@ var Character = class _Character {
11428
11775
  sourceBoneCount: source.sourceBoneCount
11429
11776
  });
11430
11777
  this.armature.linkSplat(splat);
11778
+ splat.depthWriteAlpha = 0;
11431
11779
  splat.setLayers(layerIds);
11432
11780
  splat.setRenderingEnabled(this._active);
11433
11781
  this._applyLook(splat);
@@ -12731,7 +13079,7 @@ import {
12731
13079
  BufferGeometry as BufferGeometry2,
12732
13080
  CustomBlending as CustomBlending2,
12733
13081
  Mesh as Mesh2,
12734
- NoBlending,
13082
+ NoBlending as NoBlending2,
12735
13083
  NormalBlending,
12736
13084
  OneFactor as OneFactor2,
12737
13085
  OrthographicCamera,
@@ -12816,7 +13164,7 @@ var WebGLPostFXProgram = class {
12816
13164
  depthTest: false,
12817
13165
  depthWrite: false,
12818
13166
  transparent: blend !== "none",
12819
- blending: blend === "add" ? CustomBlending2 : blend === "alpha" ? NormalBlending : NoBlending,
13167
+ blending: blend === "add" ? CustomBlending2 : blend === "alpha" ? NormalBlending : NoBlending2,
12820
13168
  blendEquation: AddEquation,
12821
13169
  blendSrc: OneFactor2,
12822
13170
  blendDst: OneFactor2,
@@ -16803,6 +17151,7 @@ var LodBundle = class {
16803
17151
  splat.effects.x = previous.effects.x;
16804
17152
  splat.minPixelSize = previous.minPixelSize;
16805
17153
  splat.antiAlias = previous.antiAlias;
17154
+ splat.depthWriteAlpha = previous.depthWriteAlpha;
16806
17155
  if (packed.numSplats === 0) {
16807
17156
  previous.destroy();
16808
17157
  this._splat = splat;
@@ -16988,8 +17337,9 @@ var Environment = class _Environment {
16988
17337
  /**
16989
17338
  * Fetch, decode and add an environment splat.
16990
17339
  *
16991
- * Reads uncompressed 3D Gaussian Splatting `.ply`, `.spz` (versions 1 to
16992
- * 4), and SOG (a `.sog` bundle or an unbundled `meta.json`). The format is
17340
+ * Reads 3D Gaussian Splatting `.ply` (uncompressed, or PlayCanvas's
17341
+ * `.compressed.ply`), `.spz` (versions 1 to 4), and SOG (a `.sog` bundle or
17342
+ * an unbundled `meta.json`). The format is
16993
17343
  * told by the file name: pass `filename` when the URL does not end in one.
16994
17344
  * `.spz` and SOG decode in a worker.
16995
17345
  *
@@ -17103,6 +17453,95 @@ var SplatSPZLoader = class extends Loader2 {
17103
17453
  }
17104
17454
  };
17105
17455
 
17456
+ // src/three/sog-loader.ts
17457
+ import { FileLoader as FileLoader3, Loader as Loader3 } from "three";
17458
+ function isMetaJson(url) {
17459
+ return url.split(/[?#]/)[0].toLowerCase().endsWith("meta.json");
17460
+ }
17461
+ function meshName(url) {
17462
+ const parts = url.split(/[?#]/)[0].split("/").filter(Boolean);
17463
+ const file = parts.pop() ?? "";
17464
+ if (isMetaJson(file) && parts.length > 0) return parts.pop();
17465
+ return file.replace(/\.\w+$/, "") || "splat";
17466
+ }
17467
+ var SplatSOGLoader = class extends Loader3 {
17468
+ /**
17469
+ * The engine loaded meshes belong to: the lights they respond to. Leave
17470
+ * unset to use {@link SplatEngine.shared}.
17471
+ */
17472
+ engine = null;
17473
+ constructor(manager) {
17474
+ super(manager);
17475
+ }
17476
+ setEngine(engine) {
17477
+ this.engine = engine;
17478
+ return this;
17479
+ }
17480
+ /**
17481
+ * Decode a `.sog` bundle already in memory. `name` labels the mesh; for a
17482
+ * loaded file it is the file's base name. An unbundled `meta.json` cannot
17483
+ * be parsed this way, since its images are separate files: {@link load} it.
17484
+ */
17485
+ async parseAsync(data, name = "splat") {
17486
+ return this._toMesh(await decodeSogBundle(new Uint8Array(data)), name);
17487
+ }
17488
+ load(url, onLoad, onProgress, onError) {
17489
+ const fail2 = (error) => {
17490
+ if (onError) onError(error);
17491
+ else console.error(error);
17492
+ this.manager.itemError(url);
17493
+ };
17494
+ if (isMetaJson(url)) {
17495
+ this._loadMeta(url, onProgress).then(onLoad, (error) => {
17496
+ if (onError) onError(error);
17497
+ else console.error(error);
17498
+ });
17499
+ return;
17500
+ }
17501
+ const loader = this._fileLoader("arraybuffer");
17502
+ loader.setPath(this.path);
17503
+ loader.load(
17504
+ url,
17505
+ (data) => {
17506
+ this.parseAsync(data, meshName(this.path + url)).then(onLoad, fail2);
17507
+ },
17508
+ onProgress,
17509
+ onError
17510
+ );
17511
+ }
17512
+ async _loadMeta(url, onProgress) {
17513
+ const metaLoader = this._fileLoader("json");
17514
+ metaLoader.setPath(this.path);
17515
+ const meta = await metaLoader.loadAsync(url, onProgress);
17516
+ const base = this.path + url;
17517
+ const imageLoader = this._fileLoader("arraybuffer");
17518
+ const images = await Promise.all(
17519
+ imageFiles(meta).map(async (file) => new Uint8Array(await imageLoader.loadAsync(resolveUrl(file, base))))
17520
+ );
17521
+ try {
17522
+ return this._toMesh(await decodeImages(meta, images), meshName(base));
17523
+ } catch (error) {
17524
+ this.manager.itemError(url);
17525
+ throw error;
17526
+ }
17527
+ }
17528
+ /** A `FileLoader` carrying this loader's manager, headers and credentials mode. */
17529
+ _fileLoader(responseType) {
17530
+ const loader = new FileLoader3(this.manager);
17531
+ loader.setResponseType(responseType);
17532
+ loader.setRequestHeader(this.requestHeader);
17533
+ loader.setWithCredentials(this.withCredentials);
17534
+ return loader;
17535
+ }
17536
+ _toMesh(packed, name) {
17537
+ const splat = new Splat(this.engine ?? SplatEngine.shared, { packed });
17538
+ const mesh = splat.object3D;
17539
+ mesh.name = name;
17540
+ mesh.renderOrder = -1;
17541
+ return mesh;
17542
+ }
17543
+ };
17544
+
17106
17545
  // src/lighting/sun-light.ts
17107
17546
  import { Color as Color5, Vector3 as Vector314 } from "three";
17108
17547
 
@@ -17973,7 +18412,7 @@ var ParticleEmitter = class {
17973
18412
  let outLife = inLife + delta;
17974
18413
  const nlife = Math.min(1, Math.max(0, outLife / lifetime));
17975
18414
  const [a, b, c] = nearestPair(nlife);
17976
- const lerp = (q) => q[a] + (q[b] - q[a]) * c;
18415
+ const lerp2 = (q) => q[a] + (q[b] - q[a]) * c;
17977
18416
  const p = i * 3;
17978
18417
  if (inLife <= 0 || outLife >= lifetime) {
17979
18418
  this._spawnPosition(rx, ry, rz, _v);
@@ -17983,8 +18422,8 @@ var ParticleEmitter = class {
17983
18422
  this.angle[i] = this._startAngle + (this._startAngle2 - this._startAngle) * rx;
17984
18423
  }
17985
18424
  const px = this.pos[p], py = this.pos[p + 1], pz = this.pos[p + 2];
17986
- const radial = lerp(g.radial);
17987
- const radialDiv = lerp(g.radial2) - radial;
18425
+ const radial = lerp2(g.radial);
18426
+ const radialDiv = lerp2(g.radial2) - radial;
17988
18427
  let rvx = px - _emitterPos.x, rvy = py - _emitterPos.y, rvz = pz - _emitterPos.z;
17989
18428
  const rl = rvx * rvx + rvy * rvy + rvz * rvz;
17990
18429
  if (rl > 1e-8) {
@@ -17998,16 +18437,16 @@ var ParticleEmitter = class {
17998
18437
  rvx += radialDiv * rx;
17999
18438
  rvy += radialDiv * ry;
18000
18439
  rvz += radialDiv * rz;
18001
- let lvx = lerp(g.localVel[0]), lvy = lerp(g.localVel[1]), lvz = lerp(g.localVel[2]);
18002
- lvx += (lerp(g.localVel2[0]) - lvx) * rx;
18003
- lvy += (lerp(g.localVel2[1]) - lvy) * ry;
18004
- lvz += (lerp(g.localVel2[2]) - lvz) * rz;
18005
- let vx = lerp(g.vel[0]), vy = lerp(g.vel[1]), vz = lerp(g.vel[2]);
18006
- vx += (lerp(g.vel2[0]) - vx) * rx;
18007
- vy += (lerp(g.vel2[1]) - vy) * ry;
18008
- vz += (lerp(g.vel2[2]) - vz) * rz;
18009
- const rot = lerp(g.rotSpeed);
18010
- const rotSpeed = rot + (lerp(g.rotSpeed2) - rot) * ry;
18440
+ let lvx = lerp2(g.localVel[0]), lvy = lerp2(g.localVel[1]), lvz = lerp2(g.localVel[2]);
18441
+ lvx += (lerp2(g.localVel2[0]) - lvx) * rx;
18442
+ lvy += (lerp2(g.localVel2[1]) - lvy) * ry;
18443
+ lvz += (lerp2(g.localVel2[2]) - lvz) * rz;
18444
+ let vx = lerp2(g.vel[0]), vy = lerp2(g.vel[1]), vz = lerp2(g.vel[2]);
18445
+ vx += (lerp2(g.vel2[0]) - vx) * rx;
18446
+ vy += (lerp2(g.vel2[1]) - vy) * ry;
18447
+ vz += (lerp2(g.vel2[2]) - vz) * rz;
18448
+ const rot = lerp2(g.rotSpeed);
18449
+ const rotSpeed = rot + (lerp2(g.rotSpeed2) - rot) * ry;
18011
18450
  if (this._box) {
18012
18451
  lvz -= iv;
18013
18452
  } else if (iv !== 0) {
@@ -23048,6 +23487,7 @@ export {
23048
23487
  EngineDisposedError,
23049
23488
  packSplats,
23050
23489
  SplatQuality,
23490
+ effectiveDepthWriteAlpha,
23051
23491
  effectiveMinPixelSize,
23052
23492
  beginFrameTimings,
23053
23493
  pruneSplats,
@@ -23126,6 +23566,7 @@ export {
23126
23566
  Ragdoll,
23127
23567
  Environment,
23128
23568
  SplatSPZLoader,
23569
+ SplatSOGLoader,
23129
23570
  SunLight,
23130
23571
  SkyLight,
23131
23572
  PointLight,
@@ -23179,4 +23620,4 @@ export {
23179
23620
  SplatRelight,
23180
23621
  setPlayCanvasLightColor
23181
23622
  };
23182
- //# sourceMappingURL=chunk-2GKWT24B.js.map
23623
+ //# sourceMappingURL=chunk-NNGE5LDC.js.map