aframe-interpolation.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.Euler();
  56. let rot = r.copy(this.node.interpolate.rotation.selfTick);
  57. if (rot && this.node.needTransformRestore) {
  58. this.el.object3D.rotation.set(rot.x, rot.y, rot.z)
  59. this.node.needTransformRestore = false;
  60. }
  61. },
  62. setInterpolatedTransforms: function (deltaTime) {
  63. var step = (this.node.tickTime) / (this.driver.realTickDif);
  64. step = Math.min(step, 1);
  65. deltaTime = Math.min(deltaTime, this.driver.realTickDif)
  66. this.node.tickTime += deltaTime || 0;
  67. this.interpolatePosition(step);
  68. this.interpolateRotation(step);
  69. },
  70. radians: function (degrees) {
  71. // return degrees * Math.PI / 180.0;
  72. return THREE.Math.degToRad(degrees)
  73. },
  74. interpolateRotation: function (step) {
  75. let last = this.node.interpolate.rotation.lastTick;
  76. let now = this.node.interpolate.rotation.selfTick;
  77. if (last && now) {
  78. let comp = this.vecCmp(last.toVector3(), now.toVector3(), this.deltaRot);
  79. if (!comp) {
  80. // console.log('Last:', last, ' Now: ', now);
  81. let lastV = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  82. (last.x),
  83. (last.y),
  84. (last.z), 'YXZ'
  85. ));
  86. let nowV = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
  87. (now.x),
  88. (now.y),
  89. (now.z), 'YXZ'
  90. ));
  91. let q = new THREE.Quaternion();
  92. let e = new THREE.Euler();
  93. THREE.Quaternion.slerp(lastV, nowV, q, step || 0);
  94. let interp = e.setFromQuaternion(q, 'YXZ');
  95. this.el.object3D.rotation.set(interp.x, interp.y, interp.z);
  96. this.node.needTransformRestore = true;
  97. }
  98. }
  99. },
  100. interpolatePosition: function (step) {
  101. var last = this.node.interpolate.position.lastTick; //this.node.lastTickTransform;
  102. var now = this.node.interpolate.position.selfTick; //Transform;
  103. if (last && now) {
  104. let comp = this.vecCmp(last, now, this.deltaPos);
  105. if (!comp) {
  106. var lastV = (new THREE.Vector3()).copy(last);
  107. var nowV = (new THREE.Vector3()).copy(now);
  108. var interp = lastV.lerp(nowV, step || 0);
  109. //this.el.setAttribute('position',interp);
  110. this.el.object3D.position.set(interp.x, interp.y, interp.z);
  111. this.node.needTransformRestore = true;
  112. }
  113. }
  114. },
  115. pause: function () { },
  116. play: function () { }
  117. });