Skip to content

Commit a573987

Browse files
authored
Dispose of spherical harmonics arrays and packedArray in PackedSplats (#239)
* Dispose of spherical harmonics arrays and packedArray in PackedSplats * Don't dispose SplatMesh instances in interactivity example
1 parent 2dcc03d commit a573987

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

examples/interactivity/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ <h2>Food scans by <a href="https://x.com/tipatat" target="_blank">Tipatat</a></h
176176
if (food) food.opacity = 1 - easeInOutSine(fadeOutTime / TRANSITION_LENGTH);
177177
} else {
178178
// Fade out finished
179-
if (food) food.dispose();
180179
fadeOutTime = null;
181180
// Fade in next food
182181
fadeInTime = 0;

src/PackedSplats.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
import {
1313
DynoProgram,
1414
DynoProgramTemplate,
15+
DynoSampler2D,
16+
type DynoType,
1517
DynoUniform,
1618
DynoVec2,
1719
DynoVec4,
@@ -241,12 +243,32 @@ export class PackedSplats {
241243
dispose() {
242244
if (this.target) {
243245
this.target.dispose();
246+
this.target.texture.source.data = null;
244247
this.target = null;
245248
}
246249
if (this.source) {
247250
this.source.dispose();
251+
this.source.source.data = null;
248252
this.source = null;
249253
}
254+
255+
this.packedArray = null;
256+
257+
for (const key in this.extra) {
258+
const dyno = this.extra[key] as DynoUniform<
259+
DynoType,
260+
string,
261+
THREE.Texture
262+
>;
263+
if (dyno instanceof DynoUniform) {
264+
const texture = dyno.value;
265+
if (texture?.isTexture) {
266+
texture.dispose();
267+
texture.source.data = null;
268+
}
269+
}
270+
}
271+
this.extra = {};
250272
}
251273

252274
// Ensures that this.packedArray can fit numSplats Gsplats. If it's too small,
@@ -432,7 +454,9 @@ export class PackedSplats {
432454
if (this.target && (maxSplats ?? 1) <= this.maxSplats) {
433455
return false;
434456
}
435-
this.dispose();
457+
if (this.target) {
458+
this.target.dispose();
459+
}
436460

437461
const textureSize = getTextureSize(maxSplats ?? 1);
438462
const { width, height, depth } = textureSize;
@@ -687,7 +711,7 @@ export class PackedSplats {
687711
static programTemplate: DynoProgramTemplate | null = null;
688712

689713
// Cache for GsplatGenerator programs
690-
static generatorProgram = new Map<GsplatGenerator, DynoProgram>();
714+
static generatorProgram = new WeakMap<GsplatGenerator, DynoProgram>();
691715

692716
// Static full-screen quad for pseudo-compute shader rendering
693717
static fullScreenQuad = new FullScreenQuad(

src/dyno/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class DynoProgramTemplate {
9898
}
9999
}
100100

101-
const programMaterial = new Map<DynoProgram, THREE.RawShaderMaterial>();
101+
const programMaterial = new WeakMap<DynoProgram, THREE.RawShaderMaterial>();
102102

103103
function getMaterial(program: DynoProgram): THREE.RawShaderMaterial {
104104
let material = programMaterial.get(program);

0 commit comments

Comments
 (0)