aframeComponent.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. viewEdit: false
  76. };
  77. let entityID = this.state.nodes[childID].parentID;
  78. let viewDriver = vwf.views["/drivers/view/aframe"];
  79. let entityNode = Object.entries(viewDriver.nodes).find(el =>
  80. el[1].id == entityID
  81. )[1];
  82. if (!entityNode.components)
  83. entityNode.components = {}
  84. entityNode.components[this.state.nodes[childID].name] = childID;
  85. //console.log(entityNode);
  86. }
  87. // else if (this.state.nodes[childID] && this.state.nodes[childID].aframeObj) {
  88. // this.nodes[childID] = {
  89. // id:childID,
  90. // extends:childExtendsID
  91. // };
  92. // }
  93. },
  94. initializedNode: function (nodeID, childID) {
  95. let self = this;
  96. var node = this.state.nodes[childID];
  97. if (!node) {
  98. return;
  99. }
  100. if (this.nodes[childID].extends == "proxy/aframe/a-sound-component.vwf") {
  101. console.log(vwf.getProperty(childID, 'isPlaying'));
  102. //self.kernel.callMethod(childID, "playSound");
  103. }
  104. },
  105. createdProperty: function (nodeId, propertyName, propertyValue) {
  106. return this.satProperty(nodeId, propertyName, propertyValue);
  107. },
  108. initializedProperty: function (nodeId, propertyName, propertyValue) {
  109. return this.satProperty(nodeId, propertyName, propertyValue);
  110. },
  111. satProperty: function (nodeId, propertyName, propertyValue) {
  112. let self = this;
  113. var node = this.state.nodes[nodeId];
  114. if (!(node && node.aframeObj)) {
  115. return;
  116. }
  117. if (node.prototypes.includes("proxy/aframe/aMaterialComponent.vwf")) {
  118. if (propertyName == 'repeat') {
  119. self.kernel.callMethod(nodeId, "refreshSrc", []);
  120. // let src = node.aframeObj.el.getAttribute('material').src;
  121. // if (src) {
  122. // let srcID = src.id;
  123. // let elID = '#'+ srcID;
  124. // if(srcID) self.kernel.callMethod(nodeId, "updateSrc", [elID])
  125. // }
  126. }
  127. }
  128. },
  129. firedEvent: function (nodeID, eventName, eventParameters) {
  130. let self = this;
  131. var node = this.state.nodes[nodeID];
  132. if (!(node)) {
  133. return;
  134. }
  135. },
  136. executed: function (nodeID, scriptText, scriptType) {
  137. let self = this;
  138. var node = this.state.nodes[nodeID];
  139. if (!(node)) {
  140. return;
  141. }
  142. },
  143. ticked: function (vwfTime) {
  144. _self_.lerpTick();
  145. },
  146. tocked: function (vwfTime) {
  147. // lerpTock ();
  148. },
  149. gotProperty: function (nodeId, propertyName, propertyValue) {
  150. var node = this.state.nodes[nodeId];
  151. if (!(node && node.aframeObj)) {
  152. return;
  153. }
  154. // if (this.nodes[nodeId].extends == "proxy/aframe/a-sound-component.vwf"){
  155. // if (propertyName == "currentTime"){
  156. // console.log(node.aframeObj.el.components.sound.listener.context.currentTime);
  157. // }
  158. // }
  159. },
  160. calledMethod: function (nodeID, methodName, methodParameters, methodValue) {
  161. let self = this;
  162. var node = this.state.nodes[nodeID];
  163. if (!(node && node.aframeObj)) {
  164. return;
  165. }
  166. if (this.nodes[nodeID].extends == "proxy/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. }
  189. lerpTick() {
  190. // var now = performance.now();
  191. // self.realTickDif = now - self.lastRealTick;
  192. // self.lastRealTick = now;
  193. //reset - loading can cause us to get behind and always but up against the max prediction value
  194. // self.tickTime = 0;
  195. let self = this.instance;
  196. let interNodes = Object.entries(self.nodes).filter(node =>
  197. node[1].extends == 'proxy/aframe/interpolation-component.vwf');
  198. interNodes.forEach(node => {
  199. let nodeID = node[0];
  200. if (self.state.nodes[nodeID]) {
  201. if (!self.nodes[nodeID].lastRealTick)
  202. self.nodes[nodeID].lastRealTick = performance.now();
  203. var now = performance.now();
  204. self.nodes[nodeID].realTickDif = now - self.nodes[nodeID].lastRealTick;
  205. self.nodes[nodeID].lastRealTick = now;
  206. self.nodes[nodeID].tickTime = 0;
  207. //console.log(self.nodes[nodeID].realTickDif);
  208. if (!self.nodes[nodeID].interpolate) {
  209. self.nodes[nodeID].interpolate = {
  210. 'position': {},
  211. 'rotation': {},
  212. 'scale': {}
  213. }
  214. }
  215. self.nodes[nodeID].interpolate.position.lastTick = (self.nodes[nodeID].interpolate.position.selfTick);
  216. self.nodes[nodeID].interpolate.position.selfTick = self.getPosition(nodeID);
  217. self.nodes[nodeID].interpolate.rotation.lastTick = (self.nodes[nodeID].interpolate.rotation.selfTick);
  218. self.nodes[nodeID].interpolate.rotation.selfTick = self.getRotation(nodeID);
  219. self.nodes[nodeID].interpolate.scale.lastTick = (self.nodes[nodeID].interpolate.scale.selfTick);
  220. self.nodes[nodeID].interpolate.scale.selfTick = self.getScale(nodeID);
  221. //console.log(self.nodes[nodeID].interpolate.rotation.selfTick);
  222. //self.nodes[nodeID].lastTickTransform = self.nodes[nodeID].selfTickTransform;
  223. //self.nodes[nodeID].selfTickTransform = getTransform(nodeID);
  224. }
  225. })
  226. }
  227. }
  228. export { AFrameComponentView as default }