aframeComponent.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. "use strict";
  2. // VWF & A-Frame view driver
  3. // Copyright 2017 Krestianstvo.org project
  4. //
  5. // Copyright 2012 United States Government, as represented by the Secretary of Defense, Under
  6. // Secretary of Defense (Personnel & Readiness).
  7. //
  8. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  9. // in compliance with the License. You may obtain a copy of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software distributed under the License
  14. // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  15. // or implied. See the License for the specific language governing permissions and limitations under
  16. // the License.
  17. /// vwf/view/lesson creates a view interface for instruction text.
  18. ///
  19. /// @module vwf/view/aframe
  20. /// @requires vwf/view
  21. define(["module", "vwf/view"], function (module, view) {
  22. var self;
  23. return view.load(module, {
  24. // == Module Definition ====================================================================
  25. initialize: function (options) {
  26. self = this;
  27. this.nodes = {};
  28. this.tickTime = 0;
  29. this.realTickDif = 50;
  30. this.lastrealTickDif = 50;
  31. this.lastRealTick = performance.now();
  32. this.state.appInitialized = false;
  33. if (typeof options == "object") {
  34. this.rootSelector = options["application-root"];
  35. }
  36. else {
  37. this.rootSelector = options;
  38. }
  39. },
  40. // initializedNode: function( nodeID, childID ) {
  41. // },
  42. createdNode: function (nodeID, childID, childExtendsID, childImplementsIDs,
  43. childSource, childType, childIndex, childName, callback /* ( ready ) */) {
  44. var node = this.state.nodes[childID];
  45. // If the "nodes" object does not have this object in it, it must not be one that
  46. // this driver cares about
  47. if (!node) {
  48. return;
  49. }
  50. if(this.state.nodes[childID]) {
  51. this.nodes[childID] = {id:childID,extends:childExtendsID};
  52. }
  53. else if (this.state.nodes[childID] && this.state.nodes[childID].aframeObj) {
  54. this.nodes[childID] = {
  55. id:childID,
  56. extends:childExtendsID
  57. };
  58. }
  59. },
  60. initializedNode: function( nodeID, childID ) {
  61. var node = this.state.nodes[childID];
  62. if (!node) {
  63. return;
  64. }
  65. if (this.nodes[childID].extends == "http://vwf.example.com/aframe/a-sound-component.vwf"){
  66. console.log(vwf.getProperty(childID, 'isPlaying'));
  67. self.kernel.callMethod(childID, "playSound");
  68. }
  69. },
  70. createdProperty: function (nodeId, propertyName, propertyValue) {
  71. return this.satProperty(nodeId, propertyName, propertyValue);
  72. },
  73. initializedProperty: function (nodeId, propertyName, propertyValue) {
  74. return this.satProperty(nodeId, propertyName, propertyValue);
  75. },
  76. satProperty: function (nodeId, propertyName, propertyValue) {
  77. var node = this.state.nodes[ nodeId ];
  78. if ( !( node && node.aframeObj ) ) {
  79. return;
  80. }
  81. if (node.name == "material" && propertyName == 'repeat') {
  82. let srcID = node.aframeObj.el.getAttribute('material').src.id;
  83. let elID = '#'+ srcID;
  84. if(srcID) self.kernel.callMethod(nodeId, "updateSrc", [elID])
  85. }
  86. // if (node.name == "material" && propertyName == 'color') {
  87. // console.log("sat color on material");
  88. // // let nodeColor = vwf.getProperty(node.parentID, propertyName);
  89. // }
  90. // switch (propertyName) {
  91. // case "color":
  92. // if (propertyValue) {
  93. // // self.kernel.callMethod (nodeId, 'initLang');
  94. // // console.log("sat color "+ propertyValue)
  95. // }
  96. // break;
  97. // }
  98. },
  99. // firedEvent: function (nodeID, eventName, eventParameters) {
  100. // },
  101. ticked: function (vwfTime) {
  102. lerpTick ();
  103. },
  104. gotProperty: function (nodeId, propertyName, propertyValue) {
  105. var selfs = this;
  106. var node = this.state.nodes[nodeId];
  107. if (!(node && node.aframeObj)) {
  108. return;
  109. }
  110. // if (this.nodes[nodeId].extends == "http://vwf.example.com/aframe/a-sound-component.vwf"){
  111. // if (propertyName == "currentTime"){
  112. // console.log(node.aframeObj.el.components.sound.listener.context.currentTime);
  113. // }
  114. // }
  115. },
  116. calledMethod: function( nodeID, methodName, methodParameters, methodValue ) {
  117. var node = this.state.nodes[nodeID];
  118. if (!(node && node.aframeObj)) {
  119. return;
  120. }
  121. if (this.nodes[nodeID].extends == "http://vwf.example.com/aframe/a-sound-component.vwf"){
  122. if (methodName == "stopSound"){
  123. console.log("stop sound");
  124. node.aframeObj.el.components.sound.stopSound();
  125. self.kernel.setProperty(nodeID, "isPlaying", false);
  126. //node.aframeObj.stopSound();
  127. }
  128. if (methodName == "playSound"){
  129. console.log("play sound");
  130. node.aframeObj.el.components.sound.playSound();
  131. self.kernel.setProperty(nodeID, "isPlaying", true);
  132. //node.aframeObj.stopSound();
  133. }
  134. if (methodName == "pauseSound"){
  135. console.log("pause sound");
  136. node.aframeObj.el.components.sound.pauseSound();
  137. self.kernel.setProperty(nodeID, "isPlaying", false);
  138. //node.aframeObj.stopSound();
  139. }
  140. }
  141. }
  142. });
  143. function lerpTick () {
  144. var now = performance.now();
  145. self.realTickDif = now - self.lastRealTick;
  146. self.lastRealTick = now;
  147. //reset - loading can cause us to get behind and always but up against the max prediction value
  148. // self.tickTime = 0;
  149. let interNodes = Object.entries(self.nodes).filter(node =>
  150. node[1].extends == 'http://vwf.example.com/aframe/interpolation-component.vwf');
  151. interNodes.forEach(node => {
  152. let nodeID = node[0];
  153. if ( self.state.nodes[nodeID] ) {
  154. self.nodes[nodeID].tickTime = 0;
  155. if(!self.nodes[nodeID].interpolate)
  156. {
  157. self.nodes[nodeID].interpolate = {
  158. 'position': {},
  159. 'rotation': {}
  160. }
  161. }
  162. self.nodes[nodeID].interpolate.position.lastTick = (self.nodes[nodeID].interpolate.position.selfTick);
  163. self.nodes[nodeID].interpolate.position.selfTick = getPosition(nodeID);
  164. self.nodes[nodeID].interpolate.rotation.lastTick = (self.nodes[nodeID].interpolate.rotation.selfTick);
  165. self.nodes[nodeID].interpolate.rotation.selfTick = getRotation(nodeID);
  166. //console.log(self.nodes[nodeID].interpolate.rotation.selfTick);
  167. //self.nodes[nodeID].lastTickTransform = self.nodes[nodeID].selfTickTransform;
  168. //self.nodes[nodeID].selfTickTransform = getTransform(nodeID);
  169. }
  170. })
  171. // for ( var nodeID in interNodes ) {
  172. // if ( self.state.nodes[nodeID] ) {
  173. // self.nodes[nodeID].tickTime = 0;
  174. // self.nodes[nodeID].lastTickTransform = self.nodes[nodeID].selfTickTransform;
  175. // self.nodes[nodeID].selfTickTransform = getTransform(nodeID);
  176. // }
  177. // }
  178. }
  179. function getRotation(id) {
  180. let r = new THREE.Euler();
  181. let rot = r.copy(self.state.nodes[id].aframeObj.el.object3D.rotation);
  182. //let rot = self.state.nodes[id].aframeObj.el.getAttribute('rotation');
  183. //let interp = (new THREE.Vector3()).fromArray(Object.values(rot))//goog.vec.Mat4.clone(self.state.nodes[id].threeObject.matrix.elements);
  184. return rot;
  185. }
  186. function getPosition(id) {
  187. let p = new THREE.Vector3();
  188. let pos = p.copy(self.state.nodes[id].aframeObj.el.object3D.position);
  189. //let pos = self.state.nodes[id].aframeObj.el.getAttribute('position');
  190. //let interp = (new THREE.Vector3()).fromArray(Object.values(pos))//goog.vec.Mat4.clone(self.state.nodes[id].threeObject.matrix.elements);
  191. return pos;
  192. }
  193. });