aframeComponent.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2020 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  4. */
  5. // VWF & A-Frame components view driver
  6. import {Fabric} from '/core/vwf/fabric.js';
  7. class AFrameComponentView extends Fabric {
  8. constructor(module) {
  9. console.log("AFrameComponentView constructor");
  10. super(module, 'View');
  11. }
  12. factory() {
  13. let _self_ = this;
  14. return this.load(this.module,
  15. {
  16. // == Module Definition ====================================================================
  17. initialize: function (options) {
  18. let self = this;
  19. this.fabric = _self_;
  20. this.nodes = {};
  21. this.tickTime = 0;
  22. this.realTickDif = 50;
  23. this.lastrealTickDif = 50;
  24. this.lastRealTick = performance.now();
  25. this.interpolateView = true;
  26. this.state.appInitialized = false;
  27. if (typeof options == "object") {
  28. this.rootSelector = options["application-root"];
  29. }
  30. else {
  31. this.rootSelector = options;
  32. }
  33. this.getRotation = function(id) {
  34. let r = new THREE.Euler();
  35. let rot = r.copy(self.state.nodes[id].aframeObj.el.object3D.rotation);
  36. return rot;
  37. }
  38. this.getPosition = function (id) {
  39. // let p = (new THREE.Vector3()).copy(self.state.nodes[id].aframeObj.el.object3D.position);
  40. // let pos = goog.vec.Vec3.createFromValues(p.x, p.y, p.z)
  41. return self.state.nodes[id].aframeObj.el.object3D.position.clone();
  42. }
  43. this.getScale = function (id) {
  44. // let p = (new THREE.Vector3()).copy(self.state.nodes[id].aframeObj.el.object3D.scale);
  45. // let data = goog.vec.Vec3.createFromValues(p.x, p.y, p.z)
  46. return self.state.nodes[id].aframeObj.el.object3D.scale.clone();
  47. }
  48. // this.resetInterpolation = function (nodeID, propertyName) {
  49. // if (propertyName == 'position') {
  50. // self.nodes[nodeID].interpolate.position.lastTick = getPosition(nodeID);
  51. // self.nodes[nodeID].interpolate.position.selfTick = goog.vec.Vec3.clone(self.nodes[nodeID].interpolate.position.lastTick);
  52. // }
  53. // if (propertyName == 'rotation') {
  54. // self.nodes[nodeID].interpolate.rotation.lastTick = getRotation(nodeID);
  55. // self.nodes[nodeID].interpolate.rotation.selfTick = self.nodes[nodeID].interpolate.rotation.lastTick;
  56. // }
  57. // }
  58. },
  59. // initializedNode: function( nodeID, childID ) {
  60. // },
  61. createdNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  62. childSource, childType, childIndex, childName, callback /* ( ready ) */) {
  63. var node = this.state.nodes[childID];
  64. // If the "nodes" object does not have this object in it, it must not be one that
  65. // this driver cares about
  66. if (!node) {
  67. return;
  68. }
  69. if (this.state.nodes[childID]) {
  70. this.nodes[childID] = {
  71. id: childID,
  72. extends: childExtendsID,
  73. entityID: this.state.nodes[childID].parentID,
  74. liveBindings: {}
  75. };
  76. let entityID = this.state.nodes[childID].parentID;
  77. let viewDriver = vwf.views["/drivers/view/aframe"];
  78. let entityNode = Object.entries(viewDriver.nodes).find(el =>
  79. el[1].id == entityID
  80. )[1];
  81. if (!entityNode.components)
  82. entityNode.components = {}
  83. entityNode.components[this.state.nodes[childID].name] = childID;
  84. //console.log(entityNode);
  85. }
  86. // else if (this.state.nodes[childID] && this.state.nodes[childID].aframeObj) {
  87. // this.nodes[childID] = {
  88. // id:childID,
  89. // extends:childExtendsID
  90. // };
  91. // }
  92. },
  93. initializedNode: function (nodeID, childID) {
  94. let self = this;
  95. var node = this.state.nodes[childID];
  96. if (!node) {
  97. return;
  98. }
  99. if (this.nodes[childID].extends == "proxy/aframe/a-sound-component.vwf") {
  100. console.log(vwf.getProperty(childID, 'isPlaying'));
  101. self.kernel.callMethod(childID, "playSound");
  102. }
  103. },
  104. createdProperty: function (nodeId, propertyName, propertyValue) {
  105. return this.satProperty(nodeId, propertyName, propertyValue);
  106. },
  107. initializedProperty: function (nodeId, propertyName, propertyValue) {
  108. return this.satProperty(nodeId, propertyName, propertyValue);
  109. },
  110. satProperty: function (nodeId, propertyName, propertyValue) {
  111. let self = this;
  112. var node = this.state.nodes[nodeId];
  113. if (!(node && node.aframeObj)) {
  114. return;
  115. }
  116. if (node.name == "material") {
  117. if (propertyName == 'repeat') {
  118. self.kernel.callMethod(nodeId, "refreshSrc", []);
  119. // let src = node.aframeObj.el.getAttribute('material').src;
  120. // if (src) {
  121. // let srcID = src.id;
  122. // let elID = '#'+ srcID;
  123. // if(srcID) self.kernel.callMethod(nodeId, "updateSrc", [elID])
  124. // }
  125. }
  126. }
  127. },
  128. firedEvent: function (nodeID, eventName, eventParameters) {
  129. let self = this;
  130. var node = this.state.nodes[nodeID];
  131. if (!(node)) {
  132. return;
  133. }
  134. },
  135. executed: function (nodeID, scriptText, scriptType) {
  136. let self = this;
  137. var node = this.state.nodes[nodeID];
  138. if (!(node)) {
  139. return;
  140. }
  141. },
  142. ticked: function (vwfTime) {
  143. _self_.lerpTick();
  144. },
  145. tocked: function (vwfTime) {
  146. // lerpTock ();
  147. },
  148. gotProperty: function (nodeId, propertyName, propertyValue) {
  149. var node = this.state.nodes[nodeId];
  150. if (!(node && node.aframeObj)) {
  151. return;
  152. }
  153. // if (this.nodes[nodeId].extends == "proxy/aframe/a-sound-component.vwf"){
  154. // if (propertyName == "currentTime"){
  155. // console.log(node.aframeObj.el.components.sound.listener.context.currentTime);
  156. // }
  157. // }
  158. },
  159. calledMethod: function (nodeID, methodName, methodParameters, methodValue) {
  160. let self = this;
  161. var node = this.state.nodes[nodeID];
  162. if (!(node && node.aframeObj)) {
  163. return;
  164. }
  165. if (this.nodes[nodeID].extends == "proxy/aframe/a-sound-component.vwf") {
  166. if (methodName == "stopSound") {
  167. console.log("stop sound");
  168. node.aframeObj.el.components.sound.stopSound();
  169. self.kernel.setProperty(nodeID, "isPlaying", false);
  170. //node.aframeObj.stopSound();
  171. }
  172. if (methodName == "playSound") {
  173. console.log("play sound");
  174. node.aframeObj.el.components.sound.playSound();
  175. self.kernel.setProperty(nodeID, "isPlaying", true);
  176. //node.aframeObj.stopSound();
  177. }
  178. if (methodName == "pauseSound") {
  179. console.log("pause sound");
  180. node.aframeObj.el.components.sound.pauseSound();
  181. self.kernel.setProperty(nodeID, "isPlaying", false);
  182. //node.aframeObj.stopSound();
  183. }
  184. }
  185. }
  186. });
  187. }
  188. 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 self = this.instance;
  195. let interNodes = Object.entries(self.nodes).filter(node =>
  196. node[1].extends == 'proxy/aframe/interpolation-component.vwf');
  197. interNodes.forEach(node => {
  198. let nodeID = node[0];
  199. if (self.state.nodes[nodeID]) {
  200. if (!self.nodes[nodeID].lastRealTick)
  201. self.nodes[nodeID].lastRealTick = performance.now();
  202. var now = performance.now();
  203. self.nodes[nodeID].realTickDif = now - self.nodes[nodeID].lastRealTick;
  204. self.nodes[nodeID].lastRealTick = now;
  205. self.nodes[nodeID].tickTime = 0;
  206. //console.log(self.nodes[nodeID].realTickDif);
  207. if (!self.nodes[nodeID].interpolate) {
  208. self.nodes[nodeID].interpolate = {
  209. 'position': {},
  210. 'rotation': {},
  211. 'scale': {}
  212. }
  213. }
  214. self.nodes[nodeID].interpolate.position.lastTick = (self.nodes[nodeID].interpolate.position.selfTick);
  215. self.nodes[nodeID].interpolate.position.selfTick = self.getPosition(nodeID);
  216. self.nodes[nodeID].interpolate.rotation.lastTick = (self.nodes[nodeID].interpolate.rotation.selfTick);
  217. self.nodes[nodeID].interpolate.rotation.selfTick = self.getRotation(nodeID);
  218. self.nodes[nodeID].interpolate.scale.lastTick = (self.nodes[nodeID].interpolate.scale.selfTick);
  219. self.nodes[nodeID].interpolate.scale.selfTick = self.getScale(nodeID);
  220. //console.log(self.nodes[nodeID].interpolate.rotation.selfTick);
  221. //self.nodes[nodeID].lastTickTransform = self.nodes[nodeID].selfTickTransform;
  222. //self.nodes[nodeID].selfTickTransform = getTransform(nodeID);
  223. }
  224. })
  225. }
  226. }
  227. export { AFrameComponentView as default }