123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- /*
- The MIT License (MIT)
- Copyright (c) 2017 Nikolai Suslov
- Updated for using in LiveCoding.space and A-Frame 0.6.x
- Interpolate component for A-Frame VR. https://github.com/scenevr/aframe-interpolate-component.git
- The MIT License (MIT)
- Copyright (c) 2015 Kevin Ngo
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- */
- /* globals AFRAME, performance, THREE */
- 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 degrees * Math.PI / 180.0;
- 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) {
- // fixme - ignore multiple calls
- return;
- }
- if (!this.previous) {
- // this.previous = new THREE.Quaternion();
- // this.next = new THREE.Quaternion();
- 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.object3D.position);
- this.lastPosition = new THREE.Vector3().copy(this.component.el.getAttribute('position'));
- //this.lastPosition = 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) {
- //console.log(this.getTime());
- if (this.getTime()< 0.5) {
- // fixme - ignore multiple calls
- 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 = currentPosition;
- this.lastPosition.copy(currentPosition);
- this.time = this.getMillis();
- }
- }
- /**
- * Interpolate component for A-Frame.
- */
- AFRAME.registerComponent('interpolation', {
- schema: {
- duration: { default: 50 },
- enabled: { default: true },
- deltaPos: { default: 0 },
- deltaRot: { default: 0 }
- },
- /**
- * Called once when component is attached. Generally for initial setup.
- */
- init: function () {
- // Set up the tick throttling.
- //this.tick = AFRAME.utils.throttleTick(this.throttledTick, 0, this);
- //var el = this.el;
- //this.lastPosition = el.getAttribute('position');
- //this.timestep = 50;
- //this.time = this.getMillis();
- // this.previous = (new THREE.Vector3()).fromArray(Object.values(this.lastPosition));
- // this.next = (new THREE.Vector3()).fromArray(Object.values(this.lastPosition));
- },
- // throttledTick: function (time, deltaTime) {
- // },
- /**
- * Called when component is attached and when component data changes.
- * Generally modifies the entity based on the data.
- */
- 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);
- }
- }
- // if (!this.interpolation) {
- // var timestep = parseInt(this.data.duration, 10);
- // this.positionInterpolator = new PositionInterpolator(timestep, this);
- // this.rotationInterpolator = new RotationInterpolator(timestep, this);
- // }
- },
- /**
- * Called when a component is removed (e.g., via removeAttribute).
- * Generally undoes all modifications to the entity.
- */
- remove: function () { },
- /**
- * Called on each scene tick.
- */
- tick: function () {
-
- //let currentPosition = this.el.getAttribute('position');
- //let currentPosition = new THREE.Vector3().copy(this.el.getAttribute('position'));
- //let currentPosition = new THREE.Vector3().copy(this.el.object3D.position);
- //console.log(dt);
- //let currentRotation = this.el.getAttribute('rotation');
- 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);
- // if (this.posInterpolator.lastPosition.equals(currentPosition) === false) {
- // //if (this.posInterpolator.lastPosition != currentPosition) {
- // this.posInterpolator.inTick(currentPosition)
- // }
- // if (this.posInterpolator.active() && this.posInterpolator.testForLerp()) {
- // let newPos = this.posInterpolator.makeInterpolation();
- // this.el.setAttribute('position',this.posInterpolator.makeInterpolation())
-
- // //this.el.object3D.position.copy(this.posInterpolator.makeInterpolation());
- // }
- // if (this.rotInterpolator.lastRotation != currentRotation) {
- // this.rotInterpolator.inTick(currentRotation)
- // }
- // if (this.rotInterpolator.active() && this.rotInterpolator.testForLerp()) {
- // this.el.object3D.rotation.copy(this.rotInterpolator.makeInterpolation());
- // }
- }
- // if (this.positionInterpolator && this.positionInterpolator.active()) {
- // this.el.object3D.position.copy(this.positionInterpolator.get());
- // }
- // if (this.rotationInterpolator && this.rotationInterpolator.active()) {
- // this.el.object3D.rotation.copy(this.rotationInterpolator.get());
- // }
- },
- /**
- * Called when entity pauses.
- * Use to stop or remove any dynamic or background behavior such as events.
- */
- pause: function () { },
- /**
- * Called when entity resumes.
- * Use to continue or add any dynamic or background behavior such as events.
- */
- play: function () { },
- });
|