123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- if (typeof AFRAME === 'undefined') {
- throw new Error('Component attempted to register before AFRAME was available.');
- }
- class Interpolator {
- constructor(comp) {
- this.component = comp;
- this.time = this.getMillis();
- }
- active() {
- return this.previous && this.next && (this.getTime() < 1);
- }
- getMillis() {
- return new Date().getTime();
- }
- getTime() {
- return (this.getMillis() - this.time) / this.component.timestep;
- }
- vecCmp(a, b, delta) {
- let distance = a.distanceTo(b);
- if (distance > delta) {
- return false;
- }
- return true;
- }
- }
- class RotationInterpolator extends Interpolator {
- constructor(comp) {
- super(comp);
- this.lastRotation = this.component.el.getAttribute('rotation');
- this.previous = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
- this.radians(this.lastRotation.x),
- this.radians(this.lastRotation.y),
- this.radians(this.lastRotation.z), 'YXZ'
- ));
- this.next = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
- this.radians(this.lastRotation.x),
- this.radians(this.lastRotation.y),
- this.radians(this.lastRotation.z), 'YXZ'
- ));
- }
- radians(degrees) {
-
- return THREE.Math.degToRad(degrees)
- }
- makeInterpolation() {
- let q = new THREE.Quaternion();
- let e = new THREE.Euler();
- THREE.Quaternion.slerp(this.previous, this.next, q, this.getTime());
- return e.setFromQuaternion(q);
- }
- testForLerp() {
- if (this.component.deltaRot == 0) {
- return true
- }
- let prev = (new THREE.Euler()).setFromQuaternion(this.previous).toVector3();
- let next = (new THREE.Euler()).setFromQuaternion(this.next).toVector3();
- if (prev && next && this.vecCmp(prev, next, this.component.deltaRot)) {
- return true
- }
- return false
- }
- inTick(currentRotation) {
- if (this.getTime() < 0.5) {
-
- return;
- }
- if (!this.previous) {
-
-
- this.previous = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
- this.radians(this.lastRotation.x),
- this.radians(this.lastRotation.y),
- this.radians(this.lastRotation.z), 'YXZ'
- ));
- this.next = (new THREE.Quaternion()).setFromEuler(new THREE.Euler(
- this.radians(currentRotation.x),
- this.radians(currentRotation.y),
- this.radians(currentRotation.z), 'YXZ'
- ));
- }
- this.time = this.getMillis();
- this.previous.copy(this.next);
- this.next.setFromEuler(new THREE.Euler(
- this.radians(currentRotation.x),
- this.radians(currentRotation.y),
- this.radians(currentRotation.z), 'YXZ'
- ));
- this.lastRotation = currentRotation;
- }
- }
- class PositionInterpolator extends Interpolator {
- constructor(comp) {
- super(comp);
-
- this.lastPosition = new THREE.Vector3().copy(this.component.el.getAttribute('position'));
-
- this.previous = (new THREE.Vector3()).fromArray(Object.values(this.lastPosition));
- this.next = (new THREE.Vector3()).fromArray(Object.values(this.lastPosition));
- }
- testForLerp() {
- if (this.component.deltaPos == 0) {
- return true
- }
- if (this.previous && this.next && this.vecCmp(this.previous, this.next, this.component.deltaPos)) {
- return true
- }
- return false
- }
- makeInterpolation() {
- return this.previous.lerp(this.next, this.getTime());
- }
- inTick(currentPosition) {
-
- if (this.getTime()< 0.5) {
-
- return;
- }
- if (!this.previous) {
- this.previous = (new THREE.Vector3()).fromArray(Object.values(this.lastPosition));
- this.next = (new THREE.Vector3()).fromArray(Object.values(currentPosition));
- }
-
- this.previous.copy(this.next);
- this.next.copy(currentPosition);
-
- this.lastPosition.copy(currentPosition);
- this.time = this.getMillis();
- }
- }
- AFRAME.registerComponent('interpolation', {
- schema: {
- duration: { default: 50 },
- enabled: { default: true },
- deltaPos: { default: 0 },
- deltaRot: { default: 0 }
- },
-
- init: function () {
-
-
-
-
-
-
-
-
- },
-
-
-
- update: function (oldData) {
- if (!this.interpolation) {
- this.timestep = parseInt(this.data.duration, 10);
- this.deltaPos = parseFloat(this.data.deltaPos);
- this.deltaRot = THREE.Math.degToRad(parseFloat(this.data.deltaRot));
- this.enabled = JSON.parse(this.data.enabled);
- if (this.enabled) {
- this.posInterpolator = new PositionInterpolator(this);
- this.rotInterpolator = new RotationInterpolator(this);
- }
- }
-
-
-
-
-
- },
-
- remove: function () { },
-
- tick: function () {
-
-
-
-
-
-
- if (this.enabled) {
- var el = this.el;
- var rotationTmp = this.rotationTmp = this.rotationTmp || {x: 0, y: 0, z: 0};
- var rotation = el.getAttribute('rotation');
- rotationTmp.x = rotation.x + 0.1;
- rotationTmp.y = rotation.y + 0.1;
- rotationTmp.z = rotation.z + 0.1;
- el.setAttribute('rotation', rotationTmp);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
-
-
-
-
-
-
- },
-
- pause: function () { },
-
- play: function () { },
- });
|