Enable delay initialization of the MeshoptDecoder (#2146)

* feat: enable delay initialization of the `MeshoptDecoder` of glTF
This commit is contained in:
Hu Song
2024-07-02 11:29:49 +08:00
committed by GitHub
parent 1ed35fa4f5
commit 5669ab9ece
4 changed files with 111 additions and 79 deletions

View File

@@ -3,7 +3,7 @@
* @category Animation
*/
import { OrbitControl } from "@galacean/engine-toolkit";
import { Camera, DirectLight, GLTFResource, Logger, Vector3, WebGLEngine } from "@galacean/engine";
import { Camera, DirectLight, GLTFLoader, GLTFResource, Logger, Vector3, WebGLEngine } from "@galacean/engine";
import { initScreenshot, updateForE2E } from "./.mockForE2E";
Logger.enable();
@@ -37,5 +37,8 @@ WebGLEngine.create({ canvas: "canvas" }).then((engine) => {
updateForE2E(engine, 100);
initScreenshot(engine, camera);
})
.then(() => {
GLTFLoader.release();
});
});

View File

@@ -10,7 +10,7 @@ import {
} from "@galacean/engine-core";
import { GLTFResource } from "./gltf/GLTFResource";
import { GLTFParserContext } from "./gltf/parser";
import { MeshoptDecoder } from "./gltf/extensions/MeshoptDecoder";
import { getMeshoptDecoder, ready } from "./gltf/extensions/MeshoptDecoder";
@resourceLoader(AssetType.GLTF, ["gltf", "glb"])
export class GLTFLoader extends Loader<GLTFResource> {
@@ -19,14 +19,19 @@ export class GLTFLoader extends Loader<GLTFResource> {
* @remarks If use loader after releasing, we should release again.
*/
static release(): void {
MeshoptDecoder.release();
if (ready) {
getMeshoptDecoder().then((meshoptDecoder) => {
meshoptDecoder.release();
});
}
}
override initialize(_: Engine, configuration: EngineConfiguration): Promise<void> {
const meshOptOptions = configuration.glTF?.meshOpt;
const meshOptOptions = configuration.glTFLoader?.meshOpt ?? configuration.glTF?.meshOpt;
if (meshOptOptions) {
MeshoptDecoder.workerCount = meshOptOptions.workerCount;
MeshoptDecoder.useWorkers();
return getMeshoptDecoder().then((meshoptDecoder) => {
meshoptDecoder.useWorkers(meshOptOptions.workerCount);
});
}
return Promise.resolve();
}

View File

@@ -2,18 +2,20 @@ import { registerGLTFExtension } from "../parser/GLTFParser";
import { GLTFParserContext, GLTFParserType } from "../parser/GLTFParserContext";
import { GLTFExtensionMode, GLTFExtensionParser } from "./GLTFExtensionParser";
import { IEXTMeshoptCompressionSchema } from "./GLTFExtensionSchema";
import { MeshoptDecoder } from "./MeshoptDecoder";
import { getMeshoptDecoder } from "./MeshoptDecoder";
@registerGLTFExtension("EXT_meshopt_compression", GLTFExtensionMode.CreateAndParse)
class EXT_meshopt_compression extends GLTFExtensionParser {
override createAndParse(context: GLTFParserContext, schema: IEXTMeshoptCompressionSchema): Promise<Uint8Array> {
return context.get<ArrayBuffer>(GLTFParserType.Buffer, schema.buffer).then((arrayBuffer) => {
return MeshoptDecoder.decodeGltfBuffer(
schema.count,
schema.byteStride,
new Uint8Array(arrayBuffer, schema.byteOffset, schema.byteLength),
schema.mode,
schema.filter
return getMeshoptDecoder().then((decoder) =>
decoder.decodeGltfBuffer(
schema.count,
schema.byteStride,
new Uint8Array(arrayBuffer, schema.byteOffset, schema.byteLength),
schema.mode,
schema.filter
)
);
});
}
@@ -21,12 +23,25 @@ class EXT_meshopt_compression extends GLTFExtensionParser {
declare module "@galacean/engine-core" {
interface EngineConfiguration {
/** glTF loader options. */
glTFLoader?: {
/** Meshopt options. If set this option and workCount is great than 0, workers will be created. */
meshOpt?: {
/**
* Worker count for transcoder.
* @defaultValue 4
*/
workerCount?: number;
};
};
/** @deprecated glTF loader options. */
glTF?: {
/** Meshopt options. If set this option and workCount is great than 0, workers will be created. */
meshOpt?: {
/** Worker count for transcoder, default is 4. */
workerCount: number;
/**
* Worker count for transcoder.
* @defaultValue 4
*/
workerCount?: number;
};
};
}

File diff suppressed because one or more lines are too long