aentity.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. this.getChildByName = function (name) {
  2. let nodes = this.children.filter(el => el.displayName == name);
  3. return nodes[0]
  4. }
  5. this.nodeDef = function () {
  6. let def = _app.helpers.getNodeDef(this.id);
  7. return def
  8. }
  9. this.clone = function () {
  10. let nodeDef = _app.helpers.getNodeDef(this.id);
  11. return nodeDef
  12. }
  13. this.getScene = function () {
  14. let scene = this.find("/")[0];
  15. return scene
  16. }
  17. this.setGizmoMode = function (mode) {
  18. if (this.gizmo) {
  19. this.gizmo.properties.mode = mode
  20. }
  21. }
  22. this.showCloseGizmo = function () {
  23. let gizmoNode =
  24. {
  25. "extends": "proxy/aframe/gizmoComponent.vwf",
  26. "type": "component",
  27. "properties":
  28. {
  29. "mode": "translate"
  30. }
  31. }
  32. if (this.properties.edit) {
  33. this.children.create("gizmo", gizmoNode);
  34. } else {
  35. if (this.gizmo) {
  36. this.children.delete(this.gizmo)
  37. }
  38. }
  39. }
  40. // Parse a parameter as a translation specification.
  41. this.translationFromValue = function (propertyValue) {
  42. var value = new THREE.Vector3();//goog.vec.Vec3.create();
  43. if (propertyValue.hasOwnProperty('x')) {
  44. value = value.set(propertyValue.x, propertyValue.y, propertyValue.z)
  45. }
  46. else if (Array.isArray(propertyValue)) {
  47. value = value.fromArray(propertyValue);
  48. }
  49. else if (typeof propertyValue === 'string') {
  50. let val = propertyValue.includes(',') ? AFRAME.utils.coordinates.parse(propertyValue.split(',').join(' ')) : AFRAME.utils.coordinates.parse(propertyValue);
  51. value = value.set(val.x, val.y, val.z)
  52. } else if (propertyValue.hasOwnProperty('0')) {
  53. value = value.set(propertyValue[0], propertyValue[1], propertyValue[2])
  54. }
  55. return value
  56. // return value && value.length >= 3 ?
  57. // value :
  58. // goog.vec.Vec3.create();
  59. };
  60. this.sendOSC = function (msg) {
  61. //sending OSC msg
  62. vwf_view.kernel.fireEvent(this.id, "sendOSC", [msg]);
  63. // if (_OSCManager.port !== null) {
  64. // _OSCManager.port.send(msg);
  65. // }
  66. //on driver side
  67. }
  68. // this.clickEvent = function () {
  69. // //this.intersectEventMethod();
  70. // }
  71. // this.intersectEvent = function () {
  72. // //this.intersectEventMethod();
  73. // }
  74. // this.clearIntersectEvent = function () {
  75. // //this.clearIntersectEventMethod();
  76. // }
  77. this.intersectEventMethod = function (point) {
  78. //intersect method
  79. }
  80. this.clearIntersectEventMethod = function () {
  81. //clearIntersect method
  82. }
  83. this.hitstartEventMethod = function () {
  84. //intersect method
  85. }
  86. this.hitendEventMethod = function () {
  87. //clearIntersect method
  88. }
  89. this.clickEventMethod = function (value) {
  90. //clickEventMethod
  91. }
  92. this.mousedownEventMethod = function (value) {
  93. //clickEventMethod
  94. }
  95. this.mouseupEventMethod = function (value) {
  96. //clickEventMethod
  97. }
  98. this.setOwner = function (param) {
  99. var clients = this.find("doc('proxy/clients.vwf')")[0];
  100. if (clients !== undefined) {
  101. //console.log(clients.children);
  102. let clientsArray = [];
  103. clients.children.forEach(function (element) {
  104. clientsArray.push(element.name);
  105. });
  106. //console.log(clientsArray);
  107. if (this.ownedBy) {
  108. if (clientsArray.includes(this.ownedBy)) {
  109. console.log(this.id + " already owned by: " + param);
  110. } else {
  111. this.ownedBy = param;
  112. //console.log(this.id + ' set owner to: ' + param);
  113. }
  114. } else {
  115. this.ownedBy = param;
  116. //console.log(this.id + ' set owner to: ' + param);
  117. }
  118. }
  119. }
  120. this.updateMethod = function (methodName, methodBody, params) {
  121. vwf.setMethod(this.id, methodName, { body: methodBody, type: "application/javascript", parameters: params });
  122. }
  123. this.callMethod = function (methodName, params) {
  124. vwf.callMethod(this.id, methodName, params)
  125. }
  126. this.do = function () {
  127. //do in step
  128. }
  129. this.step = function () {
  130. if (this.stepping) {
  131. this.do();
  132. }
  133. let t = this.stepTime ? this.stepTime : 0.05;
  134. this.future(t).step();
  135. }
  136. this.onGlobalBeat = function (obj) {
  137. //dispatch the beat example send OSC
  138. let transportNode = this.find('//' + obj.name)[0];
  139. let rate = transportNode.animationRate; // 1 by default
  140. let drumSeq = [
  141. { beat: 0, msg: 0 },
  142. { beat: 30, msg: 0 }];
  143. drumSeq.forEach(el => {
  144. if (el.beat / rate == obj.beat) {
  145. let msg = {
  146. address: "/trigger/sample01",
  147. args: [this.time, 'bd_808', 3]
  148. };
  149. //for synth {beat:0, msg: "A"}
  150. //let msg = {
  151. // address: "/trigger/synth01",
  152. // args: [this.time, 'piano', el.msg, 0.1, 1]};
  153. // this.sendOSC(msg);
  154. // this.changeVisual();
  155. }
  156. })
  157. }
  158. this.changeVisual = function () {
  159. //code for changing me
  160. this.future(0.1).resetVisual();
  161. }
  162. this.resetVisual = function () {
  163. }
  164. this.getRandomColor = function () {
  165. var letters = '0123456789ABCDEF';
  166. var color = '#';
  167. for (var i = 0; i < 6; i++) {
  168. color += letters[Math.floor(this.random() * 16)];
  169. }
  170. return color;
  171. }
  172. this.randomize = function () {
  173. }
  174. // this.position_set = function (value) {
  175. // var position = this.translationFromValue(value); // parse incoming value
  176. // if (!position || !this.position) {
  177. // this.position = new THREE.Vector3();
  178. // } else if (!this.position.equals(position)){ //!goog.vec.Vec3.equals(this.position || goog.vec.Vec3.create(), position)) {
  179. // this.position = position;
  180. // this.positionChanged(position);
  181. // }
  182. // }
  183. // this.position_get = function () {
  184. // return this.position || new THREE.Vector3();
  185. // }
  186. // this.rotation_set = function (value) {
  187. // var rotation = this.translationFromValue(value); // parse incoming value
  188. // if (!rotation || !this.rotation) {
  189. // this.rotation = new THREE.Vector3();
  190. // } else if (!this.rotation.equals(rotation)){ //!goog.vec.Vec3.equals(this.rotation || goog.vec.Vec3.create(), rotation)) {
  191. // this.rotation = rotation;
  192. // this.rotationChanged(rotation);
  193. // }
  194. // }
  195. // this.rotation_get = function () {
  196. // return this.rotation || new THREE.Vector3();
  197. // }
  198. // this.scale_set = function (value) {
  199. // var scale = this.translationFromValue( value ); // parse incoming value
  200. // if(!scale || !this.scale){
  201. // this.scale = new THREE.Vector3();
  202. // } else if (!this.scale.equals(scale)){ //! goog.vec.Vec3.equals( this.scale || goog.vec.Vec3.create(), scale ) ) {
  203. // this.scale = scale;
  204. // this.scaleChanged( scale);
  205. // }
  206. // }
  207. // this.scale_get = function () {
  208. // return this.scale || new THREE.Vector3();
  209. // }
  210. this.createEditTool = function() {
  211. var self = this;
  212. //let scene = this.getScene();
  213. if(!this.editTool){
  214. let nodeName = 'editTool';
  215. let node = {
  216. "extends": "proxy/objects/edittool.vwf",
  217. "properties": {}
  218. }
  219. this.children.create(nodeName, node, function( child ) {
  220. child.createVisual();
  221. })
  222. }
  223. }
  224. this.globalToLocalRotation = function(aQ, order){
  225. let ord = order ? order: 'XYZ';
  226. let q = this.localQuaternion().inverse().multiply(aQ); //new THREE.Quaternion().setFromEuler(euler)
  227. let localEuler = new THREE.Euler().setFromQuaternion(q, ord);
  228. return [
  229. THREE.Math.radToDeg(localEuler.x),
  230. THREE.Math.radToDeg(localEuler.y),
  231. THREE.Math.radToDeg(localEuler.z)
  232. ]
  233. }
  234. this.placeInFrontOf = function(nodeID, dist) {
  235. // fixed distance from camera to the object
  236. let node = this.getScene().findNodeByID(nodeID);
  237. let cwd = node.worldDirection(); //new THREE.Vector3();
  238. cwd.multiplyScalar(dist);
  239. cwd.add(node.position);
  240. let nodeQ = node.localQuaternion();
  241. let localEuler = new THREE.Euler().setFromQuaternion(nodeQ, 'XYZ');
  242. let rotation = [
  243. THREE.Math.radToDeg(localEuler.x),
  244. THREE.Math.radToDeg(localEuler.y),
  245. THREE.Math.radToDeg(localEuler.z)
  246. ];
  247. this.position = cwd;
  248. this.rotation = rotation;
  249. }