aframe-interpolation 2.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. The MIT License (MIT) Copyright (c) 2015 Kevin Ngo
  5. https://github.com/scenevr/aframe-interpolate-component.git
  6. */
  7. // Interpolate component for A-Frame VR. For using in LiveCoding.space and A-Frame 0.8.x
  8. /* globals AFRAME, performance, THREE */
  9. if (typeof AFRAME === 'undefined') {
  10. throw new Error('Component attempted to register before AFRAME was available.');
  11. }
  12. class Interpolator {
  13. constructor(comp) {
  14. this.component = comp;
  15. this.time = this.getMillis();
  16. }
  17. active() {
  18. return this.previous && this.next && (this.getTime() < 1);
  19. }
  20. getMillis() {
  21. return new Date().getTime();
  22. }
  23. getTime() {
  24. return (this.getMillis() - this.time) / this.component.timestep;
  25. }
  26. vecCmp(a, b, delta) {
  27. let distance = a.distanceTo(b);
  28. if (distance > delta) {
  29. return false;
  30. }
  31. return true;
  32. }
  33. }
  34. class RotationInterpolator extends Interpolator {
  35. constructor(comp) {
  36. super(comp);
  37. this.lastRotation = this.component.el.getAttribute('rotation');
  38. this.previous = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  39. this.radians(this.lastRotation.x),
  40. this.radians(this.lastRotation.y),
  41. this.radians(this.lastRotation.z), 'YXZ'
  42. ));
  43. this.next = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  44. this.radians(this.lastRotation.x),
  45. this.radians(this.lastRotation.y),
  46. this.radians(this.lastRotation.z), 'YXZ'
  47. ));
  48. }
  49. radians(degrees) {
  50. // return degrees * Math.PI / 180.0;
  51. return THREE.Math.degToRad(degrees)
  52. }
  53. makeInterpolation() {
  54. let q = new THREE.Quaternion();
  55. let e = new THREE.Euler();
  56. THREE.Quaternion.slerp(this.previous, this.next, q, this.getTime());
  57. return e.setFromQuaternion(q);
  58. }
  59. testForLerp() {
  60. if (this.component.deltaRot == 0) {
  61. return true
  62. }
  63. let prev = (new THREE.Euler()).setFromQuaternion(this.previous).toVector3();
  64. let next = (new THREE.Euler()).setFromQuaternion(this.next).toVector3();
  65. if (prev && next && this.vecCmp(prev, next, this.component.deltaRot)) {
  66. return true
  67. }
  68. return false
  69. }
  70. inTick(currentRotation) {
  71. if (this.getTime() < 0.5) {
  72. // fixme - ignore multiple calls
  73. return;
  74. }
  75. if (!this.previous) {
  76. // this.previous = new THREE.Quaternion();
  77. // this.next = new THREE.Quaternion();
  78. this.previous = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  79. this.radians(this.lastRotation.x),
  80. this.radians(this.lastRotation.y),
  81. this.radians(this.lastRotation.z), 'YXZ'
  82. ));
  83. this.next = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  84. this.radians(currentRotation.x),
  85. this.radians(currentRotation.y),
  86. this.radians(currentRotation.z), 'YXZ'
  87. ));
  88. }
  89. this.time = this.getMillis();
  90. this.previous.copy(this.next);
  91. this.next.setFromEuler(new THREE.Euler(
  92. this.radians(currentRotation.x),
  93. this.radians(currentRotation.y),
  94. this.radians(currentRotation.z), 'YXZ'
  95. ));
  96. this.lastRotation = currentRotation;
  97. }
  98. }
  99. class PositionInterpolator extends Interpolator {
  100. constructor(comp) {
  101. super(comp);
  102. //this.lastPosition = new THREE.Vector3().copy(this.component.el.object3D.position);
  103. this.lastPosition = new THREE.Vector3().copy(this.component.el.getAttribute('position'));
  104. //this.lastPosition = this.component.el.getAttribute('position');
  105. this.previous = (new THREE.Vector3()).fromArray(Object.values(this.lastPosition));
  106. this.next = (new THREE.Vector3()).fromArray(Object.values(this.lastPosition));
  107. }
  108. testForLerp() {
  109. if (this.component.deltaPos == 0) {
  110. return true
  111. }
  112. if (this.previous && this.next && this.vecCmp(this.previous, this.next, this.component.deltaPos)) {
  113. return true
  114. }
  115. return false
  116. }
  117. makeInterpolation() {
  118. return this.previous.lerp(this.next, this.getTime());
  119. }
  120. inTick(currentPosition) {
  121. //console.log(this.getTime());
  122. if (this.getTime()< 0.5) {
  123. // fixme - ignore multiple calls
  124. return;
  125. }
  126. if (!this.previous) {
  127. this.previous = (new THREE.Vector3()).fromArray(Object.values(this.lastPosition));
  128. this.next = (new THREE.Vector3()).fromArray(Object.values(currentPosition));
  129. }
  130. this.previous.copy(this.next);
  131. this.next.copy(currentPosition);
  132. //this.lastPosition = currentPosition;
  133. this.lastPosition.copy(currentPosition);
  134. this.time = this.getMillis();
  135. }
  136. }
  137. /**
  138. * Interpolate component for A-Frame.
  139. */
  140. AFRAME.registerComponent('interpolation', {
  141. schema: {
  142. duration: { default: 50 },
  143. enabled: { default: true },
  144. deltaPos: { default: 0 },
  145. deltaRot: { default: 0 }
  146. },
  147. /**
  148. * Called once when component is attached. Generally for initial setup.
  149. */
  150. init: function () {
  151. // Set up the tick throttling.
  152. //this.tick = AFRAME.utils.throttleTick(this.throttledTick, 0, this);
  153. //var el = this.el;
  154. //this.lastPosition = el.getAttribute('position');
  155. //this.timestep = 50;
  156. //this.time = this.getMillis();
  157. // this.previous = (new THREE.Vector3()).fromArray(Object.values(this.lastPosition));
  158. // this.next = (new THREE.Vector3()).fromArray(Object.values(this.lastPosition));
  159. },
  160. // throttledTick: function (time, deltaTime) {
  161. // },
  162. /**
  163. * Called when component is attached and when component data changes.
  164. * Generally modifies the entity based on the data.
  165. */
  166. update: function (oldData) {
  167. if (!this.interpolation) {
  168. this.timestep = parseInt(this.data.duration, 10);
  169. this.deltaPos = parseFloat(this.data.deltaPos);
  170. this.deltaRot = THREE.Math.degToRad(parseFloat(this.data.deltaRot));
  171. this.enabled = JSON.parse(this.data.enabled);
  172. if (this.enabled) {
  173. this.posInterpolator = new PositionInterpolator(this);
  174. this.rotInterpolator = new RotationInterpolator(this);
  175. }
  176. }
  177. // if (!this.interpolation) {
  178. // var timestep = parseInt(this.data.duration, 10);
  179. // this.positionInterpolator = new PositionInterpolator(timestep, this);
  180. // this.rotationInterpolator = new RotationInterpolator(timestep, this);
  181. // }
  182. },
  183. /**
  184. * Called when a component is removed (e.g., via removeAttribute).
  185. * Generally undoes all modifications to the entity.
  186. */
  187. remove: function () { },
  188. /**
  189. * Called on each scene tick.
  190. */
  191. tick: function () {
  192. //let currentPosition = this.el.getAttribute('position');
  193. //let currentPosition = new THREE.Vector3().copy(this.el.getAttribute('position'));
  194. //let currentPosition = new THREE.Vector3().copy(this.el.object3D.position);
  195. //console.log(dt);
  196. //let currentRotation = this.el.getAttribute('rotation');
  197. if (this.enabled) {
  198. var el = this.el;
  199. var rotationTmp = this.rotationTmp = this.rotationTmp || {x: 0, y: 0, z: 0};
  200. var rotation = el.getAttribute('rotation');
  201. rotationTmp.x = rotation.x + 0.1;
  202. rotationTmp.y = rotation.y + 0.1;
  203. rotationTmp.z = rotation.z + 0.1;
  204. el.setAttribute('rotation', rotationTmp);
  205. // if (this.posInterpolator.lastPosition.equals(currentPosition) === false) {
  206. // //if (this.posInterpolator.lastPosition != currentPosition) {
  207. // this.posInterpolator.inTick(currentPosition)
  208. // }
  209. // if (this.posInterpolator.active() && this.posInterpolator.testForLerp()) {
  210. // let newPos = this.posInterpolator.makeInterpolation();
  211. // this.el.setAttribute('position',this.posInterpolator.makeInterpolation())
  212. // //this.el.object3D.position.copy(this.posInterpolator.makeInterpolation());
  213. // }
  214. // if (this.rotInterpolator.lastRotation != currentRotation) {
  215. // this.rotInterpolator.inTick(currentRotation)
  216. // }
  217. // if (this.rotInterpolator.active() && this.rotInterpolator.testForLerp()) {
  218. // this.el.object3D.rotation.copy(this.rotInterpolator.makeInterpolation());
  219. // }
  220. }
  221. // if (this.positionInterpolator && this.positionInterpolator.active()) {
  222. // this.el.object3D.position.copy(this.positionInterpolator.get());
  223. // }
  224. // if (this.rotationInterpolator && this.rotationInterpolator.active()) {
  225. // this.el.object3D.rotation.copy(this.rotationInterpolator.get());
  226. // }
  227. },
  228. /**
  229. * Called when entity pauses.
  230. * Use to stop or remove any dynamic or background behavior such as events.
  231. */
  232. pause: function () { },
  233. /**
  234. * Called when entity resumes.
  235. * Use to continue or add any dynamic or background behavior such as events.
  236. */
  237. play: function () { },
  238. });