-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
153 lines (126 loc) · 3.53 KB
/
Copy pathindex.js
File metadata and controls
153 lines (126 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* 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();
}
function PositionInterpolator (timestep, entity) {
var time = getMillis();
var previous;
var next;
entity.el.addEventListener('componentchanged', function (event) {
if (getTime() < 0.5) {
// 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.0);
};
var v = new THREE.Vector3();
this.get = function () {
return v.lerpVectors(previous, next, getTime());
};
}
function radians(degrees) {
return degrees * Math.PI / 180.0;
}
function RotationInterpolator (timestep, entity) {
var time = getMillis();
var previous;
var next;
entity.el.addEventListener('componentchanged', function (event) {
if (getTime() < 0.5) {
// 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)
));
}
});
function getTime () {
return (getMillis() - time) / timestep;
}
this.active = function () {
return previous && next && (getTime() < 1.0);
};
var e = new THREE.Euler();
var q = new THREE.Quaternion();
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: 200 }
},
/**
* Called once when component is attached. Generally for initial setup.
*/
init: function () {
},
/**
* Called when component is attached and when component data changes.
* Generally modifies the entity based on the data.
*/
update: function (oldData) {
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) {
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 () { },
});