rayzee 7.31.1 → 8.1.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.
Files changed (53) hide show
  1. package/README.md +164 -0
  2. package/dist/rayzee.es.js +6728 -4735
  3. package/dist/rayzee.umd.js +121 -73
  4. package/package.json +2 -2
  5. package/src/EngineDefaults.js +168 -13
  6. package/src/EngineIssues.js +5 -0
  7. package/src/PathTracerApp.js +151 -25
  8. package/src/Processor/ArchiveReader.js +538 -0
  9. package/src/Processor/AssetLoader.js +265 -23
  10. package/src/Processor/BVHBuilder.js +48 -32
  11. package/src/Processor/BVHRefitter.js +244 -99
  12. package/src/Processor/ChunkedRecords.js +302 -0
  13. package/src/Processor/EmissiveTriangleBuilder.js +48 -21
  14. package/src/Processor/GeometryExtractor.js +580 -93
  15. package/src/Processor/HostMemory.js +222 -0
  16. package/src/Processor/InstanceTable.js +548 -97
  17. package/src/Processor/PBRT/EqualAreaOctahedral.js +32 -8
  18. package/src/Processor/PBRT/PBRTCurves.js +249 -0
  19. package/src/Processor/PBRT/PBRTMath.js +19 -0
  20. package/src/Processor/PBRT/PBRTParser.js +187 -41
  21. package/src/Processor/PBRT/PBRTSceneBuilder.js +586 -42
  22. package/src/Processor/PBRT/PBRTTokenizer.js +205 -65
  23. package/src/Processor/PBRT/index.js +257 -50
  24. package/src/Processor/PackedRayBuffer.js +29 -18
  25. package/src/Processor/ParallelBVHBuilder.js +9 -8
  26. package/src/Processor/QueueManager.js +5 -3
  27. package/src/Processor/SceneProcessor.js +1046 -365
  28. package/src/Processor/TLASBuilder.js +546 -201
  29. package/src/Processor/TextureCreator.js +11 -9
  30. package/src/Processor/VRAMTracker.js +12 -3
  31. package/src/Processor/Workers/BVHRefitWorker.js +16 -11
  32. package/src/Processor/Workers/BVHWorker.js +12 -12
  33. package/src/Processor/Workers/TLASWorker.js +37 -0
  34. package/src/Processor/utils.js +8 -20
  35. package/src/Stages/NormalDepth.js +6 -2
  36. package/src/Stages/PathTracer.js +5 -5
  37. package/src/Stages/PathTracerStage.js +163 -41
  38. package/src/TSL/BVHTraversal.js +208 -90
  39. package/src/TSL/Common.js +135 -2
  40. package/src/TSL/Displacement.js +21 -11
  41. package/src/TSL/EmissiveSampling.js +49 -25
  42. package/src/TSL/ExtendKernel.js +3 -2
  43. package/src/TSL/LightBVHSampling.js +7 -4
  44. package/src/TSL/LightsDirect.js +30 -29
  45. package/src/TSL/ShadeKernel.js +8 -6
  46. package/src/TSL/SortGlobalKernels.js +3 -4
  47. package/src/TSL/Struct.js +3 -0
  48. package/src/TSL/TextureSampling.js +24 -9
  49. package/src/TSL/patches.js +149 -84
  50. package/src/index.js +3 -0
  51. package/src/managers/AnimationManager.js +86 -76
  52. package/src/managers/MaterialDataManager.js +29 -23
  53. package/src/managers/TransformManager.js +9 -192
@@ -1,4 +1,5 @@
1
1
  import { storage } from 'three/tsl';
2
+ import { gpuOnlyStorageAttribute, uploadStorageChunkRange, uploadStorageChunks } from '../TSL/patches.js';
2
3
  import { StorageInstancedBufferAttribute } from 'three/webgpu';
3
4
  import {
4
5
  NearestFilter, Vector2, Matrix4,
@@ -109,17 +110,21 @@ export class PathTracerStage extends RenderStage {
109
110
  // Initialize material data manager
110
111
  this.materialData = new MaterialDataManager( this.sdfs );
111
112
  this.materialData.callbacks.onReset = () => this.reset();
112
- // Triangle data carries the per-triangle `side` flag (NORMAL_C.w). The
113
- // authoritative CPU array is triangleStorageAttr.array (not sdfs.triangleData,
114
- // which isn't populated on the PathTracerApp build path). The patch mutates
115
- // the array in place — only a dirty flag is needed for GPU re-upload.
113
+ // Triangle data carries the per-triangle `side` flag (NORMAL_C.w). The authoritative
114
+ // CPU copy is `_triangleRecords` when the scene is chunked and triangleStorageAttr.array
115
+ // otherwise (not sdfs.triangleData, which isn't populated on the PathTracerApp build
116
+ // path). The patch mutates it in place, then the GPU copy is refreshed.
116
117
  this.materialData.callbacks.getTriangleData = () => ( {
117
118
  array: this.triangleStorageAttr?.array,
119
+ records: this._triangleRecords,
118
120
  count: this.triangleCount,
119
121
  } );
120
122
  this.materialData.callbacks.onTriangleDataChanged = () => {
121
123
 
122
- if ( this.triangleStorageAttr ) this.triangleStorageAttr.needsUpdate = true;
124
+ if ( ! this.triangleStorageAttr ) return;
125
+ // A chunked attribute owns no CPU array for three.js to re-upload.
126
+ if ( this._triangleRecords ) uploadStorageChunks( this.renderer, this.triangleStorageAttr, this._triangleRecords.chunks );
127
+ else this.triangleStorageAttr.needsUpdate = true;
123
128
 
124
129
  };
125
130
 
@@ -171,11 +176,13 @@ export class PathTracerStage extends RenderStage {
171
176
 
172
177
  // Triangle data (storage buffer for WebGPU)
173
178
  this.triangleStorageAttr = null;
179
+ this._triangleRecords = null;
174
180
  this.triangleStorageNode = null;
175
181
  this.triangleCount = 0;
176
182
 
177
183
  // BVH data (storage buffer for WebGPU)
178
184
  this.bvhStorageAttr = null;
185
+ this._bvhRecords = null;
179
186
  this.bvhStorageNode = null;
180
187
  this.bvhNodeCount = 0;
181
188
 
@@ -704,19 +711,28 @@ export class PathTracerStage extends RenderStage {
704
711
  * On first call, creates the storage buffer and node.
705
712
  * On subsequent calls, creates a new attribute with the correct size
706
713
  * and updates the storage node's value to preserve shader graph references.
707
- * @param {Float32Array} triangleData - Raw triangle data
714
+ * @param {Uint32Array} triangleData - Packed triangle records (uvec4 lanes)
708
715
  * @param {number} triangleCount - Number of triangles
709
716
  */
710
717
  setTriangleData( triangleData, triangleCount ) {
711
718
 
712
719
  if ( ! triangleData ) return;
713
720
 
714
- const vec4Count = triangleData.length / 4;
721
+ // Past the ~2 GB array cap the records arrive as several chunks; they still become one
722
+ // GPU buffer, written at their running byte offsets, so no binding or shader changes.
723
+ const chunked = triangleData.chunks && triangleData.chunks.length > 1 ? triangleData : null;
724
+ const flat = chunked ? null : ( triangleData.chunks ? triangleData.chunks[ 0 ] : triangleData );
725
+ const lanes = chunked ? chunked.recordCount * chunked.lanesPerRecord : flat.length;
726
+ const vec4Count = lanes / 4;
727
+
728
+ const makeAttr = () => chunked
729
+ ? gpuOnlyStorageAttribute( vec4Count, 4, Uint32Array )
730
+ : new StorageInstancedBufferAttribute( flat, 4 );
715
731
 
716
732
  if ( this.triangleStorageNode ) {
717
733
 
718
734
  // Create new attribute with correct size (old one is GC'd, backend WeakMap cleans up GPU buffer)
719
- this.triangleStorageAttr = new StorageInstancedBufferAttribute( triangleData, 4 );
735
+ this.triangleStorageAttr = makeAttr();
720
736
 
721
737
  // Update storage node references (preserves compiled shader graph)
722
738
  this.triangleStorageNode.value = this.triangleStorageAttr;
@@ -725,11 +741,14 @@ export class PathTracerStage extends RenderStage {
725
741
  } else {
726
742
 
727
743
  // First time: create storage buffer and node
728
- this.triangleStorageAttr = new StorageInstancedBufferAttribute( triangleData, 4 );
729
- this.triangleStorageNode = storage( this.triangleStorageAttr, 'vec4', vec4Count ).toReadOnly();
744
+ this.triangleStorageAttr = makeAttr();
745
+ this.triangleStorageNode = storage( this.triangleStorageAttr, 'uvec4', vec4Count ).toReadOnly();
730
746
 
731
747
  }
732
748
 
749
+ this._triangleRecords = chunked;
750
+ if ( chunked ) uploadStorageChunks( this.renderer, this.triangleStorageAttr, chunked.chunks );
751
+
733
752
  this.triangleCount = triangleCount;
734
753
 
735
754
  log.debug( `${fmt.n( this.triangleCount )} triangles (storage buffer)` );
@@ -744,21 +763,33 @@ export class PathTracerStage extends RenderStage {
744
763
 
745
764
  if ( ! bvhImageData ) return;
746
765
 
747
- const vec4Count = bvhImageData.length / 4;
766
+ // Same story as the triangles: past the ~2 GB array cap the nodes arrive as several
767
+ // chunks and still become one GPU buffer.
768
+ const chunked = bvhImageData.chunks && bvhImageData.chunks.length > 1 ? bvhImageData : null;
769
+ const flat = chunked ? null : ( bvhImageData.chunks ? bvhImageData.chunks[ 0 ] : bvhImageData );
770
+ const lanes = chunked ? chunked.recordCount * chunked.lanesPerRecord : flat.length;
771
+ const vec4Count = lanes / 4;
772
+
773
+ const makeAttr = () => chunked
774
+ ? gpuOnlyStorageAttribute( vec4Count, 4, Float32Array )
775
+ : new StorageInstancedBufferAttribute( flat, 4 );
748
776
 
749
777
  if ( this.bvhStorageNode ) {
750
778
 
751
- this.bvhStorageAttr = new StorageInstancedBufferAttribute( bvhImageData, 4 );
779
+ this.bvhStorageAttr = makeAttr();
752
780
  this.bvhStorageNode.value = this.bvhStorageAttr;
753
781
  this.bvhStorageNode.bufferCount = vec4Count;
754
782
 
755
783
  } else {
756
784
 
757
- this.bvhStorageAttr = new StorageInstancedBufferAttribute( bvhImageData, 4 );
785
+ this.bvhStorageAttr = makeAttr();
758
786
  this.bvhStorageNode = storage( this.bvhStorageAttr, 'vec4', vec4Count ).toReadOnly();
759
787
 
760
788
  }
761
789
 
790
+ this._bvhRecords = chunked;
791
+ if ( chunked ) uploadStorageChunks( this.renderer, this.bvhStorageAttr, chunked.chunks );
792
+
762
793
  this.bvhNodeCount = Math.floor( vec4Count / BVH_VEC4_PER_NODE );
763
794
  log.debug( `${fmt.n( this.bvhNodeCount )} BVH nodes (storage buffer)` );
764
795
 
@@ -784,13 +815,31 @@ export class PathTracerStage extends RenderStage {
784
815
 
785
816
  if ( ! meshes || meshes.length === 0 || ! this._instanceTable ) return;
786
817
 
787
- for ( let i = 0; i < meshes.length; i ++ ) {
818
+ this._patchVisibilityFromMeshes( meshes );
819
+ this._flushBVHEdits();
788
820
 
789
- this._patchTLASLeafVisibility( i, this._isWorldVisible( meshes[ i ] ) );
821
+ }
790
822
 
791
- }
823
+ /**
824
+ * Resolve each placement's visibility from the object it came from. Entries are per
825
+ * instance, so an InstancedMesh covers a whole run of them with one authored flag.
826
+ * @private
827
+ */
828
+ _patchVisibilityFromMeshes( meshes ) {
792
829
 
793
- if ( this.bvhStorageAttr ) this.bvhStorageAttr.needsUpdate = true;
830
+ const table = this._instanceTable;
831
+ const cache = new Map();
832
+
833
+ for ( let i = 0; i < table.count; i ++ ) {
834
+
835
+ if ( ! table.isSet[ i ] ) continue;
836
+
837
+ const src = table.sourceMesh[ i ];
838
+ let visible = cache.get( src );
839
+ if ( visible === undefined ) cache.set( src, visible = this._isWorldVisible( meshes[ src ] ) );
840
+ this._patchTLASLeafVisibility( i, visible );
841
+
842
+ }
794
843
 
795
844
  }
796
845
 
@@ -802,7 +851,7 @@ export class PathTracerStage extends RenderStage {
802
851
  updateMeshVisibility( meshIndex, visible ) {
803
852
 
804
853
  if ( ! this._patchTLASLeafVisibility( meshIndex, visible ) ) return;
805
- if ( this.bvhStorageAttr ) this.bvhStorageAttr.needsUpdate = true;
854
+ this._flushBVHEdits();
806
855
 
807
856
  }
808
857
 
@@ -814,13 +863,8 @@ export class PathTracerStage extends RenderStage {
814
863
 
815
864
  if ( ! this._meshRefs || ! this._instanceTable ) return;
816
865
 
817
- for ( let i = 0; i < this._meshRefs.length; i ++ ) {
818
-
819
- this._patchTLASLeafVisibility( i, this._isWorldVisible( this._meshRefs[ i ] ) );
820
-
821
- }
822
-
823
- if ( this.bvhStorageAttr ) this.bvhStorageAttr.needsUpdate = true;
866
+ this._patchVisibilityFromMeshes( this._meshRefs );
867
+ this._flushBVHEdits();
824
868
 
825
869
  }
826
870
 
@@ -831,15 +875,51 @@ export class PathTracerStage extends RenderStage {
831
875
  */
832
876
  _patchTLASLeafVisibility( meshIndex, visible ) {
833
877
 
834
- const entry = this._instanceTable?.entries?.[ meshIndex ];
835
- if ( ! entry || entry.tlasLeafIndex < 0 || ! this.bvhStorageAttr ) return false;
878
+ const table = this._instanceTable;
879
+ if ( ! table || ! table.isSet?.[ meshIndex ] || ! this.bvhStorageAttr ) return false;
836
880
 
837
- entry.visible = visible;
838
- this.bvhStorageAttr.array[ entry.tlasLeafIndex * 16 + 2 ] = visible ? 1.0 : 0.0;
881
+ const leaf = table.tlasLeafIndex[ meshIndex ];
882
+ if ( leaf < 0 ) return false;
883
+
884
+ table.visible[ meshIndex ] = visible ? 1 : 0;
885
+
886
+ // A chunked attribute owns no CPU array; write into the chunk that holds this leaf.
887
+ if ( this._bvhRecords ) this._bvhRecords.chunkFor( leaf )[ this._bvhRecords.baseOf( leaf ) + 2 ] = visible ? 1.0 : 0.0;
888
+ else this.bvhStorageAttr.array[ leaf * 16 + 2 ] = visible ? 1.0 : 0.0;
889
+
890
+ this._dirtyBVHLeaves ??= new Set();
891
+ this._dirtyBVHLeaves.add( leaf );
839
892
  return true;
840
893
 
841
894
  }
842
895
 
896
+ /**
897
+ * Push BVH edits to the GPU. A chunked attribute cannot go through three.js's dirty flag,
898
+ * so the touched leaves are written straight into the buffer.
899
+ * @private
900
+ */
901
+ _flushBVHEdits() {
902
+
903
+ if ( ! this.bvhStorageAttr ) return;
904
+
905
+ if ( ! this._bvhRecords ) {
906
+
907
+ this.bvhStorageAttr.needsUpdate = true;
908
+ this._dirtyBVHLeaves?.clear();
909
+ return;
910
+
911
+ }
912
+
913
+ for ( const leaf of this._dirtyBVHLeaves ?? [] ) {
914
+
915
+ uploadStorageChunkRange( this.renderer, this.bvhStorageAttr, this._bvhRecords, leaf * 16, 16 );
916
+
917
+ }
918
+
919
+ this._dirtyBVHLeaves?.clear();
920
+
921
+ }
922
+
843
923
  /**
844
924
  * Collect mesh references from scene, ordered by meshIndex (assigned during extraction).
845
925
  * @param {Object3D} scene
@@ -903,14 +983,28 @@ export class PathTracerStage extends RenderStage {
903
983
  /** Update triangle positions in the existing GPU buffer (full). */
904
984
  updateTriangleData( triangleData ) {
905
985
 
906
- this._updateStorageBuffer( this.triangleStorageAttr, triangleData );
986
+ if ( this._triangleRecords ) {
987
+
988
+ uploadStorageChunks( this.renderer, this.triangleStorageAttr, this._triangleRecords.chunks );
989
+ return;
990
+
991
+ }
992
+
993
+ this._updateStorageBuffer( this.triangleStorageAttr, triangleData?.chunks ? triangleData.chunks[ 0 ] : triangleData );
907
994
 
908
995
  }
909
996
 
910
997
  /** Update BVH node data in the existing GPU buffer (full). */
911
998
  updateBVHData( bvhData ) {
912
999
 
913
- this._updateStorageBuffer( this.bvhStorageAttr, bvhData );
1000
+ if ( this._bvhRecords ) {
1001
+
1002
+ uploadStorageChunks( this.renderer, this.bvhStorageAttr, this._bvhRecords.chunks );
1003
+ return;
1004
+
1005
+ }
1006
+
1007
+ this._updateStorageBuffer( this.bvhStorageAttr, bvhData?.chunks ? bvhData.chunks[ 0 ] : bvhData );
914
1008
 
915
1009
  }
916
1010
 
@@ -925,29 +1019,55 @@ export class PathTracerStage extends RenderStage {
925
1019
 
926
1020
  if ( this.triangleStorageAttr && triRanges.length > 0 ) {
927
1021
 
928
- this.triangleStorageAttr.clearUpdateRanges();
1022
+ if ( this._triangleRecords ) {
929
1023
 
930
- for ( const r of triRanges ) {
1024
+ // Chunked: three.js cannot re-upload an attribute with no CPU array, so write
1025
+ // the dirty lanes straight into the GPU buffer.
1026
+ for ( const r of triRanges ) {
931
1027
 
932
- this.triangleStorageAttr.addUpdateRange( r.offset, r.count );
1028
+ uploadStorageChunkRange( this.renderer, this.triangleStorageAttr, this._triangleRecords, r.offset, r.count );
933
1029
 
934
- }
1030
+ }
1031
+
1032
+ } else {
1033
+
1034
+ this.triangleStorageAttr.clearUpdateRanges();
935
1035
 
936
- this.triangleStorageAttr.version ++;
1036
+ for ( const r of triRanges ) {
1037
+
1038
+ this.triangleStorageAttr.addUpdateRange( r.offset, r.count );
1039
+
1040
+ }
1041
+
1042
+ this.triangleStorageAttr.version ++;
1043
+
1044
+ }
937
1045
 
938
1046
  }
939
1047
 
940
1048
  if ( this.bvhStorageAttr && bvhRanges.length > 0 ) {
941
1049
 
942
- this.bvhStorageAttr.clearUpdateRanges();
1050
+ if ( this._bvhRecords ) {
943
1051
 
944
- for ( const r of bvhRanges ) {
1052
+ for ( const r of bvhRanges ) {
945
1053
 
946
- this.bvhStorageAttr.addUpdateRange( r.offset, r.count );
1054
+ uploadStorageChunkRange( this.renderer, this.bvhStorageAttr, this._bvhRecords, r.offset, r.count );
947
1055
 
948
- }
1056
+ }
949
1057
 
950
- this.bvhStorageAttr.version ++;
1058
+ } else {
1059
+
1060
+ this.bvhStorageAttr.clearUpdateRanges();
1061
+
1062
+ for ( const r of bvhRanges ) {
1063
+
1064
+ this.bvhStorageAttr.addUpdateRange( r.offset, r.count );
1065
+
1066
+ }
1067
+
1068
+ this.bvhStorageAttr.version ++;
1069
+
1070
+ }
951
1071
 
952
1072
  }
953
1073
 
@@ -1449,8 +1569,10 @@ export class PathTracerStage extends RenderStage {
1449
1569
 
1450
1570
  // Clear data references
1451
1571
  this.triangleStorageAttr = null;
1572
+ this._triangleRecords = null;
1452
1573
  this.triangleStorageNode = null;
1453
1574
  this.bvhStorageAttr = null;
1575
+ this._bvhRecords = null;
1454
1576
  this.bvhStorageNode = null;
1455
1577
  this.placeholderTexture = null;
1456
1578