-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaframe-trail-component.js
170 lines (101 loc) · 3.86 KB
/
aframe-trail-component.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
AFRAME.registerSystem('trail', {
schema: {}, // System schema. Parses into `this.data`.
init: function () {
// Called on scene initialization.
},
trails: {
haveTrails: [],
},
createTrail: function createTrail( object, length, width, resolution, color, offset ) {
// resolution must be less than the length
if ( resolution > length ) {
resolution = length;
}
if(!object.userData.trails) object.userData.trails = [];
const trail = {
length: Math.round( length ),
width: width,
resolution: Math.round( resolution ),
trailHistory: [],
trailVertexPositions: [],
worldDirection: new THREE.Vector3(),
}
object.userData.trails.push(trail);
// trail geo
var geometry = new THREE.PlaneGeometry( 1, length, 1, resolution );
var material = new THREE.MeshBasicMaterial( { color: color, side: THREE.DoubleSide, wireframe: false, transparent: true, opacity: 0.2 } ); // opacity: 0.2
trail.mesh = new THREE.Mesh( geometry, material );
trail.mesh.position.add(offset);
this.el.sceneEl.object3D.add( trail.mesh );
this.trails.haveTrails.push( object );
// setting frustumCulled to false is important because we move the vertices outside the frustum, not the geometry itself
trail.mesh.frustumCulled = false;
// create history and store vertices
trail.trailHistory = [];
trail.trailVertexPositions = geometry.getAttribute("position");
},
updateTrailHistory: function updateTrailHistory( object ) {
object.userData.trails.forEach(trail=>{
object.getWorldDirection( trail.worldDirection );
trail.trailHistory.push( [ object.position.x, object.position.y, object.position.z, trail.worldDirection.x, trail.worldDirection.z ] );
if ( trail.trailHistory.length > trail.length ) {
trail.trailHistory.shift();
}
});
},
updateTrails: function updateTrails() {
for ( let i = 0; i < this.trails.haveTrails.length; i++ ) {
const object = this.trails.haveTrails[i];
this.updateTrailHistory( object );
object.userData.trails.forEach(trail=>{
for ( var j = 0; j < trail.resolution + 1; j++ ) {
var index = Math.round( trail.trailHistory.length / trail.resolution * j );
if ( index === trail.trailHistory.length ) {
index = trail.trailHistory.length - 1;
}
var pos = trail.trailHistory[index];
// custom the shape changing this width parameter
var width = THREE.MathUtils.mapLinear( j, 0, trail.resolution + 1, 0, 1 ) * trail.width / 2;
if ( typeof pos != "undefined" ) {
// update vertices using a "2D cross product"
// one side of the trail, left or right
const leftIndex = j * 2 * 3;
trail.trailVertexPositions.array[leftIndex] = pos[0] - pos[4] * width;
trail.trailVertexPositions.array[leftIndex + 1] = pos[1];
trail.trailVertexPositions.array[leftIndex + 2] = pos[2] + pos[3] * width;
// the other side of the trail
const rightIndex = (j * 2 + 1) * 3;
trail.trailVertexPositions.array[rightIndex] = pos[0] + pos[4] * width;
trail.trailVertexPositions.array[rightIndex + 1] = pos[1];
trail.trailVertexPositions.array[rightIndex + 2] = pos[2] - pos[3] * width;
}
}
trail.trailVertexPositions.needsUpdate = true;
});
}
},
resetTrail: function resetTrail( object ) {
object.userData.trails.forEach(trail=>{
trail.trailHistory = [];
});
},
tick: function(t,dt){
this.updateTrails();
}
});
AFRAME.registerComponent('trail', {
schema: {
length: {default: 80},
width: {default: 0.8},
resolution: {default: 18},//must be less than length
color: {default: 'white'},
offset: {type: 'vec3'}
},
multiple: true,
init: function () {
this.system.createTrail(this.el.object3D,this.data.length,this.data.width,this.data.resolution,this.data.color,this.data.offset);
},
reset: function(){
this.system.reset(this.el.object3D);
}
});