Guest User

Untitled

a guest
May 26th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. // How to use this script:
  2. // (1) Create a new AR Studio project
  3. // (2) Add a new PlaneTracker "planeTracker0"
  4. // (3) Add 10 objects as a child of the planeTacker, call them "object0", "object1", etc..
  5. // (4) Assign a different material to each object
  6. // (5) Import this script
  7. // (6) Run
  8.  
  9. var S = require('Scene');
  10. var D = require('Diagnostics');
  11. var Time = require('Time');
  12.  
  13. // Return a list of objects from a Scene node
  14. function getAllObjects(parent, objectName, count) {
  15. var objects = [];
  16. for (var i = 0; i < count; i++) {
  17. objects.push(parent.child(objectName + i));
  18. }
  19. return objects;
  20. }
  21.  
  22. // The ObjectManager entity takes a list of objects created in the Scene before hand
  23. // and presents 2 methods add() and remove() that enable you to virtually add and
  24. // remove object from the Scene
  25. function ObjectManager(objects, name) {
  26. this.name = name || 'ObjectManager' + (ObjectManager.id++);
  27. this.objects = objects;
  28. this.unusedObjects = objects.splice(0);
  29. this.usedObjects = [];
  30. }
  31.  
  32. ObjectManager.id = 0;
  33.  
  34. // Set the props of a created object
  35. ObjectManager.setProps = function (object, props) {
  36. if (props.x) object.transform.x = props.x;
  37. if (props.y) object.transform.y = props.y;
  38. if (props.z) object.transform.z = props.z;
  39. object.material.opacity = props.opacity ? props.opacity : 1.0;
  40. };
  41.  
  42. // Reset the props of a removed object
  43. ObjectManager.resetProps = function (object) {
  44. object.transform.x = 0.0;
  45. object.transform.y = 0.0;
  46. object.transform.z = 0.0;
  47. object.material.opacity = 0.0;
  48. };
  49.  
  50. ObjectManager.prototype = {
  51. // Add an object with the given props in the Scene
  52. // and returns a reference to this object
  53. add: function (props) {
  54. if (this.isFull()) {
  55. D.log(this.name + ': Could not add object because all occurences are used');
  56. return;
  57. }
  58. var object = this.unusedObjects.shift();
  59. this.usedObjects.push(object);
  60. ObjectManager.setProps(object, props);
  61. return object;
  62. },
  63. // Remove an object from the scene
  64. remove: function (object) {
  65. var index = this.usedObjects.indexOf(object);
  66. if (index > -1) {
  67. var object = this.usedObjects[index];
  68. ObjectManager.resetProps(object);
  69. this.usedObjects.splice(index, 1);
  70. this.unusedObjects.push(object);
  71. } else {
  72. if (this.unusedObjects.indexOf(object) > -1) {
  73. D.log(this.name + ': Attempt to remove object "' + object.name + '" which is not used');
  74. } else {
  75. D.log(this.name + ': Unknown object "' + object.name + '"');
  76. }
  77. }
  78. },
  79. isFull: function () {
  80. return this.unusedObjects.length === 0;
  81. },
  82. isEmpty: function () {
  83. return this.usedObjects.length === 0;
  84. }
  85. };
  86.  
  87. // Get the list of template objects and create the ObjectManager instance
  88. var templates = getAllObjects(S.root.child('planeTracker0'), 'object', 10);
  89. var objMgr = new ObjectManager(templates, 'objMgr');
  90.  
  91. // Add or remove objects from the Scene
  92. Time.ms.interval(100).subscribe(function (elapsedTime) {
  93. var action = '';
  94. if (objMgr.isFull()) action = 'remove';
  95. else if (objMgr.isEmpty()) action = 'add';
  96. else action = Math.random() > 0.5 ? 'add' : 'remove';
  97.  
  98. if (action === 'add') {
  99. // add object at random position
  100. objMgr.add({
  101. x: (Math.random() - 0.5) * 50.0,
  102. y: (Math.random() - 0.5) * 50.0,
  103. z: (Math.random() - 0.5) * 50.0
  104. });
  105. } else if (action === 'remove') {
  106. // remove random object
  107. var index = Math.floor(Math.random() * objMgr.usedObjects.length);
  108. var object = objMgr.usedObjects[index];
  109. objMgr.remove(object);
  110. }
  111. });
Add Comment
Please, Sign In to add comment