Skip to content

Official Codebase of 3DCodeVerse

Python

scene prompts: the cookbook, contract and scene prompts call the lib where they used to contradict the catalog

gaoypeng58d5f14f0971
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/zones/meadow.js b/harness/codeverse3d/languages/scene_threejs/starter/src/zones/meadow.js
index 16cf1f1a..88f7e1bd 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/zones/meadow.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/zones/meadow.js
@@ -5,9 +5,7 @@
import * as THREE from 'three';
import { pineTreeParts } from '../assets/pine_tree.js';
import { buildWindmill } from '../assets/windmill.js';
-
-// deterministic PRNG: same layout every run (the harness diffs renders between rounds)
-function rng(seed) { let s = seed >>> 0; return () => ((s = (s * 1664525 + 1013904223) >>> 0) / 4294967296); }
+import { mulberry32 } from '../lib/noise.js';
export function build(ctx) {
const { heightAt } = ctx;
@@ -15,7 +13,7 @@ export function build(ctx) {
zone.name = 'Meadow';
// --- instanced pine trees (one InstancedMesh per part; shared matrices)
- const rand = rng(7);
+ const rand = mulberry32(7); // seeded: the same layout every run (the harness diffs rounds)
const N = 140;
const mats = [];
for (let i = 0; i < N; i++) {
diff --git a/harness/codeverse3d/prompts/scene_threejs/contract.md b/harness/codeverse3d/prompts/scene_threejs/contract.md
index 5cee415f..722b4072 100644
--- a/harness/codeverse3d/prompts/scene_threejs/contract.md
+++ b/harness/codeverse3d/prompts/scene_threejs/contract.md
@@ -79,7 +79,7 @@ bloom: author them at peak 1.5–4, not 20. `--no-post` / `C3D_POST=0` turns it
## Forbidden
`document.*` / `window.*` except `window.innerWidth` — never create canvases or DOM;
external network requests or arbitrary `fetch`/XHR (use `loaders.gltf` for assets
-and the local FileLoader pattern above for shader text); `Math.random()` (use a seeded hash `rand(i)`);
+and the local FileLoader pattern above for shader text); `Math.random()` (use `mulberry32(seed)` from `lib/noise.js`);
`Date.now()`/`performance.now()` (use `t`); `requestAnimationFrame`; creating a renderer;
changing renderer settings; environment sniffing (`navigator.userAgent`, headless checks).
@@ -122,8 +122,9 @@ export function buildEnv({ THREE, scene }) {
`src/zones/grove.js`
```js
import * as THREE from 'three';
-const rand = (i) => { const s = Math.sin(i * 12.9898 + 78.233) * 43758.5453; return s - Math.floor(s); };
+import { mulberry32 } from '../lib/noise.js';
export function build({ THREE, heightAt }) {
+ const rand = mulberry32(7); // seeded: the same grove every build
const zone = new THREE.Group(); zone.name = 'Grove';
const N = 40;
const trunk = new THREE.InstancedMesh(new THREE.CylinderGeometry(0.12, 0.18, 2.4, 8),
@@ -132,8 +133,8 @@ export function build({ THREE, heightAt }) {
new THREE.MeshStandardMaterial({ color: 0x2f6b2a, roughness: 0.8 }), N);
const m = new THREE.Matrix4(), q = new THREE.Quaternion(), s = new THREE.Vector3(), p = new THREE.Vector3();
for (let i = 0; i < N; i++) {
- const x = -18 + 36 * rand(i), z = -18 + 36 * rand(i + 100), y = heightAt(x, z);
- const k = 0.8 + 0.5 * rand(i + 200);
+ const x = -18 + 36 * rand(), z = -18 + 36 * rand(), y = heightAt(x, z);
+ const k = 0.8 + 0.5 * rand();
p.set(x, y + 1.2 * k, z); s.set(k, k, k); m.compose(p, q, s); trunk.setMatrixAt(i, m);
p.set(x, y + 2.4 * k + 1.4 * k, z); m.compose(p, q, s); crown.setMatrixAt(i, m);
}
diff --git a/harness/codeverse3d/prompts/scene_threejs/cookbook.md b/harness/codeverse3d/prompts/scene_threejs/cookbook.md
index 9a02cc85..06c124e2 100644
--- a/harness/codeverse3d/prompts/scene_threejs/cookbook.md
+++ b/harness/codeverse3d/prompts/scene_threejs/cookbook.md
@@ -22,8 +22,7 @@ five repeatable defects, one chapter each:
```js
const BOUNDS = { min: [-50, 0, -50], max: [50, 30, 50] }; // export const in real env.js
-const rand = (i) => { const s = Math.sin(i * 12.9898 + 78.233) * 43758.5453; return s - Math.floor(s); }; // seeded, never Math.random
-const SUN_DIR = new THREE.Vector3(0.55, 0.62, 0.35).normalize(); // one sun direction, reused everywhere
+const rand = mulberry32(7); // lib/noise.js: the seeded PRNG — never Math.random
function heightAt(x, z) {
// deterministic analytic terrain — EVERY placement must call this, or things float
@@ -36,24 +35,20 @@ function buildEnv({ THREE, scene }) {
scene.background = new THREE.Color(0xa8c4dd);
// sky dome gradient (cheap, always works; the Sky addon is the fancy alternative)
const skyGeo = new THREE.SphereGeometry(240, 24, 12);
- const skyMat = new THREE.ShaderMaterial({
- side: THREE.BackSide, fog: false, depthWrite: false,
+ const skyMat = makeShaderMaterial({ // lib/shader.js: tonemap + colorspace included
+ name: 'SkyDome', side: THREE.BackSide, fog: false, depthWrite: false,
uniforms: { uTop: { value: new THREE.Color(0x4e7fb8) }, uHorizon: { value: new THREE.Color(0xd8e4ec) } },
- vertexShader: 'varying vec3 vP; void main() { vP = position; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }',
- fragmentShader: `uniform vec3 uTop; uniform vec3 uHorizon; varying vec3 vP;
- void main() { float h = clamp(normalize(vP).y, 0.0, 1.0); gl_FragColor = vec4(mix(uHorizon, uTop, pow(h, 0.55)), 1.0);
- #include <tonemapping_fragment>
- #include <colorspace_fragment>
- }`,
+ varyings: 'varying vec3 vP;', vertexMain: ' vP = position;',
+ fragmentHead: 'uniform vec3 uTop; uniform vec3 uHorizon;',
+ fragmentMain: ' float h = clamp(normalize(vP).y, 0.0, 1.0); gl_FragColor = vec4(mix(uHorizon, uTop, pow(h, 0.55)), 1.0);',
});
const sky = new THREE.Mesh(skyGeo, skyMat); sky.name = 'SkyDome'; scene.add(sky);
- // lights: hemisphere fill + ONE shadow-casting sun. Physical intensities (no legacy lights).
- scene.add(new THREE.HemisphereLight(0xbfd6f0, 0x5e5442, 0.55));
- const sun = new THREE.DirectionalLight(0xfff0d8, 2.6);
- sun.position.copy(SUN_DIR).multiplyScalar(80); sun.castShadow = true;
- sun.shadow.mapSize.set(2048, 2048); sun.shadow.bias = -0.0005;
- Object.assign(sun.shadow.camera, { left: -55, right: 55, top: 55, bottom: -55, near: 5, far: 200 });
- scene.add(sun, sun.target);
+ // lights: ONE sunRig (lib/environment.js) — key, hemisphere fill, the environment map,
+ // the disc, and a shadow camera, map size and bias it fits to `bounds` itself
+ const rig = sunRig({ mood: 'day', azimuth: 35, elevation: 48, bounds: 50 });
+ scene.add(rig.sun, rig.sun.target, rig.fill);
+ if (rig.sunDisc) scene.add(rig.sunDisc);
+ scene.environment = rig.envTex;
// ground displaced by heightAt, vertex-coloured by slope/height (no textures)
const g = new THREE.PlaneGeometry(100, 100, 128, 128); g.rotateX(-Math.PI / 2);
const pos = g.attributes.position, col = new Float32Array(pos.count * 3);
@@ -72,8 +67,8 @@ function buildEnv({ THREE, scene }) {
}
```
-Numbers: sun 2.2–3.2, hemisphere 0.4–0.7 (never both high — washed out); shadow frustum
-just past the bounds; fog near ≈ half the bounds, far ≈ 2 × the diagonal; ground ≈ 1
+Numbers: the rig's own per-mood intensities (tint it, do not add lights); `bounds` = the
+content radius, which fits the shadows; fog near ≈ half the bounds, far ≈ 2 × the diagonal; ground ≈ 1
segment per metre. Full palettes per time of day are in the time-of-day chapter.
## The exact contract the harness assembler enforces
@@ -449,15 +444,17 @@ species**, per-instance **scale / hue / rotation jitter**, **clustered** placeme
**ground-cover** layer — all instanced, so a whole forest is 6–10 draw calls.
```js
-// One species = one geometry pair (trunk + crown) with its own silhouette + hue band.
+// One species = a trunk + a crown with its own silhouette + hue band. A conifer's crown is
+// a cone; a broadleaf or scrub crown is LEAVES — `makeCanopy` (lib/canopy.js), never an
+// icosahedron or any displaced ball (the catalog's crown rule).
function speciesGeometries(THREE) {
return {
conifer: { trunk: new THREE.CylinderGeometry(0.10, 0.20, 3.0, 6).translate(0, 1.5, 0),
crown: new THREE.ConeGeometry(1.15, 4.4, 8).translate(0, 4.4, 0), hue: 0.32, sat: 0.42, lum: 0.20, h: [0.8, 1.5] },
broadleaf: { trunk: new THREE.CylinderGeometry(0.14, 0.26, 2.4, 7).translate(0, 1.2, 0),
- crown: new THREE.IcosahedronGeometry(1.7, 1).translate(0, 3.4, 0), hue: 0.24, sat: 0.45, lum: 0.28, h: [0.9, 1.6] },
+ canopy: { y: 3.4, radius: 1.7 }, hue: 0.24, sat: 0.45, lum: 0.28, h: [0.9, 1.6] },
scrub: { trunk: new THREE.CylinderGeometry(0.07, 0.11, 0.7, 5).translate(0, 0.35, 0),
- crown: new THREE.DodecahedronGeometry(0.75, 0).translate(0, 1.0, 0), hue: 0.18, sat: 0.35, lum: 0.30, h: [0.7, 1.3] },
+ canopy: { y: 1.0, radius: 0.75 }, hue: 0.18, sat: 0.35, lum: 0.30, h: [0.7, 1.3] },
};
}
@@ -488,7 +485,8 @@ function buildForest(THREE, opts = {}) {
for (const [name, count] of Object.entries(mix)) {
const sp = SP[name]; if (!sp || count <= 0) continue;
const trunks = new THREE.InstancedMesh(sp.trunk, bark, count);
- const crowns = new THREE.InstancedMesh(sp.crown, new THREE.MeshStandardMaterial({ roughness: 0.85, flatShading: name !== 'broadleaf' }), count);
+ const crowns = sp.crown ? new THREE.InstancedMesh(sp.crown, new THREE.MeshStandardMaterial({ roughness: 0.85, flatShading: true }), count) : null;
+ const leafy = []; // this species' makeCanopy crowns
const pts = clusteredPoints(count, area, seed + si * 37);
pts.forEach(([x, z], i) => {
const k = sp.h[0] + (sp.h[1] - sp.h[0]) * gHash(seed + si * 31 + i, 211); // 1) scale jitter
@@ -496,18 +494,25 @@ function buildForest(THREE, opts = {}) {
q.setFromAxisAngle(up, gHash(seed + i, 217) * 6.283); // 3) yaw jitter
const qq = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(1, 0, 0.3).normalize(), lean).multiply(q);
p.set(x, heightAtFn(x, z), z); s.set(k * (0.85 + 0.3 * gHash(i, 219)), k, k * (0.85 + 0.3 * gHash(i, 221)));
- m.compose(p, qq, s); trunks.setMatrixAt(i, m); crowns.setMatrixAt(i, m);
+ m.compose(p, qq, s); trunks.setMatrixAt(i, m);
+ if (!crowns) { leafy.push({ position: [x, p.y + sp.canopy.y * k, z], radius: sp.canopy.radius * k }); return; }
+ crowns.setMatrixAt(i, m);
tint.setHSL(sp.hue + (gHash(seed + i, 223) - 0.5) * 0.055, // 4) hue jitter ±0.03
sp.sat + (gHash(seed + i, 227) - 0.5) * 0.18,
sp.lum + (gHash(seed + i, 229) - 0.5) * 0.10); // 5) value jitter
crowns.setColorAt(i, tint);
});
- trunks.instanceMatrix.needsUpdate = crowns.instanceMatrix.needsUpdate = true;
- if (crowns.instanceColor) crowns.instanceColor.needsUpdate = true;
- trunks.castShadow = crowns.castShadow = true; crowns.receiveShadow = true;
- trunks.computeBoundingSphere(); crowns.computeBoundingSphere();
- trunks.name = `${name}Trunks`; crowns.name = `${name}Crowns`;
- group.add(trunks, crowns); si++;
+ trunks.instanceMatrix.needsUpdate = true; trunks.castShadow = true;
+ trunks.computeBoundingSphere(); trunks.name = `${name}Trunks`; group.add(trunks);
+ if (crowns) {
+ crowns.instanceMatrix.needsUpdate = true;
+ if (crowns.instanceColor) crowns.instanceColor.needsUpdate = true;
+ crowns.castShadow = crowns.receiveShadow = true; crowns.computeBoundingSphere();
+ crowns.name = `${name}Crowns`; group.add(crowns);
+ } else { // one leaf mesh per species
+ group.add(makeCanopy({ crowns: leafy, color: new THREE.Color().setHSL(sp.hue, sp.sat, sp.lum), seed: seed + si, name: `${name}Crowns` }));
+ }
+ si++;
}
return group;
}
@@ -692,7 +697,8 @@ Why: a scene mixes cheap instanced fillers with a few hero assets; GLBs come in
// point shaders only if the plan demands). Recycle positions inside update.
function buildFireflies(THREE, n = 120, area = { x: 0, z: 0, w: 30, d: 30 }) {
const pos = new Float32Array(n * 3);
- for (let i = 0; i < n; i++) pos.set([area.x + (rand(i) - 0.5) * area.w, 0.6 + rand(i + 300) * 2.2, area.z + (rand(i + 600) - 0.5) * area.d], i * 3);
+ const rnd = mulberry32(11);
+ for (let i = 0; i < n; i++) pos.set([area.x + (rnd() - 0.5) * area.w, 0.6 + rnd() * 2.2, area.z + (rnd() - 0.5) * area.d], i * 3);
const geo = new THREE.BufferGeometry(); geo.setAttribute('position', new THREE.BufferAttribute(pos, 3));
const pts = new THREE.Points(geo, new THREE.PointsMaterial({ color: 0xd9f76e, size: 0.06, sizeAttenuation: true, transparent: true, opacity: 0.9 }));
pts.name = 'Fireflies';
@@ -864,9 +870,10 @@ still read without it (`emissiveIntensity` ≤ 3, no white-out). Do not build a
ratio (sun ≈ 4–6 × hemisphere).
4. **Shadows missing** → `castShadow` on the light AND meshes, `receiveShadow` on ground,
shadow camera frustum too small, or light too far (increase `far`).
-5. **Shadow acne / peter-panning** → `sun.shadow.bias = -0.0005`, keep mapSize 2048.
+5. **Shadow acne / peter-panning** → a hand-set bias or map size on the rig's sun; `sunRig`
+ fits both to `bounds` — pass the content radius and leave them alone.
6. **`Math.random()` anywhere** → different geometry every build; renders not reproducible;
- use `rand(i)` hashes with explicit seeds.
+ use `mulberry32(seed)` from `lib/noise.js`.
7. **Low fps** → count draw calls first (`scene_probe`); the usual criminals: hundreds of
Meshes that should be instances, per-frame `traverse`, 4096 shadow maps, fog off with a
500 m far plane.
@@ -894,9 +901,9 @@ still read without it (`emissiveIntensity` ≤ 3, no white-out). Do not build a
17. **The backdrop reads as pale cardboard** → hand-lerped toward a light fog colour
(double haze → brighter than the ground) and/or built from flat planes. Darken the
base colour, let `scene.fog` haze it, use solid geometry.
-18. **Jagged / stair-stepped shadows** → shadow texel = `2 × shadowSpan / mapSize`; keep it
- ≤ 0.05 m. A 60 m span needs 3072², a 100 m span needs 4096² — or shrink the span to
- the bounds instead of the whole terrain.
+18. **Jagged / stair-stepped shadows** → the shadow span is the whole terrain instead of the
+ content: `sunRig({ bounds })` sizes the map to the span it is given, so give it the
+ content radius, not the ground size.
19. **Monochrome orange (or blue) frame** → key and fill share a hue. The fill
(HemisphereLight sky colour) must be ≥ 60° of hue away from the sun colour.
20. **The scene is a diorama on a plane** → no foreground layer and no background layer.
diff --git a/harness/codeverse3d/prompts/tracks/scene_env.j2 b/harness/codeverse3d/prompts/tracks/scene_env.j2
index 0f0b1aea..fb4b747a 100644
--- a/harness/codeverse3d/prompts/tracks/scene_env.j2
+++ b/harness/codeverse3d/prompts/tracks/scene_env.j2
@@ -89,9 +89,9 @@ The chapters behind these rules are printed in full at the end of this brief
`lib/environment.js` and assigns its `envTex` to `scene.environment`; metals and glass read
from that map and render near black without it. Change the mood/azimuth through the rig's
options, not by replacing it with bare lights.
-5. **Shadows must not be jagged.** Shadow texel = `2 x shadowSpan / mapSize` <= 0.05 m: fit the
- shadow camera to the BOUNDS (not the whole ground), then pick mapSize 2048 (span <= 30 m) /
- 3072 (<= 60 m) / 4096 (larger). `bias = -0.0004`, `normalBias = 0.02`.
+5. **Shadows must not be jagged.** `sunRig` fits the shadow camera, map size and bias to its
+ `bounds`: pass the content radius (the plan bounds, not the whole ground) and never set
+ `sun.shadow.*` by hand.
6. No {% if not (textures is defined and textures) %}textures, no {% endif %}DOM, no fetch, no Math.random.
## DON'T
diff --git a/harness/codeverse3d/prompts/tracks/scene_zone.j2 b/harness/codeverse3d/prompts/tracks/scene_zone.j2
index b493f253..0f4f9466 100644
--- a/harness/codeverse3d/prompts/tracks/scene_zone.j2
+++ b/harness/codeverse3d/prompts/tracks/scene_zone.j2
@@ -87,7 +87,7 @@ The recipes for all five points below are printed in full at the end of this bri
## DON'T
- No lights except small point lights for lanterns; no cameras; no sky/ground here.
-- No DOM, {% if not (textures is defined and textures) %}no textures, {% endif %}no fetch, no Math.random (hash `rand(i)` with an explicit seed).
+- No DOM, {% if not (textures is defined and textures) %}no textures, {% endif %}no fetch, no Math.random (`mulberry32(seed)` from `lib/noise.js`).
- Do not import assets marked NOT AVAILABLE; do not edit other files.
{% if tool_cards and not single_shot %}
diff --git a/harness/codeverse3d/skills/c3d-scene-motion/SKILL.md b/harness/codeverse3d/skills/c3d-scene-motion/SKILL.md
index 07eb86b4..1e577fa0 100644
--- a/harness/codeverse3d/skills/c3d-scene-motion/SKILL.md
+++ b/harness/codeverse3d/skills/c3d-scene-motion/SKILL.md
@@ -143,8 +143,8 @@ Two more rules that follow from the pair of frames:
in the t = 0 photograph and the scene reads dead in the very frame the judge
looks at first. Give each element a phase offset, or drive it with
`Math.sin(t / P * 6.283 + 1.0)`.
-* **Seed the phase per instance.** `phase = rand(i) * 6.283` from your seeded
- hash. A hundred trees sharing one phase is one big object nodding, which both
+* **Seed the phase per instance.** `phase = rand() * 6.283` from your seeded
+ `mulberry32`. A hundred trees sharing one phase is one big object nodding, which both
looks wrong and cancels itself out in the pixel diff when half the crowd moves
into the space the other half left.
diff --git a/harness/tests/prompts/test_snippets_run.py b/harness/tests/prompts/test_snippets_run.py
index 08234f38..0854b487 100644
--- a/harness/tests/prompts/test_snippets_run.py
+++ b/harness/tests/prompts/test_snippets_run.py
@@ -168,9 +168,18 @@ console.log('GLB_OK', verts, buf.byteLength);
assert (tmp_path / "object.glb").stat().st_size > 1000
+# the lib calls the scene cookbook's snippets make (their `import` lines are stripped)
+_SCENE_LIB_IMPORTS = (
+ "import { mulberry32 } from './lib/noise.js';\n"
+ "import { makeShaderMaterial } from './lib/shader.js';\n"
+ "import { makeCanopy } from './lib/canopy.js';\n"
+)
+
+
@pytest.mark.node
def test_scene_cookbook_runs(tmp_path) -> None:
stub = (
+ _SCENE_LIB_IMPORTS +
"function collectAnimatedMaterials(scene){const mats=[];scene.traverse(o=>{"
"const l=Array.isArray(o.material)?o.material:(o.material?[o.material]:[]);"
"for(const m of l) if(m.userData&&typeof m.userData.update==='function'&&!mats.includes(m))"
@@ -212,11 +221,13 @@ console.log('SCENE_OK', r.cameras.map((c) => c.name).join(','));
@pytest.mark.node
def test_scene_cookbook_sky_shader_compiles() -> None:
- """The inline sky-dome ShaderMaterial from the scene cookbook must also compile — as a Mesh
+ """The sky dome the scene cookbook builds with makeShaderMaterial must also compile — as a Mesh
(the dome) and as an InstancedMesh, through the production host (``check_shaders.mjs``)."""
body = "".join("\n" + strip_imports_exports(b) for b in blocks("scene_threejs/cookbook.md", "js")[:2])
scene = (
- "import * as THREE from 'three';\n" + body
+ "import * as THREE from 'three';\nimport { sunRig } from './lib/environment.js';\n"
+ "import { mulberry32 } from './lib/noise.js';\nimport { makeShaderMaterial } from './lib/shader.js';\n"
+ + body
+ "\nexport function createScene() {\n"
" const scene = new THREE.Scene();\n"
" const env = buildEnv({ THREE, scene });\n"
@@ -225,5 +236,5 @@ def test_scene_cookbook_sky_shader_compiles() -> None:
" return { scene, cameras: [{ name: 'a', position: [3, 3, 5], lookAt: [0, 0, 0] }], update: env.update };\n"
"}\n"
)
- code, out = compile_scene(scene)
+ code, out = compile_scene(scene, ("environment.js", "sky.js"))
assert code == 0, f"sky shader failed to compile:\n{out}"
diff --git a/harness/tests/scene_gates/test_live_frames.py b/harness/tests/scene_gates/test_live_frames.py
index a079fae3..322863ae 100644
--- a/harness/tests/scene_gates/test_live_frames.py
+++ b/harness/tests/scene_gates/test_live_frames.py
@@ -95,7 +95,7 @@ def test_example_scene_passes_frame_gate_and_judge_subset(starter_ws: Workspace)
tile_h = Image.open(rs.views[0].path).size[1]
assert sheet.size[1] < 5 * tile_h # 8 tiles in 4 columns → 2 rows, not 5
- assert m["framing_bbox"]["size"][0] == pytest.approx(84.4, abs=0.5) # the meadow (≈ 84 m) fits → framed whole
+ assert m["framing_bbox"]["size"][0] == pytest.approx(89.9, abs=0.5) # the meadow (≈ 90 m) fits → framed whole
y_wide = next(v for v in rs.views if v.name == SCENE_VIEWS[0].name).camera_position[1]
tight_out = starter_ws.renders_dir(1) # meadow sprawls past 33 m → the pondside zone
tight = render_scene(starter_ws, tight_out, times=(0.0,), orbit_views=SCENE_VIEWS[:1], width=320, height=180,