Skip to content

Commit

Permalink
initial structure meshoptDecoder as manual addon
Browse files Browse the repository at this point in the history
  • Loading branch information
MatsErdkamp committed May 30, 2024
1 parent 0f347a3 commit 4cf2aa7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/gltf/gltf-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { Skin } from "../skinning/skin";
import { Joint } from "../skinning/joint";
import { StandardMaterialFactory } from "../material/standard/standard-material-factory";
import { glTFChannelFactory } from "./animation/gltf-channel-factory";
import { MeshoptDecoder } from "meshoptimizer";

/**
* Parses glTF assets and creates models and meshes.
Expand All @@ -27,16 +26,19 @@ export class glTFParser {
private _materialFactory: MaterialFactory;
private _descriptor: any;
private _decompressedBuffers: Map<number, ArrayBuffer>;
private _meshoptDecoder: any;

/**
* Creates a new parser using the specified asset.
* @param asset The asset to parse.
* @param materialFactory The material factory to use.
* @param meshoptDecoder optionally provide meshoptDecoder to parse meshopt meshes.
*/
constructor(asset: glTFAsset, materialFactory?: MaterialFactory) {
constructor(asset: glTFAsset, materialFactory?: MaterialFactory, meshoptDecoder?: any) {
this._asset = asset;
this._materialFactory = materialFactory || new StandardMaterialFactory();
this._descriptor = this._asset.descriptor;
this._meshoptDecoder = meshoptDecoder;
this._decompressedBuffers = new Map<number, ArrayBuffer>();
if (asset.textures.length === 0) {
for (let i = 0; i < this._descriptor.textures?.length; i++) {
Expand All @@ -50,8 +52,8 @@ export class glTFParser {
* @param asset The asset to create the model from.
* @param materialFactory The material factory to use.
*/
static createModel(asset: glTFAsset, materialFactory?: MaterialFactory) {
return new glTFParser(asset, materialFactory).parseModel();
static createModel(asset: glTFAsset, materialFactory?: MaterialFactory, meshoptDecoder ?: any) {
return new glTFParser(asset, materialFactory, meshoptDecoder).parseModel();
}

/**
Expand Down Expand Up @@ -98,6 +100,12 @@ export class glTFParser {
let buffer = this._asset.buffers[bufferView.buffer];

if (bufferView.extensions?.EXT_meshopt_compression != undefined) {

if (this._meshoptDecoder == undefined) {
console.error('Buffer uses EXT_meshopt_compression but meshoptDecoder is not provided.');
return;
}

const meshoptExtension = bufferView.extensions.EXT_meshopt_compression;
buffer = this.decodeMeshoptBuffer(meshoptExtension, accessor.bufferView);
offset = accessor.byteOffset || 0;
Expand Down Expand Up @@ -138,7 +146,7 @@ export class glTFParser {

const resultBuffer = new Uint8Array(count * stride);

MeshoptDecoder.decodeGltfBuffer(
this._meshoptDecoder.decodeGltfBuffer(
resultBuffer,
count,
stride,
Expand Down
4 changes: 2 additions & 2 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class Model extends Container3D {
* @param source The source to create the model from.
* @param materialFactory The factory to use for creating materials.
*/
static from(source: glTFAsset, materialFactory?: MaterialFactory) {
return glTFParser.createModel(source, materialFactory)
static from(source: glTFAsset, materialFactory?: MaterialFactory, meshoptDecoder ?: any) {
return glTFParser.createModel(source, materialFactory, meshoptDecoder)
}

/**
Expand Down

0 comments on commit 4cf2aa7

Please sign in to comment.