123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- /* 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.');
- }
- function getMillis () {
- return new Date().getTime();
- //return performance.now()
- }
- function PositionInterpolator (timestep, entity) {
- var time = getMillis();
- var previous;
- var next;
- var v = new THREE.Vector3();
- entity.el.addEventListener('componentchanged', function (event) {
- if (getTime() < 0.5 || getTime() == 0) {
- // fixme - ignore multiple calls
- return;
- }
- if (event.detail.name === 'position') {
- if (!previous) {
- previous = new THREE.Vector3();
- next = new THREE.Vector3();
- }
- time = getMillis();
- previous.copy(next);
- next.copy(event.detail.newData);
- }
- });
- function getTime () {
- return (getMillis() - time) / timestep;
- }
- this.active = function () {
- return previous && next && (getTime() < 1);
- };
- this.get = function () {
- //return v.lerpVectors(previous, next, getTime());
- return previous.lerp(next, getTime());
- };
- }
- function radians(degrees) {
- // return degrees * Math.PI / 180.0;
- return THREE.Math.degToRad(degrees)
- }
- function RotationInterpolator (timestep, entity) {
- var time = getMillis();
- var previous;
- var next;
- var e = new THREE.Euler();
- var q = new THREE.Quaternion();
- entity.el.addEventListener('componentchanged', function (event) {
- if (getTime() < 0.5 || getTime() == 0) {
- // fixme - ignore multiple calls
- return;
- }
- if (event.detail.name === 'rotation') {
- if (!previous) {
- previous = new THREE.Quaternion();
- next = new THREE.Quaternion();
- }
- time = getMillis();
- previous.copy(next);
- next.setFromEuler(new THREE.Euler(
- radians(event.detail.newData.x),
- radians(event.detail.newData.y),
- radians(event.detail.newData.z),'YXZ'
- ));
- }
- });
- // var data = this.data;
- // var object3D = this.el.object3D;
- // object3D.rotation.set(degToRad(data.x), degToRad(data.y), degToRad(data.z));
- // object3D.rotation.order = 'YXZ';
- function getTime () {
- return (getMillis() - time) / timestep;
- }
- this.active = function () {
- return previous && next && (getTime() < 1);
- };
-
- this.get = function () {
- THREE.Quaternion.slerp(previous, next, q, getTime());
- return e.setFromQuaternion(q);
- };
- }
- /**
- * Interpolate component for A-Frame.
- */
- AFRAME.registerComponent('interpolation', {
- schema: {
- duration: { default: 50 }
- },
- /**
- * 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.lastRotation = el.getAttribute('rotation');
- this.getMillis = function() {
- return new Date().getTime();
- //return performance.now()
- }
- this.timestep = 50;
- this.time = this.getMillis();
- this.previous = new THREE.Vector3();
- this.next = new THREE.Vector3();
- this.active = function () {
- return this.previous && this.next && (this.getTime() < 1);
- };
-
- this.get = function () {
- return this.previous.lerp(this.next, this.getTime());
- };
- this.getTime = function () {
- return (this.getMillis() - this.time) / this.timestep;
- }
- this.matCmp = function (a,b,delta) {
- let distance = a.distanceTo(b);
- if (distance > delta) {
- return false;
- }
- // for(var i =0; i < 2; i++) {
- // if(Math.abs(a[i] - b[i]) > delta)
- // return false;
- // }
-
- return true;
- }
- this.testForLerp = function(){
- if(this.previous && this.next && !this.matCmp(this.previous,this.next,.0001) ) {
- return true
- }
- return false
- }
- },
- // 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);
- }
- // 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 (t, dt) {
- let currentPosition = this.el.getAttribute('position');
-
- if (this.lastPosition != currentPosition)
- {
- if (this.getTime() < 0.5) {
- // fixme - ignore multiple calls
- return;
- }
- if (!this.previous) {
- this.previous = new THREE.Vector3();
- this.next = new THREE.Vector3();
- }
- this.time = this.getMillis();
- this.previous.copy(this.next);
- this.next.copy(currentPosition);
- this.lastPosition = currentPosition;
- }
- if (this.active() && this.testForLerp())
- {
- this.el.object3D.position.copy(this.get());
- }
-
- // 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 () { },
- });
|