-
Notifications
You must be signed in to change notification settings - Fork 351
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
{ | ||
sceneLoadingCallback = cb; | ||
} | ||
|
||
export function dropHandler(ev) { | ||
if (debugFileHandling) console.log('File(s) dropped', ev.dataTransfer.items, ev.dataTransfer.files); | ||
|
||
|
@@ -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')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) { | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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); | ||
}); | ||
|
@@ -155,6 +156,13 @@ function init() | |
viewer.getScene().setUpdateTransforms(); | ||
}); | ||
|
||
setSceneLoadingCallback(file => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
There was a problem hiding this comment.
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.