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

Add geometry drag & drop to web viewer #1663

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
56 changes: 49 additions & 7 deletions javascript/MaterialXView/source/dropHandling.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import * as THREE from 'three';
import * as fflate from 'three/examples/jsm/libs/fflate.module.js';

const debugFileHandling = false;
let loadingCallback;
let loadingCallback = null;
let sceneLoadingCallback = null;

export function setLoadingCallback(cb) {
loadingCallback = cb;
}

export function setSceneLoadingCallback(cb)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Allow setting of callback to load geometry.

{
sceneLoadingCallback = cb;
}

export function dropHandler(ev) {
if (debugFileHandling) console.log('File(s) dropped', ev.dataTransfer.items, ev.dataTransfer.files);

Expand Down Expand Up @@ -130,14 +136,22 @@ async function handleFilesystemEntries(entries) {
'node_modules',
]

let isGLB = false;
let haveMtlx = false;
for (let entry of entries) {
if (debugFileHandling) console.log("file entry", entry)
if (entry.isFile) {
if (debugFileHandling) console.log("single file", entry);
if (debugFileHandling)
console.log("single file", entry);
if (fileIgnoreList.includes(entry.name)) {
continue;
}
allFiles.push(entry);

if (entry.name.endsWith('glb')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test for glb file.

isGLB = true;
break;
}
}
else if (entry.isDirectory) {
if (dirIgnoreList.includes(entry.name)) {
Expand Down Expand Up @@ -198,7 +212,28 @@ async function handleFilesystemEntries(entries) {
return 0;
});

if (debugFileHandling) console.log("all files", allFiles);
if (isGLB)
{
console.log("Load GLB file", allFiles[0]);

const rootFile = allFiles[0];
THREE.Cache.add(rootFile.fullPath, await getBufferFromFile(rootFile));

if (debugFileHandling) console.log("CACHE", THREE.Cache.files);

sceneLoadingCallback(rootFile);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Call callback if glb file dropped.

return;
}

if (!allFiles[0].name.endsWith('mtlx'))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add filrewall check if no mtlx file otherwise dropped don't continue as it will cache a bunch of files with no mtlx file.

{
console.log("No MaterialX files dropped. Skipping content.");
return;
}

if (debugFileHandling) {
console.log("- All files", allFiles);
}

// put all files in three' Cache
for (const fileEntry of allFiles) {
Expand All @@ -222,12 +257,19 @@ async function handleFilesystemEntries(entries) {
// TODO we could also allow dropping of multiple MaterialX files (or folders with them inside)
// and seed the dropdown from that.
// At that point, actually reading files and textures into memory should be deferred until they are actually used.
const rootFile = allFiles[0];
THREE.Cache.add(rootFile.fullPath, await getBufferFromFile(rootFile));
if (allFiles.length > 0)
{
const rootFile = allFiles[0];
THREE.Cache.add(rootFile.fullPath, await getBufferFromFile(rootFile));

if (debugFileHandling) console.log("CACHE", THREE.Cache.files);
if (debugFileHandling) console.log("CACHE", THREE.Cache.files);

loadingCallback(rootFile);
loadingCallback(rootFile);
}
else
{
console.log('No files to add cache.')
}
}

async function readDirectory(directory) {
Expand Down
10 changes: 9 additions & 1 deletion javascript/MaterialXView/source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass.js';
import { GammaCorrectionShader } from 'three/examples/jsm/shaders/GammaCorrectionShader.js';

import { Viewer } from './viewer.js'
import { dropHandler, dragOverHandler, setLoadingCallback } from './dropHandling.js';
import { dropHandler, dragOverHandler, setLoadingCallback, setSceneLoadingCallback } from './dropHandling.js';

let renderer, composer, orbitControls;

Expand Down Expand Up @@ -66,6 +66,7 @@ function init()
let geometrySelect = document.getElementById('geometry');
geometrySelect.value = scene.getGeometryURL();
geometrySelect.addEventListener('change', (e) => {
console.log('Change geometry to:', e.target.value);
scene.setGeometryURL(e.target.value);
scene.loadGeometry(viewer, orbitControls);
});
Expand Down Expand Up @@ -155,6 +156,13 @@ function init()
viewer.getScene().setUpdateTransforms();
});

setSceneLoadingCallback(file => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Set callback to do geometry load. Same as what's done from the menu but with the URI from the drop.

let glbFileName = file.fullPath || file.name;
console.log('Drop geometry to:', glbFileName);
scene.setGeometryURL(glbFileName);
scene.loadGeometry(viewer, orbitControls);
});

// enable three.js Cache so that dropped files can reference each other
THREE.Cache.enabled = true;
}
Expand Down