Skip to content

Instantly share code, notes, and snippets.

@mattrossman
Last active November 23, 2020 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattrossman/7dd2b990820c4753685d34f4ab8c1094 to your computer and use it in GitHub Desktop.
Save mattrossman/7dd2b990820c4753685d34f4ab8c1094 to your computer and use it in GitHub Desktop.
Hubs stash and unstash
/* Usage:
let data = stash()
// Or in the (Firefox) console, right click on the output array -> "Copy Object" to move data across scenes.
let elements = unstash(data)
*/
function getAllMedia() {
return document.querySelectorAll("[media-loader][networked]")
}
function loadMedia(url) {
var el = document.createElement("a-entity")
AFRAME.scenes[0].appendChild(el)
el.setAttribute("media-loader", { src: url, fitToBox: true, resolve: true })
el.setAttribute("networked", { template: "#interactable-media" } )
return el
}
function stash() {
const out = []
getAllMedia().forEach(el => {
out.push({
src: el.getAttribute('media-loader').src,
position: el.getAttribute('position'),
rotation: el.getAttribute('rotation'),
scale: el.getAttribute('scale'),
})
})
return out
}
function unstash(data) {
const out = []
data.forEach(obj => {
el = loadMedia(obj.src)
el.setAttribute('position', obj.position)
el.setAttribute('rotation', obj.rotation)
el.setAttribute('scale', obj.scale)
out.push(el)
})
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment