aframeComponent.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. "use strict";
  2. /*
  3. The MIT License (MIT)
  4. Copyright (c) 2014-2018 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  5. Virtual World Framework Apache 2.0 license (https://github.com/NikolaySuslov/livecodingspace/blob/master/licenses/LICENSE_VWF.md)
  6. */
  7. // VWF & A-Frame components view driver
  8. define(["module", "vwf/view"], function (module, view) {
  9. var self;
  10. return view.load(module, {
  11. // == Module Definition ====================================================================
  12. initialize: function (options) {
  13. self = this;
  14. this.nodes = {};
  15. this.tickTime = 0;
  16. this.realTickDif = 50;
  17. this.lastrealTickDif = 50;
  18. this.lastRealTick = performance.now();
  19. this.interpolateView = true;
  20. this.state.appInitialized = false;
  21. if (typeof options == "object") {
  22. this.rootSelector = options["application-root"];
  23. }
  24. else {
  25. this.rootSelector = options;
  26. }
  27. this.resetInterpolation = function (nodeID, propertyName) {
  28. if (propertyName == 'position') {
  29. self.nodes[nodeID].interpolate.position.lastTick = getPosition(nodeID);
  30. self.nodes[nodeID].interpolate.position.selfTick = goog.vec.Vec3.clone(self.nodes[nodeID].interpolate.position.lastTick);
  31. }
  32. if (propertyName == 'rotation') {
  33. self.nodes[nodeID].interpolate.rotation.lastTick = getRotation(nodeID);
  34. self.nodes[nodeID].interpolate.rotation.selfTick = self.nodes[nodeID].interpolate.rotation.lastTick;
  35. }
  36. }
  37. },
  38. // initializedNode: function( nodeID, childID ) {
  39. // },
  40. createdNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  41. childSource, childType, childIndex, childName, callback /* ( ready ) */) {
  42. var node = this.state.nodes[childID];
  43. // If the "nodes" object does not have this object in it, it must not be one that
  44. // this driver cares about
  45. if (!node) {
  46. return;
  47. }
  48. if (this.state.nodes[childID]) {
  49. this.nodes[childID] = {
  50. id: childID,
  51. extends: childExtendsID,
  52. entityID: this.state.nodes[childID].parentID,
  53. liveBindings: {}
  54. };
  55. let entityID = this.state.nodes[childID].parentID;
  56. let viewDriver = vwf.views["vwf/view/aframe"];
  57. let entityNode = Object.entries(viewDriver.nodes).find(el =>
  58. el[1].id == entityID
  59. )[1];
  60. if (!entityNode.components)
  61. entityNode.components = {}
  62. entityNode.components[this.state.nodes[childID].name] = childID;
  63. //console.log(entityNode);
  64. }
  65. // else if (this.state.nodes[childID] && this.state.nodes[childID].aframeObj) {
  66. // this.nodes[childID] = {
  67. // id:childID,
  68. // extends:childExtendsID
  69. // };
  70. // }
  71. },
  72. initializedNode: function (nodeID, childID) {
  73. var node = this.state.nodes[childID];
  74. if (!node) {
  75. return;
  76. }
  77. if (this.nodes[childID].extends == "http://vwf.example.com/aframe/a-sound-component.vwf") {
  78. console.log(vwf.getProperty(childID, 'isPlaying'));
  79. self.kernel.callMethod(childID, "playSound");
  80. }
  81. },
  82. createdProperty: function (nodeId, propertyName, propertyValue) {
  83. return this.satProperty(nodeId, propertyName, propertyValue);
  84. },
  85. initializedProperty: function (nodeId, propertyName, propertyValue) {
  86. return this.satProperty(nodeId, propertyName, propertyValue);
  87. },
  88. satProperty: function (nodeId, propertyName, propertyValue) {
  89. var node = this.state.nodes[nodeId];
  90. if (!(node && node.aframeObj)) {
  91. return;
  92. }
  93. if (node.name == "material") {
  94. if (propertyName == 'repeat') {
  95. self.kernel.callMethod(nodeId, "refreshSrc", []);
  96. // let src = node.aframeObj.el.getAttribute('material').src;
  97. // if (src) {
  98. // let srcID = src.id;
  99. // let elID = '#'+ srcID;
  100. // if(srcID) self.kernel.callMethod(nodeId, "updateSrc", [elID])
  101. // }
  102. }
  103. }
  104. },
  105. firedEvent: function (nodeID, eventName, eventParameters) {
  106. var node = this.state.nodes[nodeID];
  107. if (!(node)) {
  108. return;
  109. }
  110. if (eventName == "printIt") {
  111. var clientThatSatProperty = self.kernel.client();
  112. var me = self.kernel.moniker();
  113. // If the transform property was initially updated by this view....
  114. if (clientThatSatProperty == me) {
  115. let selectedText = eventParameters[0];
  116. let printText = _app.helpers.replaceSubStringALL(selectedText, 'this', 'me');
  117. if (!self.nodes[nodeID].liveBindings) {
  118. self.nodes[nodeID].liveBindings = {}
  119. }
  120. //let bindObject = self.nodes[nodeID].liveBindings;
  121. let bindString = 'vwf.views["vwf/view/aframeComponent"].nodes["' + nodeID + '"].liveBindings';
  122. let sender = JSON.stringify(eventParameters[1]);
  123. let scriptText = 'let binding = ' + bindString + '; binding.me = this; binding.sender = ' + sender + '; lively.vm.syncEval("var print = ' + printText + '",{topLevelVarRecorder: binding});';
  124. vwf_view.kernel.execute(nodeID, scriptText);
  125. }
  126. }
  127. },
  128. executed: function (nodeID, scriptText, scriptType) {
  129. var node = this.state.nodes[nodeID];
  130. if (!(node)) {
  131. return;
  132. }
  133. if (scriptText.includes('var print')) {
  134. let print = self.nodes[nodeID].liveBindings.print;
  135. let me = self.kernel.moniker();
  136. let sender = self.nodes[nodeID].liveBindings.sender;
  137. if (me == sender) {
  138. let data = lively.lang.obj.inspect(print); //JSON.stringify(print);
  139. self.kernel.fireEvent(nodeID, "printEvent", [data]);
  140. }
  141. }
  142. },
  143. ticked: function (vwfTime) {
  144. lerpTick();
  145. },
  146. tocked: function (vwfTime) {
  147. // lerpTock ();
  148. },
  149. gotProperty: function (nodeId, propertyName, propertyValue) {
  150. var selfs = this;
  151. var node = this.state.nodes[nodeId];
  152. if (!(node && node.aframeObj)) {
  153. return;
  154. }
  155. // if (this.nodes[nodeId].extends == "http://vwf.example.com/aframe/a-sound-component.vwf"){
  156. // if (propertyName == "currentTime"){
  157. // console.log(node.aframeObj.el.components.sound.listener.context.currentTime);
  158. // }
  159. // }
  160. },
  161. calledMethod: function (nodeID, methodName, methodParameters, methodValue) {
  162. var node = this.state.nodes[nodeID];
  163. if (!(node && node.aframeObj)) {
  164. return;
  165. }
  166. if (this.nodes[nodeID].extends == "http://vwf.example.com/aframe/a-sound-component.vwf") {
  167. if (methodName == "stopSound") {
  168. console.log("stop sound");
  169. node.aframeObj.el.components.sound.stopSound();
  170. self.kernel.setProperty(nodeID, "isPlaying", false);
  171. //node.aframeObj.stopSound();
  172. }
  173. if (methodName == "playSound") {
  174. console.log("play sound");
  175. node.aframeObj.el.components.sound.playSound();
  176. self.kernel.setProperty(nodeID, "isPlaying", true);
  177. //node.aframeObj.stopSound();
  178. }
  179. if (methodName == "pauseSound") {
  180. console.log("pause sound");
  181. node.aframeObj.el.components.sound.pauseSound();
  182. self.kernel.setProperty(nodeID, "isPlaying", false);
  183. //node.aframeObj.stopSound();
  184. }
  185. }
  186. }
  187. });
  188. function lerpTick() {
  189. // var now = performance.now();
  190. // self.realTickDif = now - self.lastRealTick;
  191. // self.lastRealTick = now;
  192. //reset - loading can cause us to get behind and always but up against the max prediction value
  193. // self.tickTime = 0;
  194. let interNodes = Object.entries(self.nodes).filter(node =>
  195. node[1].extends == 'http://vwf.example.com/aframe/interpolation-component.vwf');
  196. interNodes.forEach(node => {
  197. let nodeID = node[0];
  198. if (self.state.nodes[nodeID]) {
  199. if (!self.nodes[nodeID].lastRealTick)
  200. self.nodes[nodeID].lastRealTick = performance.now();
  201. var now = performance.now();
  202. self.nodes[nodeID].realTickDif = now - self.nodes[nodeID].lastRealTick;
  203. self.nodes[nodeID].lastRealTick = now;
  204. self.nodes[nodeID].tickTime = 0;
  205. //console.log(self.nodes[nodeID].realTickDif);
  206. if (!self.nodes[nodeID].interpolate) {
  207. self.nodes[nodeID].interpolate = {
  208. 'position': {},
  209. 'rotation': {},
  210. 'scale': {}
  211. }
  212. }
  213. self.nodes[nodeID].interpolate.position.lastTick = (self.nodes[nodeID].interpolate.position.selfTick);
  214. self.nodes[nodeID].interpolate.position.selfTick = getPosition(nodeID);
  215. self.nodes[nodeID].interpolate.rotation.lastTick = (self.nodes[nodeID].interpolate.rotation.selfTick);
  216. self.nodes[nodeID].interpolate.rotation.selfTick = getRotation(nodeID);
  217. self.nodes[nodeID].interpolate.scale.lastTick = (self.nodes[nodeID].interpolate.scale.selfTick);
  218. self.nodes[nodeID].interpolate.scale.selfTick = getScale(nodeID);
  219. //console.log(self.nodes[nodeID].interpolate.rotation.selfTick);
  220. //self.nodes[nodeID].lastTickTransform = self.nodes[nodeID].selfTickTransform;
  221. //self.nodes[nodeID].selfTickTransform = getTransform(nodeID);
  222. }
  223. })
  224. }
  225. function getRotation(id) {
  226. let r = new THREE.Euler();
  227. let rot = r.copy(self.state.nodes[id].aframeObj.el.object3D.rotation);
  228. //let rot = self.state.nodes[id].aframeObj.el.getAttribute('rotation');
  229. //let interp = (new THREE.Vector3()).fromArray(Object.values(rot))//goog.vec.Mat4.clone(self.state.nodes[id].threeObject.matrix.elements);
  230. return rot;
  231. }
  232. function getPosition(id) {
  233. // let p = new THREE.Vector3();
  234. // let pos = p.copy(self.state.nodes[id].aframeObj.el.object3D.position);
  235. let p = (new THREE.Vector3()).copy(self.state.nodes[id].aframeObj.el.object3D.position);
  236. let pos = goog.vec.Vec3.createFromValues(p.x, p.y, p.z)
  237. //let pos = self.state.nodes[id].aframeObj.el.getAttribute('position');
  238. //let interp = (new THREE.Vector3()).fromArray(Object.values(pos))//goog.vec.Mat4.clone(self.state.nodes[id].threeObject.matrix.elements);
  239. return pos;
  240. }
  241. function getScale(id) {
  242. // let p = new THREE.Vector3();
  243. // let pos = p.copy(self.state.nodes[id].aframeObj.el.object3D.position);
  244. let p = (new THREE.Vector3()).copy(self.state.nodes[id].aframeObj.el.object3D.scale);
  245. let data = goog.vec.Vec3.createFromValues(p.x, p.y, p.z)
  246. //let pos = self.state.nodes[id].aframeObj.el.getAttribute('position');
  247. //let interp = (new THREE.Vector3()).fromArray(Object.values(pos))//goog.vec.Mat4.clone(self.state.nodes[id].threeObject.matrix.elements);
  248. return data;
  249. }
  250. });