Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Merge main into feat/775-spatial-audio

Nguyen Xuan An4f7532eee8f9
diff --cc README.md
index e2687a61,381490e6..d8016a84
--- a/README.md
+++ b/README.md
@@@ -140,7 -140,7 +140,8 @@@ Each addon is a separate import, so a p
| Physics | `67js/addons/physics` | [Rapier contacts and joints](docs/physics.md) recorded into a timeline |
| Live games | `67js/addons/runtime` | [Game loop](docs/runtime.md) with fixed or variable steps and pluggable systems |
| Live games | `67js/addons/input` | [Input](docs/input.md): keyboard, mouse, gamepad and custom sources mapped to named actions and axes |
+| Live games | `67js/addons/audio` | [Spatial audio](docs/audio.md): sounds on nodes, played from contacts, markers and input, with buses and a limiter |
+ | Live games | `67js/addons/player` | [Player](docs/player.md): character controller, follow, first-person and top-down camera rigs, swappable at runtime |
| Modeling | `67js/addons/mesh`, `67js/addons/modeling`, `67js/addons/sculpt` | [Mesh editing](docs/mesh.md), [modeling](docs/modeling.md) and [sculpt brushes](https://67js.codepawl.com/sculpt.md) |
| Offline light | `67js/addons/path-tracer`, `67js/addons/light-bake` | [Path-traced stills](https://67js.codepawl.com/path-tracer.md) and [baked lightmaps](https://67js.codepawl.com/light-bake.md) |
| Post passes | `67js/addons/user-pass` | [Your own full-screen GLSL passes](docs/user-passes.md): dither, pixel downscale, quantise |
diff --cc bench/runtime-loop/main.js
index 7ef2dfaf,17e794de..0d65e4e0
--- a/bench/runtime-loop/main.js
+++ b/bench/runtime-loop/main.js
@@@ -3,7 -3,7 +3,8 @@@
import { Renderer67, box, sphere } from '/src/index.js';
import { createRuntime } from '/src/runtime.js';
import { createInput, gamepadSource, keyboardSource } from '/src/input.js';
+import { createAudio } from '/src/audio.js';
+ import { createCharacterController, createFirstPersonCamera, createFollowCamera, createTopDownCamera } from '/src/player.js';
const GRAVITY = -9.8;
const FIXED_RATE = 20; // Low on purpose, so the difference between fixed and interpolated is visible.
@@@ -27,18 -23,12 +28,24 @@@ function rotationAroundY(angle, x, y, z
return new Float32Array([cosine, 0, -sine, 0, 0, 1, 0, 0, sine, 0, cosine, 0, x, y, z, 1]);
}
+ function boxMatrix({ min, max }) {
+ const size = max.map((value, axis) => value - min[axis]);
+ const center = max.map((value, axis) => (value + min[axis]) / 2);
+ return new Float32Array([size[0], 0, 0, 0, 0, size[1], 0, 0, 0, 0, size[2], 0, center[0], center[1], center[2], 1]);
+ }
+
+// Short synthesized tones stand in for sound files: a decaying sine at the given pitch.
+function createTone(frequency, seconds) {
+ const sampleRate = 44100;
+ const tone = new AudioBuffer({ length: Math.round(sampleRate * seconds), sampleRate, numberOfChannels: 1 });
+ const samples = tone.getChannelData(0);
+ for (let index = 0; index < samples.length; index += 1) {
+ const time = index / sampleRate;
+ samples[index] = Math.sin(2 * Math.PI * frequency * time) * Math.exp(-time * 12 / seconds);
+ }
+ return tone;
+}
+
const ballPart = {
name: 'Ball',
geometry: sphere(),
@@@ -124,16 -128,27 +151,38 @@@ const input = createInput(
},
});
+// Sounds come from a contact (the bounce, louder with the impact speed), an animation marker (the cube's half turns) and
- // an input action (Space). The listener sits on the camera, so the cube's tick pans as the cube slides.
- const audio = createAudio({ input, listener: () => CAMERA });
++// an input action (Space). The listener sits on the active camera, so the cube's tick pans as the cube slides.
++let cameraFrame = CAMERA;
++const audio = createAudio({ input, listener: () => cameraFrame });
+audio.defineSound('thud', { buffer: createTone(110, 0.3), spatial: { referenceDistance: 4 } });
+audio.defineSound('tick', { buffer: createTone(880, 0.08), spatial: { referenceDistance: 4 }, volume: 0.4 });
+audio.defineSound('hop', { buffer: createTone(440, 0.15) });
+audio.bindEvent('contact', 'thud', { volume: data => Math.min(1, data.impulse / 10) });
+audio.bindEvent('marker:half-turn', 'tick');
+audio.bindAction('jump', 'hop');
+
+ // The player faces and walks relative to whichever camera rig is active, so swapping the rig keeps the controls natural.
+ const player = createCharacterController({
+ input,
+ position: [0, 0, 3],
+ obstacles,
+ cameraYaw: () => runtime?.getSystem('camera')?.yaw ?? 0,
+ });
+ const RIG_FACTORIES = {
+ follow: () => createFollowCamera({ subject: player.state, input }),
+ firstPerson: () => createFirstPersonCamera({ subject: player.state, input }),
+ topDown: () => createTopDownCamera({ subject: player.state }),
+ };
+ const requestedRig = new URLSearchParams(location.search).get('rig');
+ let rigKind = Object.hasOwn(RIG_FACTORIES, requestedRig ?? '') || requestedRig === 'static' ? requestedRig : 'follow';
+
+ // Only own keys: a name like "toString" from the URL or a button must not reach the prototype.
+ function createRig(kind) {
+ if (!Object.hasOwn(RIG_FACTORIES, kind)) throw new Error(`Unknown camera rig "${kind}"; use follow, firstPerson, topDown or static.`);
+ return RIG_FACTORIES[kind]();
+ }
+
const canvas = document.querySelector('canvas');
const renderer = new Renderer67(canvas, parts, { samples: 4, memoryBudgetMiB: 256 });
renderer.resize();
@@@ -144,8 -174,10 +208,10 @@@ function buildFrame(context)
const height = physicsSystem.previousHeight + (physicsSystem.height - physicsSystem.previousHeight) * blend;
ballPart.matrix = translation(-1.2, height, 0, BALL_RADIUS);
cubePart.matrix = rotationAroundY(spinSystem.angle, spinSystem.x, 0.5, 0);
+ placePlayer(context.alpha);
renderer.updateParts(parts);
- return { ...CAMERA, time: context.elapsedSeconds };
- const pose = runtime.getSystem('camera')?.pose(context) ?? { eye: [0, 3, 8], target: [0, 1.2, 0], up: [0, 1, 0] };
- return { ...pose, time: context.elapsedSeconds };
++ cameraFrame = runtime.getSystem('camera')?.pose(context) ?? CAMERA;
++ return { ...cameraFrame, time: context.elapsedSeconds };
}
let runtime = null;
@@@ -156,8 -194,9 +228,9 @@@ function startMode(mode)
mode,
fixedRate: FIXED_RATE,
frame: buildFrame,
- systems: [input, audio, physicsSystem, spinSystem],
- systems: [input, physicsSystem, spinSystem, player],
++ systems: [input, audio, physicsSystem, spinSystem, player],
});
+ if (rigKind !== 'static') runtime.addSystem(createRig(rigKind));
runtime.start();
for (const button of document.querySelectorAll('button[data-mode]')) {
button.setAttribute('aria-pressed', String(button.dataset.mode === mode));
@@@ -176,7 -230,8 +264,8 @@@ setInterval(() =>
`${stats.framesPerSecond.toFixed(0)} fps, ${stats.averageFrameMilliseconds.toFixed(1)} ms average, ${stats.maximumFrameMilliseconds.toFixed(1)} ms worst`,
`update ${stats.updateMilliseconds.toFixed(2)} ms, render ${stats.renderMilliseconds.toFixed(2)} ms`,
`fixed steps ${stats.fixedSteps}, alpha ${stats.alpha.toFixed(2)}`,
- 'A/D or arrows slide the cube, Space kicks the ball (sound starts at your first key press)',
+ `player ${player.state.grounded ? 'on the ground' : 'airborne'}, speed ${player.state.speed.toFixed(1)}`,
- 'WASD walks, Shift runs, Space jumps, arrows look, J/L slide the cube',
++ 'WASD walks, Shift runs, Space jumps, arrows look, J/L slide the cube (sound starts at your first key press)',
].join('\n');
}, 250);
diff --cc package.json
index 147d7358,a6e93059..319863db
--- a/package.json
+++ b/package.json
@@@ -43,7 -43,7 +43,8 @@@
"docs/physics.md",
"docs/runtime.md",
"docs/input.md",
+ "docs/audio.md",
+ "docs/player.md",
"docs/scene-query.md",
"docs/user-passes.md",
"docs/skeleton-pose.md",
@@@ -121,7 -121,7 +122,8 @@@
"./addons/cloth": "./build/cloth.js",
"./addons/runtime": "./build/runtime.js",
"./addons/input": "./build/input.js",
+ "./addons/audio": "./build/audio.js",
+ "./addons/player": "./build/player.js",
"./addons/update-part-vertices": "./build/update-part-vertices.js",
"./package.json": "./package.json"
},
diff --cc scripts/prepare-release.mjs
index dc6915b5,7ca8bfcd..d042c870
--- a/scripts/prepare-release.mjs
+++ b/scripts/prepare-release.mjs
@@@ -53,7 -53,7 +53,7 @@@ const sourceDocuments =
'vehicle.md',
'vegetation.md', 'terrain.md', 'grass.md', 'clutter.md', 'light-bake.md', 'path-tracer.md', 'gaussian-splats.md',
'camera-path.md', 'lighting.md', 'volumetric.md', 'film-physics.md',
- 'runtime.md', 'input.md', 'audio.md', 'surface-attachment.md', 'agent-scene.md', 'mcp.md', 'agent-look.md', 'agent-asset.md', 'agent-render-jobs.md', 'agent-motion.md', 'agent-character-motion.md', 'agent-review.md',
- 'runtime.md', 'input.md', 'player.md', 'surface-attachment.md', 'agent-scene.md', 'mcp.md', 'agent-look.md', 'agent-asset.md', 'agent-render-jobs.md', 'agent-motion.md', 'agent-character-motion.md', 'agent-review.md',
++ 'runtime.md', 'input.md', 'player.md', 'audio.md', 'surface-attachment.md', 'agent-scene.md', 'mcp.md', 'agent-look.md', 'agent-asset.md', 'agent-render-jobs.md', 'agent-motion.md', 'agent-character-motion.md', 'agent-review.md',
];
for (const name of sourceDocuments) {
await copyFile(resolve(root, 'docs', name), resolve(root, 'public', name));
diff --cc src/docs-live.js
index 6ce684bc,36cb15a8..52117612
--- a/src/docs-live.js
+++ b/src/docs-live.js
@@@ -64,7 -64,7 +64,8 @@@ const addonLoaders = import.meta.glob(
'./cloth.js',
'./runtime.js',
'./input.js',
+ './audio.js',
+ './player.js',
'./update-part-vertices.js',
]);
const moduleCache = new Map();
diff --cc vite.lib.config.js
index 69b1438b,7814be9f..cfc31ea7
--- a/vite.lib.config.js
+++ b/vite.lib.config.js
@@@ -107,7 -107,7 +107,8 @@@ export default defineConfig(
cloth: 'src/cloth.js',
runtime: 'src/runtime.js',
input: 'src/input.js',
+ audio: 'src/audio.js',
+ player: 'src/player.js',
'update-part-vertices':'src/update-part-vertices.js',
'replace-textured-parts': 'src/replace-textured-parts.js',
// Page of the MCP preview. It imports the engine chunks above instead of bundling its own copy.