aentity.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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.fromhitstartEventMethod = function (value) {
  90. //intersect method
  91. }
  92. this.fromhitendEventMethod = function (value) {
  93. //clearIntersect method
  94. }
  95. this.clickEventMethod = function (value) {
  96. //clickEventMethod
  97. }
  98. this.mousedownEventMethod = function (value) {
  99. //clickEventMethod
  100. }
  101. this.mouseupEventMethod = function (value) {
  102. //clickEventMethod
  103. }
  104. this.setOwner = function (param) {
  105. var clients = this.find("doc('proxy/clients.vwf')")[0];
  106. if (clients !== undefined) {
  107. //console.log(clients.children);
  108. let clientsArray = [];
  109. clients.children.forEach(function (element) {
  110. clientsArray.push(element.name);
  111. });
  112. //console.log(clientsArray);
  113. if (this.ownedBy) {
  114. if (clientsArray.includes(this.ownedBy)) {
  115. console.log(this.id + " already owned by: " + param);
  116. } else {
  117. this.ownedBy = param;
  118. //console.log(this.id + ' set owner to: ' + param);
  119. }
  120. } else {
  121. this.ownedBy = param;
  122. //console.log(this.id + ' set owner to: ' + param);
  123. }
  124. }
  125. }
  126. this.updateMethod = function (methodName, methodBody, params) {
  127. vwf.setMethod(this.id, methodName, { body: methodBody, type: "application/javascript", parameters: params });
  128. }
  129. this.callMethod = function (methodName, params) {
  130. vwf.callMethod(this.id, methodName, params)
  131. }
  132. this.do = function () {
  133. //do in step
  134. }
  135. this.step = function () {
  136. if (this.stepping) {
  137. this.do();
  138. }
  139. let t = this.stepTime ? this.stepTime : 0.05;
  140. this.future(t).step();
  141. }
  142. this.onGlobalBeat = function (obj) {
  143. //dispatch the beat example send OSC
  144. let transportNode = this.find('//' + obj.name)[0];
  145. let rate = transportNode.animationRate; // 1 by default
  146. let drumSeq = [
  147. { beat: 0, msg: 0 },
  148. { beat: 30, msg: 0 }];
  149. drumSeq.forEach(el => {
  150. if (el.beat / rate == obj.beat) {
  151. let msg = {
  152. address: "/trigger/sample01",
  153. args: [this.time, 'bd_808', 3]
  154. };
  155. //for synth {beat:0, msg: "A"}
  156. //let msg = {
  157. // address: "/trigger/synth01",
  158. // args: [this.time, 'piano', el.msg, 0.1, 1]};
  159. // this.sendOSC(msg);
  160. // this.changeVisual();
  161. }
  162. })
  163. }
  164. this.changeVisual = function () {
  165. //code for changing me
  166. this.future(0.1).resetVisual();
  167. }
  168. this.resetVisual = function () {
  169. }
  170. this.getRandomColor = function () {
  171. var letters = '0123456789ABCDEF';
  172. var color = '#';
  173. for (var i = 0; i < 6; i++) {
  174. color += letters[Math.floor(this.random() * 16)];
  175. }
  176. return color;
  177. }
  178. this.randomize = function () {
  179. }
  180. // this.position_set = function (value) {
  181. // var position = this.translationFromValue(value); // parse incoming value
  182. // if (!position || !this.position) {
  183. // this.position = new THREE.Vector3();
  184. // } else if (!this.position.equals(position)){ //!goog.vec.Vec3.equals(this.position || goog.vec.Vec3.create(), position)) {
  185. // this.position = position;
  186. // this.positionChanged(position);
  187. // }
  188. // }
  189. // this.position_get = function () {
  190. // return this.position || new THREE.Vector3();
  191. // }
  192. // this.rotation_set = function (value) {
  193. // var rotation = this.translationFromValue(value); // parse incoming value
  194. // if (!rotation || !this.rotation) {
  195. // this.rotation = new THREE.Vector3();
  196. // } else if (!this.rotation.equals(rotation)){ //!goog.vec.Vec3.equals(this.rotation || goog.vec.Vec3.create(), rotation)) {
  197. // this.rotation = rotation;
  198. // this.rotationChanged(rotation);
  199. // }
  200. // }
  201. // this.rotation_get = function () {
  202. // return this.rotation || new THREE.Vector3();
  203. // }
  204. // this.scale_set = function (value) {
  205. // var scale = this.translationFromValue( value ); // parse incoming value
  206. // if(!scale || !this.scale){
  207. // this.scale = new THREE.Vector3();
  208. // } else if (!this.scale.equals(scale)){ //! goog.vec.Vec3.equals( this.scale || goog.vec.Vec3.create(), scale ) ) {
  209. // this.scale = scale;
  210. // this.scaleChanged( scale);
  211. // }
  212. // }
  213. // this.scale_get = function () {
  214. // return this.scale || new THREE.Vector3();
  215. // }
  216. this.createEditTool = function() {
  217. var self = this;
  218. //let scene = this.getScene();
  219. if(!this.editTool){
  220. let nodeName = 'editTool';
  221. let node = {
  222. "extends": "proxy/objects/edittool.vwf",
  223. "properties": {}
  224. }
  225. this.children.create(nodeName, node, function( child ) {
  226. child.createVisual();
  227. })
  228. }
  229. }
  230. this.globalToLocalRotation = function(aQ, order){
  231. let ord = order ? order: 'XYZ';
  232. let q = this.localQuaternion().invert().multiply(aQ); //new THREE.Quaternion().setFromEuler(euler)
  233. let localEuler = new THREE.Euler().setFromQuaternion(q, ord);
  234. return [
  235. THREE.Math.radToDeg(localEuler.x),
  236. THREE.Math.radToDeg(localEuler.y),
  237. THREE.Math.radToDeg(localEuler.z)
  238. ]
  239. }
  240. this.placeInFrontOf = function(nodeID, dist) {
  241. // fixed distance from camera to the object
  242. let node = this.getScene().findNodeByID(nodeID);
  243. let cwd = node.worldDirection(); //new THREE.Vector3();
  244. cwd.multiplyScalar(dist);
  245. cwd.add(node.position);
  246. let nodeQ = node.localQuaternion();
  247. let localEuler = new THREE.Euler().setFromQuaternion(nodeQ, 'XYZ');
  248. let rotation = [
  249. THREE.Math.radToDeg(localEuler.x),
  250. THREE.Math.radToDeg(localEuler.y),
  251. THREE.Math.radToDeg(localEuler.z)
  252. ];
  253. this.position = cwd;
  254. this.rotation = rotation;
  255. }
  256. this.doButtonTriggerdownAction = function(button, controllerID, point){
  257. //do button action
  258. console.log('TriggerdownAction form: ', button)
  259. }
  260. this.triggerdownAction = function(){
  261. }
  262. this.triggerupAction = function(){
  263. }
  264. this.mousedownAction = function(){
  265. }
  266. this.mouseupAction = function(){
  267. }
  268. this.createChild = function(id,nodeDef){
  269. let self = this;
  270. this.children.create(id, nodeDef, function(child){
  271. if(child.stepping){
  272. child.step();
  273. }
  274. })
  275. }
  276. this.createChildComponent = function(name,nodeDef){
  277. let self = this;
  278. if(this[name]){
  279. this.children.delete(this[name])
  280. }
  281. let dn = nodeDef.properties.displayName;
  282. if(this[dn]){
  283. this.children.delete(this[dn])
  284. }
  285. this.children.create(name, nodeDef, function(child){})
  286. }
  287. this.positionChanged = function(){
  288. }
  289. this.setPosition = function(value){
  290. this.position = value;
  291. this.positionChanged();
  292. }