Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // How to use this script:
- // (1) Create a new AR Studio project
- // (2) Add a new PlaneTracker "planeTracker0"
- // (3) Add 10 objects as a child of the planeTacker, call them "object0", "object1", etc..
- // (4) Assign a different material to each object
- // (5) Import this script
- // (6) Run
- var S = require('Scene');
- var D = require('Diagnostics');
- var Time = require('Time');
- // Return a list of objects from a Scene node
- function getAllObjects(parent, objectName, count) {
- var objects = [];
- for (var i = 0; i < count; i++) {
- objects.push(parent.child(objectName + i));
- }
- return objects;
- }
- // The ObjectManager entity takes a list of objects created in the Scene before hand
- // and presents 2 methods add() and remove() that enable you to virtually add and
- // remove object from the Scene
- function ObjectManager(objects, name) {
- this.name = name || 'ObjectManager' + (ObjectManager.id++);
- this.objects = objects;
- this.unusedObjects = objects.splice(0);
- this.usedObjects = [];
- }
- ObjectManager.id = 0;
- // Set the props of a created object
- ObjectManager.setProps = function (object, props) {
- if (props.x) object.transform.x = props.x;
- if (props.y) object.transform.y = props.y;
- if (props.z) object.transform.z = props.z;
- object.material.opacity = props.opacity ? props.opacity : 1.0;
- };
- // Reset the props of a removed object
- ObjectManager.resetProps = function (object) {
- object.transform.x = 0.0;
- object.transform.y = 0.0;
- object.transform.z = 0.0;
- object.material.opacity = 0.0;
- };
- ObjectManager.prototype = {
- // Add an object with the given props in the Scene
- // and returns a reference to this object
- add: function (props) {
- if (this.isFull()) {
- D.log(this.name + ': Could not add object because all occurences are used');
- return;
- }
- var object = this.unusedObjects.shift();
- this.usedObjects.push(object);
- ObjectManager.setProps(object, props);
- return object;
- },
- // Remove an object from the scene
- remove: function (object) {
- var index = this.usedObjects.indexOf(object);
- if (index > -1) {
- var object = this.usedObjects[index];
- ObjectManager.resetProps(object);
- this.usedObjects.splice(index, 1);
- this.unusedObjects.push(object);
- } else {
- if (this.unusedObjects.indexOf(object) > -1) {
- D.log(this.name + ': Attempt to remove object "' + object.name + '" which is not used');
- } else {
- D.log(this.name + ': Unknown object "' + object.name + '"');
- }
- }
- },
- isFull: function () {
- return this.unusedObjects.length === 0;
- },
- isEmpty: function () {
- return this.usedObjects.length === 0;
- }
- };
- // Get the list of template objects and create the ObjectManager instance
- var templates = getAllObjects(S.root.child('planeTracker0'), 'object', 10);
- var objMgr = new ObjectManager(templates, 'objMgr');
- // Add or remove objects from the Scene
- Time.ms.interval(100).subscribe(function (elapsedTime) {
- var action = '';
- if (objMgr.isFull()) action = 'remove';
- else if (objMgr.isEmpty()) action = 'add';
- else action = Math.random() > 0.5 ? 'add' : 'remove';
- if (action === 'add') {
- // add object at random position
- objMgr.add({
- x: (Math.random() - 0.5) * 50.0,
- y: (Math.random() - 0.5) * 50.0,
- z: (Math.random() - 0.5) * 50.0
- });
- } else if (action === 'remove') {
- // remove random object
- var index = Math.floor(Math.random() * objMgr.usedObjects.length);
- var object = objMgr.usedObjects[index];
- objMgr.remove(object);
- }
- });
Add Comment
Please, Sign In to add comment