rayzee 7.15.1 → 7.16.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.
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Rayzee Engine
2
2
 
3
3
  [![npm](https://img.shields.io/npm/v/rayzee?label=npm)](https://www.npmjs.com/package/rayzee)
4
- [![minzipped size](https://img.shields.io/badge/minzipped-169.6%20KB-blue)](https://www.npmjs.com/package/rayzee)
4
+ [![minzipped size](https://img.shields.io/badge/minzipped-170.7%20KB-blue)](https://www.npmjs.com/package/rayzee)
5
5
  [![downloads](https://img.shields.io/npm/dw/rayzee?label=downloads)](https://www.npmjs.com/package/rayzee)
6
6
  [![jsDelivr](https://img.shields.io/jsdelivr/npm/hm/rayzee?label=jsDelivr)](https://www.jsdelivr.com/package/npm/rayzee)
7
7
 
package/dist/rayzee.es.js CHANGED
@@ -6026,48 +6026,49 @@ function qs(e, t) {
6026
6026
  //#region src/Processor/EmissiveTriangleBuilder.js
6027
6027
  var Js = class {
6028
6028
  constructor() {
6029
- this.emissiveTriangles = [], this.emissiveCount = 0, this.totalEmissivePower = 0, this.emissiveIndicesArray = null, this.emissivePowerArray = null, this.cdfArray = null, this.lightBVHNodeData = null, this.lightBVHNodeCount = 0, this.emissiveBitTrailMap = null, this._totalTriangleCount = 0;
6029
+ this.emissiveTriangles = [], this.emissiveCount = 0, this.totalEmissivePower = 0, this.emissiveIndicesArray = null, this.emissivePowerArray = null, this.cdfArray = null, this.lightBVHNodeData = null, this.lightBVHNodeCount = 0, this.emissiveBitTrailMap = null, this._totalTriangleCount = 0, this._hiddenMeshes = /* @__PURE__ */ new Set();
6030
6030
  }
6031
6031
  extractEmissiveTriangles(e, t, n) {
6032
6032
  console.log("[EmissiveTriangleBuilder] Extracting emissive triangles..."), this.emissiveTriangles = [], this.totalEmissivePower = 0, this._totalTriangleCount = n;
6033
- let r = Z.FLOATS_PER_TRIANGLE, i = Z.UV_C_MAT_OFFSET + 2;
6034
- for (let a = 0; a < n; a++) {
6035
- let n = a * r, o = Math.floor(e[n + i]), s = t[o];
6036
- if (!s) continue;
6037
- let c = s.emissive || {
6033
+ let r = Z.FLOATS_PER_TRIANGLE, i = Z.UV_C_MAT_OFFSET + 2, a = Z.UV_C_MAT_OFFSET + 3;
6034
+ for (let o = 0; o < n; o++) {
6035
+ let n = o * r, s = Math.floor(e[n + i]), c = Math.floor(e[n + a]), l = t[s];
6036
+ if (!l) continue;
6037
+ let u = l.emissive || {
6038
6038
  r: 0,
6039
6039
  g: 0,
6040
6040
  b: 0
6041
- }, l = s.emissiveIntensity || 0;
6042
- if (l > 0 && (c.r > 0 || c.g > 0 || c.b > 0)) {
6043
- let t = e[n + 0], r = e[n + 1], i = e[n + 2], u = e[n + 4], d = e[n + 5], f = e[n + 6], p = e[n + 8], m = e[n + 9], h = e[n + 10], g = this._calculateTriangleArea(t, r, i, u, d, f, p, m, h), _ = (.2126 * c.r + .7152 * c.g + .0722 * c.b) * l * g, v = (d - r) * (h - i) - (f - i) * (m - r), y = (f - i) * (p - t) - (u - t) * (h - i), b = (u - t) * (m - r) - (d - r) * (p - t), x = Math.sqrt(v * v + y * y + b * b) || 1, S = s.side === 1 ? -1 : 1;
6044
- v = v / x * S, y = y / x * S, b = b / x * S;
6045
- let C = s.side === 2, w = (t + u + p) / 3, T = (r + d + m) / 3, E = (i + f + h) / 3, D = Math.min(t, u, p), O = Math.min(r, d, m), k = Math.min(i, f, h), A = Math.max(t, u, p), j = Math.max(r, d, m), M = Math.max(i, f, h);
6041
+ }, d = l.emissiveIntensity || 0;
6042
+ if (d > 0 && (u.r > 0 || u.g > 0 || u.b > 0)) {
6043
+ let t = e[n + 0], r = e[n + 1], i = e[n + 2], a = e[n + 4], f = e[n + 5], p = e[n + 6], m = e[n + 8], h = e[n + 9], g = e[n + 10], _ = this._calculateTriangleArea(t, r, i, a, f, p, m, h, g), v = (.2126 * u.r + .7152 * u.g + .0722 * u.b) * d * _, y = (f - r) * (g - i) - (p - i) * (h - r), b = (p - i) * (m - t) - (a - t) * (g - i), x = (a - t) * (h - r) - (f - r) * (m - t), S = Math.sqrt(y * y + b * b + x * x) || 1, C = l.side === 1 ? -1 : 1;
6044
+ y = y / S * C, b = b / S * C, x = x / S * C;
6045
+ let w = l.side === 2, T = (t + a + m) / 3, E = (r + f + h) / 3, D = (i + p + g) / 3, O = Math.min(t, a, m), k = Math.min(r, f, h), A = Math.min(i, p, g), j = Math.max(t, a, m), M = Math.max(r, f, h), ee = Math.max(i, p, g);
6046
6046
  this.emissiveTriangles.push({
6047
- triangleIndex: a,
6048
- materialIndex: o,
6049
- power: _,
6050
- area: g,
6047
+ triangleIndex: o,
6048
+ materialIndex: s,
6049
+ meshIndex: c,
6050
+ power: v,
6051
+ area: _,
6051
6052
  emissive: {
6052
- r: c.r,
6053
- g: c.g,
6054
- b: c.b
6053
+ r: u.r,
6054
+ g: u.g,
6055
+ b: u.b
6055
6056
  },
6056
- emissiveIntensity: l,
6057
- cx: w,
6058
- cy: T,
6059
- cz: E,
6060
- bMinX: D,
6061
- bMinY: O,
6062
- bMinZ: k,
6063
- bMaxX: A,
6064
- bMaxY: j,
6065
- bMaxZ: M,
6066
- nx: v,
6067
- ny: y,
6068
- nz: b,
6069
- twoSided: C
6070
- }), this.totalEmissivePower += _;
6057
+ emissiveIntensity: d,
6058
+ cx: T,
6059
+ cy: E,
6060
+ cz: D,
6061
+ bMinX: O,
6062
+ bMinY: k,
6063
+ bMinZ: A,
6064
+ bMaxX: j,
6065
+ bMaxY: M,
6066
+ bMaxZ: ee,
6067
+ nx: y,
6068
+ ny: b,
6069
+ nz: x,
6070
+ twoSided: w
6071
+ }), this.totalEmissivePower += v;
6071
6072
  }
6072
6073
  }
6073
6074
  return this.emissiveCount = this.emissiveTriangles.length, console.log(`[EmissiveTriangleBuilder] Found ${this.emissiveCount} emissive triangles (${(this.emissiveCount / n * 100).toFixed(2)}%)`), console.log(`[EmissiveTriangleBuilder] Total emissive power: ${this.totalEmissivePower.toFixed(2)}`), this._buildDataArrays(), this.emissiveCount;
@@ -6161,44 +6162,57 @@ var Js = class {
6161
6162
  if (s !== this.emissiveTriangles.some((t) => t.materialIndex === e)) return this.extractEmissiveTriangles(n, r, i), !0;
6162
6163
  if (!s) return !1;
6163
6164
  let c = .2126 * a.r + .7152 * a.g + .0722 * a.b;
6164
- this.totalEmissivePower = 0;
6165
- for (let t = 0; t < this.emissiveCount; t++) {
6166
- let n = this.emissiveTriangles[t];
6167
- n.materialIndex === e && (n.power = c * o * n.area, n.emissive = {
6168
- r: a.r,
6169
- g: a.g,
6170
- b: a.b
6171
- }, n.emissiveIntensity = o, this.emissivePowerArray[t] = n.power), this.totalEmissivePower += this.emissiveTriangles[t].power;
6165
+ for (let t of this.emissiveTriangles) t.materialIndex === e && (t.power = c * o * t.area, t.emissive = {
6166
+ r: a.r,
6167
+ g: a.g,
6168
+ b: a.b
6169
+ }, t.emissiveIntensity = o);
6170
+ return !0;
6171
+ }
6172
+ setHiddenMeshes(e) {
6173
+ let t = /* @__PURE__ */ new Set();
6174
+ for (let e of this.emissiveTriangles) t.add(e.meshIndex);
6175
+ let n = /* @__PURE__ */ new Set();
6176
+ for (let r of e) t.has(r) && n.add(r);
6177
+ let r = this._hiddenMeshes, i = n.size !== r.size;
6178
+ if (!i) {
6179
+ for (let e of n) if (!r.has(e)) {
6180
+ i = !0;
6181
+ break;
6182
+ }
6172
6183
  }
6173
- return this._buildCDF(), !0;
6184
+ return this._hiddenMeshes = n, i;
6174
6185
  }
6175
6186
  buildLightBVH() {
6176
- if (this.emissiveCount === 0) return this.lightBVHNodeData = new Float32Array(16), this.lightBVHNodeData[7] = 1, this.lightBVHNodeData[14] = 1, this.lightBVHNodeData[15] = -1, this.lightBVHNodeCount = 1, this.emissiveBitTrailMap = new Float32Array(Math.max(this._totalTriangleCount, 1)).fill(-1), 1;
6177
- let { nodeData: e, nodeCount: t, sortedPerm: n, bitTrails: r } = new Gs().build(this.emissiveTriangles);
6178
- this.lightBVHNodeData = e, this.lightBVHNodeCount = t, this._rebuildSortedEmissiveData(n), this.emissiveBitTrailMap = new Float32Array(Math.max(this._totalTriangleCount, 1)).fill(-1);
6179
- for (let e = 0; e < n.length; e++) {
6180
- let t = this.emissiveTriangles[n[e]].triangleIndex;
6181
- this.emissiveBitTrailMap[t] = r[e];
6187
+ let e = this._hiddenMeshes.size === 0 ? this.emissiveTriangles : this.emissiveTriangles.filter((e) => !this._hiddenMeshes.has(e.meshIndex));
6188
+ this.emissiveCount = e.length, this.totalEmissivePower = 0;
6189
+ for (let t of e) this.totalEmissivePower += t.power;
6190
+ if (e.length === 0) return this.lightBVHNodeData = new Float32Array(16), this.lightBVHNodeData[7] = 1, this.lightBVHNodeData[14] = 1, this.lightBVHNodeData[15] = -1, this.lightBVHNodeCount = 1, this.emissiveBitTrailMap = new Float32Array(Math.max(this._totalTriangleCount, 1)).fill(-1), this.emissiveIndicesArray = new Int32Array(), this.emissivePowerArray = new Float32Array(), this._buildCDF(), this.emissiveTriangleData = new Float32Array(8), 1;
6191
+ let { nodeData: t, nodeCount: n, sortedPerm: r, bitTrails: i } = new Gs().build(e);
6192
+ this.lightBVHNodeData = t, this.lightBVHNodeCount = n, this._rebuildSortedEmissiveData(r, e), this.emissiveBitTrailMap = new Float32Array(Math.max(this._totalTriangleCount, 1)).fill(-1);
6193
+ for (let t = 0; t < r.length; t++) {
6194
+ let n = e[r[t]].triangleIndex;
6195
+ this.emissiveBitTrailMap[n] = i[t];
6182
6196
  }
6183
- return t;
6197
+ return n;
6184
6198
  }
6185
- _rebuildSortedEmissiveData(e) {
6186
- let t = e.length;
6187
- this.emissiveIndicesArray = new Int32Array(t), this.emissivePowerArray = new Float32Array(t);
6188
- for (let n = 0; n < t; n++) {
6189
- let t = e[n];
6190
- this.emissiveIndicesArray[n] = this.emissiveTriangles[t].triangleIndex, this.emissivePowerArray[n] = this.emissiveTriangles[t].power;
6199
+ _rebuildSortedEmissiveData(e, t) {
6200
+ let n = e.length;
6201
+ this.emissiveIndicesArray = new Int32Array(n), this.emissivePowerArray = new Float32Array(n);
6202
+ for (let r = 0; r < n; r++) {
6203
+ let n = e[r];
6204
+ this.emissiveIndicesArray[r] = t[n].triangleIndex, this.emissivePowerArray[r] = t[n].power;
6191
6205
  }
6192
6206
  this._buildCDF();
6193
- let n = new Float32Array(t * 8);
6194
- for (let r = 0; r < t; r++) {
6195
- let t = e[r], i = this.emissiveTriangles[t], a = r * 8;
6196
- n[a + 0] = i.triangleIndex, n[a + 1] = i.power, n[a + 2] = this.cdfArray[r], n[a + 3] = this.totalEmissivePower > 0 ? i.power / this.totalEmissivePower : 0, n[a + 4] = i.emissive.r * i.emissiveIntensity, n[a + 5] = i.emissive.g * i.emissiveIntensity, n[a + 6] = i.emissive.b * i.emissiveIntensity, n[a + 7] = i.area;
6207
+ let r = new Float32Array(n * 8);
6208
+ for (let i = 0; i < n; i++) {
6209
+ let n = t[e[i]], a = i * 8;
6210
+ r[a + 0] = n.triangleIndex, r[a + 1] = n.power, r[a + 2] = this.cdfArray[i], r[a + 3] = this.totalEmissivePower > 0 ? n.power / this.totalEmissivePower : 0, r[a + 4] = n.emissive.r * n.emissiveIntensity, r[a + 5] = n.emissive.g * n.emissiveIntensity, r[a + 6] = n.emissive.b * n.emissiveIntensity, r[a + 7] = n.area;
6197
6211
  }
6198
- this.emissiveTriangleData = n, console.log(`[EmissiveTriangleBuilder] Rebuilt sorted emissive data: ${t} entries`);
6212
+ this.emissiveTriangleData = r, console.log(`[EmissiveTriangleBuilder] Rebuilt sorted emissive data: ${n} entries`);
6199
6213
  }
6200
6214
  clear() {
6201
- this.emissiveTriangles = [], this.emissiveCount = 0, this.totalEmissivePower = 0, this.emissiveIndicesArray = null, this.emissivePowerArray = null, this.cdfArray = null, this.lightBVHNodeData = null, this.lightBVHNodeCount = 0, this.emissiveBitTrailMap = null, this._totalTriangleCount = 0;
6215
+ this.emissiveTriangles = [], this.emissiveCount = 0, this.totalEmissivePower = 0, this.emissiveIndicesArray = null, this.emissivePowerArray = null, this.cdfArray = null, this.lightBVHNodeData = null, this.lightBVHNodeCount = 0, this.emissiveBitTrailMap = null, this._totalTriangleCount = 0, this._hiddenMeshes = /* @__PURE__ */ new Set();
6202
6216
  }
6203
6217
  }, Ys = class {
6204
6218
  constructor(e = "Build") {
@@ -6558,7 +6572,7 @@ var Js = class {
6558
6572
  for (let n of this.materials) n.map = t(n.map, e.albedo), n.emissiveMap = t(n.emissiveMap, e.emissive), n.normalMap = t(n.normalMap, e.normal), n.bumpMap = t(n.bumpMap, e.bump), n.roughnessMap = t(n.roughnessMap, e.roughness), n.metalnessMap = t(n.metalnessMap, e.metalness), n.displacementMap = t(n.displacementMap, e.displacement), n.anisotropyMap = t(n.anisotropyMap, e.anisotropy), n.transmissionMap = t(n.transmissionMap, e.transmission), n.clearcoatMap = t(n.clearcoatMap, e.clearcoat), n.clearcoatRoughnessMap = t(n.clearcoatRoughnessMap, e.clearcoatRoughness), n.sheenColorMap = t(n.sheenColorMap, e.sheenColor), n.sheenRoughnessMap = t(n.sheenRoughnessMap, e.sheenRoughness), n.iridescenceMap = t(n.iridescenceMap, e.iridescence), n.iridescenceThicknessMap = t(n.iridescenceThicknessMap, e.iridescenceThickness), n.specularIntensityMap = t(n.specularIntensityMap, e.specularIntensity), n.specularColorMap = t(n.specularColorMap, e.specularColor);
6559
6573
  }
6560
6574
  _buildEmissiveData() {
6561
- this.emissiveTriangleCount = this.emissiveTriangleBuilder.extractEmissiveTriangles(this.triangleData, this.materials, this.triangleCount), this.emissiveTriangleData = this.emissiveTriangleBuilder.createEmissiveRawData(), this.emissiveTotalPower = this.emissiveTriangleBuilder.totalEmissivePower, this._log("Emissive triangle extraction complete", this.emissiveTriangleBuilder.getStats()), this.emissiveTriangleBuilder.buildLightBVH(), this.lightBVHNodeData = this.emissiveTriangleBuilder.lightBVHNodeData, this.lightBVHNodeCount = this.emissiveTriangleBuilder.lightBVHNodeCount, this.emissiveTriangleData = this.emissiveTriangleBuilder.emissiveTriangleData || this.emissiveTriangleData, this.emissiveBitTrailMap = this.emissiveTriangleBuilder.emissiveBitTrailMap;
6575
+ this.emissiveTriangleCount = this.emissiveTriangleBuilder.extractEmissiveTriangles(this.triangleData, this.materials, this.triangleCount), this.emissiveTriangleData = this.emissiveTriangleBuilder.createEmissiveRawData(), this.emissiveTotalPower = this.emissiveTriangleBuilder.totalEmissivePower, this._log("Emissive triangle extraction complete", this.emissiveTriangleBuilder.getStats()), this.emissiveTriangleBuilder.buildLightBVH(), this.lightBVHNodeData = this.emissiveTriangleBuilder.lightBVHNodeData, this.lightBVHNodeCount = this.emissiveTriangleBuilder.lightBVHNodeCount, this.emissiveTriangleData = this.emissiveTriangleBuilder.emissiveTriangleData || this.emissiveTriangleData, this.emissiveBitTrailMap = this.emissiveTriangleBuilder.emissiveBitTrailMap, this.emissiveTriangleCount = this.emissiveTriangleBuilder.emissiveCount, this.emissiveTotalPower = this.emissiveTriangleBuilder.totalEmissivePower;
6562
6576
  }
6563
6577
  _createSpheres() {
6564
6578
  return [];
@@ -6713,14 +6727,20 @@ var Js = class {
6713
6727
  updateMaterialEmissive(e, t, n) {
6714
6728
  if (!this.emissiveTriangleBuilder) return null;
6715
6729
  let r = this.materials[e];
6716
- return !r || (t === "emissive" ? r.emissive = n : t === "emissiveIntensity" && (r.emissiveIntensity = n), !this.emissiveTriangleBuilder.updateMaterialEmissive(e, r, this.triangleData, this.materials, this.triangleCount)) ? null : (this.emissiveTriangleBuilder.buildLightBVH(), this.lightBVHNodeData = this.emissiveTriangleBuilder.lightBVHNodeData, this.lightBVHNodeCount = this.emissiveTriangleBuilder.lightBVHNodeCount, this.emissiveTriangleData = this.emissiveTriangleBuilder.emissiveTriangleData, this.emissiveBitTrailMap = this.emissiveTriangleBuilder.emissiveBitTrailMap, this.emissiveTriangleCount = this.emissiveTriangleBuilder.emissiveCount, this.emissiveTotalPower = this.emissiveTriangleBuilder.totalEmissivePower, {
6717
- rawData: this.emissiveTriangleBuilder.emissiveTriangleData,
6718
- emissiveCount: this.emissiveTriangleBuilder.emissiveCount,
6719
- totalPower: this.emissiveTriangleBuilder.totalEmissivePower,
6730
+ return !r || (t === "emissive" ? r.emissive = n : t === "emissiveIntensity" && (r.emissiveIntensity = n), !this.emissiveTriangleBuilder.updateMaterialEmissive(e, r, this.triangleData, this.materials, this.triangleCount)) ? null : this._collectEmissivePayload();
6731
+ }
6732
+ rebuildEmissiveForVisibility(e, t = !1) {
6733
+ return !this.emissiveTriangleBuilder || !this.emissiveTriangleBuilder.setHiddenMeshes(e) && !t ? null : this._collectEmissivePayload();
6734
+ }
6735
+ _collectEmissivePayload() {
6736
+ return this.emissiveTriangleBuilder.buildLightBVH(), this.lightBVHNodeData = this.emissiveTriangleBuilder.lightBVHNodeData, this.lightBVHNodeCount = this.emissiveTriangleBuilder.lightBVHNodeCount, this.emissiveTriangleData = this.emissiveTriangleBuilder.emissiveTriangleData, this.emissiveBitTrailMap = this.emissiveTriangleBuilder.emissiveBitTrailMap, this.emissiveTriangleCount = this.emissiveTriangleBuilder.emissiveCount, this.emissiveTotalPower = this.emissiveTriangleBuilder.totalEmissivePower, {
6737
+ rawData: this.emissiveTriangleData,
6738
+ emissiveCount: this.emissiveTriangleCount,
6739
+ totalPower: this.emissiveTotalPower,
6720
6740
  bitTrailMap: this.emissiveBitTrailMap,
6721
6741
  lightBVHNodeData: this.lightBVHNodeData,
6722
6742
  lightBVHNodeCount: this.lightBVHNodeCount
6723
- });
6743
+ };
6724
6744
  }
6725
6745
  _updateMeshTrianglePositions(e, t) {
6726
6746
  let n = Z.FLOATS_PER_TRIANGLE, r = Z.POSITION_A_OFFSET, i = Z.POSITION_B_OFFSET, a = Z.POSITION_C_OFFSET, o = Z.NORMAL_A_OFFSET, s = Z.NORMAL_B_OFFSET, c = Z.NORMAL_C_OFFSET, l = e.bvhToOriginal;
@@ -7188,7 +7208,7 @@ var cc = 4, lc = class extends $n {
7188
7208
  }, this.environment = new _i(this.scene, this.uniforms), this.environment.callbacks.onReset = () => this.reset(), this.environment.callbacks.getSceneTextureNodes = () => this.shaderBuilder.getSceneTextureNodes(), this.shaderBuilder = new us(), this._initRenderingState(), this.setupBlueNoise(), this.tempVector2 = new nn(), this.lastCameraMatrix = new bt(), this.lastProjectionMatrix = new bt(), this.lastRenderMode = -1, this.renderModeChangeTimeout = null, this.renderModeChangeDelay = 50, this.pendingRenderMode = null, this.lastInteractionModeState = !1, this.cameraChanged = !1, this.updateCompletionThreshold();
7189
7209
  }
7190
7210
  _initDataTextures() {
7191
- this.triangleStorageAttr = null, this.triangleStorageNode = null, this.triangleCount = 0, this.bvhStorageAttr = null, this.bvhStorageNode = null, this.bvhNodeCount = 0, this.directionalLightsData = null, this.pointLightsData = null, this.spotLightsData = null, this.areaLightsData = null, this.goboMaps = null, this.iesProfiles = null, this.stbnScalarTexture = null, this.stbnVec2Texture = null, this.lightStorageAttr = new o(new Float32Array(16), 4), this.lightStorageNode = Se(this.lightStorageAttr, "vec4", 1).toReadOnly(), this._lbvhDataCache = null, this._emissiveDataCache = null, this._bitTrailMapCache = null, this._instanceTable = null, this.spheres = [];
7211
+ this.triangleStorageAttr = null, this.triangleStorageNode = null, this.triangleCount = 0, this.bvhStorageAttr = null, this.bvhStorageNode = null, this.bvhNodeCount = 0, this.directionalLightsData = null, this.pointLightsData = null, this.spotLightsData = null, this.areaLightsData = null, this.goboMaps = null, this.iesProfiles = null, this.stbnScalarTexture = null, this.stbnVec2Texture = null, this.lightStorageAttr = new o(new Float32Array(16), 4), this.lightStorageNode = Se(this.lightStorageAttr, "vec4", 1).toReadOnly(), this._lightBufferRealloc = !1, this._lbvhDataCache = null, this._emissiveDataCache = null, this._bitTrailMapCache = null, this._instanceTable = null, this.spheres = [];
7192
7212
  }
7193
7213
  _defineUniformGetters() {
7194
7214
  let e = this.uniforms;
@@ -7478,8 +7498,15 @@ var cc = 4, lc = class extends $n {
7478
7498
  this.stbnScalarTexture = e, e && (Fn.value = e);
7479
7499
  }
7480
7500
  _rebuildLightBuffer() {
7481
- let e = this._lbvhDataCache, t = this._emissiveDataCache, n = this._bitTrailMapCache, r = e ? e.length : 0, i = t ? t.length : 0, a = n ? Math.ceil(n.length / 4) * 4 : 0, s = Math.max(r + i + a, 4), c = new Float32Array(s);
7482
- e && c.set(e, 0), t && c.set(t, r), n && c.set(n, r + i), this.lightStorageAttr = new o(c, 4), this.lightStorageNode.value = this.lightStorageAttr, this.lightStorageNode.bufferCount = c.length / 4, this.emissiveVec4Offset.value = (this.lightBVHNodeCount.value || 0) * 4, this.reverseMapVec4Offset.value = (r + i) / 4;
7501
+ let e = this._lbvhDataCache, t = this._emissiveDataCache, n = this._bitTrailMapCache, r = e ? e.length : 0, i = t ? t.length : 0, a = n ? Math.ceil(n.length / 4) * 4 : 0, s = Math.max(r + i + a, 4);
7502
+ if (this.lightStorageAttr && s <= this.lightStorageAttr.array.length) {
7503
+ let a = this.lightStorageAttr.array;
7504
+ e && a.set(e, 0), t && a.set(t, r), n && a.set(n, r + i), this.lightStorageAttr.needsUpdate = !0;
7505
+ } else {
7506
+ let a = new Float32Array(s);
7507
+ e && a.set(e, 0), t && a.set(t, r), n && a.set(n, r + i), this.lightStorageAttr = new o(a, 4), this.lightStorageNode.value = this.lightStorageAttr, this.lightStorageNode.bufferCount = a.length / 4, this._lightBufferRealloc = !0;
7508
+ }
7509
+ this.emissiveVec4Offset.value = (this.lightBVHNodeCount.value || 0) * 4, this.reverseMapVec4Offset.value = (r + i) / 4;
7483
7510
  }
7484
7511
  setEmissiveTriangleData(e, t, n = 0, r = null) {
7485
7512
  e && (this._emissiveDataCache = e, r && (this._bitTrailMapCache = r), this.emissiveTriangleCount.value = t, this.emissiveTotalPower.value = n, this._rebuildLightBuffer(), console.log(`PathTracer: ${t} emissive triangles, totalPower=${n.toFixed(4)} (storage buffer)`));
@@ -8896,8 +8923,8 @@ var vl = /* @__PURE__ */ G("\n fn equirectDirectionToUv( direction: vec3f, envir
8896
8923
  }), cu = f(([e, t, n, r]) => {
8897
8924
  let i = t.sub(e), a = n.sub(e), o = n.sub(t), s = F(A(i, i), F(A(a, a), A(o, o))), c = O(i, a), l = A(c, c), u = T(!1).toVar();
8898
8925
  return p(l.greaterThan(1e-20), () => {
8899
- let t = A(c, r.sub(e)), n = t.mul(t).div(l);
8900
- u.assign(s.greaterThan(n));
8926
+ let t = A(c, r.sub(e)), n = t.mul(t).div(l), i = s.mul(s).lessThan(l.mul(64));
8927
+ u.assign(s.greaterThan(n).and(i));
8901
8928
  }), u;
8902
8929
  }), lu = wn({
8903
8930
  direction: "vec3",
@@ -9818,7 +9845,7 @@ var Yu = class extends lc {
9818
9845
  super.setupMaterial(), this.materialData?.materialCount > 0 && (this._kernelManager && this._kernelManager.dispose(), this._wavefrontReady = !1, this._buildWavefrontKernels());
9819
9846
  }
9820
9847
  render(e) {
9821
- if (!this.isReady || !this._wavefrontReady) return;
9848
+ if (!this.isReady || !this._wavefrontReady || this._lightBufferRealloc && (this._kernelManager && this._kernelManager.dispose(), this._wavefrontReady = !1, this._buildWavefrontKernels(), !this._wavefrontReady)) return;
9822
9849
  if (this.isComplete || this.frameCount >= this.completionThreshold || this._isConvergedComplete()) {
9823
9850
  this.isComplete ||= !0;
9824
9851
  return;
@@ -9957,6 +9984,7 @@ var Yu = class extends lc {
9957
9984
  _buildWavefrontKernels() {
9958
9985
  let e = this.shaderBuilder.getSceneTextureNodes();
9959
9986
  if (!e) return;
9987
+ this._lightBufferRealloc = !1;
9960
9988
  let t = this.storageTextures.renderWidth, n = this.storageTextures.renderHeight, r = t * n;
9961
9989
  this._bounceEarlyExitThreshold !== -1 && (this._bounceEarlyExitThreshold = Math.max(100, Math.floor(r / 1e3))), this._packedBuffers ? this._packedBuffers.resize(r) : this._packedBuffers = new hc(r, this.renderer);
9962
9990
  let i = hc.requiredCapacity(r) * 1;
@@ -14255,21 +14283,21 @@ var rf = {
14255
14283
  prev: i
14256
14284
  }));
14257
14285
  }
14258
- setMany(e, { silent: t } = {}) {
14259
- let n = !1;
14260
- for (let [r, i] of Object.entries(e)) {
14261
- let e = this._values.get(r);
14286
+ setMany(e, { silent: t, reset: n } = {}) {
14287
+ let r = !1;
14288
+ for (let [n, i] of Object.entries(e)) {
14289
+ let e = this._values.get(n);
14262
14290
  if (e === i) continue;
14263
- this._values.set(r, i);
14264
- let a = of[r];
14265
- a && (this._applyRoute(a, i, e), (a.reset ?? !0) && (n = !0), t || this.dispatchEvent({
14291
+ this._values.set(n, i);
14292
+ let a = of[n];
14293
+ a && (this._applyRoute(a, i, e), (a.reset ?? !0) && (r = !0), t || this.dispatchEvent({
14266
14294
  type: Y.SETTING_CHANGED,
14267
- key: r,
14295
+ key: n,
14268
14296
  value: i,
14269
14297
  prev: e
14270
14298
  }));
14271
14299
  }
14272
- n && this._resetCallback?.();
14300
+ (n === void 0 ? r : n) && this._resetCallback?.();
14273
14301
  }
14274
14302
  get(e) {
14275
14303
  return this._values.get(e);
@@ -14294,44 +14322,89 @@ var rf = {
14294
14322
  constructor(e) {
14295
14323
  super();
14296
14324
  let t = e.clientWidth, n = e.clientHeight;
14297
- this.camera = new At(60, t / n || 1, .01, 1e3), this.camera.position.set(0, 0, 5), this.controls = new gn(this.camera, e), this.controls.screenSpacePanning = !0, this.controls.zoomToCursor = !0, this.controls.saveState(), this.interactionManager = null, this.cameras = [this.camera], this.currentCameraIndex = 0, this.autoFocusMode = xr.SMOOTHING_FACTOR ? "auto" : "manual", this.afScreenPoint = {
14325
+ this.camera = new At(60, t / n || 1, .01, 1e3), this.camera.position.set(0, 0, 5), this.controls = new gn(this.camera, e), this.controls.screenSpacePanning = !0, this.controls.zoomToCursor = !0, this.controls.saveState(), this.interactionManager = null, this.cameras = [this.camera], this.currentCameraIndex = 0, this._userCameraCounter = 0, this.autoFocusMode = xr.SMOOTHING_FACTOR ? "auto" : "manual", this.afScreenPoint = {
14298
14326
  x: .5,
14299
14327
  y: .5
14300
- }, this.afSmoothingFactor = xr.SMOOTHING_FACTOR, this._lastValidFocusDistance = null, this._smoothedFocusDistance = null, this._afPointDirty = !1, this._afSuspended = !1, this._defaultCameraState = null, this._onResize = null, this._onReset = null, this._getSettings = null;
14328
+ }, this.afSmoothingFactor = xr.SMOOTHING_FACTOR, this._lastValidFocusDistance = null, this._smoothedFocusDistance = null, this._afPointDirty = !1, this._afSuspended = !1, this._defaultCameraState = null, this._onResize = null, this._onReset = null, this._getSettings = null, this._applySettings = null;
14301
14329
  }
14302
14330
  setCameras(e) {
14303
- this.cameras = e;
14331
+ this.cameras = e, this._userCameraCounter = 0;
14332
+ }
14333
+ addCameraFromView(e) {
14334
+ let t = this.camera, n = new At(t.fov, t.aspect, t.near, t.far);
14335
+ return n.position.copy(t.position), n.quaternion.copy(t.quaternion), n.updateMatrixWorld(!0), n.userData.__rayzeeUserCamera = !0, n.userData.__rayzeeEffects = this._captureEffects(), this.controls && (n.userData.__rayzeeOrbitTarget = this.controls.target.clone()), this._userCameraCounter += 1, n.name = e || `View Camera ${this._userCameraCounter}`, this.cameras.push(n), this.cameras.length - 1;
14336
+ }
14337
+ removeCamera(e) {
14338
+ if (!Number.isInteger(e) || e <= 0 || e >= this.cameras.length || !this.cameras[e]?.userData?.__rayzeeUserCamera) return !1;
14339
+ let t = this.currentCameraIndex === e;
14340
+ return this.cameras.splice(e, 1), t ? (this.currentCameraIndex = -1, this.switchCamera(0)) : this.currentCameraIndex > e && --this.currentCameraIndex, !0;
14304
14341
  }
14305
14342
  getCameraNames() {
14306
14343
  return !this.cameras || this.cameras.length === 0 ? ["Default Camera"] : this.cameras.map((e, t) => t === 0 ? "Default Camera" : e.name || `Camera ${t}`);
14307
14344
  }
14308
- initCallbacks({ onResize: e, onReset: t, getSettings: n }) {
14309
- this._onResize = e, this._onReset = t, this._getSettings = n;
14345
+ initCallbacks({ onResize: e, onReset: t, getSettings: n, applySettings: r }) {
14346
+ this._onResize = e, this._onReset = t, this._getSettings = n, this._applySettings = r;
14347
+ }
14348
+ _captureEffects() {
14349
+ let e = this._getSettings;
14350
+ return e ? {
14351
+ enableDOF: e("enableDOF"),
14352
+ focusDistance: e("focusDistance"),
14353
+ aperture: e("aperture"),
14354
+ focalLength: e("focalLength"),
14355
+ apertureScale: e("apertureScale"),
14356
+ anamorphicRatio: e("anamorphicRatio"),
14357
+ autoFocusMode: this.autoFocusMode,
14358
+ afScreenPoint: { ...this.afScreenPoint }
14359
+ } : null;
14360
+ }
14361
+ _applyEffects(e) {
14362
+ e && (this._applySettings?.({
14363
+ enableDOF: e.enableDOF,
14364
+ focusDistance: e.focusDistance,
14365
+ aperture: e.aperture,
14366
+ focalLength: e.focalLength,
14367
+ apertureScale: e.apertureScale,
14368
+ anamorphicRatio: e.anamorphicRatio
14369
+ }), e.autoFocusMode !== void 0 && this.setAutoFocusMode(e.autoFocusMode), e.afScreenPoint && this.setAFScreenPoint(e.afScreenPoint.x, e.afScreenPoint.y));
14370
+ }
14371
+ _getSceneScale() {
14372
+ return this._afContext?.assetLoader?.getSceneScale?.() || 1;
14310
14373
  }
14311
14374
  switchCamera(e, t, n, r) {
14312
- if (t ??= this._getSettings?.("focusDistance"), n ??= this._onResize, r ??= this._onReset, !(!this.cameras || this.cameras.length === 0)) {
14313
- if ((e < 0 || e >= this.cameras.length) && (console.warn(`CameraManager: Invalid camera index ${e}. Using default camera.`), e = 0), this.currentCameraIndex === 0 && e !== 0 && (this._defaultCameraState = {
14314
- position: this.camera.position.clone(),
14315
- quaternion: this.camera.quaternion.clone(),
14316
- fov: this.camera.fov,
14317
- near: this.camera.near,
14318
- far: this.camera.far,
14319
- target: this.controls ? this.controls.target.clone() : null
14320
- }), this.currentCameraIndex = e, e === 0 && this._defaultCameraState) {
14321
- let e = this._defaultCameraState;
14322
- this.camera.position.copy(e.position), this.camera.quaternion.copy(e.quaternion), this.camera.fov = e.fov, this.camera.near = e.near, this.camera.far = e.far, this.camera.updateProjectionMatrix(), this.camera.updateMatrixWorld(!0), this.controls && e.target && (this.controls.target.copy(e.target), this.controls.update());
14323
- } else {
14324
- let n = this.cameras[e];
14325
- if (this.camera.position.copy(n.position), this.camera.quaternion.copy(n.quaternion), this.camera.fov = n.fov, this.camera.near = n.near, this.camera.far = n.far, this.camera.updateProjectionMatrix(), this.camera.updateMatrixWorld(!0), this.controls) {
14375
+ if (t ??= this._getSettings?.("focusDistance"), n ??= this._onResize, r ??= this._onReset, !this.cameras || this.cameras.length === 0) return;
14376
+ (e < 0 || e >= this.cameras.length) && (console.warn(`CameraManager: Invalid camera index ${e}. Using default camera.`), e = 0);
14377
+ let i = this.cameras[this.currentCameraIndex];
14378
+ if (i && (i.userData.__rayzeeEffects = this._captureEffects()), this.currentCameraIndex === 0 && e !== 0 && (this._defaultCameraState = {
14379
+ position: this.camera.position.clone(),
14380
+ quaternion: this.camera.quaternion.clone(),
14381
+ fov: this.camera.fov,
14382
+ near: this.camera.near,
14383
+ far: this.camera.far,
14384
+ target: this.controls ? this.controls.target.clone() : null
14385
+ }), this.currentCameraIndex = e, e === 0 && this._defaultCameraState) {
14386
+ let e = this._defaultCameraState;
14387
+ this.camera.position.copy(e.position), this.camera.quaternion.copy(e.quaternion), this.camera.fov = e.fov, this.camera.near = e.near, this.camera.far = e.far, this.camera.updateProjectionMatrix(), this.camera.updateMatrixWorld(!0), this.controls && e.target && (this.controls.target.copy(e.target), this.controls.update());
14388
+ } else {
14389
+ let n = this.cameras[e];
14390
+ if (this.camera.position.copy(n.position), this.camera.quaternion.copy(n.quaternion), this.camera.fov = n.fov, this.camera.near = n.near, this.camera.far = n.far, this.camera.updateProjectionMatrix(), this.camera.updateMatrixWorld(!0), this.controls) {
14391
+ let e = n.userData?.__rayzeeOrbitTarget;
14392
+ if (e) this.controls.target.copy(e);
14393
+ else {
14326
14394
  let e = new rn(0, 0, -1).applyQuaternion(n.quaternion), r = t || 5;
14327
- this.controls.target.copy(this.camera.position).addScaledVector(e, r), this.controls.update();
14395
+ this.controls.target.copy(this.camera.position).addScaledVector(e, r);
14328
14396
  }
14397
+ this.controls.update();
14329
14398
  }
14330
- n?.(), r?.(), this.dispatchEvent({
14331
- type: "CameraSwitched",
14332
- cameraIndex: e
14333
- });
14334
14399
  }
14400
+ this._applyEffects(this.cameras[e]?.userData?.__rayzeeEffects), n?.(), r?.();
14401
+ let a = this._captureEffects();
14402
+ a && (a.focusDistance /= this._getSceneScale()), this.dispatchEvent({
14403
+ type: "CameraSwitched",
14404
+ cameraIndex: e,
14405
+ effects: a,
14406
+ fov: this.camera.fov
14407
+ });
14335
14408
  }
14336
14409
  focusOn(e) {
14337
14410
  !e || !this.controls || (this.controls.target.copy(e), this.controls.update(), this._onReset?.());
@@ -16344,11 +16417,7 @@ var Yf = {
16344
16417
  if (this._loadingInProgress) throw Error("PathTracerApp: another load is already in progress");
16345
16418
  this._loadingInProgress = !0;
16346
16419
  try {
16347
- await e(), this._emissiveSamplingUserSet = !1, this._clearAppendedModels(), this._syncControlsAfterLoad(), await this.loadSceneData(), this.pipeline?.eventBus.emit("autoexposure:resetHistory"), this.reset(), this.cameraManager.currentCameraIndex = 0, this.dispatchEvent(t), this.dispatchEvent({
16348
- type: "CamerasUpdated",
16349
- cameras: this.cameraManager.cameras,
16350
- cameraNames: this.cameraManager.getCameraNames()
16351
- });
16420
+ await e(), this._emissiveSamplingUserSet = !1, this._clearAppendedModels(), this._syncControlsAfterLoad(), await this.loadSceneData(), this.pipeline?.eventBus.emit("autoexposure:resetHistory"), this.reset(), this.cameraManager.currentCameraIndex = 0, this.dispatchEvent(t), this._dispatchCamerasUpdated();
16352
16421
  } finally {
16353
16422
  this._loadingInProgress = !1;
16354
16423
  }
@@ -16360,7 +16429,7 @@ var Yf = {
16360
16429
  status: "Transferring data to GPU...",
16361
16430
  progress: 86
16362
16431
  }), await new Promise((e) => setTimeout(e, 0)), e.start("GPU data transfer"), !this._sdf.uploadToPathTracer(this.stages.pathTracer, this.lightManager, this.meshScene, t)) return !1;
16363
- this.stages.pathTracer._meshRefs = this.stages.pathTracer._collectMeshRefs(this.meshScene), this.stages.pathTracer.setMeshVisibilityData(this.stages.pathTracer._meshRefs), e.end("GPU data transfer"), ir({
16432
+ this.stages.pathTracer._meshRefs = this.stages.pathTracer._collectMeshRefs(this.meshScene), this.stages.pathTracer.setMeshVisibilityData(this.stages.pathTracer._meshRefs), this._refreshEmissiveForVisibility(), e.end("GPU data transfer"), ir({
16364
16433
  status: "Compiling shaders...",
16365
16434
  progress: 90
16366
16435
  }), await new Promise((e) => setTimeout(e, 0)), e.start("Material setup (TSL compile)"), this.stages.pathTracer.setupMaterial(), e.end("Material setup (TSL compile)"), e.start("Pipeline precompile");
@@ -16375,7 +16444,7 @@ var Yf = {
16375
16444
  }), await n, this.stages.pathTracer.environment.applyCDFResults());
16376
16445
  let r = this.getSceneMinY();
16377
16446
  if (this.settings.set("groundProjectionLevel", r, { reset: !1 }), this.settings.set("groundCatcherHeight", r, { reset: !1 }), !this._emissiveSamplingUserSet) {
16378
- let e = (this._sdf?.emissiveTriangleCount ?? 0) > 0;
16447
+ let e = (this._sdf?.emissiveTriangleBuilder?.emissiveTriangles?.length ?? this._sdf?.emissiveTriangleCount ?? 0) > 0;
16379
16448
  this.settings.set("enableEmissiveTriangleSampling", e, { reset: !1 });
16380
16449
  }
16381
16450
  return e.start("Apply settings"), this.settings.applyAll(), this.stages.compositor.setTransparentBackground(this.settings.get("transparentBackground")), e.end("Apply settings"), e.print(), rr(), this._initAnimationAndTransforms(), this.dispatchEvent({ type: "SceneRebuild" }), !0;
@@ -16441,6 +16510,24 @@ var Yf = {
16441
16510
  setSceneObjectVisibility(e, t) {
16442
16511
  return this.setMeshVisibilityByUuid(e, t);
16443
16512
  }
16513
+ get currentCameraIndex() {
16514
+ return this.cameraManager?.currentCameraIndex ?? 0;
16515
+ }
16516
+ addCamera({ name: e } = {}) {
16517
+ let t = this.cameraManager.addCameraFromView(e);
16518
+ return this.cameraManager.switchCamera(t), this._dispatchCamerasUpdated(), t;
16519
+ }
16520
+ removeCamera(e) {
16521
+ let t = this.cameraManager.removeCamera(e);
16522
+ return t && this._dispatchCamerasUpdated(), t;
16523
+ }
16524
+ _dispatchCamerasUpdated() {
16525
+ this.dispatchEvent({
16526
+ type: "CamerasUpdated",
16527
+ cameras: this.cameraManager.cameras,
16528
+ cameraNames: this.cameraManager.getCameraNames()
16529
+ });
16530
+ }
16444
16531
  async refitBVH(e, t) {
16445
16532
  let n = await this._sdf.refitBVH(e, t);
16446
16533
  return this.stages.pathTracer.updateTriangleData(this._sdf.triangleData), this.stages.pathTracer.updateBVHData(this._sdf.bvhData), this.reset(), n;
@@ -16553,23 +16640,29 @@ var Yf = {
16553
16640
  return Number.isFinite(e.min.y) ? e.min.y : 0;
16554
16641
  }
16555
16642
  setMaterialProperty(e, t, n) {
16556
- if (this.stages.pathTracer?.materialData.updateMaterialProperty(e, t, n), ["emissive", "emissiveIntensity"].includes(t) && this.stages.pathTracer?.enableEmissiveTriangleSampling?.value) {
16557
- let r = this._sdf.updateMaterialEmissive(e, t, n);
16558
- r && (this.stages.pathTracer.setEmissiveTriangleData(r.rawData, r.emissiveCount, r.totalPower, r.bitTrailMap), r.lightBVHNodeData && this.stages.pathTracer.setLightBVHData(r.lightBVHNodeData, r.lightBVHNodeCount));
16559
- }
16560
- this.reset();
16643
+ this.stages.pathTracer?.materialData.updateMaterialProperty(e, t, n), ["emissive", "emissiveIntensity"].includes(t) && this._sdf && this._uploadEmissivePayload(this._sdf.updateMaterialEmissive(e, t, n)), this.reset();
16644
+ }
16645
+ _uploadEmissivePayload(e) {
16646
+ !e || !this.stages.pathTracer || (this.stages.pathTracer.setEmissiveTriangleData(e.rawData, e.emissiveCount, e.totalPower, e.bitTrailMap), e.lightBVHNodeData && this.stages.pathTracer.setLightBVHData(e.lightBVHNodeData, e.lightBVHNodeCount));
16647
+ }
16648
+ _refreshEmissiveForVisibility() {
16649
+ let e = this._sdf?.instanceTable?.entries;
16650
+ if (!e) return;
16651
+ let t = /* @__PURE__ */ new Set();
16652
+ for (let n of e) n && n.visible === !1 && t.add(n.meshIndex);
16653
+ this._uploadEmissivePayload(this._sdf.rebuildEmissiveForVisibility(t));
16561
16654
  }
16562
16655
  setEmissiveTriangleSampling(e) {
16563
16656
  this._emissiveSamplingUserSet = !0, this.settings.set("enableEmissiveTriangleSampling", e);
16564
16657
  }
16565
16658
  hasEmissiveGeometry() {
16566
- return (this._sdf?.emissiveTriangleCount ?? 0) > 0;
16659
+ return (this._sdf?.emissiveTriangleBuilder?.emissiveTriangles?.length ?? this._sdf?.emissiveTriangleCount ?? 0) > 0;
16567
16660
  }
16568
16661
  setMeshVisibility(e, t) {
16569
- this.stages.pathTracer?.updateMeshVisibility(e, t), this.reset();
16662
+ this.stages.pathTracer?.updateMeshVisibility(e, t), this._refreshEmissiveForVisibility(), this.reset();
16570
16663
  }
16571
16664
  updateAllMeshVisibility() {
16572
- this.stages.pathTracer?.updateAllMeshVisibility(), this.reset();
16665
+ this.stages.pathTracer?.updateAllMeshVisibility(), this._refreshEmissiveForVisibility(), this.reset();
16573
16666
  }
16574
16667
  getScene() {
16575
16668
  return this.meshScene || this.scene;
@@ -16665,7 +16758,11 @@ var Yf = {
16665
16758
  }), this.cameraManager.initCallbacks({
16666
16759
  onResize: () => this.onResize(),
16667
16760
  onReset: () => this.reset(),
16668
- getSettings: (e) => this.settings.get(e)
16761
+ getSettings: (e) => this.settings.get(e),
16762
+ applySettings: (e) => this.settings.setMany(e, {
16763
+ silent: !0,
16764
+ reset: !1
16765
+ })
16669
16766
  }), this.cameraManager.initAutoFocus({
16670
16767
  meshScene: this.meshScene,
16671
16768
  assetLoader: this.assetLoader,