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.

feat(editor): a mesh says what it is

esenginec2b8d4834a01
diff --git a/desktop/src/engine/meshSummary.ts b/desktop/src/engine/meshSummary.ts
new file mode 100644
index 000000000..62a92fd9e
--- /dev/null
+++ b/desktop/src/engine/meshSummary.ts
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file meshSummary.ts — what a `.esmesh` is, in the words the inspector shows.
+ *
+ * A mesh is the one asset whose file says everything about it and whose panel
+ * said nothing: an import writes several, named by index, and the only way to
+ * tell a body from a banner was to draw them. The file is self-describing —
+ * channel table, vertex count, bounds — so this reads it rather than guessing
+ * from a thumbnail.
+ */
+import { decodeMesh, MeshChannel } from 'esengine';
+
+export interface MeshSummary {
+ vertices: number;
+ triangles: number;
+ /** Local-space size, `w × h` (× d when the geometry has depth). */
+ extent: string;
+ /** The channels the file declares, in its own order: `position, uv, normal`. */
+ channels: string;
+}
+
+const CHANNEL_NAMES: Record<number, string> = {
+ [MeshChannel.Position]: 'position',
+ [MeshChannel.TexCoord0]: 'uv',
+ [MeshChannel.Color]: 'color',
+ [MeshChannel.Normal]: 'normal',
+ [MeshChannel.Tangent]: 'tangent',
+};
+
+/** Trim a measurement to something readable without lying about a small one. */
+function num(v: number): string {
+ return Math.abs(v) >= 100 ? String(Math.round(v)) : String(Math.round(v * 100) / 100);
+}
+
+/** Describe the mesh in `bytes`, or null when they are not a `.esmesh`. */
+export function summarizeMesh(bytes: Uint8Array): MeshSummary | null {
+ let mesh;
+ try {
+ mesh = decodeMesh(bytes);
+ } catch {
+ return null;
+ }
+ const size = mesh.aabbMax.map((v, i) => v - (mesh.aabbMin[i] ?? 0));
+ const flat = Math.abs(size[2] ?? 0) < 1e-6;
+ return {
+ vertices: mesh.vertexCount,
+ triangles: Math.floor(mesh.indices.length / 3),
+ extent: (flat ? size.slice(0, 2) : size).map(num).join(' × '),
+ channels: mesh.channels
+ .map((c) => CHANNEL_NAMES[c.semantic] ?? `#${c.semantic}`)
+ .join(', '),
+ };
+}
+
+/** Read and describe the mesh at a project path; null if it cannot be read. */
+export async function readMeshSummary(path: string): Promise<MeshSummary | null> {
+ try {
+ const res = await fetch(`estella://project/${path}`);
+ if (!res.ok) return null;
+ return summarizeMesh(new Uint8Array(await res.arrayBuffer()));
+ } catch {
+ return null;
+ }
+}
diff --git a/desktop/src/i18n/messages/details.ts b/desktop/src/i18n/messages/details.ts
index 2c3a288c5..442a3cfaa 100644
--- a/desktop/src/i18n/messages/details.ts
+++ b/desktop/src/i18n/messages/details.ts
@@ -245,6 +245,9 @@ export const detailsMessages = defineMessages({
'det.type': { en: 'Type', zh: '类型' },
'det.metaPath': { en: 'Path', zh: '路径' },
'det.metaDimensions': { en: 'Dimensions', zh: '尺寸' },
+ 'det.metaVertices': { en: 'Vertices', zh: '顶点数' },
+ 'det.metaTriangles': { en: 'Triangles', zh: '三角形' },
+ 'det.metaChannels': { en: 'Channels', zh: '通道' },
'det.metaSize': { en: 'Size', zh: '大小' },
'det.metaModified': { en: 'Modified', zh: '修改时间' },
'det.metaUuid': { en: 'UUID', zh: 'UUID' },
diff --git a/desktop/src/panels/Details.tsx b/desktop/src/panels/Details.tsx
index 33689744a..9376ee027 100644
--- a/desktop/src/panels/Details.tsx
+++ b/desktop/src/panels/Details.tsx
@@ -74,6 +74,7 @@ import type { AssetInspectorContribution, ComponentInspectorContribution } from
import * as imap from '@/project/inputMapDoc';
import * as ldoc from '@/project/localeTableDoc';
import { buildImporterComponent, applyImporterEdit } from '@/project/assetImporter';
+import { readMeshSummary, type MeshSummary } from '@/engine/meshSummary';
import { readTextureCookSettings } from '../../../pipeline/src/project/importSettings';
import { findAssetUsages } from '@/project/assetUsages';
import { FindUsagesDialog } from '@/components/FindUsagesDialog';
@@ -1901,6 +1902,7 @@ function GenericAssetInspector({ path }: { path: string }) {
const [dirty, setDirty] = useState(false);
const [stat, setStat] = useState<{ size: number; mtimeMs: number } | null>(null);
const [dims, setDims] = useState<string | null>(null);
+ const [mesh, setMesh] = useState<MeshSummary | null>(null);
const [refCount, setRefCount] = useState<number | null>(null);
const [collapsed, setCollapsed] = useState(false);
const [usagesOpen, setUsagesOpen] = useState(false);
@@ -1938,6 +1940,10 @@ function GenericAssetInspector({ path }: { path: string }) {
setDims(null);
setRefCount(null);
void window.estella?.fs?.stat(path).then((s) => alive && setStat(s)).catch(() => {});
+ // A mesh describes itself — channel table, counts, bounds — and nothing else
+ // in the editor can tell one import product from the next.
+ setMesh(null);
+ if (type === 'mesh') void readMeshSummary(path).then((m) => alive && setMesh(m));
if (isImage) {
const img = new Image();
img.onload = () => alive && setDims(`${img.naturalWidth} × ${img.naturalHeight}`);
@@ -1952,7 +1958,7 @@ function GenericAssetInspector({ path }: { path: string }) {
return () => {
alive = false;
};
- }, [path, isImage]);
+ }, [path, isImage, type]);
const assetRef = AssetRegistry.assetRef(path);
const comp = importer ? buildImporterComponent(type, importer) : null;
@@ -2081,6 +2087,10 @@ function GenericAssetInspector({ path }: { path: string }) {
<div className="cb-meta" style={{ padding: '8px 10px 0' }}>
<MetaRow k={t('det.metaPath')} v={path} mono />
{dims && <MetaRow k={t('det.metaDimensions')} v={dims} />}
+ {mesh && <MetaRow k={t('det.metaDimensions')} v={mesh.extent} />}
+ {mesh && <MetaRow k={t('det.metaVertices')} v={String(mesh.vertices)} />}
+ {mesh && <MetaRow k={t('det.metaTriangles')} v={String(mesh.triangles)} />}
+ {mesh && <MetaRow k={t('det.metaChannels')} v={mesh.channels} mono />}
{stat && <MetaRow k={t('det.metaSize')} v={formatBytes(stat.size)} />}
{stat && <MetaRow k={t('det.metaModified')} v={new Date(stat.mtimeMs).toLocaleString()} />}
{assetRef && <MetaRow k={t('det.metaUuid')} v={assetRef} mono />}
diff --git a/desktop/tests/mesh-summary.test.ts b/desktop/tests/mesh-summary.test.ts
new file mode 100644
index 000000000..81d905abc
--- /dev/null
+++ b/desktop/tests/mesh-summary.test.ts
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file mesh-summary.test.ts — a `.esmesh` describing itself to the inspector.
+ *
+ * The file is the authority (its channel table is written out per mesh), so the
+ * summary is read from the bytes rather than inferred from the import that made
+ * them — a hand-authored mesh has to read the same way.
+ */
+import { describe, it, expect } from 'vitest';
+import { encodeMesh, packChannels, MeshChannel, MeshChannelType } from 'esengine';
+import { summarizeMesh } from '@/engine/meshSummary';
+
+/** One quad, 200 x 100, with the channels a lit import writes. */
+function quad(withNormals: boolean): Uint8Array {
+ const { channels, vertexStride } = packChannels([
+ { semantic: MeshChannel.Position, components: 3, type: MeshChannelType.Float32 },
+ { semantic: MeshChannel.TexCoord0, components: 2, type: MeshChannelType.Float32 },
+ { semantic: MeshChannel.Color, components: 4, type: MeshChannelType.UNorm8 },
+ ...(withNormals
+ ? [{ semantic: MeshChannel.Normal, components: 3, type: MeshChannelType.Float32 }] : []),
+ ]);
+ return encodeMesh({
+ channels,
+ vertexStride,
+ vertexCount: 4,
+ vertices: new Uint8Array(4 * vertexStride),
+ indices: Uint32Array.from([0, 1, 2, 1, 3, 2]),
+ aabbMin: [-100, -50, 0],
+ aabbMax: [100, 50, 0],
+ });
+}
+
+describe('summarizeMesh', () => {
+ it('reads the counts, the size and the channels the file declares', () => {
+ expect(summarizeMesh(quad(true))).toEqual({
+ vertices: 4,
+ triangles: 2,
+ extent: '200 × 100', // flat: the depth is not a dimension worth a column
+ channels: 'position, uv, color, normal',
+ });
+ });
+
+ it('says which channels are absent by not naming them', () => {
+ expect(summarizeMesh(quad(false))?.channels).toBe('position, uv, color');
+ });
+
+ it('reports depth when the geometry has some', () => {
+ const { channels, vertexStride } = packChannels([
+ { semantic: MeshChannel.Position, components: 3, type: MeshChannelType.Float32 },
+ ]);
+ const box = encodeMesh({
+ channels, vertexStride, vertexCount: 3,
+ vertices: new Uint8Array(3 * vertexStride),
+ indices: Uint32Array.from([0, 1, 2]),
+ aabbMin: [0, 0, 0], aabbMax: [1.5, 2, 3],
+ });
+ expect(summarizeMesh(box)?.extent).toBe('1.5 × 2 × 3');
+ });
+
+ it('answers null for bytes that are not a mesh', () => {
+ expect(summarizeMesh(new TextEncoder().encode('not a mesh at all'))).toBeNull();
+ });
+});