@@ -6,6 +6,29 @@ import { uniformArray } from './UniformArrayNode.js';
66import { builtin } from './BuiltinNode.js' ;
77import { screenSize } from '../display/ScreenNode.js' ;
88
9+ // Cache node uniforms
10+
11+ let _cameraProjectionMatrixBase = null ;
12+ let _cameraProjectionMatrixArray = null ;
13+
14+ let _cameraProjectionMatrixInverseBase = null ;
15+ let _cameraProjectionMatrixInverseArray = null ;
16+
17+ let _cameraViewMatrixBase = null ;
18+ let _cameraViewMatrixArray = null ;
19+
20+ let _cameraWorldMatrixBase = null ;
21+ let _cameraWorldMatrixArray = null ;
22+
23+ let _cameraNormalMatrixBase = null ;
24+ let _cameraNormalMatrixArray = null ;
25+
26+ let _cameraPositionBase = null ;
27+ let _cameraPositionArray = null ;
28+
29+ let _cameraViewportBase = null ;
30+ let _cameraViewportArray = null ;
31+
932/**
1033 * TSL object that represents the current `index` value of the camera if used ArrayCamera.
1134 *
@@ -50,13 +73,27 @@ export const cameraProjectionMatrix = /*@__PURE__*/ ( Fn( ( { camera } ) => {
5073
5174 }
5275
53- const cameraProjectionMatrices = uniformArray ( matrices ) . setGroup ( renderGroup ) . setName ( 'cameraProjectionMatrices' ) ;
76+ if ( _cameraProjectionMatrixArray === null ) {
77+
78+ _cameraProjectionMatrixArray = uniformArray ( matrices ) . setGroup ( renderGroup ) . setName ( 'cameraProjectionMatrices' ) ;
5479
55- cameraProjectionMatrix = cameraProjectionMatrices . element ( camera . isMultiViewCamera ? builtin ( 'gl_ViewID_OVR' ) : cameraIndex ) . toConst ( 'cameraProjectionMatrix' ) ;
80+ } else {
81+
82+ _cameraProjectionMatrixArray . array = matrices ;
83+
84+ }
85+
86+ cameraProjectionMatrix = _cameraProjectionMatrixArray . element ( camera . isMultiViewCamera ? builtin ( 'gl_ViewID_OVR' ) : cameraIndex ) . toConst ( 'cameraProjectionMatrix' ) ;
5687
5788 } else {
5889
59- cameraProjectionMatrix = uniform ( 'mat4' ) . setName ( 'cameraProjectionMatrix' ) . setGroup ( renderGroup ) . onRenderUpdate ( ( { camera } ) => camera . projectionMatrix ) ;
90+ if ( _cameraProjectionMatrixBase === null ) {
91+
92+ _cameraProjectionMatrixBase = uniform ( camera . projectionMatrix ) . setName ( 'cameraProjectionMatrix' ) . setGroup ( renderGroup ) . onRenderUpdate ( ( { camera } ) => camera . projectionMatrix ) ;
93+
94+ }
95+
96+ cameraProjectionMatrix = _cameraProjectionMatrixBase ;
6097
6198 }
6299
@@ -84,13 +121,27 @@ export const cameraProjectionMatrixInverse = /*@__PURE__*/ ( Fn( ( { camera } )
84121
85122 }
86123
87- const cameraProjectionMatricesInverse = uniformArray ( matrices ) . setGroup ( renderGroup ) . setName ( 'cameraProjectionMatricesInverse' ) ;
124+ if ( _cameraProjectionMatrixInverseArray === null ) {
125+
126+ _cameraProjectionMatrixInverseArray = uniformArray ( matrices ) . setGroup ( renderGroup ) . setName ( 'cameraProjectionMatricesInverse' ) ;
88127
89- cameraProjectionMatrixInverse = cameraProjectionMatricesInverse . element ( camera . isMultiViewCamera ? builtin ( 'gl_ViewID_OVR' ) : cameraIndex ) . toConst ( 'cameraProjectionMatrixInverse' ) ;
128+ } else {
129+
130+ _cameraProjectionMatrixInverseArray . array = matrices ;
131+
132+ }
133+
134+ cameraProjectionMatrixInverse = _cameraProjectionMatrixInverseArray . element ( camera . isMultiViewCamera ? builtin ( 'gl_ViewID_OVR' ) : cameraIndex ) . toConst ( 'cameraProjectionMatrixInverse' ) ;
90135
91136 } else {
92137
93- cameraProjectionMatrixInverse = uniform ( 'mat4' ) . setName ( 'cameraProjectionMatrixInverse' ) . setGroup ( renderGroup ) . onRenderUpdate ( ( { camera } ) => camera . projectionMatrixInverse ) ;
138+ if ( _cameraProjectionMatrixInverseBase === null ) {
139+
140+ _cameraProjectionMatrixInverseBase = uniform ( camera . projectionMatrixInverse ) . setName ( 'cameraProjectionMatrixInverse' ) . setGroup ( renderGroup ) . onRenderUpdate ( ( { camera } ) => camera . projectionMatrixInverse ) ;
141+
142+ }
143+
144+ cameraProjectionMatrixInverse = _cameraProjectionMatrixInverseBase ;
94145
95146 }
96147
@@ -118,13 +169,27 @@ export const cameraViewMatrix = /*@__PURE__*/ ( Fn( ( { camera } ) => {
118169
119170 }
120171
121- const cameraViewMatrices = uniformArray ( matrices ) . setGroup ( renderGroup ) . setName ( 'cameraViewMatrices' ) ;
172+ if ( _cameraViewMatrixArray === null ) {
173+
174+ _cameraViewMatrixArray = uniformArray ( matrices ) . setGroup ( renderGroup ) . setName ( 'cameraViewMatrices' ) ;
122175
123- cameraViewMatrix = cameraViewMatrices . element ( camera . isMultiViewCamera ? builtin ( 'gl_ViewID_OVR' ) : cameraIndex ) . toConst ( 'cameraViewMatrix' ) ;
176+ } else {
177+
178+ _cameraViewMatrixArray . array = matrices ;
179+
180+ }
181+
182+ cameraViewMatrix = _cameraViewMatrixArray . element ( camera . isMultiViewCamera ? builtin ( 'gl_ViewID_OVR' ) : cameraIndex ) . toConst ( 'cameraViewMatrix' ) ;
124183
125184 } else {
126185
127- cameraViewMatrix = uniform ( 'mat4' ) . setName ( 'cameraViewMatrix' ) . setGroup ( renderGroup ) . onRenderUpdate ( ( { camera } ) => camera . matrixWorldInverse ) ;
186+ if ( _cameraViewMatrixBase === null ) {
187+
188+ _cameraViewMatrixBase = uniform ( camera . matrixWorldInverse ) . setName ( 'cameraViewMatrix' ) . setGroup ( renderGroup ) . onRenderUpdate ( ( { camera } ) => camera . matrixWorldInverse ) ;
189+
190+ }
191+
192+ cameraViewMatrix = _cameraViewMatrixBase ;
128193
129194 }
130195
@@ -152,13 +217,27 @@ export const cameraWorldMatrix = /*@__PURE__*/ ( Fn( ( { camera } ) => {
152217
153218 }
154219
155- const cameraWorldMatrices = uniformArray ( matrices ) . setGroup ( renderGroup ) . setName ( 'cameraWorldMatrices' ) ;
220+ if ( _cameraWorldMatrixArray === null ) {
221+
222+ _cameraWorldMatrixArray = uniformArray ( matrices ) . setGroup ( renderGroup ) . setName ( 'cameraWorldMatrices' ) ;
156223
157- cameraWorldMatrix = cameraWorldMatrices . element ( camera . isMultiViewCamera ? builtin ( 'gl_ViewID_OVR' ) : cameraIndex ) . toConst ( 'cameraWorldMatrix' ) ;
224+ } else {
225+
226+ _cameraWorldMatrixArray . array = matrices ;
227+
228+ }
229+
230+ cameraWorldMatrix = _cameraWorldMatrixArray . element ( camera . isMultiViewCamera ? builtin ( 'gl_ViewID_OVR' ) : cameraIndex ) . toConst ( 'cameraWorldMatrix' ) ;
158231
159232 } else {
160233
161- cameraWorldMatrix = uniform ( 'mat4' ) . setName ( 'cameraWorldMatrix' ) . setGroup ( renderGroup ) . onRenderUpdate ( ( { camera } ) => camera . matrixWorld ) ;
234+ if ( _cameraWorldMatrixBase === null ) {
235+
236+ _cameraWorldMatrixBase = uniform ( camera . matrixWorld ) . setName ( 'cameraWorldMatrix' ) . setGroup ( renderGroup ) . onRenderUpdate ( ( { camera } ) => camera . matrixWorld ) ;
237+
238+ }
239+
240+ cameraWorldMatrix = _cameraWorldMatrixBase ;
162241
163242 }
164243
@@ -186,13 +265,27 @@ export const cameraNormalMatrix = /*@__PURE__*/ ( Fn( ( { camera } ) => {
186265
187266 }
188267
189- const cameraNormalMatrices = uniformArray ( matrices ) . setGroup ( renderGroup ) . setName ( 'cameraNormalMatrices' ) ;
268+ if ( _cameraNormalMatrixArray === null ) {
269+
270+ _cameraNormalMatrixArray = uniformArray ( matrices ) . setGroup ( renderGroup ) . setName ( 'cameraNormalMatrices' ) ;
190271
191- cameraNormalMatrix = cameraNormalMatrices . element ( camera . isMultiViewCamera ? builtin ( 'gl_ViewID_OVR' ) : cameraIndex ) . toConst ( 'cameraNormalMatrix' ) ;
272+ } else {
273+
274+ _cameraNormalMatrixArray . array = matrices ;
275+
276+ }
277+
278+ cameraNormalMatrix = _cameraNormalMatrixArray . element ( camera . isMultiViewCamera ? builtin ( 'gl_ViewID_OVR' ) : cameraIndex ) . toConst ( 'cameraNormalMatrix' ) ;
192279
193280 } else {
194281
195- cameraNormalMatrix = uniform ( 'mat3' ) . setName ( 'cameraNormalMatrix' ) . setGroup ( renderGroup ) . onRenderUpdate ( ( { camera } ) => camera . normalMatrix ) ;
282+ if ( _cameraNormalMatrixBase === null ) {
283+
284+ _cameraNormalMatrixBase = uniform ( camera . normalMatrix ) . setName ( 'cameraNormalMatrix' ) . setGroup ( renderGroup ) . onRenderUpdate ( ( { camera } ) => camera . normalMatrix ) ;
285+
286+ }
287+
288+ cameraNormalMatrix = _cameraNormalMatrixBase ;
196289
197290 }
198291
@@ -220,24 +313,38 @@ export const cameraPosition = /*@__PURE__*/ ( Fn( ( { camera } ) => {
220313
221314 }
222315
223- const cameraPositions = uniformArray ( positions ) . setGroup ( renderGroup ) . setName ( 'cameraPositions' ) . onRenderUpdate ( ( { camera } , self ) => {
316+ if ( _cameraPositionArray === null ) {
317+
318+ _cameraPositionArray = uniformArray ( positions ) . setGroup ( renderGroup ) . setName ( 'cameraPositions' ) . onRenderUpdate ( ( { camera } , self ) => {
224319
225- const subCameras = camera . cameras ;
226- const array = self . array ;
320+ const subCameras = camera . cameras ;
321+ const array = self . array ;
227322
228- for ( let i = 0 , l = subCameras . length ; i < l ; i ++ ) {
323+ for ( let i = 0 , l = subCameras . length ; i < l ; i ++ ) {
229324
230- array [ i ] . setFromMatrixPosition ( subCameras [ i ] . matrixWorld ) ;
325+ array [ i ] . setFromMatrixPosition ( subCameras [ i ] . matrixWorld ) ;
231326
232- }
327+ }
233328
234- } ) ;
329+ } ) ;
235330
236- cameraPosition = cameraPositions . element ( camera . isMultiViewCamera ? builtin ( 'gl_ViewID_OVR' ) : cameraIndex ) . toConst ( 'cameraPosition' ) ;
331+ } else {
332+
333+ _cameraPositionArray . array = positions ;
334+
335+ }
336+
337+ cameraPosition = _cameraPositionArray . element ( camera . isMultiViewCamera ? builtin ( 'gl_ViewID_OVR' ) : cameraIndex ) . toConst ( 'cameraPosition' ) ;
237338
238339 } else {
239340
240- cameraPosition = uniform ( new Vector3 ( ) ) . setName ( 'cameraPosition' ) . setGroup ( renderGroup ) . onRenderUpdate ( ( { camera } , self ) => self . value . setFromMatrixPosition ( camera . matrixWorld ) ) ;
341+ if ( _cameraPositionBase === null ) {
342+
343+ _cameraPositionBase = uniform ( new Vector3 ( ) ) . setName ( 'cameraPosition' ) . setGroup ( renderGroup ) . onRenderUpdate ( ( { camera } , self ) => self . value . setFromMatrixPosition ( camera . matrixWorld ) ) ;
344+
345+ }
346+
347+ cameraPosition = _cameraPositionBase ;
241348
242349 }
243350
@@ -266,14 +373,28 @@ export const cameraViewport = /*@__PURE__*/ ( Fn( ( { camera } ) => {
266373
267374 }
268375
269- const cameraViewports = uniformArray ( viewports , 'vec4' ) . setGroup ( renderGroup ) . setName ( 'cameraViewports' ) ;
376+ if ( _cameraViewportArray === null ) {
377+
378+ _cameraViewportArray = uniformArray ( viewports , 'vec4' ) . setGroup ( renderGroup ) . setName ( 'cameraViewports' ) ;
270379
271- cameraViewport = cameraViewports . element ( cameraIndex ) . toConst ( 'cameraViewport' ) ;
380+ } else {
381+
382+ _cameraViewportArray . array = viewports ;
383+
384+ }
385+
386+ cameraViewport = _cameraViewportArray . element ( cameraIndex ) . toConst ( 'cameraViewport' ) ;
272387
273388 } else {
274389
275- // Fallback for single camera
276- cameraViewport = vec4 ( 0 , 0 , screenSize . x , screenSize . y ) . toConst ( 'cameraViewport' ) ;
390+ if ( _cameraViewportBase === null ) {
391+
392+ // Fallback for single camera
393+ _cameraViewportBase = vec4 ( 0 , 0 , screenSize . x , screenSize . y ) . toConst ( 'cameraViewport' ) ;
394+
395+ }
396+
397+ cameraViewport = _cameraViewportBase ;
277398
278399 }
279400
0 commit comments