aframe-interpolation.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. deltaRot: { default: 0.1 }
  13. },
  14. init: function () {
  15. this.driver = vwf.views["vwf/view/aframeComponent"];
  16. },
  17. update: function (oldData) {
  18. if (!this.interpolation) {
  19. this.deltaPos = parseFloat(this.data.deltaPos);
  20. this.deltaRot = THREE.Math.degToRad(parseFloat(this.data.deltaRot));
  21. this.enabled = JSON.parse(this.data.enabled);
  22. if (this.enabled) {
  23. }
  24. }
  25. },
  26. /**
  27. * Called when a component is removed (e.g., via removeAttribute).
  28. * Generally undoes all modifications to the entity.
  29. */
  30. remove: function () { },
  31. /**
  32. * Called on each scene tick.
  33. */
  34. tick: function (t, dt) {
  35. if (!this.node) {
  36. let interNode = Object.entries(this.driver.state.nodes).find(el =>
  37. el[1].parentID == this.el.id && el[1].extendsID == "http://vwf.example.com/aframe/interpolation-component.vwf"
  38. );
  39. this.node = this.driver.nodes[interNode[0]];
  40. this.nodeState = interNode[1];
  41. }
  42. if (this.enabled && this.node && this.node.interpolate) {
  43. this.setInterpolatedTransforms(dt);
  44. this.restoreTransforms();
  45. }
  46. },
  47. vecCmp: function (a, b, delta) {
  48. let distance = a.distanceTo(b);
  49. if (distance > delta) {
  50. return false;
  51. }
  52. return true;
  53. },
  54. restoreTransforms: function () {
  55. let r = new THREE.Vector3();
  56. let rot = r.copy(this.node.interpolate.position.selfTick);
  57. if (rot && this.node.needTransformRestore) {
  58. this.el.object3D.position.set(rot.x, rot.y, rot.z)
  59. this.node.needTransformRestore = false;
  60. }
  61. // let r = new THREE.Euler();
  62. // let rot = r.copy(this.node.interpolate.rotation.selfTick);
  63. // if (rot && this.node.needTransformRestore) {
  64. // this.el.object3D.rotation.set(rot.x, rot.y, rot.z)
  65. // this.node.needTransformRestore = false;
  66. // }
  67. },
  68. setInterpolatedTransforms: function (deltaTime) {
  69. var step = (this.driver.tickTime) / (this.driver.realTickDif);
  70. step = Math.min(step, 1);
  71. deltaTime = Math.min(deltaTime, this.driver.realTickDif)
  72. this.driver.tickTime += deltaTime || 0;
  73. this.interpolatePosition(step);
  74. //this.interpolateRotation(step);
  75. },
  76. radians: function (degrees) {
  77. // return degrees * Math.PI / 180.0;
  78. return THREE.Math.degToRad(degrees)
  79. },
  80. interpolateRotation: function (step) {
  81. let last = this.node.interpolate.rotation.lastTick;
  82. let now = this.node.interpolate.rotation.selfTick;
  83. if (last && now) {
  84. let comp = this.vecCmp(last.toVector3(), now.toVector3(), this.deltaRot);
  85. if (!comp) {
  86. // console.log('Last:', last, ' Now: ', now);
  87. let lastV = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  88. (last.x),
  89. (last.y),
  90. (last.z), 'YXZ'
  91. ));
  92. let nowV = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  93. (now.x),
  94. (now.y),
  95. (now.z), 'YXZ'
  96. ));
  97. let q = new THREE.Quaternion();
  98. let e = new THREE.Euler();
  99. THREE.Quaternion.slerp(lastV, nowV, q, step || 0);
  100. let interp = e.setFromQuaternion(q, 'YXZ');
  101. this.el.object3D.rotation.set(interp.x, interp.y, interp.z);
  102. this.node.needTransformRestore = true;
  103. }
  104. }
  105. },
  106. interpolatePosition: function (step) {
  107. var last = this.node.interpolate.position.lastTick; //this.node.lastTickTransform;
  108. var now = this.node.interpolate.position.selfTick; //Transform;
  109. if (last && now) {
  110. let comp = this.vecCmp(last, now, this.deltaPos);
  111. if (!comp) {
  112. var lastV = (new THREE.Vector3()).copy(last);
  113. var nowV = (new THREE.Vector3()).copy(now);
  114. var interp = lastV.lerp(nowV, step || 0);
  115. //this.el.setAttribute('position',interp);
  116. this.el.object3D.position.set(interp.x, interp.y, interp.z);
  117. this.node.needTransformRestore = true;
  118. }
  119. }
  120. },
  121. pause: function () { },
  122. play: function () { }
  123. });