Skip to content

Commit 42d1477

Browse files
authored
Fix visible flag bug in SplatGenerator/SplatMesh. (#101)
1 parent 6c0810f commit 42d1477

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/SparkRenderer.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,8 @@ export class SparkRenderer extends THREE.Mesh {
603603
}, new Map<SplatGenerator, GeneratorMapping>());
604604

605605
// Traverse visible scene to find all SplatGenerators and global SplatEdits
606-
const { generators, globalEdits } = this.compileScene(scene);
606+
const { generators, visibleGenerators, globalEdits } =
607+
this.compileScene(scene);
607608

608609
// Let all SplatGenerators run their frameUpdate() method
609610
for (const object of generators) {
@@ -616,11 +617,14 @@ export class SparkRenderer extends THREE.Mesh {
616617
});
617618
}
618619

620+
const visibleGenHash = new Set(visibleGenerators.map((g) => g.uuid));
621+
619622
// Make sure we have new version numbers for any objects with either
620623
// generator or numSplats that have changed since the last frame.
621624
for (const object of generators) {
622625
const current = activeMapping.get(object);
623-
const numSplats = object.generator ? object.numSplats : 0;
626+
const isVisible = object.generator && visibleGenHash.has(object.uuid);
627+
const numSplats = isVisible ? object.numSplats : 0;
624628
if (
625629
object.generator !== current?.generator ||
626630
numSplats !== current?.count
@@ -665,7 +669,7 @@ export class SparkRenderer extends THREE.Mesh {
665669
// Compute an ordering of the generators with the rough goal
666670
// of keeping unchanging generators near the front to minimize
667671
// the number of Gsplats that need to be regenerated.
668-
const sorted = generators
672+
const sorted = visibleGenerators
669673
.map((g, gIndex): [number, number, SplatGenerator] => {
670674
const lastGen = activeMapping.get(g);
671675
// If no previous generator, sort by absolute version, which will
@@ -748,17 +752,27 @@ export class SparkRenderer extends THREE.Mesh {
748752

749753
private compileScene(scene: THREE.Scene): {
750754
generators: SplatGenerator[];
755+
visibleGenerators: SplatGenerator[];
751756
globalEdits: SplatEdit[];
752757
} {
753758
// Take a snapshot of the SplatGenerators and SplatEdits in the scene
754759
// to be used to run an update.
755760
const generators: SplatGenerator[] = [];
761+
// Collect all SplatGenerators, even if not visible, because we want to
762+
// be able to call their update functions every frame.
756763
scene.traverse((node) => {
757764
if (node instanceof SplatGenerator) {
758765
generators.push(node);
759766
}
760767
});
761768

769+
const visibleGenerators: SplatGenerator[] = [];
770+
scene.traverseVisible((node) => {
771+
if (node instanceof SplatGenerator) {
772+
visibleGenerators.push(node);
773+
}
774+
});
775+
762776
const globalEdits = new Set<SplatEdit>();
763777
scene.traverseVisible((node) => {
764778
if (node instanceof SplatEdit) {
@@ -772,7 +786,11 @@ export class SparkRenderer extends THREE.Mesh {
772786
}
773787
}
774788
});
775-
return { generators, globalEdits: Array.from(globalEdits) };
789+
return {
790+
generators,
791+
visibleGenerators,
792+
globalEdits: Array.from(globalEdits),
793+
};
776794
}
777795

778796
// Renders out the scene to an environment map that can be used for

0 commit comments

Comments
 (0)