Skip to content

Commit

Permalink
Move inside GLTFHubsPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Jul 4, 2023
1 parent 94a4174 commit a48c672
Showing 1 changed file with 51 additions and 63 deletions.
114 changes: 51 additions & 63 deletions src/components/gltf-model-plus.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,25 @@ const convertStandardMaterialsIfNeeded = object => {
let ktxLoader;
let dracoLoader;

const OBJECT3D_EXT = [
"ambient-light",
"audio",
"directional-light",
"hemisphere-light",
"image",
"link",
"model",
"particle-emitter",
"pdf",
"point-light",
"reflection-probe",
"simple-water",
"skybox",
"spot-light",
"text",
"video"
];

class GLTFHubsPlugin {
constructor(parser, jsonPreprocessor) {
this.parser = parser;
Expand Down Expand Up @@ -449,6 +468,38 @@ class GLTFHubsPlugin {
}

node.extras.gltfIndex = i;

if (qsTruthy("newLoader")) {
/**
* This guarantees that components that add Object3Ds (ie. through addObject3DComponent) are not attached to non-Object3D
* entities as it's not supported in the BitECS loader.
* This was supported by the AFrame loader so this extension ensures backwards compatibility with all the existing scenes.
* For more context about this see: https://github.com/mozilla/hubs/pull/6121
*/
if (node.mesh !== undefined || node.camera !== undefined) {
const exts = node.extensions?.MOZ_hubs_components;
if (exts) {
const children = [];
for (const [key, value] of Object.entries(exts)) {
if (OBJECT3D_EXT.includes(key)) {
const newNode = {
name: `${node.name}_${key}`,
extensions: {
MOZ_hubs_components: { [key]: value }
}
};
delete exts[key];
children.push(newNode);
}
}
node.children = node.children || [];
children.forEach(child => {
const idx = nodes.push(child) - 1;
node.children.push(idx);
});
}
}
}
}
}
}
Expand Down Expand Up @@ -482,66 +533,6 @@ class GLTFHubsPlugin {
}
}

const OBJECT3D_EXT = [
"ambient-light",
"audio",
"directional-light",
"hemisphere-light",
"image",
"link",
"model",
"particle-emitter",
"pdf",
"point-light",
"reflection-probe",
"simple-water",
"skybox",
"spot-light",
"text",
"video"
];

/**
* This extension guarantees that components that add Object3Ds (ie. through addObject3DComponent) are not attached to non-Object3D
* entities as it's not supported in the BitECS loader.
* This was supported by the AFrame loader so this extension ensures backwards compatibility with all the existing scenes.
* For more context about this see: https://github.com/mozilla/hubs/pull/6121
*/
class GLTFHubsCompatExtension {
constructor(parser) {
this.parser = parser;
}

beforeRoot() {
const nodes = this.parser.json.nodes;
nodes.forEach(node => {
if (node.mesh !== undefined || node.camera !== undefined) {
const exts = node.extensions?.MOZ_hubs_components;
if (exts) {
const children = [];
for (const [key, value] of Object.entries(exts)) {
if (OBJECT3D_EXT.includes(key)) {
const newNode = {
name: `${node.name}_${key}`,
extensions: {
MOZ_hubs_components: { [key]: value }
}
};
delete exts[key];
children.push(newNode);
}
}
node.children = node.children || [];
children.forEach(child => {
const idx = nodes.push(child) - 1;
node.children.push(idx);
});
}
}
});
}
}

class GLTFHubsComponentsExtension {
constructor(parser) {
this.parser = parser;
Expand Down Expand Up @@ -743,9 +734,6 @@ export async function loadGLTF(src, contentType, onProgress, jsonPreprocessor) {
const loadingManager = new THREE.LoadingManager();
loadingManager.setURLModifier(getCustomGLTFParserURLResolver(gltfUrl));
const gltfLoader = new GLTFLoader(loadingManager);
if (qsTruthy("newLoader")) {
gltfLoader.register(parser => new GLTFHubsCompatExtension(parser));
}
gltfLoader
.register(parser => new GLTFHubsComponentsExtension(parser))
.register(parser => new GLTFHubsPlugin(parser, jsonPreprocessor))
Expand Down

0 comments on commit a48c672

Please sign in to comment.