Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Show mesh blend and live cloth in the nature demo (part of #510)

Nguyen Xuan An7170cc8184d8
diff --git a/changes/510.md b/changes/510.md
new file mode 100644
index 00000000..c959408f
--- /dev/null
+++ b/changes/510.md
@@ -0,0 +1 @@
+- The nature demo softens the seam where rocks and tree trunks meet the ground (mesh blend) and flies a small cloth flag on the footbridge, which follows the Wind slider. `?softening=0` draws the valley without both, to compare.
diff --git a/src/nature-flag.js b/src/nature-flag.js
new file mode 100644
index 00000000..a4616a6c
--- /dev/null
+++ b/src/nature-flag.js
@@ -0,0 +1,66 @@
+import { createCloth, weldVertices } from './cloth.js';
+import { updatePartVertices } from './update-part-vertices.js';
+import { recomputeNormals } from './geometry-normals.js';
+
+const STEP_SECONDS = 1 / 60;
+// A seek replays this much time from rest, so a frame at time t looks the same however the page got there.
+const REPLAY_SECONDS = 3;
+const BREEZE_METRES_PER_SECOND = 1.5;
+const GUST_METRES_PER_SECOND = 4.5;
+
+// Steps the flag's cloth with the page clock and writes it into the part's GPU buffers. The wind runs along -x, the length of a flag
+// pinned on its right edge. `windStrength` is the page's 0..1 wind slider.
+export function createFlagAnimation(renderer, flagPart) {
+ const geometry = flagPart.geometry;
+ const welded = weldVertices(geometry.positions, geometry.indices);
+ const restPositions = new Float32Array(welded.representatives.length * 3);
+ welded.representatives.forEach((vertex, particle) => {
+ restPositions.set(geometry.positions.subarray(vertex * 3, vertex * 3 + 3), particle * 3);
+ });
+ let poleEdge = -Infinity;
+ for (let offset = 0; offset < restPositions.length; offset += 3) poleEdge = Math.max(poleEdge, restPositions[offset]);
+ const pinned = [];
+ for (let particle = 0; particle < restPositions.length / 3; particle++) {
+ if (restPositions[particle * 3] > poleEdge - 1e-4) pinned.push(particle);
+ }
+ const restGeometry = { positions: Float32Array.from(geometry.positions), normals: Float32Array.from(geometry.normals), indices: geometry.indices };
+ const vertexNormals = new Float32Array(geometry.normals.length);
+ let cloth = null;
+ let clothTime = 0;
+
+ function restart(startTime) {
+ cloth = createCloth(restPositions, welded.triangles, { pinned, density: 0.25 });
+ clothTime = startTime;
+ }
+
+ function windAt(time, windStrength) {
+ const gust = 0.5 + 0.5 * Math.sin(time * 1.3) * Math.sin(time * 0.47 + 1);
+ const speed = BREEZE_METRES_PER_SECOND + windStrength * (GUST_METRES_PER_SECOND * gust + 1.5);
+ return [-speed, 0, 0.35 * speed * Math.sin(time * 0.9)];
+ }
+
+ function write() {
+ const positions = geometry.positions;
+ welded.particleOfVertex.forEach((particle, vertex) => {
+ positions.set(cloth.positions.subarray(particle * 3, particle * 3 + 3), vertex * 3);
+ });
+ vertexNormals.set(recomputeNormals(restGeometry, positions));
+ updatePartVertices(renderer, flagPart, positions, vertexNormals);
+ }
+
+ return {
+ update(time, windStrength) {
+ let changed = false;
+ if (!cloth || time < clothTime - STEP_SECONDS || time - clothTime > REPLAY_SECONDS) {
+ restart(Math.max(0, time - REPLAY_SECONDS));
+ changed = true;
+ }
+ while (clothTime + STEP_SECONDS <= time) {
+ cloth.step(STEP_SECONDS, { wind: windAt(clothTime, windStrength) });
+ clothTime += STEP_SECONDS;
+ changed = true;
+ }
+ if (changed) write();
+ },
+ };
+}
diff --git a/src/nature-viewer.js b/src/nature-viewer.js
index 65e8b4e4..e6aba4fe 100644
--- a/src/nature-viewer.js
+++ b/src/nature-viewer.js
@@ -3,6 +3,8 @@ import './brand.js';
import { vec4 } from 'gl-matrix';
import { Renderer67 } from './renderer.js';
import { createNatureWorld } from './nature.js';
+import { installMeshBlend } from './mesh-blend.js';
+import { createFlagAnimation } from './nature-flag.js';
const renderParameters = new URLSearchParams(location.search);
const canvas = document.querySelector('#viewport');
@@ -15,6 +17,7 @@ const state = {
};
let world = createNatureWorld();
let renderer;
+let flagAnimation = null;
let scheduledFrame = 0;
let lastFrameTime = 0;
let dragging;
@@ -64,6 +67,7 @@ function environment() {
}
function renderFrame() {
+ flagAnimation?.update(state.time, state.wind);
renderer?.render({ ...camera(), time: state.time, wind: state.wind, waterRipple: state.waterRipple, rippleTime: state.rippleTime, environment: environment(), effects: { saturation: 1.06, contrast: 1.03, temperature: (1 - state.daylight) * 0.1, vignette: 0.07, bloom: { strength: 0.1, threshold: 1.2, radius: 1.3 } } });
}
@@ -97,10 +101,15 @@ function updateStatistics() {
}
function initializeRenderer() {
- renderer = new Renderer67(canvas, world.parts, {
- world: true, ground: false, hdr: true, environment: world.environment,
+ // ?softening=0 draws the valley without the seam blend, the flagpole and the flag, to compare frames and frame time.
+ const softening = renderParameters.get('softening') !== '0';
+ const parts = softening ? world.parts : world.parts.filter(part => part !== world.flag && part.name !== 'Flagpole').map(({ blend, ...part }) => part);
+ renderer = new Renderer67(canvas, parts, {
+ world: true, ground: false, hdr: true, depth: softening, environment: world.environment,
pixelRatio: renderParameters.get('export') === '1' ? 1 : 1.25, memoryBudgetMiB: Number(renderParameters.get('memory') ?? 256), samples: Number(renderParameters.get('samples') ?? 4),
});
+ if (softening) installMeshBlend(renderer);
+ flagAnimation = softening ? createFlagAnimation(renderer, world.flag) : null;
updateStatistics();
renderFrame();
invalidate();
diff --git a/src/nature.js b/src/nature.js
index bc9269f5..89010dc2 100644
--- a/src/nature.js
+++ b/src/nature.js
@@ -1,5 +1,5 @@
import { mat4, quat } from 'gl-matrix';
-import { box, cylinder, sphere } from './geometry.js';
+import { box, cylinder, grid, sphere } from './geometry.js';
import './world-material-shader.js';
const FULL_TURN = Math.PI * 2;
@@ -19,6 +19,24 @@ function randomGenerator(seed) {
};
}
+// Seam softening where a rock or a trunk meets the ground (mesh-blend.js), in metres. One radius per group keeps the
+// instanced batches culled.
+const ROCK_BLEND = Object.freeze({ radius: 0.1 });
+const TRUNK_BLEND = Object.freeze({ radius: 0.08 });
+
+export const FLAG_SIZE = Object.freeze({ width: 1.2, height: 0.75, segments: 12 });
+
+// A flat sheet in world space, hanging from the pole at `topRight` and streaming toward -x over the bridge. nature-flag.js moves its vertices every frame.
+function createFlagPart(topRight) {
+ const sheet = grid(FLAG_SIZE.segments);
+ for (let vertex = 0; vertex < sheet.positions.length; vertex += 3) {
+ sheet.positions[vertex] = topRight[0] - FLAG_SIZE.width + (sheet.positions[vertex] + 0.5) * FLAG_SIZE.width;
+ sheet.positions[vertex + 1] = topRight[1] - (0.5 - sheet.positions[vertex + 1]) * FLAG_SIZE.height;
+ sheet.positions[vertex + 2] = topRight[2];
+ }
+ return { name: 'Flag', geometry: sheet, matrix: mat4.create(), material: { ...material('cloth', [0.64, 0.09, 0.06], 0.78), doubleSided: true } };
+}
+
function material(tag, color, roughness = 0.9, metallic = 0) {
return { tag, color, roughness, metallic, emission: 0 };
}
@@ -230,8 +248,10 @@ export function createNatureWorld({ seed = 67, detail = 'balanced' } = {}) {
const reedMesh = grassTuft(5, 5, true);
const blossomMesh = flowerHead(sphere(8, 5));
- function add(name, mesh, position, scale, surface, rotation) {
- parts.push({ name, geometry: mesh, matrix: transform(position, scale, rotation), material: surface });
+ function add(name, mesh, position, scale, surface, rotation, blend) {
+ const part = { name, geometry: mesh, matrix: transform(position, scale, rotation), material: surface };
+ if (blend) part.blend = blend;
+ parts.push(part);
}
function streamCenter(depth) {
return Math.sin(depth * 0.09 + 0.7) * 3.7 + Math.sin(depth * 0.034) * 3;
@@ -284,7 +304,7 @@ export function createNatureWorld({ seed = 67, detail = 'balanced' } = {}) {
function tree(horizontal, depth, height, evergreen, index) {
const ground = terrainHeight(horizontal, depth);
const trunkRadius = height * (evergreen ? 0.033 : 0.047);
- add(`Tree ${index} trunk`, trunkMesh, [horizontal, ground + height * 0.44, depth], [trunkRadius, height * 0.88, trunkRadius], evergreen ? bark : birchBark, [0.025, random() * FULL_TURN, 0.035]);
+ add(`Tree ${index} trunk`, trunkMesh, [horizontal, ground + height * 0.44, depth], [trunkRadius, height * 0.88, trunkRadius], evergreen ? bark : birchBark, [0.025, random() * FULL_TURN, 0.035], TRUNK_BLEND);
const mesh = depth < -29 ? distantCanopyMesh : canopyMesh;
const yaw = random() * FULL_TURN;
if (evergreen) {
@@ -344,9 +364,9 @@ export function createNatureWorld({ seed = 67, detail = 'balanced' } = {}) {
const horizontal = streamCenter(depth) + side * (streamWidth(depth) + random() * 2.4);
const radius = 0.15 + random() ** 2 * 0.95;
const ground = terrainHeight(horizontal, depth);
- add(`River stone ${index}`, rockMesh, [horizontal, ground + radius * 0.19, depth], [radius * 1.3, radius * 0.64, radius], stones[index % stones.length], [random() * 0.3, random() * FULL_TURN, random() * 0.4]);
+ add(`River stone ${index}`, rockMesh, [horizontal, ground + radius * 0.19, depth], [radius * 1.3, radius * 0.64, radius], stones[index % stones.length], [random() * 0.3, random() * FULL_TURN, random() * 0.4], ROCK_BLEND);
}
- add('Glacial boulder', rockMesh, [-7.8, terrainHeight(-7.8, 12) + 0.6, 12], [2.3, 1.25, 1.65], stones[1], [0.1, 0.4, 0.2]);
+ add('Glacial boulder', rockMesh, [-7.8, terrainHeight(-7.8, 12) + 0.6, 12], [2.3, 1.25, 1.65], stones[1], [0.1, 0.4, 0.2], ROCK_BLEND);
add('Moss on glacial boulder', rockMesh, [-7.85, terrainHeight(-7.8, 12) + 1.56, 12], [1.65, 0.25, 1.14], foliage[0], [0.1, 0.4, 0.2]);
const flowerPatches = [[-7, 16], [8, 4], [-11, -3], [4, -18]];
@@ -384,11 +404,17 @@ export function createNatureWorld({ seed = 67, detail = 'balanced' } = {}) {
add(`Bridge post cap ${side} ${post}`, timberMesh, [x, 2.3, bridgeDepth + side * 0.88], [0.21, 0.08, 0.21], iron);
}
}
+ const flagPoleX = bridgeCenter + bridgeLength / 2;
+ const flagPoleDepth = bridgeDepth + 0.88;
+ add('Flagpole', cylinder(10), [flagPoleX, 3.3, flagPoleDepth], [0.06, 2, 0.06], warmWood);
+ const flag = createFlagPart([flagPoleX - 0.03, 4.25, flagPoleDepth]);
+ parts.push(flag);
const camera = { eye: [15, 8, 23], target: [-2, 2, -12] };
return {
name: 'Silverwater valley',
description: 'A winding alpine stream, sunlit birches, wildflower meadows and distant snowfields.',
parts,
+ flag,
camera,
bounds: worldBounds(parts),
environment: {