Skip to content

Added support for GLTF format, removed deprecated JSONLoader #30

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"bugs": "https://github.com/fand/vedajs/issues",
"dependencies": {
"is-video": "^1.0.1",
"three-gltf-loader": "^1.99.1",
"three": "^0.100.0",
"three-mtl-loader": "^1.0.2",
"three-obj-loader": "^1.1.3"
Expand Down
50 changes: 33 additions & 17 deletions src/model-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require('three-obj-loader')(THREE); // tslint:disable-line

type IConstructable<T> = new () => T;
const MTLLoader: IConstructable<THREE.MTLLoader> = require('three-mtl-loader'); // tslint:disable-line
const GLTFLoader: IConstructable<THREE.GLTFLoader> = require('three-gltf-loader'); // tslint:disable-line

interface ICache {
url: string;
Expand All @@ -30,7 +31,7 @@ export default class ModelLoader {
private objLoader = new THREE.OBJLoader();
private mtlLoader = new MTLLoader();
private objectLoader = new THREE.ObjectLoader();
private jsonLoader = new THREE.JSONLoader();
private gltfLoader = new GLTFLoader();

async load(model: IPassModel): Promise<THREE.Object3D> {
const url = model.PATH;
Expand All @@ -46,6 +47,8 @@ export default class ModelLoader {
obj = await this.loadObjAndMtl(model);
} else if (/\.js(on)?\/?$/.test(url)) {
obj = await this.loadJson(model);
} else if (/\.gltf|.glb\/?$/.test(url)) {
obj = await this.loadGLTF(model);
} else {
throw new TypeError('Unsupported model URL: ' + url);
}
Expand All @@ -59,27 +62,40 @@ export default class ModelLoader {
private async loadJson(model: IPassModel): Promise<THREE.Object3D> {
const obj = await Promise.race([
new Promise<THREE.Object3D>((resolve, reject) => {
this.jsonLoader.load(
this.objectLoader.load(model.PATH, resolve, undefined, reject);
}),

// timeout
new Promise<THREE.Object3D>((_resolve, reject) => {
setTimeout(() => reject('Request Timeout'), 50000);
}),
]);

if (
obj instanceof THREE.Mesh &&
obj.geometry instanceof THREE.Geometry
) {
obj.geometry = new THREE.BufferGeometry().fromGeometry(
obj.geometry,
);
}

const group = new THREE.Group();
group.add(obj);

return group;
}

private async loadGLTF(model: IPassModel): Promise<THREE.Object3D> {
const obj = await Promise.race([
new Promise<THREE.Object3D>((resolve, reject) => {
this.gltfLoader.load(
model.PATH,
(geometry, materials) => {
if (materials && Array.isArray(materials)) {
resolve(
new THREE.Mesh(
geometry,
materials[0] as THREE.MeshBasicMaterial,
),
);
} else {
resolve(new THREE.Mesh(geometry));
}
},
gltf => resolve(gltf.scene),
undefined,
reject,
);
}),
new Promise<THREE.Object3D>((resolve, reject) => {
this.objectLoader.load(model.PATH, resolve, undefined, reject);
}),

// timeout
new Promise<THREE.Object3D>((_resolve, reject) => {
Expand Down
11 changes: 6 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
dependencies:
any-observable "^0.3.0"


"@types/three@^0.93.15":
version "0.93.15"
resolved "https://registry.yarnpkg.com/@types/three/-/three-0.93.15.tgz#8fbb4ee8c0648372a305469d16e37d0a939f91c3"
Expand All @@ -33,6 +34,7 @@
resolved "https://registry.yarnpkg.com/@types/webmidi/-/webmidi-2.0.2.tgz#0466cf747aaed2b914a1e2564635bbda217cb256"
integrity sha512-m/5kolDeFVi/tiE2OFaj0KCfz1mfefBh7MZ1RXowd8hP455b0qkkeOLUhjXK0fDDU+e8WKVJ7EP+NG8+LwCghA==


ansi-escapes@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-3.1.0.tgz#f73207bb81207d75fd6c83f125af26eea378ca30"
Expand Down Expand Up @@ -1891,6 +1893,10 @@ symbol-observable@^1.1.0:
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804"
integrity sha512-e900nM8RRtGhlV36KGEU9k65K3mPb1WV70OdjfxlG2EAuM1noi/E/BaW/uMhL7bPEssK8QV57vN3esixjUvcXQ==

three-gltf-loader@^1.99.1:
version "1.99.1"
resolved "https://registry.yarnpkg.com/three-gltf-loader/-/three-gltf-loader-1.99.1.tgz#08a798857b16dd03d1fa31bf535ff86a5c20b394"

three-mtl-loader@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/three-mtl-loader/-/three-mtl-loader-1.0.2.tgz#d0060f1afb8bc2c166ea56cebad250d73da18563"
Expand All @@ -1908,11 +1914,6 @@ three@^0.100.0:
resolved "https://registry.yarnpkg.com/three/-/three-0.100.0.tgz#262841c0b7d88ebd62af73f28d9f578963b3aa00"
integrity sha512-/lN2rdE1OqIwJr4/HcSaOisiCY0uVA0sqPpbCG5nil2uICEdS0LfGwSVYTtZDsIpR76r3++h5H3Hzg5D+SJBRQ==

three@^0.87.1:
version "0.87.1"
resolved "https://registry.yarnpkg.com/three/-/three-0.87.1.tgz#466a34edc4543459ced9b9d7d276b65216fe2ba8"
integrity sha1-Rmo07cRUNFnO2bnX0na2Uhb+K6g=

to-object-path@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/to-object-path/-/to-object-path-0.3.0.tgz#297588b7b0e7e0ac08e04e672f85c1f4999e17af"
Expand Down