Skip to content

Official Codebase of 3DCodeVerse

Python

scene lib: one baked 3D fBm (noise.bakeFbm3) for smoke, fire field and cloud volume

gaoypeng07ae99f3ee01
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/cloudvolume.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/cloudvolume.js
index 1ad11d42..b4414e34 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/cloudvolume.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/cloudvolume.js
@@ -17,7 +17,7 @@
import * as THREE from 'three';
import { makeShaderMaterial, keepOutOfDepthPasses } from './shader.js';
import { attachDisposal, snapshotResources } from './lifecycle.js';
-import { dataTexture3D, mulberry32, sampleGrid3 } from './noise.js';
+import { bakeFbm3, dataTexture3D, mulberry32, sampleGrid3 } from './noise.js';
const TIERS = {
low: { steps: 40, lightSteps: 6 },
@@ -41,9 +41,7 @@ function vector(value, fallback, length, label) {
function densityTexture(seed) {
const random = mulberry32(seed);
- const grids = [4, 8, 16, 32].map((n) => ({
- n, data: Float32Array.from({ length: n ** 3 }, () => random()),
- }));
+ const { values: noiseField, lattices } = bakeFbm3(random, RESOLUTION, [4, 8, 16, 32]);
// Unequal convection towers grow from a connected low bank. Small
// satellite billows belong to each tower instead of a regular 3-by-2 grid.
const lobes = [{ x: 0, y: -.15, z: 0, rx: .31, ry: .16, rz: .25 }];
@@ -71,21 +69,14 @@ function densityTexture(seed) {
let i = 0;
for (let z = 0; z < RESOLUTION; z++) for (let y = 0; y < RESOLUTION; y++) for (let x = 0; x < RESOLUTION; x++) {
const u = (x + .5) / RESOLUTION, v = (y + .5) / RESOLUTION, w = (z + .5) / RESOLUTION;
- let noise = 0, amplitude = .5333333;
- for (const grid of grids) {
- const gx = u * grid.n, gy = v * grid.n, gz = w * grid.n;
- // Hermite interpolation before each lattice lookup reduces grid creases.
- const ease = (q) => Math.floor(q) + smooth(0, 1, q - Math.floor(q));
- noise += amplitude * sampleGrid3(grid.data, grid.n, ease(gx), ease(gy), ease(gz));
- amplitude *= .5;
- }
+ const noise = noiseField[i / 4];
let shape = -4;
for (const lobe of lobes) {
const distance = Math.hypot((u - .5 - lobe.x) / lobe.rx,
(v - .5 - lobe.y) / lobe.ry, (w - .5 - lobe.z) / lobe.rz);
shape = Math.max(shape, 1 - distance);
}
- const detail = sampleGrid3(grids[2].data, 16, u * 16, v * 16, w * 16);
+ const detail = sampleGrid3(lattices[2].data, 16, u * 16, v * 16, w * 16);
// Stationary nested breakup leaves a scalloped boundary. The ellipsoid
// only limits the outer support; its zero band stays inside the proxy.
shape += (detail - .5) * .70;
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/firefield.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/firefield.js
index a0e31fe9..655c2475 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/firefield.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/firefield.js
@@ -28,7 +28,7 @@
* No global renderer hooks or recursively captured reflective materials.
*/
import * as THREE from 'three';
-import { dataTexture3D, mulberry32, sampleGrid3 } from './noise.js';
+import { bakeFbm3, dataTexture3D, mulberry32, sampleGrid3 } from './noise.js';
import { makeShaderMaterial, keepOutOfDepthPasses, withRendererState } from './shader.js';
import { snapshotResources, attachDisposal } from './lifecycle.js';
@@ -54,21 +54,8 @@ function vector(value, fallback, length, label) {
return v.slice();
}
function noiseTexture(seed) {
- const random = mulberry32(seed), grids = [4, 8, 16].map(n => ({n,
- data: Float32Array.from({length: n ** 3 * 4}, () => random())}));
- const n = NOISE_RESOLUTION, data = new Uint8Array(n ** 3 * 4); let at = 0;
- for (let z = 0; z < n; z++) for (let y = 0; y < n; y++) for (let x = 0; x < n; x++) {
- for (let c = 0; c < 4; c++) {
- let sum = 0, amplitude = 4 / 7;
- for (const grid of grids) {
- const q = [x, y, z].map(v => { const p = (v + .5) / n * grid.n;
- return Math.floor(p) + smooth(0, 1, fract(p)); });
- sum += sampleGrid3(grid.data, grid.n, ...q, 4, c) * amplitude; amplitude *= .5;
- }
- data[at++] = Math.round(clamp(sum) * 255);
- }
- }
- return dataTexture3D(data, n, true);
+ const { values } = bakeFbm3(mulberry32(seed), NOISE_RESOLUTION, [4, 8, 16], 4);
+ return dataTexture3D(Uint8Array.from(values, v => Math.round(clamp(v) * 255)), NOISE_RESOLUTION, true);
}
function sourceAtlas(emitters, box, resolution, smokeHeight, wind, rise, seed) {
const extent = box.getSize(new THREE.Vector3()), data = new Uint8Array(resolution ** 3 * 4);
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/noise.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/noise.js
index 5e7a6205..ff7ced75 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/noise.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/noise.js
@@ -188,6 +188,33 @@ export function sampleGrid3(data, n, x, y, z, stride = 1, channel = 0, periodic
return value;
}
+/**
+ * Periodic 3D value-noise fBm baked at the voxel centres of a size³ grid.
+ * One random lattice per entry of `lattices` (its cells per side), drawn from
+ * `random` in order with `channels` independent values per point; octaves
+ * are Hermite-eased and their amplitudes halve and sum to 1, so values stay
+ * in 0..1. Returns { values: size³·channels floats, x fastest, lattices }.
+ */
+export function bakeFbm3(random, size, lattices, channels = 1) {
+ const grids = lattices.map((n) => ({ n, data: Float32Array.from({ length: n ** 3 * channels }, () => random()) }));
+ const values = new Float64Array(size ** 3 * channels);
+ const first = 2 ** (grids.length - 1) / (2 ** grids.length - 1);
+ const ease = (q) => { const i = Math.floor(q), f = q - i; return i + f * f * (3 - 2 * f); };
+ let at = 0;
+ for (let z = 0; z < size; z++) for (let y = 0; y < size; y++) for (let x = 0; x < size; x++) {
+ for (let c = 0; c < channels; c++) {
+ let sum = 0, amplitude = first;
+ for (const { n, data } of grids) {
+ const q = (v) => ease((v + .5) / size * n);
+ sum += amplitude * sampleGrid3(data, n, q(x), q(y), q(z), channels, c);
+ amplitude *= .5;
+ }
+ values[at++] = sum;
+ }
+ }
+ return { values, lattices: grids };
+}
+
/**
* An RGBA8 cubic Data3DTexture, linearly filtered, repeating unless
* `repeat` is false (then clamped to the edge). needsUpdate is set.
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/smoke.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/smoke.js
index 986ae2fb..faa8bc36 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/smoke.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/smoke.js
@@ -15,7 +15,7 @@
import * as THREE from 'three';
import { makeShaderMaterial, keepOutOfDepthPasses } from './shader.js';
import { attachDisposal, snapshotResources } from './lifecycle.js';
-import { dataTexture3D, mulberry32, sampleGrid3 } from './noise.js';
+import { bakeFbm3, dataTexture3D, mulberry32, sampleGrid3 } from './noise.js';
const RESOLUTION = 48;
const TIERS = { low: [36, 4], balanced: [64, 6], high: [100, 10] };
@@ -23,24 +23,9 @@ const clamp = (x, a = 0, b = 1) => Math.max(a, Math.min(b, x));
const smooth = (a, b, x) => { const t = clamp((x - a) / (b - a)); return t * t * (3 - 2 * t); };
function noiseTexture(seed) {
- const random = mulberry32(seed);
- const grids = [4, 8, 16, 32].map(n => ({ n, data: Float32Array.from({ length: n ** 3 * 3 }, () => random()) }));
- const data = new Uint8Array(RESOLUTION ** 3 * 4);
- let index = 0;
- for (let z = 0; z < RESOLUTION; z++) for (let y = 0; y < RESOLUTION; y++) for (let x = 0; x < RESOLUTION; x++) {
- const values = [0, 0, 0];
- let amplitude = 8 / 15;
- for (const grid of grids) {
- const coords = [x, y, z].map(v => {
- const q = (v + .5) / RESOLUTION * grid.n;
- return Math.floor(q) + smooth(0, 1, q - Math.floor(q));
- });
- for (let channel = 0; channel < 3; channel++) values[channel] += amplitude * sampleGrid3(grid.data, grid.n, ...coords, 3, channel);
- amplitude *= .5;
- }
- for (const v of values) data[index++] = Math.round(v * 255);
- data[index++] = 255;
- }
+ const { values } = bakeFbm3(mulberry32(seed), RESOLUTION, [4, 8, 16, 32], 3);
+ const data = new Uint8Array(RESOLUTION ** 3 * 4).fill(255);
+ for (let i = 0; i < values.length; i++) data[i + Math.floor(i / 3)] = Math.round(values[i] * 255);
return dataTexture3D(data, RESOLUTION);
}