@@ -43,6 +43,7 @@ import { Engine } from '@etherealengine/ecs/src/Engine'
4343import { Entity } from '@etherealengine/ecs/src/Entity'
4444import { createEntity , removeEntity } from '@etherealengine/ecs/src/EntityFunctions'
4545
46+ import { getState } from '@etherealengine/hyperflux'
4647import { CameraComponent } from '../../camera/components/CameraComponent'
4748import { NameComponent } from '../../common/NameComponent'
4849import { Vector3_Zero } from '../../common/constants/MathConstants'
@@ -51,6 +52,7 @@ import { addObjectToGroup } from '../../renderer/components/GroupComponent'
5152import { VisibleComponent } from '../../renderer/components/VisibleComponent'
5253import { EntityTreeComponent } from '../../transform/components/EntityTree'
5354import { TransformComponent } from '../../transform/components/TransformComponent'
55+ import { RendererState } from '../RendererState'
5456import Frustum from './Frustum'
5557import Shader from './Shader'
5658
@@ -319,7 +321,8 @@ export class CSM {
319321 if ( this . sourceLight ) this . lightDirection . subVectors ( this . sourceLight . target . position , this . sourceLight . position )
320322 if ( this . needsUpdate ) {
321323 this . injectInclude ( )
322- this . updateFrustums ( )
324+ // Only update uniforms if WebGLRendererSystem isn't already updating them every frame
325+ this . updateFrustums ( ! getState ( RendererState ) . updateCSMFrustums )
323326 for ( const light of this . lights ) {
324327 light . shadow . map ?. dispose ( )
325328 light . shadow . map = null as any
@@ -439,7 +442,8 @@ export class CSM {
439442 updateUniforms ( ) : void {
440443 const camera = getComponent ( Engine . instance . cameraEntity , CameraComponent )
441444 const far = Math . min ( camera . far , this . maxFar )
442- this . shaders . forEach ( function ( shader : ShaderType , material : Material ) {
445+
446+ for ( const [ material , shader ] of this . shaders . entries ( ) ) {
443447 const camera = getComponent ( Engine . instance . cameraEntity , CameraComponent )
444448
445449 if ( shader !== null ) {
@@ -456,7 +460,7 @@ export class CSM {
456460 material . defines ! . CSM_FADE = ''
457461 material . needsUpdate = true
458462 }
459- } , this )
463+ }
460464 }
461465
462466 getExtendedBreaks ( target : Vector2 [ ] ) : void {
@@ -474,11 +478,11 @@ export class CSM {
474478 }
475479 }
476480
477- updateFrustums ( ) : void {
481+ updateFrustums ( updateUniforms = true ) : void {
478482 this . getBreaks ( )
479483 this . initCascades ( )
480484 this . updateShadowBounds ( )
481- this . updateUniforms ( )
485+ if ( updateUniforms ) this . updateUniforms ( )
482486 }
483487
484488 remove ( ) : void {
0 commit comments