aframe-interpolation 2.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2017 Nikolai Suslov
  4. Updated for using in LiveCoding.space and A-Frame 0.6.x
  5. Interpolate component for A-Frame VR. https://github.com/scenevr/aframe-interpolate-component.git
  6. The MIT License (MIT)
  7. Copyright (c) 2015 Kevin Ngo
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in all
  15. copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. SOFTWARE.
  23. */
  24. /* globals AFRAME, performance, THREE */
  25. if (typeof AFRAME === 'undefined') {
  26. throw new Error('Component attempted to register before AFRAME was available.');
  27. }
  28. class Interpolator {
  29. constructor(comp) {
  30. this.component = comp;
  31. this.time = this.getMillis();
  32. }
  33. active() {
  34. return this.previous && this.next && (this.getTime() < 1);
  35. }
  36. getMillis() {
  37. return new Date().getTime();
  38. }
  39. getTime() {
  40. return (this.getMillis() - this.time) / this.component.timestep;
  41. }
  42. vecCmp(a, b, delta) {
  43. let distance = a.distanceTo(b);
  44. if (distance > delta) {
  45. return false;
  46. }
  47. return true;
  48. }
  49. }
  50. class RotationInterpolator extends Interpolator {
  51. constructor(comp) {
  52. super(comp);
  53. this.lastRotation = this.component.el.getAttribute('rotation');
  54. this.previous = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  55. this.radians(this.lastRotation.x),
  56. this.radians(this.lastRotation.y),
  57. this.radians(this.lastRotation.z), 'YXZ'
  58. ));
  59. this.next = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  60. this.radians(this.lastRotation.x),
  61. this.radians(this.lastRotation.y),
  62. this.radians(this.lastRotation.z), 'YXZ'
  63. ));
  64. }
  65. radians(degrees) {
  66. // return degrees * Math.PI / 180.0;
  67. return THREE.Math.degToRad(degrees)
  68. }
  69. makeInterpolation() {
  70. let q = new THREE.Quaternion();
  71. let e = new THREE.Euler();
  72. THREE.Quaternion.slerp(this.previous, this.next, q, this.getTime());
  73. return e.setFromQuaternion(q);
  74. }
  75. testForLerp() {
  76. if (this.component.deltaRot == 0) {
  77. return true
  78. }
  79. let prev = (new THREE.Euler()).setFromQuaternion(this.previous).toVector3();
  80. let next = (new THREE.Euler()).setFromQuaternion(this.next).toVector3();
  81. if (prev && next && this.vecCmp(prev, next, this.component.deltaRot)) {
  82. return true
  83. }
  84. return false
  85. }
  86. inTick(currentRotation) {
  87. if (this.getTime() < 0.5) {
  88. // fixme - ignore multiple calls
  89. return;
  90. }
  91. if (!this.previous) {
  92. // this.previous = new THREE.Quaternion();
  93. // this.next = new THREE.Quaternion();
  94. this.previous = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  95. this.radians(this.lastRotation.x),
  96. this.radians(this.lastRotation.y),
  97. this.radians(this.lastRotation.z), 'YXZ'
  98. ));
  99. this.next = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  100. this.radians(currentRotation.x),
  101. this.radians(currentRotation.y),
  102. this.radians(currentRotation.z), 'YXZ'
  103. ));
  104. }
  105. this.time = this.getMillis();
  106. this.previous.copy(this.next);
  107. this.next.setFromEuler(new THREE.Euler(
  108. this.radians(currentRotation.x),
  109. this.radians(currentRotation.y),
  110. this.radians(currentRotation.z), 'YXZ'
  111. ));
  112. this.lastRotation = currentRotation;
  113. }
  114. }
  115. class PositionInterpolator extends Interpolator {
  116. constructor(comp) {
  117. super(comp);
  118. //this.lastPosition = new THREE.Vector3().copy(this.component.el.object3D.position);
  119. this.lastPosition = new THREE.Vector3().copy(this.component.el.getAttribute('position'));
  120. //this.lastPosition = this.component.el.getAttribute('position');
  121. this.previous = (new THREE.Vector3()).fromArray(Object.values(this.lastPosition));
  122. this.next = (new THREE.Vector3()).fromArray(Object.values(this.lastPosition));
  123. }
  124. testForLerp() {
  125. if (this.component.deltaPos == 0) {
  126. return true
  127. }
  128. if (this.previous && this.next && this.vecCmp(this.previous, this.next, this.component.deltaPos)) {
  129. return true
  130. }
  131. return false
  132. }
  133. makeInterpolation() {
  134. return this.previous.lerp(this.next, this.getTime());
  135. }
  136. inTick(currentPosition) {
  137. //console.log(this.getTime());
  138. if (this.getTime()< 0.5) {
  139. // fixme - ignore multiple calls
  140. return;
  141. }
  142. if (!this.previous) {
  143. this.previous = (new THREE.Vector3()).fromArray(Object.values(this.lastPosition));
  144. this.next = (new THREE.Vector3()).fromArray(Object.values(currentPosition));
  145. }
  146. this.previous.copy(this.next);
  147. this.next.copy(currentPosition);
  148. //this.lastPosition = currentPosition;
  149. this.lastPosition.copy(currentPosition);
  150. this.time = this.getMillis();
  151. }
  152. }
  153. /**
  154. * Interpolate component for A-Frame.
  155. */
  156. AFRAME.registerComponent('interpolation', {
  157. schema: {
  158. duration: { default: 50 },
  159. enabled: { default: true },
  160. deltaPos: { default: 0 },
  161. deltaRot: { default: 0 }
  162. },
  163. /**
  164. * Called once when component is attached. Generally for initial setup.
  165. */
  166. init: function () {
  167. // Set up the tick throttling.
  168. //this.tick = AFRAME.utils.throttleTick(this.throttledTick, 0, this);
  169. //var el = this.el;
  170. //this.lastPosition = el.getAttribute('position');
  171. //this.timestep = 50;
  172. //this.time = this.getMillis();
  173. // this.previous = (new THREE.Vector3()).fromArray(Object.values(this.lastPosition));
  174. // this.next = (new THREE.Vector3()).fromArray(Object.values(this.lastPosition));
  175. },
  176. // throttledTick: function (time, deltaTime) {
  177. // },
  178. /**
  179. * Called when component is attached and when component data changes.
  180. * Generally modifies the entity based on the data.
  181. */
  182. update: function (oldData) {
  183. if (!this.interpolation) {
  184. this.timestep = parseInt(this.data.duration, 10);
  185. this.deltaPos = parseFloat(this.data.deltaPos);
  186. this.deltaRot = THREE.Math.degToRad(parseFloat(this.data.deltaRot));
  187. this.enabled = JSON.parse(this.data.enabled);
  188. if (this.enabled) {
  189. this.posInterpolator = new PositionInterpolator(this);
  190. this.rotInterpolator = new RotationInterpolator(this);
  191. }
  192. }
  193. // if (!this.interpolation) {
  194. // var timestep = parseInt(this.data.duration, 10);
  195. // this.positionInterpolator = new PositionInterpolator(timestep, this);
  196. // this.rotationInterpolator = new RotationInterpolator(timestep, this);
  197. // }
  198. },
  199. /**
  200. * Called when a component is removed (e.g., via removeAttribute).
  201. * Generally undoes all modifications to the entity.
  202. */
  203. remove: function () { },
  204. /**
  205. * Called on each scene tick.
  206. */
  207. tick: function () {
  208. //let currentPosition = this.el.getAttribute('position');
  209. //let currentPosition = new THREE.Vector3().copy(this.el.getAttribute('position'));
  210. //let currentPosition = new THREE.Vector3().copy(this.el.object3D.position);
  211. //console.log(dt);
  212. //let currentRotation = this.el.getAttribute('rotation');
  213. if (this.enabled) {
  214. var el = this.el;
  215. var rotationTmp = this.rotationTmp = this.rotationTmp || {x: 0, y: 0, z: 0};
  216. var rotation = el.getAttribute('rotation');
  217. rotationTmp.x = rotation.x + 0.1;
  218. rotationTmp.y = rotation.y + 0.1;
  219. rotationTmp.z = rotation.z + 0.1;
  220. el.setAttribute('rotation', rotationTmp);
  221. // if (this.posInterpolator.lastPosition.equals(currentPosition) === false) {
  222. // //if (this.posInterpolator.lastPosition != currentPosition) {
  223. // this.posInterpolator.inTick(currentPosition)
  224. // }
  225. // if (this.posInterpolator.active() && this.posInterpolator.testForLerp()) {
  226. // let newPos = this.posInterpolator.makeInterpolation();
  227. // this.el.setAttribute('position',this.posInterpolator.makeInterpolation())
  228. // //this.el.object3D.position.copy(this.posInterpolator.makeInterpolation());
  229. // }
  230. // if (this.rotInterpolator.lastRotation != currentRotation) {
  231. // this.rotInterpolator.inTick(currentRotation)
  232. // }
  233. // if (this.rotInterpolator.active() && this.rotInterpolator.testForLerp()) {
  234. // this.el.object3D.rotation.copy(this.rotInterpolator.makeInterpolation());
  235. // }
  236. }
  237. // if (this.positionInterpolator && this.positionInterpolator.active()) {
  238. // this.el.object3D.position.copy(this.positionInterpolator.get());
  239. // }
  240. // if (this.rotationInterpolator && this.rotationInterpolator.active()) {
  241. // this.el.object3D.rotation.copy(this.rotationInterpolator.get());
  242. // }
  243. },
  244. /**
  245. * Called when entity pauses.
  246. * Use to stop or remove any dynamic or background behavior such as events.
  247. */
  248. pause: function () { },
  249. /**
  250. * Called when entity resumes.
  251. * Use to continue or add any dynamic or background behavior such as events.
  252. */
  253. play: function () { },
  254. });