@gnsx/three 0.184.9 → 0.184.11

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.
@@ -7,9 +7,14 @@ import {
7
7
  } from 'three';
8
8
  import { NodeMaterial } from 'three/webgpu';
9
9
  import {
10
+ abs,
10
11
  cubeTexture,
11
12
  Fn,
12
- normalWorld,
13
+ max,
14
+ normalLocal,
15
+ positionLocal,
16
+ select,
17
+ vec3,
13
18
  vec4
14
19
  } from 'three/tsl';
15
20
 
@@ -17,7 +22,41 @@ function createNodeMaterial( texture ) {
17
22
 
18
23
  const fragmentNode = Fn( () => {
19
24
 
20
- return vec4( cubeTexture( texture, normalWorld ).rgb, 1.0 );
25
+ // Get local position and normal
26
+ const localPos = positionLocal;
27
+ const localNormal = normalLocal;
28
+
29
+ // Get absolute normal components to determine dominant axis
30
+ const absNormal = abs( localNormal );
31
+ const maxComp = max( max( absNormal.x, absNormal.y ), absNormal.z );
32
+
33
+ // Determine which face we're on and compute sample direction
34
+ const isX = maxComp.equal( absNormal.x );
35
+ const isY = maxComp.equal( absNormal.y ).and( isX.not() );
36
+
37
+ // Match LightProbeGrid / WebGL cubemap face conventions (see LightProbeGrid.js SH projection).
38
+ const xPositive = localNormal.x.greaterThan( 0.0 );
39
+ const xDir = select( xPositive,
40
+ vec3( 1.0, localPos.y, localPos.z.negate() ).normalize(),
41
+ vec3( - 1.0, localPos.y, localPos.z ).normalize()
42
+ );
43
+
44
+ const yPositive = localNormal.y.greaterThan( 0.0 );
45
+ const yDir = select( yPositive,
46
+ vec3( localPos.x, 1.0, localPos.z.negate() ).normalize(),
47
+ vec3( localPos.x, - 1.0, localPos.z ).normalize()
48
+ );
49
+
50
+ const zPositive = localNormal.z.greaterThan( 0.0 );
51
+ const zDir = select( zPositive,
52
+ vec3( localPos.x, localPos.y, 1.0 ).normalize(),
53
+ vec3( localPos.x, localPos.y, - 1.0 ).normalize()
54
+ );
55
+
56
+ // Select the appropriate direction based on which axis is dominant
57
+ const sampleDir = select( isX, xDir, select( isY, yDir, zDir ) );
58
+
59
+ return vec4( cubeTexture( texture, sampleDir ).rgb, 1.0 );
21
60
 
22
61
  } )();
23
62
 
@@ -40,11 +79,13 @@ function createShaderMaterial( texture ) {
40
79
 
41
80
  vertexShader: /* glsl */`
42
81
 
43
- varying vec3 vWorldNormal;
82
+ varying vec3 vLocalPosition;
83
+ varying vec3 vNormal;
44
84
 
45
85
  void main() {
46
86
 
47
- vWorldNormal = normalize( mat3( modelMatrix ) * normal );
87
+ vLocalPosition = position;
88
+ vNormal = normal;
48
89
  gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
49
90
 
50
91
  }
@@ -57,12 +98,56 @@ function createShaderMaterial( texture ) {
57
98
 
58
99
  uniform samplerCube cubeTexture;
59
100
 
60
- varying vec3 vWorldNormal;
101
+ varying vec3 vLocalPosition;
102
+ varying vec3 vNormal;
61
103
 
62
104
  void main() {
105
+ // Get the dominant axis from the normal to determine which face we're on
106
+ vec3 absNormal = abs( vNormal );
107
+ float maxComp = max( max( absNormal.x, absNormal.y ), absNormal.z );
108
+
109
+ vec3 sampleDir;
110
+
111
+ // Match LightProbeGrid / WebGL cubemap face conventions (see LightProbeGrid.js SH projection).
112
+ if ( maxComp == absNormal.x ) {
113
+
114
+ if ( vNormal.x > 0.0 ) {
115
+
116
+ sampleDir = normalize( vec3( 1.0, vLocalPosition.y, -vLocalPosition.z ) );
117
+
118
+ } else {
119
+
120
+ sampleDir = normalize( vec3( -1.0, vLocalPosition.y, vLocalPosition.z ) );
121
+
122
+ }
123
+
124
+ } else if ( maxComp == absNormal.y ) {
125
+
126
+ if ( vNormal.y > 0.0 ) {
127
+
128
+ sampleDir = normalize( vec3( vLocalPosition.x, 1.0, -vLocalPosition.z ) );
129
+
130
+ } else {
131
+
132
+ sampleDir = normalize( vec3( vLocalPosition.x, -1.0, vLocalPosition.z ) );
133
+
134
+ }
135
+
136
+ } else {
137
+
138
+ if ( vNormal.z > 0.0 ) {
139
+
140
+ sampleDir = normalize( vec3( vLocalPosition.x, vLocalPosition.y, 1.0 ) );
141
+
142
+ } else {
143
+
144
+ sampleDir = normalize( vec3( vLocalPosition.x, vLocalPosition.y, -1.0 ) );
145
+
146
+ }
147
+
148
+ }
63
149
 
64
- vec3 direction = normalize( vWorldNormal );
65
- vec3 color = textureCube( cubeTexture, direction ).rgb;
150
+ vec3 color = textureCube( cubeTexture, sampleDir ).rgb;
66
151
 
67
152
  gl_FragColor = vec4( color, 1.0 );
68
153
 
@@ -238,7 +238,11 @@ class LightProbeGrid extends Object3D {
238
238
  */
239
239
  getProbePosition( ix, iy, iz, target ) {
240
240
 
241
- const pos = this.position;
241
+ // const pos = this.position;
242
+ // WITH_GENESYS
243
+ const pos = _position;
244
+ this.getWorldPosition( pos );
245
+ // !WITH_GENESYS
242
246
  const res = this.resolution;
243
247
  const w = this.width, h = this.height, d = this.depth;
244
248
 
@@ -257,8 +261,15 @@ class LightProbeGrid extends Object3D {
257
261
  */
258
262
  updateBoundingBox() {
259
263
 
264
+ // WITH_GENESYS
265
+ this.getWorldPosition( _position );
266
+ _size.set( this.width, this.height, this.depth );
267
+ this.boundingBox.setFromCenterAndSize( _position, _size );
268
+ // !WITH_GENESYS
269
+ /*
260
270
  _size.set( this.width, this.height, this.depth );
261
271
  this.boundingBox.setFromCenterAndSize( this.position, _size );
272
+ */
262
273
 
263
274
  }
264
275
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gnsx/three",
3
- "version": "0.184.9",
3
+ "version": "0.184.11",
4
4
  "description": "JavaScript 3D library",
5
5
  "type": "module",
6
6
  "main": "./build/three.cjs",
@@ -44,6 +44,39 @@ class Background extends DataMap {
44
44
 
45
45
  }
46
46
 
47
+ // WITH_GENESYS
48
+ /**
49
+ * Disposes the renderer-owned background skybox mesh (material and geometry)
50
+ * for `scene` and clears cached mesh state.
51
+ *
52
+ * Does not dispose {@link Scene#background}.
53
+ *
54
+ * @param {Scene} scene - The scene.
55
+ */
56
+ disposeScene( scene ) {
57
+
58
+ if ( this.has( scene ) === false ) return;
59
+
60
+ const sceneData = this.get( scene );
61
+
62
+ const backgroundMesh = sceneData.backgroundMesh;
63
+
64
+ if ( backgroundMesh !== undefined ) {
65
+
66
+ this.renderer._objects.disposeObjectResources( backgroundMesh, scene );
67
+
68
+ backgroundMesh.material.dispose();
69
+ backgroundMesh.geometry.dispose();
70
+
71
+ delete sceneData.backgroundMesh;
72
+ delete sceneData.backgroundMeshNode;
73
+ delete sceneData.backgroundCacheKey;
74
+
75
+ }
76
+
77
+ }
78
+ // !WITH_GENESYS
79
+
47
80
  /**
48
81
  * Updates the background for the given scene. Depending on how `Scene.background`
49
82
  * or `Scene.backgroundNode` are configured, this method might configure a simple clear
@@ -207,6 +207,53 @@ class RenderObjects {
207
207
 
208
208
  }
209
209
 
210
+ // WITH_GENESYS
211
+ /**
212
+ * Disposes every {@link RenderObject} associated with `scene`.
213
+ *
214
+ * @param {Scene} scene - The scene being removed.
215
+ */
216
+ disposeSceneResources( scene ) {
217
+
218
+ const renderObjects = Array.from( this._renderObjects );
219
+
220
+ for ( const renderObject of renderObjects ) {
221
+
222
+ if ( renderObject.scene === scene ) {
223
+
224
+ renderObject.dispose();
225
+
226
+ }
227
+
228
+ }
229
+
230
+ }
231
+
232
+ /**
233
+ * Disposes every {@link RenderObject} for `object` drawn with `scene`.
234
+ * Call before disposing a renderer-owned mesh (e.g. background skybox) so WebGPU
235
+ * bindings are released while the renderer keeps running.
236
+ *
237
+ * @param {Object3D} object - The 3D object.
238
+ * @param {Scene} scene - The scene reference used when the object was rendered.
239
+ */
240
+ disposeObjectResources( object, scene ) {
241
+
242
+ const renderObjects = Array.from( this._renderObjects );
243
+
244
+ for ( const renderObject of renderObjects ) {
245
+
246
+ if ( renderObject.object === object && renderObject.scene === scene ) {
247
+
248
+ renderObject.dispose();
249
+
250
+ }
251
+
252
+ }
253
+
254
+ }
255
+ // !WITH_GENESYS
256
+
210
257
  /**
211
258
  * Factory method for creating render objects with the given list of parameters.
212
259
  *
@@ -2556,6 +2556,28 @@ class Renderer {
2556
2556
 
2557
2557
  }
2558
2558
 
2559
+ // WITH_GENESYS
2560
+ /**
2561
+ * Releases renderer resources scoped to `scene` when the scene is removed but
2562
+ * this renderer instance continues to run. Disposes matching {@link RenderObject}s,
2563
+ * the internal background skybox mesh, and cached background/environment nodes.
2564
+ *
2565
+ * Does not call `texture.dispose()` on {@link Scene#background} or
2566
+ * {@link Scene#environment}; the caller remains responsible for those textures.
2567
+ *
2568
+ * @param {Scene} scene - The scene being removed.
2569
+ */
2570
+ disposeSceneResources( scene ) {
2571
+
2572
+ if ( this._initialized !== true ) return;
2573
+
2574
+ this._objects.disposeSceneResources( scene );
2575
+ this._nodes.clearSceneBackgroundAndEnvironmentNodes( scene );
2576
+ this._background.disposeScene( scene );
2577
+
2578
+ }
2579
+ // !WITH_GENESYS
2580
+
2559
2581
  /**
2560
2582
  * Sets the given render target. Calling this method means the renderer does not
2561
2583
  * target the default framebuffer (meaning the canvas) anymore but a custom framebuffer.
@@ -693,6 +693,30 @@ class NodeManager extends DataMap {
693
693
 
694
694
  }
695
695
 
696
+ // WITH_GENESYS
697
+ /**
698
+ * Clears cached background and environment node state for `scene`.
699
+ *
700
+ * Does not call `texture.dispose()` on {@link Scene#background} or
701
+ * {@link Scene#environment}.
702
+ *
703
+ * @param {Scene} scene - The scene.
704
+ */
705
+ clearSceneBackgroundAndEnvironmentNodes( scene ) {
706
+
707
+ if ( this.has( scene ) === false ) return;
708
+
709
+ const sceneData = this.get( scene );
710
+
711
+ delete sceneData.backgroundNode;
712
+ delete sceneData.background;
713
+ delete sceneData.backgroundBlurriness;
714
+ delete sceneData.environmentNode;
715
+ delete sceneData.environment;
716
+
717
+ }
718
+ // !WITH_GENESYS
719
+
696
720
  /**
697
721
  * This method is part of the caching of nodes which are used to represents the
698
722
  * scene's background, fog or environment.