Official Codebase of 3DCodeVerse
Python
scene lib: one seedLattice turns a seed into a far-apart lattice offset, with the formula the five copies used
gaoypeng4d5358bc5b78
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/caustics.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/caustics.jsindex 996dfbc9..77bfb639 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/caustics.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/caustics.js@@ -11,7 +11,7 @@*/import * as THREE from 'three';-import { patchStandard, toColor, WORLD_VARYINGS, worldBody } from './shader.js';+import { patchStandard, seedLattice, toColor, WORLD_VARYINGS, worldBody } from './shader.js';// The world varyings terrain_shade and waterside share, so a submerged// bank wearing a splat, a waterline and this declares ONE pair; the@@ -190,17 +190,8 @@ function sourceReach(source, reach) {return new THREE.Vector4(source.x, source.y, source.z, 1 / r);}-/**- * A seed becomes a far-apart lattice offset, not a one-cell shift.- *- * Seeds 7 and 8 offsetting by 1 would TRANSLATE the net by one cell,- * which is the same pool twice.- */-function seedOffset(seed) {- const s = Math.abs(Math.round(seed === undefined ? 1 : seed)) % 9973;- return new THREE.Vector2(((s * 16807) % 9973) * 0.103,- ((s * 48271) % 9973) * 0.071);-}+const seedOffset = (seed) => new THREE.Vector2(seedLattice(seed) * 0.103,+ seedLattice(seed, 48271) * 0.071);/*** Throw a moving net of caustic light on what is under the water.diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/neon.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/neon.jsindex e8c4a6be..b278f90d 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/neon.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/neon.js@@ -24,7 +24,7 @@ import * as THREE from 'three';import {keepOutOfDepthPasses, makeShaderMaterial, patchStandard, sweepProfile,- tickShaders, toColor } from './shader.js';+ seedLattice, tickShaders, toColor } from './shader.js';const _UP = new THREE.Vector3(0, 1, 0);@@ -71,11 +71,7 @@ const TEMP_GLSL = ['}',].join('\n');-/** A seed becomes a far-apart hash offset, not a one-cell shift. */-function seedKey(seed) {- const s = Math.abs(Math.round(seed === undefined ? 1 : seed)) % 9973;- return ((s * 16807) % 9973) * 0.011;-}+const seedKey = (seed) => seedLattice(seed) * 0.011;/** A point in any accepted spelling as its own Vector3. */function toVec(p) {diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.jsindex 92ffff27..e5ed06e2 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.js@@ -288,6 +288,17 @@ export function readWind(value, fallback, label = 'wind') {return { x: dir.x * strength, z: dir.y * strength, dir, strength, speed };}+/**+ * A seed as a far-apart lattice coordinate, not a one-cell shift: the+ * integer seed (1 when undefined) mod 9973, times the multiplier `k`,+ * mod `m`. Seeds 7 and 8 offsetting a hash lattice by 1 would merely+ * TRANSLATE the pattern by one cell, which reads as the same object twice.+ */+export function seedLattice(seed, k = 16807, m = 9973) {+ const s = Math.abs(Math.round(seed === undefined ? 1 : seed)) % 9973;+ return (s * k) % m;+}+/** Read a Vector3, an array or an {x,y,z} into a new Vector3. */export function readVec3(p, fx = 0, fy = 0, fz = 0) {if (!p) return new THREE.Vector3(fx, fy, fz);diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/submerged.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/submerged.jsindex 71f5e6f6..316e6213 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/submerged.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/submerged.js@@ -24,7 +24,7 @@ import { attachDisposal, snapshotResources } from './lifecycle.js';import { lehmer } from './noise.js';import {composeRoughness, makeShaderMaterial, instancedQuad, keepOutOfDepthPasses,- toColor, unit, withBase, worldBase,+ seedLattice, toColor, unit, withBase, worldBase,} from './shader.js';// Water absorbs red an order of magnitude faster than blue, and that@@ -41,17 +41,8 @@ const LIFT = 0.012;// this declares ONE pair.const BASE = worldBase('submerged:base', 'subWp', 'subWn');-/**- * A seed becomes a far-apart lattice offset, not a one-cell shift.- *- * Seeds 7 and 8 offsetting by 1 would TRANSLATE the field by one cell,- * which is the same pool twice.- */-function seedOffset(seed) {- const s = Math.abs(Math.round(seed === undefined ? 1 : seed)) % 9973;- return new THREE.Vector2(((s * 16807) % 9973) * 0.091,- ((s * 48271) % 9973) * 0.067);-}+const seedOffset = (seed) => new THREE.Vector2(seedLattice(seed) * 0.091,+ seedLattice(seed, 48271) * 0.067);/*** Per-channel extinction: a scalar on water's spectrum, or threediff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/urban.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/urban.jsindex 888696bd..b3532d55 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/urban.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/urban.js@@ -24,7 +24,7 @@ import { patchNeonSpill } from './neon.js';import { fbm2, mulberry32, noiseDataTexture } from './noise.js';import {keepOutOfDepthPasses, makeShaderMaterial,- patchStandard, tickShaders, toColor, unit,+ patchStandard, seedLattice, tickShaders, toColor, unit,} from './shader.js';const _UP = new THREE.Vector3(0, 1, 0);@@ -36,16 +36,7 @@ function toVec(p) {return new THREE.Vector3(p.x || 0, p.y || 0, p.z || 0);}-/**- * A seed becomes a far-apart lattice offset, not a one-cell shift.- *- * Seeds 7 and 8 offsetting a hash lattice by 1 would merely TRANSLATE- * the pattern by one panel, which reads as the same tower twice.- */-function seedOffset(seed) {- const s = Math.abs(Math.round(seed === undefined ? 1 : seed)) % 9973;- return ((s * 16807) % 9973) * 0.0517;-}+const seedOffset = (seed) => seedLattice(seed) * 0.0517;/** Read a `[w, h]`, `{w, h}` or single number as a metric pair. */function toPair(value, dw, dh) {diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/windows.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/windows.jsindex ec1cfa0f..acbde41b 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/windows.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/windows.js@@ -12,7 +12,7 @@*/import * as THREE from 'three';-import { clonePatchedMaterial, patchStandard, toColor } from './shader.js';+import { clonePatchedMaterial, patchStandard, seedLattice, toColor } from './shader.js';import { attachDisposal } from './lifecycle.js';const WIN_VARYINGS = [@@ -241,10 +241,7 @@ const WIN_BODY = [* lattice points, where the hash is a thousand times more sensitive to* the last bit of the inputs than it is to the cell.*/-function seedOffset(seed) {- const s = Math.abs(Math.round(seed === undefined ? 1 : seed)) % 9973;- return (s * 16807) % 257;-}+const seedOffset = (seed) => seedLattice(seed, 16807, 257);/*** Shade the ROOM behind each window instead of the window itself.diff --git a/harness/tests/scene_runtime/lib/test_shader.py b/harness/tests/scene_runtime/lib/test_shader.pyindex 7e126eb2..ecd7b2cd 100644--- a/harness/tests/scene_runtime/lib/test_shader.py+++ b/harness/tests/scene_runtime/lib/test_shader.py@@ -473,3 +473,16 @@ def test_the_surface_gradient_bump_is_written_once():frame = re.compile(r"cross\(\s*\w+\s*,\s*(normal|n)\s*\)")own = sorted(p.name for p in LIB_DIR.glob("*.js") if frame.search(p.read_text(encoding="utf-8")))assert own == ["shader.js", "tree.js", "windows.js"], own+++def test_seed_lattice_keeps_the_formula_the_five_copies_used():+ """neon, urban, caustics, submerged and windows each carried this body; the one+ helper must give every recorded seed the same pattern it had."""+ out = measure("""+import { seedLattice } from './lib/shader.js';+const old = (seed, k, m) => (Math.abs(Math.round(seed === undefined ? 1 : seed)) % 9973 * k) % m;+const seeds = [undefined, 0, 1, 7, -7, 3.6, 9980, 123456];+console.log(JSON.stringify(seeds.flatMap((s) => [[16807, 9973], [48271, 9973], [16807, 257]]+ .map(([k, m]) => seedLattice(s, k, m) === old(s, k, m)))));+""", _LIBS)+ assert out and all(out), out