Skip to content

Commit 3e9ee5f

Browse files
authored
Ensure only one update timeout is scheduled at a time and call gl.flush() to encourage eager execution (#156)
1 parent 882def6 commit 3e9ee5f

1 file changed

Lines changed: 33 additions & 16 deletions

File tree

src/SparkRenderer.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,11 @@ export class SparkRenderer extends THREE.Mesh {
267267
viewpoint: SparkViewpoint;
268268

269269
// Holds data needed to perform a scheduled Gsplat update.
270-
private pendingUpdate: {
271-
scene: THREE.Scene;
272-
originToWorld: THREE.Matrix4;
273-
} | null = null;
270+
private pendingUpdate = {
271+
scene: null as THREE.Scene | null,
272+
originToWorld: new THREE.Matrix4(),
273+
timeoutId: -1,
274+
};
274275

275276
// Internal SparkViewpoint used for environment map rendering.
276277
private envViewpoint: SparkViewpoint | null = null;
@@ -668,23 +669,39 @@ export class SparkRenderer extends THREE.Mesh {
668669
}: { scene: THREE.Scene; viewToWorld?: THREE.Matrix4 }) {
669670
// Compute the transform for the SparkRenderer to use as origin
670671
// for Gsplat generation and accumulation.
671-
const originToWorld = this.matrixWorld.clone();
672+
const originToWorld = this.matrixWorld;
673+
672674
// Either do the update now, or in the next "tick" depending on preUpdate
673675
if (this.preUpdate) {
674-
this.updateInternal({ scene, originToWorld, viewToWorld });
676+
this.updateInternal({
677+
scene,
678+
originToWorld: originToWorld.clone(),
679+
viewToWorld,
680+
});
675681
} else {
676682
// Pass the update parameters to be performed on the next tick
677-
this.pendingUpdate = {
678-
scene,
679-
originToWorld,
680-
};
681-
setTimeout(() => {
682-
if (this.pendingUpdate) {
683+
this.pendingUpdate.scene = scene;
684+
this.pendingUpdate.originToWorld.copy(originToWorld);
685+
686+
// Schedule a timeout if there isn't one already
687+
if (this.pendingUpdate.timeoutId === -1) {
688+
this.pendingUpdate.timeoutId = setTimeout(() => {
683689
const { scene, originToWorld } = this.pendingUpdate;
684-
this.pendingUpdate = null;
685-
this.updateInternal({ scene, originToWorld, viewToWorld });
686-
}
687-
}, 1);
690+
this.pendingUpdate.scene = null;
691+
this.pendingUpdate.timeoutId = -1;
692+
const updated = this.updateInternal({
693+
scene: scene as THREE.Scene,
694+
originToWorld,
695+
viewToWorld,
696+
});
697+
698+
if (updated) {
699+
// Flush to encourage eager execution
700+
const gl = this.renderer.getContext() as WebGL2RenderingContext;
701+
gl.flush();
702+
}
703+
}, 1);
704+
}
688705
}
689706
}
690707

0 commit comments

Comments
 (0)