Created
December 9, 2021 06:14
-
-
Save danamuise/c5ae4d374fab33b52e6fd5163b99f74b to your computer and use it in GitHub Desktop.
Javascript for Spark AR
This file contains 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
/** | |
* | |
* By Dana Muise 12/8 | |
* generate n number of objects within a circle | |
* Have another object animate to each and remove it | |
*/ | |
// Load in the required modules | |
const Scene = require('Scene'); | |
const Materials = require('Materials'); | |
const D = require('Diagnostics'); | |
const Time = require('Time'); | |
const Reactive = require('Reactive'); | |
const Animation = require('Animation'); | |
// Enables async/await in JS | |
(async function() { | |
// Locate the focal distance object in the Scene panel and the material in the Materials panel | |
const [focalDistance, greenPlane, material0, material1] = await Promise.all([ | |
Scene.root.findFirst('Focal Distance'), | |
Scene.root.findFirst('greenPlane'), | |
Materials.findFirst('material0'), | |
Materials.findFirst('material1') | |
]); | |
var numSpawned = 15 | |
var instances = [numSpawned] | |
for(var i = 0; i<numSpawned; i++){ | |
//set the radius | |
var angle = i * 2 * Math.PI / numSpawned | |
//set x-y random between origin and radius | |
var x = GetRandom(0.0, 0.2 * Math.sin(angle)) | |
var y = GetRandom(0.0, 0.2 * Math.cos(angle)) | |
instances[i] = await Scene.create("Plane", { | |
"name": "Dynamic Plane"+i, | |
"width": 0.01, | |
"height": 0.01, | |
"x": x, | |
"y": y, | |
"hidden": false, | |
"material": material0, | |
}); | |
// Add a dynamic plane as a child of the Focal Distance object | |
focalDistance.addChild(instances[i]); | |
//for testing | |
//D.log("Instance: " + instances[i].name + ", X: " + Precise(instances[i].x.pinLastValue()) | |
// + ", Y: " + Precise(instances[i].y.pinLastValue()) ) | |
} | |
function GetRandom(min, max) { | |
return Math.random() * (max - min) + min; | |
} | |
//because the precision is too high | |
function Precise(x) { | |
return Number.parseFloat(x).toPrecision(4); | |
} | |
//Interval timer | |
const timeInMilliseconds = 1000; | |
const intervalTimer = Time.setInterval(MoveIt, timeInMilliseconds); | |
//Stop interval timer | |
function stopIntervalTimer() { | |
Time.clearInterval(intervalTimer); | |
} | |
// Create a timeout timer that calls the stopIntervalTimer function after 5 | |
// seconds have passed | |
const timeoutTimer = Time.setTimeout(stopIntervalTimer, timeInMilliseconds * 300); | |
var visited = -1 | |
// moves the green plane from one target to the next | |
function MoveIt() { | |
greenPlane.hidden = false | |
if(visited < numSpawned-1){ | |
// if this is the first move, start from the origin | |
if(visited < 0){ | |
var x1 = 0.0 | |
var y1 = 0.0 | |
var x2 = instances[0].x.pinLastValue() | |
var y2 = instances[0].y.pinLastValue() | |
visited++ | |
} else { | |
var x1 = instances[visited].x.pinLastValue() | |
var y1 = instances[visited].y.pinLastValue() | |
var x2 = instances[visited+1].x.pinLastValue() | |
var y2 = instances[visited+1].y.pinLastValue() | |
visited++ | |
} | |
//animation samplers & drivers | |
const timeDriverParameters = {durationMilliseconds: 300, loopCount: 1, mirror: false}; | |
const timeDriver = Animation.timeDriver(timeDriverParameters); | |
const quadraticSamplerX = Animation.samplers.easeInOutQuad(x1, x2); | |
const quadraticSamplerY = Animation.samplers.easeInOutQuad(y1, y2); | |
const translationAnimationX = Animation.animate(timeDriver, quadraticSamplerX); | |
const translationAnimationY = Animation.animate(timeDriver, quadraticSamplerY); | |
greenPlane.transform.x = translationAnimationX; | |
greenPlane.transform.y = translationAnimationY; | |
timeDriver.start(); | |
timeDriver.onCompleted().subscribe(function () { | |
instances[visited].hidden = true | |
}); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment