Last active
November 28, 2022 14:08
-
-
Save Utopiah/1cfc123239fa2994569fc7c5c60b2928 to your computer and use it in GitHub Desktop.
Utils functions for Mozilla Hubs scripting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const hubs_utils = { | |
removeEntitiesFromMediaList, | |
setPinEntities, | |
loadAssetsFromURLs, | |
getAvatarFromName, | |
getFirstElementFromHash, | |
objects3DFromPartialName, | |
getFirstElementFromPartialURL, | |
getAvatarFromName, | |
resetUserMediaRotation, | |
getUserMedia | |
} | |
function getUserMedia(){ | |
return document.querySelectorAll("[media-loader][id^=naf]") | |
} | |
function resetUserMediaRotation(){ | |
for (var m of getUserMedia() ) | |
m.setAttribute("rotation", "0 0 0") | |
} | |
function removeEntitiesFromMediaList(entities){ | |
for (var el of entities) | |
el.removeComponent("listed-media") | |
} | |
function setPinEntities(entities, pinned){ | |
for (var el of entities) | |
el.setAttribute("pinnable", {pinned: pinned} ) | |
} | |
function loadAssetsFromURLs(URLs){ | |
var elements = [] | |
for (var url of URLs){ | |
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" } ) | |
elements.push(el) | |
} | |
return elements | |
} | |
function getAvatarFromName(name){ | |
for (a of document.querySelectorAll("[networked-avatar]") ){ | |
var el = document.querySelector("#"+a.id) | |
if ( name.trim() == el.components["player-info"].displayName.trim() ) return el | |
} | |
return null | |
} | |
function getFirstElementFromHash(hash){ | |
var g = AFRAME.scenes[0].querySelectorAll("[media-loader]") | |
var matches = [] | |
for (let e of g){ | |
var m = e.components["media-loader"].attrValue.src.match(hash) | |
if (m && m.length) matches.push(e) | |
} | |
return matches[0] | |
} | |
function objects3DFromPartialName(name){ | |
var matches = [] | |
AFRAME.scenes[0].object3D.traverse(o=>{ | |
var match = o.name.match(name) | |
if (match && match.length && o.type == "Object3D"){ | |
matches.push(o) | |
} | |
}) | |
return matches | |
} | |
function getFirstElementFromPartialURL(partialURL){ | |
var element = null | |
var medias = document.querySelectorAll("[media-loader]") | |
for (var m of medias){ | |
var match = m.components["media-loader"].attrValue.src.match(partialURL) | |
if (match && match.length > 0) return m | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment