Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

test(assets): an animated model through the editor's import door

esengine8184f3c49952
diff --git a/desktop/scripts/editor-checks/model-animation.mjs b/desktop/scripts/editor-checks/model-animation.mjs
new file mode 100644
index 000000000..53e39043f
--- /dev/null
+++ b/desktop/scripts/editor-checks/model-animation.mjs
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file model-animation — an animated model through the editor's import door.
+ *
+ * The pixel gate plays a clip the CLI imported; this is the other door, where
+ * the products are written by the bundled main process and the prefab has to
+ * come out pointing at a file that is really beside it. A clip written under a
+ * name nothing references is exactly as invisible as no clip at all.
+ */
+import { mkdir, readFile, writeFile } from 'node:fs/promises';
+import path from 'node:path';
+import { makeProject, checker } from '../lib/editorDriver.mjs';
+import { animatedTriangle } from '../lib/gltfFixtures.mjs';
+
+export const name = 'model-animation';
+export const describes = 'an imported model writes its clip and its prefab points at it';
+
+export async function run(ed) {
+ const root = await makeProject({
+ 'project.esproject': JSON.stringify({ name: 'Animated', version: '1.0' }, null, 2),
+ 'assets/scenes/main.esscene': JSON.stringify({
+ version: '1.0', name: 'Main',
+ entities: [{
+ id: 0, name: 'Camera', parent: null, children: [], visible: true,
+ components: [
+ { type: 'Transform', data: { position: { x: 0, y: 0, z: 10 } } },
+ { type: 'Camera', data: { projectionType: 1, orthoSize: 300, isActive: true, priority: 0 } },
+ ],
+ }],
+ }, null, 1),
+ });
+ await mkdir(path.join(root, 'assets'), { recursive: true });
+ const source = path.join(root, 'assets', 'turner.gltf');
+ await writeFile(source, animatedTriangle());
+
+ const check = checker();
+ await ed.open(root, 'assets/scenes/main.esscene');
+ const result = await ed.json('import_assets', { destDir: 'assets', sources: [source] }, 120000);
+ const imported = result?.imported ?? [];
+ if (!check(imported.includes('assets/turner_Turn.estimeline'),
+ `import reported ${JSON.stringify(imported)}`)) {
+ return check.failures;
+ }
+
+ const read = async (rel) => {
+ try {
+ return JSON.parse(await readFile(path.join(root, rel), 'utf8'));
+ } catch {
+ return null;
+ }
+ };
+ const clip = await read('assets/turner_Turn.estimeline');
+ if (!check(clip != null, 'the clip is named in the import but is not on disk')) return check.failures;
+ check(clip.duration === 1, `the clip is ${clip.duration}s, not the 1s the source says`);
+ const track = clip.tracks?.[0];
+ check(track?.component === 'Transform' && track?.childPath === '',
+ `the track addresses ${JSON.stringify({ c: track?.component, p: track?.childPath })}`);
+ check(track?.channels?.map((c) => c.property).join() === 'rotation.x,rotation.y,rotation.z,rotation.w',
+ `the channels are ${JSON.stringify(track?.channels?.map((c) => c.property))}`);
+ // The lone node's own turn: a quarter circle about Y is (0, √½, 0, √½).
+ const y = track?.channels?.[1]?.keyframes?.[1]?.value ?? 0;
+ check(Math.abs(y - Math.SQRT1_2) < 1e-5, `the final rotation reads y=${y}`);
+
+ const prefab = await read('assets/turner.esprefab');
+ const player = prefab?.entities?.[0]?.components?.find((c) => c.type === 'TimelinePlayer');
+ check(player?.data?.timeline === 'assets/turner_Turn.estimeline',
+ `the prefab points at ${JSON.stringify(player?.data?.timeline)}`);
+ check(player?.data?.playing === false, 'the imported prefab plays on its own');
+ return check.failures;
+}
diff --git a/desktop/scripts/lib/gltfFixtures.mjs b/desktop/scripts/lib/gltfFixtures.mjs
index 0f45a82a7..f4105317e 100644
--- a/desktop/scripts/lib/gltfFixtures.mjs
+++ b/desktop/scripts/lib/gltfFixtures.mjs
@@ -28,6 +28,44 @@ const MESHES = [{
const raw = (a) => new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
const encode = (doc) => new TextEncoder().encode(JSON.stringify(doc));
+/**
+ * The triangle under a named node, turned a quarter circle about Y over a
+ * second — an exporter's node animation once nothing is skinned.
+ */
+export function animatedTriangle() {
+ const times = new Float32Array([0, 1]);
+ const rotations = new Float32Array([0, 0, 0, 1, 0, Math.SQRT1_2, 0, Math.SQRT1_2]);
+ const geo = Buffer.concat([Buffer.from(raw(POSITIONS)), Buffer.from(raw(UVS)), Buffer.from(raw(INDICES))]);
+ const pad = Buffer.alloc((4 - (geo.length % 4)) % 4);
+ const bytes = Buffer.concat([geo, pad, Buffer.from(raw(times)), Buffer.from(raw(rotations))]);
+ const timesAt = geo.length + pad.length;
+ return encode({
+ asset: { version: '2.0' },
+ buffers: [{ byteLength: bytes.length, uri: `data:application/octet-stream;base64,${bytes.toString('base64')}` }],
+ bufferViews: [
+ { buffer: 0, byteOffset: 0, byteLength: 36 },
+ { buffer: 0, byteOffset: 36, byteLength: 24 },
+ { buffer: 0, byteOffset: 60, byteLength: 6 },
+ { buffer: 0, byteOffset: timesAt, byteLength: times.byteLength },
+ { buffer: 0, byteOffset: timesAt + times.byteLength, byteLength: rotations.byteLength },
+ ],
+ accessors: [
+ ...ACCESSORS,
+ { bufferView: 3, componentType: 5126, count: 2, type: 'SCALAR', min: [0], max: [1] },
+ { bufferView: 4, componentType: 5126, count: 2, type: 'VEC4' },
+ ],
+ meshes: MESHES,
+ nodes: [{ name: 'Turner', mesh: 0 }],
+ scenes: [{ nodes: [0] }],
+ scene: 0,
+ animations: [{
+ name: 'Turn',
+ channels: [{ sampler: 0, target: { node: 0, path: 'rotation' } }],
+ samplers: [{ input: 3, output: 4, interpolation: 'LINEAR' }],
+ }],
+ });
+}
+
/** The triangle with its bytes as they are — what any exporter writes by default. */
export function plainTriangle() {
const bytes = Buffer.concat([Buffer.from(raw(POSITIONS)), Buffer.from(raw(UVS)), Buffer.from(raw(INDICES))]);