aframe-interpolation.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014-2018 Nikolai Suslov and the Krestianstvo.org project contributors. (https://github.com/NikolaySuslov/livecodingspace/blob/master/LICENSE.md)
  4. */
  5. if (typeof AFRAME === 'undefined') {
  6. throw new Error('Component attempted to register before AFRAME was available.');
  7. }
  8. AFRAME.registerComponent('interpolation', {
  9. schema: {
  10. enabled: { default: true },
  11. deltaPos: { default: 0.001 },
  12. deltaScale: { default: 0.001 },
  13. deltaRot: { default: 0.1 }
  14. },
  15. init: function () {
  16. this.driver = vwf.views["vwf/view/aframeComponent"];
  17. },
  18. update: function (oldData) {
  19. if (!this.interpolation) {
  20. this.deltaPos = parseFloat(this.data.deltaPos);
  21. this.deltaScale = parseFloat(this.data.deltaScale);
  22. this.deltaRot = THREE.Math.degToRad(parseFloat(this.data.deltaRot));
  23. this.enabled = JSON.parse(this.data.enabled);
  24. if (this.enabled) {
  25. }
  26. }
  27. },
  28. /**
  29. * Called when a component is removed (e.g., via removeAttribute).
  30. * Generally undoes all modifications to the entity.
  31. */
  32. remove: function () { },
  33. /**
  34. * Called on each scene tick.
  35. */
  36. tick: function (t, dt) {
  37. var now = performance.now();
  38. var timepassed = now - this.lastTime;
  39. //console.log(timepassed);
  40. if (!this.node) {
  41. let interNode = Object.entries(this.driver.state.nodes).find(el =>
  42. el[1].parentID == this.el.id && el[1].extendsID == "http://vwf.example.com/aframe/interpolation-component.vwf"
  43. );
  44. this.node = this.driver.nodes[interNode[0]];
  45. this.nodeState = interNode[1];
  46. }
  47. if (this.enabled && this.node && this.node.interpolate && this.driver.interpolateView) {
  48. this.setInterpolatedTransforms(timepassed);
  49. // this.restoreTransforms();
  50. }
  51. this.lastTime = now;
  52. },
  53. tock: function (t, dt) {
  54. if (this.node) {
  55. if (this.enabled && this.node.interpolate && this.driver.interpolateView) {
  56. this.restoreTransforms();
  57. }
  58. }
  59. },
  60. vecCmpThreeJS: function (a, b, delta) {
  61. let distance = a.distanceTo(b);
  62. //let distance = goog.vec.Vec3.distance(a,b);
  63. if (distance > delta) {
  64. return false;
  65. }
  66. return true;
  67. },
  68. vecCmp: function (a, b, delta) {
  69. // let distance = a.distanceTo(b);
  70. let distance = goog.vec.Vec3.distance(a, b);
  71. if (distance > delta) {
  72. return false;
  73. }
  74. return true;
  75. },
  76. restoreTransforms: function () {
  77. this.restorePositionTransforms();
  78. this.restoreRotationTransforms();
  79. this.restoreScaleTransforms();
  80. },
  81. restorePositionTransforms: function () {
  82. var now = this.node.interpolate.position.selfTick;
  83. if (now && this.node.needPositionRestore) {
  84. let pos = goog.vec.Vec3.clone(now);
  85. this.el.object3D.position.set(pos[0], pos[1], pos[2]);
  86. this.node.needPositionRestore = false;
  87. }
  88. },
  89. restoreRotationTransforms: function () {
  90. var now = this.node.interpolate.rotation.selfTick;
  91. if (now && this.node.needRotationRestore) {
  92. let r = new THREE.Euler();
  93. let rot = r.copy(now);
  94. this.el.object3D.rotation.set(rot.x, rot.y, rot.z)
  95. this.node.needRotationRestore = false;
  96. }
  97. },
  98. restoreScaleTransforms: function () {
  99. var now = this.node.interpolate.scale.selfTick;
  100. if (now && this.node.needScaleRestore) {
  101. let pos = goog.vec.Vec3.clone(now);
  102. this.el.object3D.scale.set(pos[0], pos[1], pos[2]);
  103. this.node.needScaleRestore = false;
  104. }
  105. },
  106. setInterpolatedTransforms: function (deltaTime) {
  107. var step = (this.node.tickTime) / (this.node.realTickDif);
  108. step = Math.min(step, 1);
  109. //step = Math.min(1, .2 * (deltaTime / 16.6)) //Math.min(step, 1);
  110. deltaTime = Math.min(deltaTime, this.node.realTickDif)
  111. this.node.tickTime += deltaTime || 0;
  112. this.interpolatePosition(step);
  113. this.interpolateRotation(step);
  114. this.interpolateScale(step);
  115. // if (this.node.tickTime == 0){
  116. // this.restoreTransforms();
  117. // }
  118. },
  119. radians: function (degrees) {
  120. // return degrees * Math.PI / 180.0;
  121. return THREE.Math.degToRad(degrees)
  122. },
  123. interpolateRotation: function (step) {
  124. let last = this.node.interpolate.rotation.lastTick;
  125. let now = this.node.interpolate.rotation.selfTick;
  126. if (last && now) {
  127. let comp = this.vecCmpThreeJS(last.toVector3(), now.toVector3(), this.deltaRot);
  128. if (!comp) {
  129. // console.log('Last:', last, ' Now: ', now);
  130. let lastV = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  131. (last.x),
  132. (last.y),
  133. (last.z), last.order//'YXZ'
  134. ));
  135. let nowV = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  136. (now.x),
  137. (now.y),
  138. (now.z), last.order//'YXZ'
  139. ));
  140. let q = new THREE.Quaternion();
  141. let e = new THREE.Euler();
  142. THREE.Quaternion.slerp(lastV, nowV, q, step || 0);
  143. let interp = e.setFromQuaternion(q, last.order); //'YXZ');
  144. //this.el.object3D.rotation.set(interp.x, interp.y, interp.z);
  145. this.setRotation(interp);
  146. this.node.needRotationRestore = true;
  147. }
  148. }
  149. },
  150. interpolatePosition: function (step) {
  151. var last = this.node.interpolate.position.lastTick; //this.node.lastTickTransform;
  152. var now = this.node.interpolate.position.selfTick; //Transform;
  153. if (last && now) {
  154. let comp = this.vecCmp(last, now, this.deltaPos);
  155. if (!comp) {
  156. var interp = goog.vec.Vec3.lerp(
  157. last, now,
  158. step || 0,
  159. goog.vec.Vec3.create()
  160. );
  161. this.setPosition(interp);
  162. this.node.needPositionRestore = true;
  163. }
  164. }
  165. },
  166. interpolateScale: function (step) {
  167. var last = this.node.interpolate.scale.lastTick; //this.node.lastTickTransform;
  168. var now = this.node.interpolate.scale.selfTick; //Transform;
  169. if (last && now) {
  170. let comp = this.vecCmp(last, now, this.deltaScale);
  171. if (!comp) {
  172. var interp = goog.vec.Vec3.lerp(
  173. last, now,
  174. step || 0,
  175. goog.vec.Vec3.create()
  176. );
  177. this.setScale(interp);
  178. this.node.needScaleRestore = true;
  179. }
  180. }
  181. },
  182. setPosition: function (interp) {
  183. let vec = goog.vec.Vec3.clone(interp);
  184. this.el.object3D.position.set(vec[0], vec[1], vec[2]);
  185. },
  186. setRotation: function (interp) {
  187. let vec = (new THREE.Euler()).copy(interp);
  188. this.el.object3D.rotation.set(vec.x, vec.y, vec.z);
  189. },
  190. setScale: function (interp) {
  191. let vec = goog.vec.Vec3.clone(interp);
  192. this.el.object3D.scale.set(vec[0], vec[1], vec[2]);
  193. },
  194. pause: function () { },
  195. play: function () { }
  196. });