Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glTF parse support custom extsnions and parser #1008

Merged
merged 26 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4376181
refactor: export gltf parsers for users
zhuxudong Sep 6, 2022
9b23f8f
Merge branch 'dev/1.0' into refactor/export-parser
zhuxudong Feb 21, 2023
62d4416
refactor: export relative variable
zhuxudong Feb 21, 2023
3ec1b17
refactor: support custom material parser
zhuxudong Feb 21, 2023
f47f002
refactor: export pipeline
zhuxudong Feb 21, 2023
e9a5c8b
refactor: rename
zhuxudong Feb 27, 2023
d5a892d
refactor: add warning
zhuxudong Feb 27, 2023
d825f22
refactor: rename
zhuxudong Feb 27, 2023
ef8e1dd
refactor: rename
zhuxudong Feb 27, 2023
0b36e5d
refactor: add resourceIndex to use customization
zhuxudong Feb 27, 2023
a9b9f86
refactor: extend resource
zhuxudong Feb 27, 2023
a7d1ad7
refactor: extension mode
zhuxudong Feb 28, 2023
ea1deea
refactor: use decorator
zhuxudong Feb 28, 2023
74d2a02
refactor: param
zhuxudong Feb 28, 2023
f1e60eb
refactor: add extension common parser
zhuxudong Mar 1, 2023
9043190
refactor: opt GLTFLoader code (#6)
GuoLei1990 Mar 1, 2023
be6b1d4
refactor: draco
zhuxudong Mar 1, 2023
80d30c8
refactor: opt code (#7)
GuoLei1990 Mar 1, 2023
55a3865
refactor: primitive additive parser
zhuxudong Mar 1, 2023
e363823
refactor: add extension data
zhuxudong Mar 1, 2023
43ee74f
refactor: private
zhuxudong Mar 1, 2023
a9d4648
refactor: add comments
zhuxudong Mar 1, 2023
583a4de
refactor: perf code
zhuxudong Mar 1, 2023
f390b7b
refactor: add comment
zhuxudong Mar 1, 2023
46b6b41
refactor: rename
zhuxudong Mar 1, 2023
ecab3fe
refactor: export type
zhuxudong Mar 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/loader/src/GLTFLoader.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { AssetPromise, AssetType, Loader, LoadItem, resourceLoader, ResourceManager } from "@oasis-engine/core";
import { GLTFParser } from "./gltf/GLTFParser";
import { GLTFPipeline } from "./gltf/GLTFPipeline";
import { GLTFResource } from "./gltf/GLTFResource";
import { ParserContext } from "./gltf/parser/ParserContext";
import { GLTFParserContext } from "./gltf/parser/GLTFParserContext";

@resourceLoader(AssetType.Prefab, ["gltf", "glb"])
export class GLTFLoader extends Loader<GLTFResource> {
load(item: LoadItem, resourceManager: ResourceManager): Record<string, AssetPromise<any>> {
const url = item.url;
const context = new ParserContext(url);
const { keepMeshData = false, pipeline } = item.params || {};
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
const context = new GLTFParserContext(url);
const glTFResource = new GLTFResource(resourceManager.engine, url);
const masterPromiseInfo = context.masterPromiseInfo;

context.glTFResource = glTFResource;
context.keepMeshData = item.params?.keepMeshData ?? false;
context.keepMeshData = keepMeshData;

masterPromiseInfo.onCancel(() => {
const { chainPromises } = context;
Expand All @@ -21,7 +22,7 @@ export class GLTFLoader extends Loader<GLTFResource> {
}
});

GLTFParser.defaultPipeline
(pipeline || GLTFPipeline.defaultPipeline)
.parse(context)
.then(masterPromiseInfo.resolve)
.catch((e) => {
Expand Down
63 changes: 0 additions & 63 deletions packages/loader/src/gltf/GLTFParser.ts

This file was deleted.

63 changes: 63 additions & 0 deletions packages/loader/src/gltf/GLTFPipeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { AssetPromise } from "@oasis-engine/core";
import { GLTFResource } from "./GLTFResource";
import { GLTFAnimationParser } from "./parser/GLTFAnimationParser";
import { GLTFBufferParser } from "./parser/GLTFBufferParser";
import { GLTFEntityParser } from "./parser/GLTFEntityParser";
import { GLTFMaterialParser } from "./parser/GLTFMaterialParser";
import { GLTFMeshParser } from "./parser/GLTFMeshParser";
import { GLTFParser } from "./parser/GLTFParser";
import { GLTFParserContext } from "./parser/GLTFParserContext";
import { GLTFSceneParser } from "./parser/GLTFSceneParser";
import { GLTFSkinParser } from "./parser/GLTFSkinParser";
import { GLTFTextureParser } from "./parser/GLTFTextureParser";
import { GLTFValidator } from "./parser/GLTFValidator";

export class GLTFPipeline {
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
static defaultPipeline = new GLTFPipeline(
GLTFBufferParser,
GLTFValidator,
GLTFTextureParser,
GLTFMaterialParser,
GLTFMeshParser,
GLTFEntityParser,
GLTFSkinParser,
GLTFAnimationParser,
GLTFSceneParser
);

private _parsers: GLTFParser[] = [];

constructor(...parsers: (new () => GLTFParser)[]) {
parsers.forEach((pipe: new () => GLTFParser, index: number) => {
this._parsers[index] = new pipe();
});
}

parse(context: GLTFParserContext): AssetPromise<GLTFResource> {
const glTFResource = context.glTFResource;
let lastParser;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add type


return new AssetPromise<GLTFResource>((resolve, reject) => {
this._parsers.forEach((parser: GLTFParser) => {
if (lastParser) {
lastParser = lastParser.then(() => {
return parser.parse(context);
});
if (lastParser.cancel) {
context.chainPromises.push(lastParser);
}
} else {
lastParser = parser.parse(context);
}
});

if (lastParser) {
lastParser
.then(() => {
resolve(glTFResource);
})
.catch(reject);
}
});
}
}
6 changes: 3 additions & 3 deletions packages/loader/src/gltf/GLTFUtil.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IndexFormat, TypedArray, VertexElementFormat } from "@oasis-engine/core";
import { Color, Vector2, Vector3, Vector4 } from "@oasis-engine/math";
import { BufferInfo, ParserContext } from "./parser/ParserContext";
import { AccessorComponentType, AccessorType, IAccessor, IBufferView, IGLTF } from "./Schema";
import { BufferInfo, GLTFParserContext } from "./parser/GLTFParserContext";
import { AccessorComponentType, AccessorType, IAccessor, IBufferView, IGLTF } from "./GLTFSchema";

const charCodeOfDot = ".".charCodeAt(0);
const reEscapeChar = /\\(\\)?/g;
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -147,7 +147,7 @@ export class GLTFUtil {
}
}

static getAccessorBuffer(context: ParserContext, gltf: IGLTF, accessor: IAccessor): BufferInfo {
static getAccessorBuffer(context: GLTFParserContext, gltf: IGLTF, accessor: IAccessor): BufferInfo {
const { buffers } = context;
const bufferViews = gltf.bufferViews;

Expand Down
22 changes: 0 additions & 22 deletions packages/loader/src/gltf/extensions/ExtensionParser.ts

This file was deleted.

22 changes: 22 additions & 0 deletions packages/loader/src/gltf/extensions/GLTFExtensionParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { EngineObject } from "@oasis-engine/core";
import { GLTFParserContext } from "../parser/GLTFParserContext";
import { GLTFExtensionSchema } from "./GLTFExtensionSchema";

export abstract class GLTFExtensionParser {
initialize(): void {}

parseEngineResource(
schema: GLTFExtensionSchema,
parseResource: EngineObject,
context: GLTFParserContext,
...extra
): void | Promise<void> {}

createEngineResource(
schema: GLTFExtensionSchema,
context: GLTFParserContext,
...extra
): EngineObject | Promise<EngineObject> {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IMaterialNormalTextureInfo, ITextureInfo } from "../Schema";
import { IMaterialNormalTextureInfo, ITextureInfo } from "../GLTFSchema";

/**
* Interfaces from the KHR_lights_punctual extension
Expand Down Expand Up @@ -159,7 +159,7 @@ export interface IOasisMaterialRemap {
isClone?: boolean;
}

export type ExtensionSchema =
export type GLTFExtensionSchema =
| IKHRLightsPunctual_Light
| IKHRDracoMeshCompression
| IKHRMaterialsClearcoat
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { DRACODecoder } from "@oasis-engine/draco";
import { GLTFUtil } from "../GLTFUtil";
import { registerExtension } from "../parser/Parser";
import { ParserContext } from "../parser/ParserContext";
import { IMeshPrimitive } from "../Schema";
import { ExtensionParser } from "./ExtensionParser";
import { IKHRDracoMeshCompression } from "./Schema";
import { registerGLTFExtension } from "../parser/GLTFParser";
import { GLTFParserContext } from "../parser/GLTFParserContext";
import { IMeshPrimitive } from "../GLTFSchema";
import { GLTFExtensionParser } from "./GLTFExtensionParser";
import { IKHRDracoMeshCompression } from "./GLTFExtensionSchema";

@registerExtension("KHR_draco_mesh_compression")
class KHR_draco_mesh_compression extends ExtensionParser {
@registerGLTFExtension("KHR_draco_mesh_compression")
class KHR_draco_mesh_compression extends GLTFExtensionParser {
private static _decoder: DRACODecoder;

initialize(): void {
Expand All @@ -16,7 +16,7 @@ class KHR_draco_mesh_compression extends ExtensionParser {
}
}

createEngineResource(schema: IKHRDracoMeshCompression, context: ParserContext, gltfPrimitive: IMeshPrimitive) {
createEngineResource(schema: IKHRDracoMeshCompression, context: GLTFParserContext, gltfPrimitive: IMeshPrimitive) {
const { gltf, buffers } = context;
const { bufferViews, accessors } = gltf;
const { bufferView: bufferViewIndex, attributes: gltfAttributeMap } = schema;
Expand Down
16 changes: 8 additions & 8 deletions packages/loader/src/gltf/extensions/KHR_lights_punctual.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { DirectLight, Entity, PointLight, SpotLight } from "@oasis-engine/core";
import { registerExtension } from "../parser/Parser";
import { ParserContext } from "../parser/ParserContext";
import { ExtensionParser } from "./ExtensionParser";
import { IKHRLightsPunctual_Light } from "./Schema";

@registerExtension("KHR_lights_punctual")
class KHR_lights_punctual extends ExtensionParser {
parseEngineResource(schema: IKHRLightsPunctual_Light, entity: Entity, context: ParserContext): void {
import { registerGLTFExtension } from "../parser/GLTFParser";
import { GLTFParserContext } from "../parser/GLTFParserContext";
import { GLTFExtensionParser } from "./GLTFExtensionParser";
import { IKHRLightsPunctual_Light } from "./GLTFExtensionSchema";

@registerGLTFExtension("KHR_lights_punctual")
class KHR_lights_punctual extends GLTFExtensionParser {
parseEngineResource(schema: IKHRLightsPunctual_Light, entity: Entity, context: GLTFParserContext): void {
const { color, intensity = 1, type, range, spot } = schema;
const glTFResource = context.glTFResource;
let light: DirectLight | PointLight | SpotLight;
Expand Down
22 changes: 11 additions & 11 deletions packages/loader/src/gltf/extensions/KHR_materials_clearcoat.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { PBRMaterial } from "@oasis-engine/core";
import { MaterialParser } from "../parser/MaterialParser";
import { registerExtension } from "../parser/Parser";
import { ParserContext } from "../parser/ParserContext";
import { ExtensionParser } from "./ExtensionParser";
import { IKHRMaterialsClearcoat } from "./Schema";
import { GLTFMaterialParser } from "../parser/GLTFMaterialParser";
import { registerGLTFExtension } from "../parser/GLTFParser";
import { GLTFParserContext } from "../parser/GLTFParserContext";
import { GLTFExtensionParser } from "./GLTFExtensionParser";
import { IKHRMaterialsClearcoat } from "./GLTFExtensionSchema";

@registerExtension("KHR_materials_clearcoat")
class KHR_materials_clearcoat extends ExtensionParser {
parseEngineResource(schema: IKHRMaterialsClearcoat, material: PBRMaterial, context: ParserContext): void {
@registerGLTFExtension("KHR_materials_clearcoat")
class KHR_materials_clearcoat extends GLTFExtensionParser {
parseEngineResource(schema: IKHRMaterialsClearcoat, material: PBRMaterial, context: GLTFParserContext): void {
const { textures } = context.glTFResource;
const {
clearcoatFactor = 0,
Expand All @@ -22,15 +22,15 @@ class KHR_materials_clearcoat extends ExtensionParser {

if (clearcoatTexture) {
material.clearCoatTexture = textures[clearcoatTexture.index];
MaterialParser._checkOtherTextureTransform(clearcoatTexture, "Clear coat");
GLTFMaterialParser._checkOtherTextureTransform(clearcoatTexture, "Clear coat");
}
if (clearcoatRoughnessTexture) {
material.clearCoatRoughnessTexture = textures[clearcoatRoughnessTexture.index];
MaterialParser._checkOtherTextureTransform(clearcoatRoughnessTexture, "Clear coat roughness");
GLTFMaterialParser._checkOtherTextureTransform(clearcoatRoughnessTexture, "Clear coat roughness");
}
if (clearcoatNormalTexture) {
material.clearCoatNormalTexture = textures[clearcoatNormalTexture.index];
MaterialParser._checkOtherTextureTransform(clearcoatNormalTexture, "Clear coat normal");
GLTFMaterialParser._checkOtherTextureTransform(clearcoatNormalTexture, "Clear coat normal");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { PBRSpecularMaterial } from "@oasis-engine/core";
import { Color } from "@oasis-engine/math";
import { MaterialParser } from "../parser/MaterialParser";
import { Parser, registerExtension } from "../parser/Parser";
import { ParserContext } from "../parser/ParserContext";
import { ExtensionParser } from "./ExtensionParser";
import { IKHRMaterialsPbrSpecularGlossiness } from "./Schema";
import { GLTFMaterialParser } from "../parser/GLTFMaterialParser";
import { GLTFParser, registerGLTFExtension } from "../parser/GLTFParser";
import { GLTFParserContext } from "../parser/GLTFParserContext";
import { GLTFExtensionParser } from "./GLTFExtensionParser";
import { IKHRMaterialsPbrSpecularGlossiness } from "./GLTFExtensionSchema";

@registerExtension("KHR_materials_pbrSpecularGlossiness")
class KHR_materials_pbrSpecularGlossiness extends ExtensionParser {
createEngineResource(schema: IKHRMaterialsPbrSpecularGlossiness, context: ParserContext): PBRSpecularMaterial {
@registerGLTFExtension("KHR_materials_pbrSpecularGlossiness")
class KHR_materials_pbrSpecularGlossiness extends GLTFExtensionParser {
createEngineResource(schema: IKHRMaterialsPbrSpecularGlossiness, context: GLTFParserContext): PBRSpecularMaterial {
const { engine, textures } = context.glTFResource;
const material = new PBRSpecularMaterial(engine);
const { diffuseFactor, diffuseTexture, specularFactor, glossinessFactor, specularGlossinessTexture } = schema;
Expand All @@ -26,7 +26,7 @@ class KHR_materials_pbrSpecularGlossiness extends ExtensionParser {
material.baseTexture = textures[diffuseTexture.index];
const KHR_texture_transform = diffuseTexture.extensions?.KHR_texture_transform;
if (KHR_texture_transform) {
Parser.parseEngineResource("KHR_texture_transform", KHR_texture_transform, material, context);
GLTFParser.parseEngineResource("KHR_texture_transform", KHR_texture_transform, material, context);
}
}

Expand All @@ -44,7 +44,7 @@ class KHR_materials_pbrSpecularGlossiness extends ExtensionParser {

if (specularGlossinessTexture) {
material.specularGlossinessTexture = textures[specularGlossinessTexture.index];
MaterialParser._checkOtherTextureTransform(specularGlossinessTexture, "Specular glossiness");
GLTFMaterialParser._checkOtherTextureTransform(specularGlossinessTexture, "Specular glossiness");
}

return material;
Expand Down
14 changes: 7 additions & 7 deletions packages/loader/src/gltf/extensions/KHR_materials_unlit.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { UnlitMaterial } from "@oasis-engine/core";
import { registerExtension } from "../parser/Parser";
import { ParserContext } from "../parser/ParserContext";
import { ExtensionParser } from "./ExtensionParser";
import { IKHRMaterialsUnlit } from "./Schema";
import { registerGLTFExtension } from "../parser/GLTFParser";
import { GLTFParserContext } from "../parser/GLTFParserContext";
import { GLTFExtensionParser } from "./GLTFExtensionParser";
import { IKHRMaterialsUnlit } from "./GLTFExtensionSchema";

@registerExtension("KHR_materials_unlit")
class KHR_materials_unlit extends ExtensionParser {
createEngineResource(schema: IKHRMaterialsUnlit, context: ParserContext): UnlitMaterial {
@registerGLTFExtension("KHR_materials_unlit")
class KHR_materials_unlit extends GLTFExtensionParser {
createEngineResource(schema: IKHRMaterialsUnlit, context: GLTFParserContext): UnlitMaterial {
const { engine } = context.glTFResource;
const material = new UnlitMaterial(engine);

Expand Down
Loading