Skip to content

Commit

Permalink
Merge pull request #126 from ux3d/fix/noScenes
Browse files Browse the repository at this point in the history
Fix gltf loading with empty scenes
  • Loading branch information
UX3D-becher authored Feb 9, 2021
2 parents be60cf3 + d8194d0 commit a52b4b7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 35 deletions.
33 changes: 20 additions & 13 deletions app_headless/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,29 @@ async function main()

const defaultScene = state.gltf.scene;
state.sceneIndex = defaultScene === undefined ? 0 : defaultScene;
const scene = state.gltf.scenes[state.sceneIndex];
scene.applyTransformHierarchy(state.gltf);
state.userCamera.fitViewToScene(state.gltf, state.sceneIndex);
if (state.gltf.scenes.length != 0)
{
if(state.sceneIndex > state.gltf.scenes.length - 1)
{
state.sceneIndex = 0;
}
const scene = state.gltf.scenes[state.sceneIndex];
scene.applyTransformHierarchy(state.gltf);
state.userCamera.fitViewToScene(state.gltf, state.sceneIndex);

view.renderFrame(state, width, height);
view.renderFrame(state, width, height);

let pixels = new Uint8Array(width * height * 4);
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
let pixels = new Uint8Array(width * height * 4);
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);

const image = {
width: width,
height: height,
data: pixels
};
const pngFile = png.encode(image);
fs.writeFileSync(__dirname + "/render.png", pngFile);
const image = {
width: width,
height: height,
data: pixels
};
const pngFile = png.encode(image);
fs.writeFileSync(__dirname + "/render.png", pngFile);
}
}

main().then(() => console.log("Done"));
21 changes: 12 additions & 9 deletions app_web/src/logic/uimodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,17 +308,20 @@ class UIModel
map( (state) => {
let gltf = state.gltf;
let cameraIndices = [{title: "User Camera", index: -1}];
cameraIndices.push(...gltf.cameras.map( (camera, index) => {
if(gltf.scenes[state.sceneIndex].includesNode(gltf, camera.node))
{
let name = camera.name;
if(name === "" || name === undefined)
if (gltf.scenes[state.sceneIndex] !== undefined)
{
cameraIndices.push(...gltf.cameras.map( (camera, index) => {
if(gltf.scenes[state.sceneIndex].includesNode(gltf, camera.node))
{
name = index;
let name = camera.name;
if(name === "" || name === undefined)
{
name = index;
}
return {title: name, index: index};
}
return {title: name, index: index};
}
}));
}));
}
cameraIndices = cameraIndices.filter(function(el) {
return el !== undefined;
});
Expand Down
36 changes: 23 additions & 13 deletions app_web/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,28 @@ async function main()
const defaultScene = state.gltf.scene;
state.sceneIndex = defaultScene === undefined ? 0 : defaultScene;
state.cameraIndex = undefined;
const scene = state.gltf.scenes[state.sceneIndex];
scene.applyTransformHierarchy(state.gltf);
state.userCamera.aspectRatio = canvas.width / canvas.height;
state.userCamera.fitViewToScene(state.gltf, state.sceneIndex);

// Try to start as many animations as possible without generating conficts.
state.animationIndices = [];
for (let i = 0; i < gltf.animations.length; i++)
if (state.gltf.scenes.length != 0)
{
if (!gltf.nonDisjointAnimations(state.animationIndices).includes(i))
if(state.sceneIndex > state.gltf.scenes.length - 1)
{
state.animationIndices.push(i);
state.sceneIndex = 0;
}
const scene = state.gltf.scenes[state.sceneIndex];
scene.applyTransformHierarchy(state.gltf);
state.userCamera.aspectRatio = canvas.width / canvas.height;
state.userCamera.fitViewToScene(state.gltf, state.sceneIndex);

// Try to start as many animations as possible without generating conficts.
state.animationIndices = [];
for (let i = 0; i < gltf.animations.length; i++)
{
if (!gltf.nonDisjointAnimations(state.animationIndices).includes(i))
{
state.animationIndices.push(i);
}
}
state.animationTimer.start();
}
state.animationTimer.start();

uiModel.exitLoadingState();

Expand All @@ -87,8 +94,11 @@ async function main()
state.sceneIndex = newSceneIndex;
state.cameraIndex = undefined;
const scene = state.gltf.scenes[state.sceneIndex];
scene.applyTransformHierarchy(state.gltf);
state.userCamera.fitViewToScene(state.gltf, state.sceneIndex);
if (scene !== undefined)
{
scene.applyTransformHierarchy(state.gltf);
state.userCamera.fitViewToScene(state.gltf, state.sceneIndex);
}
}),
multicast(sceneChangedSubject)
);
Expand Down
13 changes: 13 additions & 0 deletions source/GltfView/gltf_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class GltfView

const scene = state.gltf.scenes[state.sceneIndex];

if(scene === undefined)
{
return;
}

scene.applyTransformHierarchy(state.gltf);

this.renderer.drawScene(state, scene);
Expand All @@ -60,6 +65,14 @@ class GltfView

// gather information from the active scene
const scene = state.gltf.scenes[state.sceneIndex];
if (scene === undefined)
{
return {
meshCount: 0,
faceCount: 0,
opaqueMaterialsCount: 0,
transparentMaterialsCount: 0};
}
const nodes = scene.gatherNodes(state.gltf);
const activeMeshes = nodes.filter(node => node.mesh !== undefined).map(node => state.gltf.meshes[node.mesh]);
const activePrimitives = activeMeshes
Expand Down

0 comments on commit a52b4b7

Please sign in to comment.