super-three 0.169.1 → 0.170.1

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 (255) hide show
  1. package/build/three.cjs +24628 -24228
  2. package/build/three.core.js +37002 -0
  3. package/build/three.core.min.js +6 -0
  4. package/build/three.module.js +10519 -47108
  5. package/build/three.module.min.js +1 -1
  6. package/build/three.tsl.js +533 -0
  7. package/build/three.tsl.min.js +6 -0
  8. package/build/three.webgpu.js +17071 -53583
  9. package/build/three.webgpu.min.js +1 -1
  10. package/build/three.webgpu.nodes.js +17134 -53623
  11. package/build/three.webgpu.nodes.min.js +1 -1
  12. package/examples/jsm/Addons.js +3 -3
  13. package/examples/jsm/animation/MMDAnimationHelper.js +2 -0
  14. package/examples/jsm/animation/MMDPhysics.js +2 -0
  15. package/examples/jsm/capabilities/WebGPU.js +1 -10
  16. package/examples/jsm/controls/ArcballControls.js +25 -21
  17. package/examples/jsm/controls/OrbitControls.js +41 -9
  18. package/examples/jsm/controls/PointerLockControls.js +2 -5
  19. package/examples/jsm/controls/TransformControls.js +22 -6
  20. package/examples/jsm/csm/CSM.js +2 -2
  21. package/examples/jsm/csm/CSMFrustum.js +7 -4
  22. package/examples/jsm/csm/CSMHelper.js +2 -0
  23. package/examples/jsm/csm/CSMShadowNode.js +437 -0
  24. package/examples/jsm/curves/NURBSCurve.js +34 -3
  25. package/examples/jsm/exporters/GLTFExporter.js +138 -71
  26. package/examples/jsm/exporters/KTX2Exporter.js +35 -12
  27. package/examples/jsm/exporters/MMDExporter.js +6 -0
  28. package/examples/jsm/exporters/USDZExporter.js +21 -3
  29. package/examples/jsm/geometries/DecalGeometry.js +70 -21
  30. package/examples/jsm/geometries/InstancedPointsGeometry.js +2 -0
  31. package/examples/jsm/helpers/TextureHelperGPU.js +175 -0
  32. package/examples/jsm/interactive/HTMLMesh.js +1 -0
  33. package/examples/jsm/lighting/TiledLighting.js +18 -0
  34. package/examples/jsm/lights/RectAreaLightTexturesLib.js +1 -1
  35. package/examples/jsm/lines/LineGeometry.js +25 -0
  36. package/examples/jsm/lines/LineMaterial.js +0 -1
  37. package/examples/jsm/lines/LineSegmentsGeometry.js +2 -0
  38. package/examples/jsm/lines/webgpu/Line2.js +2 -1
  39. package/examples/jsm/lines/webgpu/LineSegments2.js +2 -16
  40. package/examples/jsm/lines/webgpu/Wireframe.js +57 -0
  41. package/examples/jsm/loaders/3DMLoader.js +1 -0
  42. package/examples/jsm/loaders/3MFLoader.js +91 -0
  43. package/examples/jsm/loaders/FBXLoader.js +12 -2
  44. package/examples/jsm/loaders/GLTFLoader.js +2 -0
  45. package/examples/jsm/loaders/KTX2Loader.js +143 -49
  46. package/examples/jsm/loaders/LDrawLoader.js +22 -136
  47. package/examples/jsm/loaders/LottieLoader.js +1 -0
  48. package/examples/jsm/loaders/MMDLoader.js +3 -0
  49. package/examples/jsm/loaders/MaterialXLoader.js +2 -2
  50. package/examples/jsm/materials/LDrawConditionalLineMaterial.js +143 -0
  51. package/examples/jsm/materials/LDrawConditionalLineNodeMaterial.js +114 -0
  52. package/examples/jsm/math/ColorSpaces.js +76 -0
  53. package/examples/jsm/misc/ProgressiveLightMap.js +52 -40
  54. package/examples/jsm/misc/ProgressiveLightMapGPU.js +293 -0
  55. package/examples/jsm/misc/VolumeSlice.js +1 -0
  56. package/examples/jsm/objects/InstancedPoints.js +2 -4
  57. package/examples/jsm/objects/LensflareMesh.js +4 -2
  58. package/examples/jsm/objects/SkyMesh.js +5 -3
  59. package/examples/jsm/objects/Water2Mesh.js +7 -3
  60. package/examples/jsm/objects/WaterMesh.js +12 -10
  61. package/examples/jsm/postprocessing/AfterimagePass.js +14 -3
  62. package/examples/jsm/postprocessing/SSRPass.js +6 -0
  63. package/examples/jsm/renderers/CSS2DRenderer.js +6 -3
  64. package/examples/jsm/renderers/CSS3DRenderer.js +7 -4
  65. package/examples/jsm/shaders/FXAAShader.js +225 -224
  66. package/examples/jsm/transpiler/AST.js +2 -2
  67. package/examples/jsm/transpiler/GLSLDecoder.js +57 -25
  68. package/examples/jsm/transpiler/ShaderToyDecoder.js +1 -1
  69. package/examples/jsm/transpiler/TSLEncoder.js +11 -6
  70. package/{src/nodes → examples/jsm/tsl}/display/AfterImageNode.js +18 -21
  71. package/{src/nodes → examples/jsm/tsl}/display/AnaglyphPassNode.js +2 -6
  72. package/{src/nodes → examples/jsm/tsl}/display/AnamorphicNode.js +12 -18
  73. package/{src/nodes → examples/jsm/tsl}/display/BleachBypass.js +1 -3
  74. package/{src/nodes → examples/jsm/tsl}/display/BloomNode.js +12 -41
  75. package/{src/nodes → examples/jsm/tsl}/display/DenoiseNode.js +60 -47
  76. package/{src/nodes → examples/jsm/tsl}/display/DepthOfFieldNode.js +4 -9
  77. package/{src/nodes → examples/jsm/tsl}/display/DotScreenNode.js +2 -9
  78. package/examples/jsm/tsl/display/FXAANode.js +316 -0
  79. package/{src/nodes → examples/jsm/tsl}/display/FilmNode.js +4 -7
  80. package/{src/nodes → examples/jsm/tsl}/display/GTAONode.js +73 -78
  81. package/{src/nodes → examples/jsm/tsl}/display/GaussianBlurNode.js +78 -42
  82. package/examples/jsm/tsl/display/LensflareNode.js +161 -0
  83. package/{src/nodes → examples/jsm/tsl}/display/Lut3DNode.js +3 -5
  84. package/{src/nodes → examples/jsm/tsl}/display/MotionBlur.js +2 -3
  85. package/examples/jsm/tsl/display/OutlineNode.js +434 -0
  86. package/{src/nodes → examples/jsm/tsl}/display/ParallaxBarrierPassNode.js +2 -5
  87. package/{src/nodes → examples/jsm/tsl}/display/PixelationPassNode.js +5 -17
  88. package/{src/nodes → examples/jsm/tsl}/display/RGBShiftNode.js +2 -6
  89. package/examples/jsm/tsl/display/SMAANode.js +620 -0
  90. package/{src/nodes → examples/jsm/tsl}/display/SSAAPassNode.js +10 -25
  91. package/examples/jsm/tsl/display/SSRNode.js +366 -0
  92. package/{src/nodes → examples/jsm/tsl}/display/Sepia.js +1 -2
  93. package/{src/nodes → examples/jsm/tsl}/display/SobelOperatorNode.js +4 -12
  94. package/{src/nodes → examples/jsm/tsl}/display/StereoCompositePassNode.js +9 -11
  95. package/{src/nodes → examples/jsm/tsl}/display/StereoPassNode.js +10 -11
  96. package/examples/jsm/tsl/display/TRAAPassNode.js +355 -0
  97. package/{src/nodes → examples/jsm/tsl}/display/TransitionNode.js +3 -7
  98. package/examples/jsm/tsl/display/hashBlur.js +24 -0
  99. package/examples/jsm/tsl/lighting/TiledLightsNode.js +389 -0
  100. package/examples/jsm/utils/SceneOptimizer.js +410 -0
  101. package/examples/jsm/utils/UVsDebug.js +1 -1
  102. package/examples/jsm/utils/{TextureUtilsGPU.js → WebGPUTextureUtils.js} +2 -1
  103. package/examples/jsm/webxr/OculusHandPointerModel.js +21 -21
  104. package/examples/jsm/webxr/Text2D.js +6 -6
  105. package/package.json +4 -4
  106. package/src/Three.Core.js +178 -0
  107. package/src/Three.TSL.js +526 -0
  108. package/src/Three.WebGPU.Nodes.js +9 -186
  109. package/src/Three.WebGPU.js +8 -186
  110. package/src/Three.js +1 -177
  111. package/src/animation/AnimationClip.js +2 -2
  112. package/src/animation/AnimationObjectGroup.js +2 -2
  113. package/src/audio/Audio.js +38 -0
  114. package/src/cameras/PerspectiveCamera.js +6 -6
  115. package/src/cameras/StereoCamera.js +2 -2
  116. package/src/constants.js +1 -6
  117. package/src/core/BufferGeometry.js +48 -8
  118. package/src/core/InterleavedBuffer.js +4 -4
  119. package/src/core/Object3D.js +2 -2
  120. package/src/extras/core/Curve.js +3 -3
  121. package/src/extras/core/Shape.js +2 -2
  122. package/src/geometries/CylinderGeometry.js +2 -2
  123. package/src/geometries/EdgesGeometry.js +2 -2
  124. package/src/geometries/LatheGeometry.js +2 -2
  125. package/src/lights/SpotLightShadow.js +2 -2
  126. package/src/materials/LineDashedMaterial.js +0 -1
  127. package/src/materials/Material.js +2 -2
  128. package/src/materials/MeshPhongMaterial.js +1 -1
  129. package/src/materials/MeshPhysicalMaterial.js +2 -2
  130. package/src/materials/MeshStandardMaterial.js +2 -2
  131. package/src/materials/nodes/Line2NodeMaterial.js +32 -13
  132. package/src/materials/nodes/LineDashedNodeMaterial.js +4 -2
  133. package/src/materials/nodes/NodeMaterial.js +93 -23
  134. package/src/materials/nodes/SpriteNodeMaterial.js +11 -2
  135. package/src/materials/nodes/manager/NodeMaterialObserver.js +119 -7
  136. package/src/math/ColorManagement.js +143 -102
  137. package/src/math/Line3.js +2 -2
  138. package/src/math/Quaternion.js +2 -2
  139. package/src/math/Spherical.js +3 -3
  140. package/src/math/Vector2.js +7 -7
  141. package/src/math/Vector3.js +9 -9
  142. package/src/math/Vector4.js +22 -9
  143. package/src/nodes/Nodes.js +2 -21
  144. package/src/nodes/TSL.js +9 -25
  145. package/src/nodes/accessors/Arrays.js +27 -0
  146. package/src/nodes/accessors/BuiltinNode.js +26 -0
  147. package/src/nodes/accessors/Camera.js +0 -1
  148. package/src/nodes/accessors/ClippingNode.js +76 -42
  149. package/src/nodes/accessors/InstanceNode.js +17 -20
  150. package/src/nodes/accessors/InstancedMeshNode.js +26 -0
  151. package/src/nodes/accessors/Lights.js +32 -0
  152. package/src/nodes/accessors/SceneNode.js +35 -1
  153. package/src/nodes/accessors/StorageBufferNode.js +61 -19
  154. package/src/nodes/accessors/StorageTextureNode.js +10 -4
  155. package/src/nodes/accessors/TextureNode.js +1 -1
  156. package/src/nodes/accessors/VelocityNode.js +13 -3
  157. package/src/nodes/code/FunctionCallNode.js +16 -5
  158. package/src/nodes/code/ScriptableNode.js +4 -4
  159. package/src/nodes/core/Node.js +1 -0
  160. package/src/nodes/core/NodeBuilder.js +31 -14
  161. package/src/nodes/core/NodeUtils.js +28 -0
  162. package/src/nodes/core/VarNode.js +15 -3
  163. package/src/nodes/core/constants.js +6 -0
  164. package/src/nodes/display/BlendModes.js +99 -0
  165. package/src/nodes/display/ColorAdjustment.js +53 -4
  166. package/src/nodes/display/ColorSpaceFunctions.js +5 -5
  167. package/src/nodes/display/ColorSpaceNode.js +27 -39
  168. package/src/nodes/display/PassNode.js +6 -4
  169. package/src/nodes/display/ToneMappingNode.js +1 -1
  170. package/src/nodes/display/ToonOutlinePassNode.js +3 -3
  171. package/src/nodes/display/ViewportDepthNode.js +48 -1
  172. package/src/nodes/functions/PhysicalLightingModel.js +21 -15
  173. package/src/nodes/functions/material/getAlphaHashThreshold.js +70 -0
  174. package/src/nodes/functions/material/getGeometryRoughness.js +8 -2
  175. package/src/nodes/functions/material/getParallaxCorrectNormal.js +22 -0
  176. package/src/nodes/gpgpu/ComputeNode.js +9 -1
  177. package/src/nodes/lighting/AnalyticLightNode.js +23 -419
  178. package/src/nodes/lighting/LightsNode.js +24 -12
  179. package/src/nodes/lighting/PointLightNode.js +41 -22
  180. package/src/nodes/lighting/PointShadowNode.js +254 -0
  181. package/src/nodes/lighting/RectAreaLightNode.js +3 -0
  182. package/src/nodes/lighting/ShadowNode.js +593 -0
  183. package/src/nodes/lighting/SpotLightNode.js +14 -2
  184. package/src/nodes/utils/LoopNode.js +2 -2
  185. package/src/nodes/utils/Oscillators.js +6 -0
  186. package/src/nodes/utils/PostProcessingUtils.js +89 -0
  187. package/src/nodes/utils/RTTNode.js +9 -1
  188. package/src/nodes/utils/ReflectorNode.js +101 -15
  189. package/src/nodes/utils/StorageArrayElementNode.js +2 -2
  190. package/src/nodes/utils/Timer.js +29 -0
  191. package/src/objects/BatchedMesh.js +472 -296
  192. package/src/objects/ClippingGroup.js +19 -0
  193. package/src/objects/Skeleton.js +2 -2
  194. package/src/renderers/WebGLRenderer.js +183 -109
  195. package/src/renderers/common/Animation.js +23 -11
  196. package/src/renderers/common/Attributes.js +4 -0
  197. package/src/renderers/common/Backend.js +2 -0
  198. package/src/renderers/common/Background.js +15 -5
  199. package/src/renderers/common/Bindings.js +23 -6
  200. package/src/renderers/common/ClippingContext.js +77 -85
  201. package/src/renderers/common/Constants.js +2 -1
  202. package/src/renderers/common/Geometries.js +20 -0
  203. package/src/renderers/common/IndirectStorageBufferAttribute.js +15 -0
  204. package/src/renderers/common/Lighting.js +45 -0
  205. package/src/renderers/common/PostProcessingUtils.js +86 -0
  206. package/src/renderers/common/RenderList.js +48 -17
  207. package/src/renderers/common/RenderLists.js +4 -2
  208. package/src/renderers/common/RenderObject.js +45 -28
  209. package/src/renderers/common/RenderObjects.js +12 -6
  210. package/src/renderers/common/Renderer.js +222 -77
  211. package/src/renderers/common/Textures.js +41 -17
  212. package/src/renderers/common/UniformsGroup.js +16 -4
  213. package/src/renderers/common/extras/PMREMGenerator.js +89 -13
  214. package/src/renderers/common/nodes/NodeLibrary.js +2 -15
  215. package/src/renderers/common/nodes/NodeStorageBuffer.js +2 -3
  216. package/src/renderers/common/nodes/NodeUniform.js +42 -0
  217. package/src/renderers/common/nodes/Nodes.js +2 -2
  218. package/src/renderers/shaders/ShaderChunk/colorspace_pars_fragment.glsl.js +4 -24
  219. package/src/renderers/shaders/ShaderChunk/emissivemap_fragment.glsl.js +8 -0
  220. package/src/renderers/shaders/ShaderChunk/map_fragment.glsl.js +2 -2
  221. package/src/renderers/webgl/WebGLBackground.js +20 -1
  222. package/src/renderers/webgl/WebGLBufferRenderer.js +2 -6
  223. package/src/renderers/webgl/WebGLCapabilities.js +0 -7
  224. package/src/renderers/webgl/WebGLGeometries.js +0 -28
  225. package/src/renderers/webgl/WebGLIndexedBufferRenderer.js +2 -6
  226. package/src/renderers/webgl/WebGLProgram.js +24 -28
  227. package/src/renderers/webgl/WebGLPrograms.js +5 -2
  228. package/src/renderers/webgl/WebGLState.js +37 -1
  229. package/src/renderers/webgl/WebGLTextures.js +29 -12
  230. package/src/renderers/webgl-fallback/WebGLBackend.js +111 -37
  231. package/src/renderers/webgl-fallback/WebGLBufferRenderer.js +2 -7
  232. package/src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.js +43 -12
  233. package/src/renderers/webgl-fallback/utils/WebGLState.js +26 -1
  234. package/src/renderers/webgl-fallback/utils/WebGLTextureUtils.js +37 -9
  235. package/src/renderers/webgpu/WebGPUBackend.js +152 -56
  236. package/src/renderers/webgpu/WebGPURenderer.Nodes.js +1 -1
  237. package/src/renderers/webgpu/WebGPURenderer.js +1 -1
  238. package/src/renderers/webgpu/nodes/BasicNodeLibrary.js +0 -8
  239. package/src/renderers/webgpu/nodes/StandardNodeLibrary.js +13 -34
  240. package/src/renderers/webgpu/nodes/WGSLNodeBuilder.js +192 -92
  241. package/src/renderers/webgpu/nodes/WGSLNodeFunction.js +12 -4
  242. package/src/renderers/webgpu/utils/WebGPUAttributeUtils.js +15 -2
  243. package/src/renderers/webgpu/utils/WebGPUBindingUtils.js +72 -7
  244. package/src/renderers/webgpu/utils/WebGPUPipelineUtils.js +29 -9
  245. package/src/renderers/webgpu/utils/WebGPUTextureUtils.js +29 -7
  246. package/src/renderers/webgpu/utils/WebGPUUtils.js +0 -1
  247. package/src/renderers/webxr/WebXRManager.js +4 -4
  248. package/src/textures/Source.js +2 -2
  249. package/src/textures/Texture.js +2 -2
  250. package/examples/jsm/cameras/CinematicCamera.js +0 -208
  251. package/src/nodes/display/BlendMode.js +0 -54
  252. package/src/nodes/display/FXAANode.js +0 -332
  253. package/src/nodes/utils/OscNode.js +0 -85
  254. package/src/nodes/utils/TimerNode.js +0 -97
  255. /package/examples/jsm/utils/{TextureUtils.js → WebGLTextureUtils.js} +0 -0
@@ -0,0 +1,389 @@
1
+ import { DataTexture, FloatType, RGBAFormat, Vector2, Vector3, LightsNode, NodeUpdateType } from 'three/webgpu';
2
+
3
+ import {
4
+ attributeArray, nodeProxy, int, float, vec2, ivec2, ivec4, uniform, Break, Loop,
5
+ Fn, If, Return, textureLoad, instanceIndex, screenCoordinate, directPointLight
6
+ } from 'three/tsl';
7
+
8
+ export const circleIntersectsAABB = /*@__PURE__*/ Fn( ( [ circleCenter, radius, minBounds, maxBounds ] ) => {
9
+
10
+ // Find the closest point on the AABB to the circle's center using method chaining
11
+ const closestX = minBounds.x.max( circleCenter.x.min( maxBounds.x ) );
12
+ const closestY = minBounds.y.max( circleCenter.y.min( maxBounds.y ) );
13
+
14
+ // Compute the distance between the circle's center and the closest point
15
+ const distX = circleCenter.x.sub( closestX );
16
+ const distY = circleCenter.y.sub( closestY );
17
+
18
+ // Calculate the squared distance
19
+ const distSquared = distX.mul( distX ).add( distY.mul( distY ) );
20
+
21
+ return distSquared.lessThanEqual( radius.mul( radius ) );
22
+
23
+ } ).setLayout( {
24
+ name: 'circleIntersectsAABB',
25
+ type: 'bool',
26
+ inputs: [
27
+ { name: 'circleCenter', type: 'vec2' },
28
+ { name: 'radius', type: 'float' },
29
+ { name: 'minBounds', type: 'vec2' },
30
+ { name: 'maxBounds', type: 'vec2' }
31
+ ]
32
+ } );
33
+
34
+ const _vector3 = /*@__PURE__*/ new Vector3();
35
+ const _size = /*@__PURE__*/ new Vector2();
36
+
37
+ class TiledLightsNode extends LightsNode {
38
+
39
+ static get type() {
40
+
41
+ return 'TiledLightsNode';
42
+
43
+ }
44
+
45
+ constructor( maxLights = 1024, tileSize = 32 ) {
46
+
47
+ super();
48
+
49
+ this.materialLights = [];
50
+ this.tiledLights = [];
51
+
52
+ this.maxLights = maxLights;
53
+ this.tileSize = tileSize;
54
+
55
+ this.bufferSize = null;
56
+ this.lightIndexes = null;
57
+ this.screenTileIndex = null;
58
+ this.compute = null;
59
+ this.lightsTexture = null;
60
+
61
+ this.lightsCount = uniform( 0, 'int' );
62
+ this.tileLightCount = 8;
63
+ this.screenSize = uniform( new Vector2() );
64
+ this.cameraProjectionMatrix = uniform( 'mat4' );
65
+ this.cameraViewMatrix = uniform( 'mat4' );
66
+
67
+ this.updateBeforeType = NodeUpdateType.RENDER;
68
+
69
+ }
70
+
71
+ updateLightsTexture() {
72
+
73
+ const { lightsTexture, tiledLights } = this;
74
+
75
+ const data = lightsTexture.image.data;
76
+ const lineSize = lightsTexture.image.width * 4;
77
+
78
+ this.lightsCount.value = tiledLights.length;
79
+
80
+ for ( let i = 0; i < tiledLights.length; i ++ ) {
81
+
82
+ const light = tiledLights[ i ];
83
+
84
+ // world position
85
+
86
+ _vector3.setFromMatrixPosition( light.matrixWorld );
87
+
88
+ // store data
89
+
90
+ const offset = i * 4;
91
+
92
+ data[ offset + 0 ] = _vector3.x;
93
+ data[ offset + 1 ] = _vector3.y;
94
+ data[ offset + 2 ] = _vector3.z;
95
+ data[ offset + 3 ] = light.distance;
96
+
97
+ data[ lineSize + offset + 0 ] = light.color.r * light.intensity;
98
+ data[ lineSize + offset + 1 ] = light.color.g * light.intensity;
99
+ data[ lineSize + offset + 2 ] = light.color.b * light.intensity;
100
+ data[ lineSize + offset + 3 ] = light.decay;
101
+
102
+ }
103
+
104
+ lightsTexture.needsUpdate = true;
105
+
106
+ }
107
+
108
+ updateBefore( frame ) {
109
+
110
+ const { renderer, camera } = frame;
111
+
112
+ this.updateProgram( renderer );
113
+
114
+ this.updateLightsTexture( camera );
115
+
116
+ this.cameraProjectionMatrix.value = camera.projectionMatrix;
117
+ this.cameraViewMatrix.value = camera.matrixWorldInverse;
118
+
119
+ renderer.getDrawingBufferSize( _size );
120
+ this.screenSize.value.copy( _size );
121
+
122
+ renderer.compute( this.compute );
123
+
124
+ }
125
+
126
+ setLights( lights ) {
127
+
128
+ const { tiledLights, materialLights } = this;
129
+
130
+ let materialindex = 0;
131
+ let tiledIndex = 0;
132
+
133
+ for ( const light of lights ) {
134
+
135
+ if ( light.isPointLight === true ) {
136
+
137
+ tiledLights[ tiledIndex ++ ] = light;
138
+
139
+ } else {
140
+
141
+ materialLights[ materialindex ++ ] = light;
142
+
143
+ }
144
+
145
+ }
146
+
147
+ materialLights.length = materialindex;
148
+ tiledLights.length = tiledIndex;
149
+
150
+ return super.setLights( materialLights );
151
+
152
+ }
153
+
154
+ getBlock( block = 0 ) {
155
+
156
+ return this.lightIndexes.element( this.screenTileIndex.mul( int( 2 ).add( int( block ) ) ) );
157
+
158
+ }
159
+
160
+ getTile( element ) {
161
+
162
+ element = int( element );
163
+
164
+ const stride = int( 4 );
165
+ const tileOffset = element.div( stride );
166
+ const tileIndex = this.screenTileIndex.mul( int( 2 ) ).add( tileOffset );
167
+
168
+ return this.lightIndexes.element( tileIndex ).element( element.modInt( stride ) );
169
+
170
+ }
171
+
172
+ getLightData( index ) {
173
+
174
+ index = int( index );
175
+
176
+ const dataA = textureLoad( this.lightsTexture, ivec2( index, 0 ) );
177
+ const dataB = textureLoad( this.lightsTexture, ivec2( index, 1 ) );
178
+
179
+ const position = dataA.xyz;
180
+ const viewPosition = this.cameraViewMatrix.mul( position );
181
+ const distance = dataA.w;
182
+ const color = dataB.rgb;
183
+ const decay = dataB.w;
184
+
185
+ return {
186
+ position,
187
+ viewPosition,
188
+ distance,
189
+ color,
190
+ decay
191
+ };
192
+
193
+ }
194
+
195
+ setupLights( builder, lightNodes ) {
196
+
197
+ this.updateProgram( builder.renderer );
198
+
199
+ //
200
+
201
+ const lightingModel = builder.context.reflectedLight;
202
+
203
+ // force declaration order, before of the loop
204
+ lightingModel.directDiffuse.append();
205
+ lightingModel.directSpecular.append();
206
+
207
+ super.setupLights( builder, lightNodes );
208
+
209
+ Fn( () => {
210
+
211
+ Loop( this.tileLightCount, ( { i } ) => {
212
+
213
+ const lightIndex = this.getTile( i );
214
+
215
+ If( lightIndex.equal( int( 0 ) ), () => {
216
+
217
+ Break();
218
+
219
+ } );
220
+
221
+ const { color, decay, viewPosition, distance } = this.getLightData( lightIndex.sub( 1 ) );
222
+
223
+ directPointLight( {
224
+ color,
225
+ lightViewPosition: viewPosition,
226
+ cutoffDistance: distance,
227
+ decayExponent: decay
228
+ } ).append();
229
+
230
+ } );
231
+
232
+ } )().append();
233
+
234
+ }
235
+
236
+ getBufferFitSize( value ) {
237
+
238
+ const multiple = this.tileSize;
239
+
240
+ return Math.ceil( value / multiple ) * multiple;
241
+
242
+ }
243
+
244
+ setSize( width, height ) {
245
+
246
+ width = this.getBufferFitSize( width );
247
+ height = this.getBufferFitSize( height );
248
+
249
+ if ( ! this.bufferSize || this.bufferSize.width !== width || this.bufferSize.height !== height ) {
250
+
251
+ this.create( width, height );
252
+
253
+ }
254
+
255
+ return this;
256
+
257
+ }
258
+
259
+ updateProgram( renderer ) {
260
+
261
+ renderer.getDrawingBufferSize( _size );
262
+
263
+ const width = this.getBufferFitSize( _size.width );
264
+ const height = this.getBufferFitSize( _size.height );
265
+
266
+ if ( this.bufferSize === null ) {
267
+
268
+ this.create( width, height );
269
+
270
+ } else if ( this.bufferSize.width !== width || this.bufferSize.height !== height ) {
271
+
272
+ this.create( width, height );
273
+
274
+ }
275
+
276
+ }
277
+
278
+ create( width, height ) {
279
+
280
+ const { tileSize, maxLights } = this;
281
+
282
+ const bufferSize = new Vector2( width, height );
283
+ const lineSize = Math.floor( bufferSize.width / tileSize );
284
+ const count = Math.floor( ( bufferSize.width * bufferSize.height ) / tileSize );
285
+
286
+ // buffers
287
+
288
+ const lightsData = new Float32Array( maxLights * 4 * 2 ); // 2048 lights, 4 elements(rgba), 2 components, 1 component per line (position, distance, color, decay)
289
+ const lightsTexture = new DataTexture( lightsData, lightsData.length / 8, 2, RGBAFormat, FloatType );
290
+
291
+ const lightIndexesArray = new Int32Array( count * 4 * 2 );
292
+ const lightIndexes = attributeArray( lightIndexesArray, 'ivec4' ).label( 'lightIndexes' );
293
+
294
+ // compute
295
+
296
+ const getBlock = ( index ) => {
297
+
298
+ const tileIndex = instanceIndex.mul( int( 2 ) ).add( int( index ) );
299
+
300
+ return lightIndexes.element( tileIndex );
301
+
302
+ };
303
+
304
+ const getTile = ( elementIndex ) => {
305
+
306
+ elementIndex = int( elementIndex );
307
+
308
+ const stride = int( 4 );
309
+ const tileOffset = elementIndex.div( stride );
310
+ const tileIndex = instanceIndex.mul( int( 2 ) ).add( tileOffset );
311
+
312
+ return lightIndexes.element( tileIndex ).element( elementIndex.modInt( stride ) );
313
+
314
+ };
315
+
316
+ const compute = Fn( () => {
317
+
318
+ const { cameraProjectionMatrix, bufferSize, screenSize } = this;
319
+
320
+ const tiledBufferSize = bufferSize.clone().divideScalar( tileSize ).floor();
321
+
322
+ const tileScreen = vec2(
323
+ instanceIndex.modInt( tiledBufferSize.width ),
324
+ instanceIndex.div( tiledBufferSize.width )
325
+ ).mul( tileSize ).div( screenSize );
326
+
327
+ const blockSize = float( tileSize ).div( screenSize );
328
+ const minBounds = tileScreen;
329
+ const maxBounds = minBounds.add( blockSize );
330
+
331
+ const index = int( 0 ).toVar();
332
+
333
+ getBlock( 0 ).assign( ivec4( 0 ) );
334
+ getBlock( 1 ).assign( ivec4( 0 ) );
335
+
336
+ Loop( this.maxLights, ( { i } ) => {
337
+
338
+ If( index.greaterThanEqual( this.tileLightCount ).or( int( i ).greaterThanEqual( int( this.lightsCount ) ) ), () => {
339
+
340
+ Return();
341
+
342
+ } );
343
+
344
+ const { viewPosition, distance } = this.getLightData( i );
345
+
346
+ const projectedPosition = cameraProjectionMatrix.mul( viewPosition );
347
+ const ndc = projectedPosition.div( projectedPosition.w );
348
+ const screenPosition = ndc.xy.mul( 0.5 ).add( 0.5 ).flipY();
349
+
350
+ const distanceFromCamera = viewPosition.z;
351
+ const pointRadius = distance.div( distanceFromCamera );
352
+
353
+ If( circleIntersectsAABB( screenPosition, pointRadius, minBounds, maxBounds ), () => {
354
+
355
+ getTile( index ).assign( i.add( int( 1 ) ) );
356
+ index.addAssign( int( 1 ) );
357
+
358
+ } );
359
+
360
+ } );
361
+
362
+ } )().compute( count );
363
+
364
+ // screen coordinate lighting indexes
365
+
366
+ const screenTile = screenCoordinate.div( tileSize ).floor().toVar();
367
+ const screenTileIndex = screenTile.x.add( screenTile.y.mul( lineSize ) );
368
+
369
+ // assigns
370
+
371
+ this.bufferSize = bufferSize;
372
+ this.lightIndexes = lightIndexes;
373
+ this.screenTileIndex = screenTileIndex;
374
+ this.compute = compute;
375
+ this.lightsTexture = lightsTexture;
376
+
377
+ }
378
+
379
+ get hasLights() {
380
+
381
+ return super.hasLights || this.tiledLights.length > 0;
382
+
383
+ }
384
+
385
+ }
386
+
387
+ export default TiledLightsNode;
388
+
389
+ export const tiledLights = /*@__PURE__*/ nodeProxy( TiledLightsNode );