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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
* @file skeletal/submitMeshes.ts
* @brief Handing one entity's posed geometry to the engine core.
*
* @details One entry point serves every skeletal runtime: it takes vertices,
* indices, a texture, a blend mode, an entity, a scale, two flips and a
* layer, and nothing in that list knows what posed them.
*
* The copy is the delicate part. A module's batch bytes live in ITS heap,
* and the core reads from the CORE's — so each batch is copied into an
* engine-side scratch arena and submitted while the source view is still
* live. Nothing intermediate is allocated, and nothing is cached between
* frames.
*/
import { withScratch } from '../wasm/wasmScratch';
import type { Entity } from '../types';
import type { MeshBatchVisitor } from './meshBatches';
import type { SkeletalProbe } from '../spine/spineMetrics';
/** How an entity's skeleton sits in the world, independent of what posed it. */
export interface SkeletalSubmitProps {
skeletonScale: number;
flipX: boolean;
flipY: boolean;
layer: number;
}
/**
* The material a skeleton is drawn with, as the engine knows it (0 = none).
* Read per frame from the entity's own component rather than mirrored into the
* manager: a material handle is whatever the component says this frame, and a
* copy kept beside the pose would answer with the previous one.
*/
export type SkeletalMaterialOf = (entity: Entity) => number;
/** The slice of the engine core this needs. */
export interface SkeletalSubmitCore {
renderer_submitSkeletalBatchByEntity?(
registry: unknown,
verticesPtr: number, vertexCount: number,
indicesPtr: number, indexCount: number,
textureId: number, blendMode: number,
entity: number, skeletonScale: number, flipX: boolean, flipY: boolean,
layer: number, depth: number, materialId: number,
): void;
renderer_submitSkeletalBatchByEntityEx?(
registry: unknown,
verticesPtr: number, vertexCount: number,
indicesPtr: number, indexCount: number,
textureId: number, blendMode: number,
entity: number, skeletonScale: number, flipX: boolean, flipY: boolean,
layer: number, depth: number, materialId: number, premultipliedColor: boolean,
): void;
_malloc?(size: number): number;
_free?(ptr: number): void;
HEAPU8?: Uint8Array;
}
/** Iterates a module's batches for one instance. */
export type MeshBatchWalker = (cb: MeshBatchVisitor) => void;
/**
* Copy each posed batch into the core's heap and submit it for `entity`.
*
* Returns false when the core cannot accept geometry at all (no submit export, no
* heap) — the caller stops rather than repeating the check per entity.
*/
export function submitEntityMeshes(
core: SkeletalSubmitCore,
registry: unknown,
entity: Entity,
props: SkeletalSubmitProps,
walk: MeshBatchWalker,
materialOf?: SkeletalMaterialOf,
probe?: SkeletalProbe,
): boolean {
const submitEx = core.renderer_submitSkeletalBatchByEntityEx;
const submit = submitEx ?? core.renderer_submitSkeletalBatchByEntity;
const heap = core.HEAPU8;
if (!submit || !heap || !core._malloc || !core._free) return false;
const allocator = { _malloc: core._malloc, _free: core._free };
const materialId = materialOf?.(entity) ?? 0;
withScratch(allocator, alloc => {
walk((vertBytes, idxBytes, vertexCount, indexCount, textureId, blendMode, premultipliedColor) => {
const dstVert = alloc(vertBytes.byteLength);
const dstIdx = alloc(idxBytes.byteLength);
heap.set(vertBytes, dstVert);
heap.set(idxBytes, dstIdx);
if (probe) {
probe.abi.submit++;
probe.bytes.coreWrite += vertBytes.byteLength + idxBytes.byteLength;
}
if (submitEx) submitEx.call(core, registry,
dstVert, vertexCount, dstIdx, indexCount, textureId, blendMode, entity as number,
props.skeletonScale, props.flipX, props.flipY, props.layer, 0, materialId,
premultipliedColor ?? (blendMode === 4 || blendMode === 5));
else core.renderer_submitSkeletalBatchByEntity!.call(core, registry,
dstVert, vertexCount, dstIdx, indexCount, textureId, blendMode, entity as number,
props.skeletonScale, props.flipX, props.flipY, props.layer, 0, materialId);
});
});
return true;
}