Skip to content

Instantly share code, notes, and snippets.

@MostlyEmre
Created April 14, 2020 04:44
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 MostlyEmre/3fbf4c025e37071c1713a5d3677f1b8b to your computer and use it in GitHub Desktop.
Save MostlyEmre/3fbf4c025e37071c1713a5d3677f1b8b to your computer and use it in GitHub Desktop.
// the assets that will be randomly selected to be shown when the marker is in view.
let assets = [
{
id: "1968",
src: "img/1968-france.jpg",
},
{
id: "2012",
src: "img/2012-Turkey.jpg",
},
{
id: "cola",
src: "img/ColaStickerv.jpg",
},
];
// define a previous number. This will be used later.
let previousNum;
// define new number. This number is used as the index value for fetching artifacts from the asset array (above)
let num = Math.floor(Math.random() * Math.floor(assets.length));
// register aframe component for the marker
AFRAME.registerComponent("markerhandler", {
// tick makes it refresh every frame. Is this necessary? Who knows
tick: function() {
// if the marker is in view ...
if (document.querySelector("a-marker").object3D.visible == true) {
document
.querySelector("a-plane")
// sets the src of the plane to the src value in the assets array
.setAttribute("src", `${assets[num].src}`);
return;
} else {
// set previous number to current number
previousNum = num;
// generate a new random number
num = Math.floor(Math.random() * Math.floor(assets.length));
// if they're the same, re-roll
if (num == previousNum) {
num = Math.floor(Math.random() * Math.floor(assets.length));
} else {
return num;
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment