Skip to content

Commit

Permalink
Merge commit 'add6e916a8327e492f8fec16bc0e7cd543a59566' into pr/conte…
Browse files Browse the repository at this point in the history
…xt-restore

* commit 'add6e916a8327e492f8fec16bc0e7cd543a59566':
  Move font map cache from `Font` to `Engine` (galacean#1387)
  "v1.0.0-alpha.0"
  glTF parse support custom extsnions and parser (galacean#1008)
  Optimization `Transform` direction related API (galacean#1381)
  refactor: opt code
  refactor: opt code
  refactor: opt code
  refactor: opt code
  refactor: opt code
  feat: refactor transform API
  Add `toJSON` in base math class (galacean#1380)
  • Loading branch information
GuoLei1990 committed Mar 2, 2023
2 parents 64c6499 + add6e91 commit 4dbb60e
Show file tree
Hide file tree
Showing 29 changed files with 262 additions and 141 deletions.
2 changes: 1 addition & 1 deletion packages/design/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oasis-engine/design",
"version": "0.9.0-beta.66",
"version": "1.0.0-alpha.0",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
Expand Down
2 changes: 1 addition & 1 deletion packages/draco/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oasis-engine/draco",
"version": "0.9.0-beta.66",
"version": "1.0.0-alpha.0",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
Expand Down
2 changes: 1 addition & 1 deletion packages/loader/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oasis-engine/loader",
"version": "0.9.0-beta.66",
"version": "1.0.0-alpha.0",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
Expand Down
2 changes: 1 addition & 1 deletion packages/loader/src/GLTFContentRestorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { AssetPromise, BlendShape, Buffer, ContentRestorer, ModelMesh, request,
import { RequestConfig } from "@oasis-engine/core/types/asset/request";
import { Vector2 } from "@oasis-engine/math";
import { GLTFResource } from "./gltf/GLTFResource";
import { IBufferView } from "./gltf/GLTFSchema";
import { GLTFUtil } from "./gltf/GLTFUtil";
import { IBufferView } from "./gltf/Schema";

/**
* @internal
Expand Down
17 changes: 10 additions & 7 deletions packages/loader/src/GLTFLoader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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";
import { GLTFContentRestorer } from "./GLTFContentRestorer";

@resourceLoader(AssetType.Prefab, ["gltf", "glb"])
Expand All @@ -10,15 +10,16 @@ export class GLTFLoader extends Loader<GLTFResource> {
* @override
*/
load(item: LoadItem, resourceManager: ResourceManager): Record<string, AssetPromise<any>> {
const url = item.url;
const context = new ParserContext(url);
const { url } = item;
const params = <GLTFParams>item.params;
const context = new GLTFParserContext(url);
const glTFResource = new GLTFResource(resourceManager.engine, url);
const restorer = new GLTFContentRestorer(glTFResource);
const masterPromiseInfo = context.masterPromiseInfo;

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

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

GLTFParser.defaultPipeline
.parse(context)
(params?.pipeline || GLTFPipeline.defaultPipeline)
._parse(context)
.then((glTFResource) => {
resourceManager.addContentRestorer(restorer);
masterPromiseInfo.resolve(glTFResource);
Expand All @@ -48,4 +49,6 @@ export class GLTFLoader extends Loader<GLTFResource> {
export interface GLTFParams {
/** Keep raw mesh data for glTF parser, default is false. */
keepMeshData: boolean;
/** Custom glTF pipeline. */
pipeline: GLTFPipeline;
}
63 changes: 0 additions & 63 deletions packages/loader/src/gltf/GLTFParser.ts

This file was deleted.

76 changes: 76 additions & 0 deletions packages/loader/src/gltf/GLTFPipeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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";

/**
* GLTF pipeline.
*/
export class GLTFPipeline {
/**
* Default pipeline.
*/
static defaultPipeline = new GLTFPipeline(
GLTFBufferParser,
GLTFValidator,
GLTFTextureParser,
GLTFMaterialParser,
GLTFMeshParser,
GLTFEntityParser,
GLTFSkinParser,
GLTFAnimationParser,
GLTFSceneParser
);

private _parsers: GLTFParser[] = [];

/**
* Constructor of GLTFPipeline.
* @param parsers - Parsers to be executed in order
*/
constructor(...parsers: (new () => GLTFParser)[]) {
parsers.forEach((pipe: new () => GLTFParser, index: number) => {
this._parsers[index] = new pipe();
});
}

/**
* @internal
*/
_parse(context: GLTFParserContext): AssetPromise<GLTFResource> {
const glTFResource = context.glTFResource;
let lastParser;

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);
}
});
}
}
7 changes: 3 additions & 4 deletions packages/loader/src/gltf/GLTFResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
Light,
Material,
ModelMesh,
Renderer,
Skin,
Texture2D
} from "@oasis-engine/core";
Expand Down Expand Up @@ -38,8 +37,8 @@ export class GLTFResource extends EngineObject {
sceneRoots: Entity[];
/** Oasis RootEntity after SceneParser. */
defaultSceneRoot: Entity;
/** Renderer can replace material by `renderer.setMaterial` if glTF use plugin-in KHR_materials_variants. */
variants?: { renderer: Renderer; material: Material; variants: string[] }[];
/** Extensions data. */
extensionsData: Record<string, any>;

constructor(engine: Engine, url: string) {
super(engine);
Expand All @@ -62,6 +61,6 @@ export class GLTFResource extends EngineObject {
this.cameras = null;
this.lights = null;
this.sceneRoots = null;
this.variants = null;
this.extensionsData = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -853,3 +853,6 @@ export interface IGLTF extends IProperty {
*/
textures?: ITexture[];
}

/** glTF extensible owner schema. */
export type GLTFExtensionOwnerSchema = IMeshPrimitive | IMaterial | ITextureInfo | INode;
28 changes: 4 additions & 24 deletions packages/loader/src/gltf/GLTFUtil.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
import { IndexFormat, TypedArray, VertexElementFormat } from "@oasis-engine/core";
import { Color, Vector2, Vector3, Vector4 } from "@oasis-engine/math";
import { BufferDataRestoreInfo, RestoreDataAccessor } from "../GLTFContentRestorer";
import { BufferInfo, ParserContext } from "./parser/ParserContext";
import { AccessorComponentType, AccessorType, IAccessor, IBufferView, IGLTF } from "./Schema";

const charCodeOfDot = ".".charCodeAt(0);
const reEscapeChar = /\\(\\)?/g;
const rePropName = RegExp(
// Match anything that isn't a dot or bracket.
"[^.[\\]]+" +
"|" +
// Or match property names within brackets.
"\\[(?:" +
// Match a non-string expression.
"([^\"'][^[]*)" +
"|" +
// Or match strings (supports escaping characters).
"([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2" +
")\\]" +
"|" +
// Or match "" as the space between consecutive dots or empty brackets.
"(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))",
"g"
);
import { AccessorComponentType, AccessorType, IAccessor, IBufferView, IGLTF } from "./GLTFSchema";
import { BufferInfo, GLTFParserContext } from "./parser/GLTFParserContext";

/**
* @internal
Expand Down Expand Up @@ -148,7 +128,7 @@ export class GLTFUtil {
}
}

static getAccessorBuffer(context: ParserContext, bufferViews: IBufferView[], accessor: IAccessor): BufferInfo {
static getAccessorBuffer(context: GLTFParserContext, bufferViews: IBufferView[], accessor: IAccessor): BufferInfo {
const { buffers } = context;

const componentType = accessor.componentType;
Expand Down Expand Up @@ -433,7 +413,7 @@ export class GLTFUtil {
* Parse the glb format.
*/
static parseGLB(
context: ParserContext,
context: GLTFParserContext,
glb: ArrayBuffer
): {
glTF: IGLTF;
Expand Down
25 changes: 14 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,16 @@
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 { GLTFExtensionMode, 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", GLTFExtensionMode.AdditiveParse)
class KHR_materials_clearcoat extends GLTFExtensionParser {
/**
* @override
*/
additiveParse(context: GLTFParserContext, material: PBRMaterial, schema: IKHRMaterialsClearcoat): void {
const { textures } = context.glTFResource;
const {
clearcoatFactor = 0,
Expand All @@ -22,15 +25,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");
}
}
}
Loading

0 comments on commit 4dbb60e

Please sign in to comment.