Skip to content

Official Codebase of 3DCodeVerse

Python

scene lib: rock, sand, stream and ocean dispose by D97's one rule — attachDisposal(snapshotResources(...))

gaoypeng5ebd07a74260
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/ocean.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/ocean.js
index 6e48bd5e..35558a6c 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/ocean.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/ocean.js
@@ -14,6 +14,7 @@
* fall back to the reflection image. No screen-space refraction or fluid solve.
*/
import * as THREE from 'three';
+import { attachDisposal, snapshotResources } from './lifecycle.js';
import { mulberry32 } from './noise.js';
import { GLSL_UTIL, planarCapture } from './shader.js';
import { Reflector } from 'three/addons/objects/Reflector.js';
@@ -550,15 +551,9 @@ export function makeOceanSurface(opts = {}) {
};
group.userData.sampleHeight = (x, z, t = time) =>
group.userData.sampleSurface(x, z, t).position.y;
- const ownedMaterial = sea.material;
- const ownedReflection = sea.getRenderTarget();
- group.userData.dispose = () => {
- if (disposed) return;
- disposed = true;
- geometry.dispose();
- ownedMaterial.dispose();
- ownedReflection.dispose();
- };
+ const owned = snapshotResources(group).add(sea.getRenderTarget());
+ owned.add({ dispose() { disposed = true; } });
+ attachDisposal(group, owned);
group.userData.surface = sea;
group.userData.description =
'32-band dispersive Gerstner ocean; mean-plane reflection; no fluid interaction or overturning breakers';
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/rock.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/rock.js
index 03e0d735..02fbc6cb 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/rock.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/rock.js
@@ -4,6 +4,7 @@
*/
import * as THREE from 'three';
import { ConvexGeometry } from 'three/addons/geometries/ConvexGeometry.js';
+import { attachDisposal, snapshotResources } from './lifecycle.js';
import { fbm3, mulberry32 } from './noise.js';
import { patchStandard } from './shader.js';
@@ -307,12 +308,7 @@ export function makeRock(opts = {}) {
rock.name = opts.name ?? `${type[0].toUpperCase() + type.slice(1)}Rock`;
rock.castShadow = true; rock.receiveShadow = true;
rock.userData.update = () => {};
- let disposed = false;
- rock.userData.dispose = () => {
- if (disposed) return;
- disposed = true;
- geometry.dispose(); material.dispose();
- };
+ attachDisposal(rock, snapshotResources(rock));
rock.userData.seed = opts.seed ?? 31;
rock.userData.rockType = type;
return rock;
@@ -324,7 +320,6 @@ export function makeRock(opts = {}) {
*/
export function makeRockField(opts = {}) {
const rand = mulberry32(opts.seed ?? 31), group = new THREE.Group();
- const owned = [];
group.name = opts.name ?? 'RockField';
const count = clamp(Math.round(opts.count ?? 24), 0, 160), radius = Math.max(0.1, opts.radius ?? 9);
for (let i = 0; i < count; i++) {
@@ -334,14 +329,8 @@ export function makeRockField(opts = {}) {
const size = Array.isArray(opts.size) ? opts.size.map((v) => v * factor) : (opts.size ?? 0.6) * factor;
const rock = makeRock({ ...opts, seed, size, detail: opts.detail ?? 3, name: `${group.name}_${i + 1}` });
rock.position.set(x, opts.heightAt?.(x, z) ?? 0, z); rock.rotation.y = rand() * Math.PI * 2;
- group.add(rock); owned.push(rock);
+ group.add(rock);
}
group.userData.update = () => {};
- let disposed = false;
- group.userData.dispose = () => {
- if (disposed) return;
- disposed = true;
- owned.forEach((rock) => rock.userData.dispose());
- };
- return group;
+ return attachDisposal(group, snapshotResources(group));
}
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/sand.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/sand.js
index 9a51bdf5..e8330bb7 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/sand.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/sand.js
@@ -3,6 +3,7 @@
* footsteps can be seated without a second, subtly different height function.
*/
import * as THREE from 'three';
+import { attachDisposal, snapshotResources } from './lifecycle.js';
import { fbm2, mulberry32 } from './noise.js';
import { patchStandard } from './shader.js';
@@ -156,16 +157,9 @@ export function makeSandTerrain(opts = {}) {
};
group.userData.sampleHeight = sampleHeight;
group.userData.update = update;
- // Assets are explicitly allowed to be parented to this terrain. Snapshot
- // only the resources constructed here, so caller-owned attachments survive.
- const owned = [geometry, surface.material, grains?.geometry, grains?.material].filter(Boolean);
- let disposed = false;
- group.userData.dispose = () => {
- if (disposed) return;
- disposed = true;
- owned.forEach((resource) => resource.dispose());
- };
group.userData.seed = seed;
update(0);
- return group;
+ // Assets may be parented to this terrain later; the construction-time
+ // snapshot keeps those caller attachments borrowed.
+ return attachDisposal(group, snapshotResources(group));
}
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/stream.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/stream.js
index 2ffddc54..69e5763d 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/stream.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/stream.js
@@ -12,6 +12,7 @@
*/
import * as THREE from 'three';
import { Reflector } from 'three/addons/objects/Reflector.js';
+import { attachDisposal, snapshotResources } from './lifecycle.js';
import { mulberry32 } from './noise.js';
import { GLSL_UTIL, planarCapture } from './shader.js';
@@ -519,7 +520,8 @@ export function makeStream(opts = {}) {
reflection.onBeforeRender(renderer, scene, camera);
uniforms.streamReflectionMatrix.value
.copy(reflection.material.uniforms.textureMatrix.value).multiply(toRigid);
- }, { mirror: reflection, frame: group, normal: gradeNormal, point: reflection.position });
+ }, { mirror: reflection, frame: group, normal: gradeNormal, point: reflection.position,
+ skip: () => disposed });
}
if (opts.bed !== false) {
const bedMaterial = new THREE.MeshStandardMaterial({
@@ -707,29 +709,12 @@ export function makeStream(opts = {}) {
width: widthAt(u),
};
};
- // Snapshot owned resources now. Callers may add children or remove meshes
- // later; neither action changes which allocations this constructor owns.
- const ownedResources = new Set();
- const ownedInstances = [];
- group.traverse((object) => {
- if (object.isInstancedMesh) ownedInstances.push(object);
- for (const resource of [
- object.geometry,
- ...(Array.isArray(object.material) ? object.material : [object.material]),
- ]) {
- if (resource) ownedResources.add(resource);
- }
- });
- group.userData.dispose = () => {
- if (disposed) return;
- disposed = true;
- for (const resource of ownedResources) resource.dispose();
- for (const object of ownedInstances) object.dispose();
- if (reflection) {
- reflection.geometry.dispose();
- reflection.dispose();
- }
- };
+ // The helper Reflector never enters the graph: its geometry, material and
+ // render target are owned explicitly.
+ const owned = snapshotResources(group);
+ if (reflection) owned.add(reflection.geometry).add(reflection);
+ owned.add({ dispose() { disposed = true; } });
+ attachDisposal(group, owned);
group.userData.surface = surface;
group.userData.length = length;
group.userData.description =