aframe-interpolation.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Interpolate component for A-Frame VR. https://github.com/scenevr/aframe-interpolate-component.git
  2. The MIT License (MIT)
  3. Copyright (c) 2015 Kevin Ngo
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. /* globals AFRAME, performance, THREE */
  21. if (typeof AFRAME === 'undefined') {
  22. throw new Error('Component attempted to register before AFRAME was available.');
  23. }
  24. function getMillis () {
  25. return new Date().getTime();
  26. }
  27. function PositionInterpolator (timestep, entity) {
  28. var time = getMillis();
  29. var previous;
  30. var next;
  31. entity.el.addEventListener('componentchanged', function (event) {
  32. if (getTime() < 0.5) {
  33. // fixme - ignore multiple calls
  34. return;
  35. }
  36. if (event.detail.name === 'position') {
  37. if (!previous) {
  38. previous = new THREE.Vector3();
  39. next = new THREE.Vector3();
  40. }
  41. time = getMillis();
  42. previous.copy(next);
  43. next.copy(event.detail.newData);
  44. }
  45. });
  46. function getTime () {
  47. return (getMillis() - time) / timestep;
  48. }
  49. this.active = function () {
  50. return previous && next && (getTime() < 1.0);
  51. };
  52. var v = new THREE.Vector3();
  53. this.get = function () {
  54. return v.lerpVectors(previous, next, getTime());
  55. };
  56. }
  57. function radians(degrees) {
  58. return degrees * Math.PI / 180.0;
  59. }
  60. function RotationInterpolator (timestep, entity) {
  61. var time = getMillis();
  62. var previous;
  63. var next;
  64. entity.el.addEventListener('componentchanged', function (event) {
  65. if (getTime() < 0.5) {
  66. // fixme - ignore multiple calls
  67. return;
  68. }
  69. if (event.detail.name === 'rotation') {
  70. if (!previous) {
  71. previous = new THREE.Quaternion();
  72. next = new THREE.Quaternion();
  73. }
  74. time = getMillis();
  75. previous.copy(next);
  76. next.setFromEuler(new THREE.Euler(
  77. radians(event.detail.newData.x),
  78. radians(event.detail.newData.y),
  79. radians(event.detail.newData.z)
  80. ));
  81. }
  82. });
  83. function getTime () {
  84. return (getMillis() - time) / timestep;
  85. }
  86. this.active = function () {
  87. return previous && next && (getTime() < 1.0);
  88. };
  89. var e = new THREE.Euler();
  90. var q = new THREE.Quaternion();
  91. this.get = function () {
  92. THREE.Quaternion.slerp(previous, next, q, getTime());
  93. return e.setFromQuaternion(q);
  94. };
  95. }
  96. /**
  97. * Interpolate component for A-Frame.
  98. */
  99. AFRAME.registerComponent('interpolation', {
  100. schema: {
  101. duration: { default: 200 }
  102. },
  103. /**
  104. * Called once when component is attached. Generally for initial setup.
  105. */
  106. init: function () {
  107. },
  108. /**
  109. * Called when component is attached and when component data changes.
  110. * Generally modifies the entity based on the data.
  111. */
  112. update: function (oldData) {
  113. if (!this.interpolation) {
  114. var timestep = parseInt(this.data.duration, 10);
  115. this.positionInterpolator = new PositionInterpolator(timestep, this);
  116. this.rotationInterpolator = new RotationInterpolator(timestep, this);
  117. }
  118. },
  119. /**
  120. * Called when a component is removed (e.g., via removeAttribute).
  121. * Generally undoes all modifications to the entity.
  122. */
  123. remove: function () { },
  124. /**
  125. * Called on each scene tick.
  126. */
  127. tick: function (t) {
  128. if (this.positionInterpolator && this.positionInterpolator.active()) {
  129. this.el.object3D.position.copy(this.positionInterpolator.get());
  130. }
  131. if (this.rotationInterpolator && this.rotationInterpolator.active()) {
  132. this.el.object3D.rotation.copy(this.rotationInterpolator.get());
  133. }
  134. },
  135. /**
  136. * Called when entity pauses.
  137. * Use to stop or remove any dynamic or background behavior such as events.
  138. */
  139. pause: function () { },
  140. /**
  141. * Called when entity resumes.
  142. * Use to continue or add any dynamic or background behavior such as events.
  143. */
  144. play: function () { },
  145. });