aframeComponent.js 11 KB

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