aframe-interpolation.js 6.6 KB

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