-
Notifications
You must be signed in to change notification settings - Fork 24
/
dynamicInstanceOnTap.js
60 lines (48 loc) · 1.94 KB
/
dynamicInstanceOnTap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Prerequisites
* Make sure you added Script Dynamic Instancing on your capability
* Make sure you seleced Tap under TouchGestures on your capability
*
* If you fail to add these to your project's Capabilities you will get error messages
*/
// Create plane Dynamically in code On Tap
// load in modules
const Scene = require('Scene');
const TouchGestures = require("TouchGestures");
const Reactive = require("Reactive");
// Create an Async function to be able to use Await with promises (which is "easier to use/read")
// Learn more about Async on this blog post by Meta Spark: https://sparkar.facebook.com/ar-studio/learn/tutorials/first-lines-of-code/
(async function () {
// Accessing the Focal Distance scene object
const [fd] = await Promise.all([
Scene.root.findFirst("Focal Distance"),
]);
// Array to store the created elements
var totalPlane = [];
var x = 0;
// Tap function
TouchGestures.onTap().subscribe(async () => {
// Promise that creates plane on tap
let [newPlane] = await Promise.all([
Scene.create("Plane", {
"name": "Plane",
"width": 0.1,
"height": 0.1,
"transform": Reactive.transform(
// The transform's position, scale, and rotation values:
Reactive.point(x,0,0),
Reactive.scale(1,1,1),
Reactive.quaternionFromEuler(0,0,0)
),
"hidden": false,
}),
]);
//Storing the created plane
totalPlane.push(newPlane);
//Adding the created plane to the parent and the scene
fd.addChild(totalPlane[totalPlane.length - 1]);
x += .1;
});
// Note: Because on each tap we increment x by 0.1, and use that x variable to create our dynamic plane
// If you tap multiple times then the planes will get instanced next to each other, instead of the same spot
})();