Skip to content

Instantly share code, notes, and snippets.

@AndreasH96
Last active February 22, 2021 01:55
Show Gist options
  • Save AndreasH96/448fcdf4879efdfa1a4021e2aebbb87e to your computer and use it in GitHub Desktop.
Save AndreasH96/448fcdf4879efdfa1a4021e2aebbb87e to your computer and use it in GitHub Desktop.
This code is based on a gist made by Utophia, https://gist.github.com/Utopiah/f2b11a8026030b726ecc8c8c9430a9b4 . It's made for being able to snap videos onto objects that are within object-groups. The code is made to be added into the file "floaty-objects.js" within the hubs client code base (2020-06-25).
snap(toSnap, snapOn) {
// Align rotation
toSnap.el.object3D.setRotationFromQuaternion(snapOn.object3D.getWorldQuaternion());
// Align position
toSnap.el.object3D.position.copy(snapOn.object3D.getWorldPosition());
// Set to same scale
toSnap.el.object3D.scale.copy(snapOn.object3D.getWorldScale());
// Move slightly to avoid texture tearing
toSnap.el.object3D.translateZ(0.002);
},
tick() {
.
.
.
if (this.wasHeld && !isHeld) {
this.onRelease();
// Custom code for snapping videos
// Check that the object is a video loader.
if (this.el.getAttribute("media-video") != null) {
// Load the objects which can be snapped on
media_loaders = AFRAME.scenes[0].querySelectorAll("[media-loader]");
var i = 0;
for (i = 0; i < media_loaders.length; i++) {
// If the object to snap onto has a 3D object
if (media_loaders[i].object3D != null) {
// Check if object has the desired custom tag
if (media_loaders[i].object3D.name.substring(0, 9) == "Snappable") {
// If close enough to the object
if (this.el.object3D.getWorldPosition().distanceTo(media_loaders[i].object3D.getWorldPosition()) < 0.5) {
// Snap onto it
this.snap(this, media_loaders[i]);
this.el.setAttribute("pinnable", {pinned:true})
break;
}
}
}
}
}
}
.
.
.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment