-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticles.js
127 lines (103 loc) · 3.57 KB
/
particles.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
function constrain(v, min, max){
if( v < min )
v = min;
else
if( v > max )
v = max;
return v;
}
function calculateParticles(splineGeo, particlesGeo, particleColors) {
var points = splineGeo.vertices;
var particleColor = splineGeo.colors[splineGeo.colors.length - 1].clone();
var particleCount = Math.floor(100000 / 8000 / points.length) + 1;
particleCount = 100;//constrain(particleCount,1,100);
var particleSize = splineGeo.size;
for( var s=0; s<particleCount; s++ ){
// var rIndex = Math.floor( Math.random() * points.length );
// var rIndex = Math.min(s,points.length-1);
var desiredIndex = s / particleCount * points.length;
var rIndex = constrain(Math.floor(desiredIndex),0,points.length-1);
var point = points[rIndex];
var particle = point.clone();
particle.moveIndex = rIndex;
particle.nextIndex = rIndex+1;
if(particle.nextIndex >= points.length )
particle.nextIndex = 0;
particle.lerpN = 0;
particle.path = points;
particlesGeo.vertices.push( particle );
particle.size = particleSize;
particleColors.push( particleColor );
}
}
function addParticles(splineOutline) {
var lineColors = [];
var particleColors = [];
var particlesGeo = new THREE.Geometry();
attributes = {
size: { type: 'f', value: [] },
customColor: { type: 'c', value: [] }
};
uniforms = {
amplitude: { type: "f", value: 10.0 },
color: { type: "c", value: new THREE.Color( 0xffffff ) },
flare: { type: "t", value: THREE.ImageUtils.loadTexture( "images/particleA.png" ) }
};
var particleShader = new THREE.ShaderMaterial( {
uniforms: uniforms,
attributes: attributes,
vertexShader: document.getElementById( 'vertexshader' ).textContent,
fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
blending: THREE.AdditiveBlending,
depthTest: true,
depthWrite: false,
transparent: true
//sizeAttenuation: true,
});
calculateParticles(splineOutline.geometry, particlesGeo, particleColors);
var particleMat = new THREE.ParticleBasicMaterial({
map: THREE.ImageUtils.loadTexture("images/map_mask.png"),
color: 0xffffff,
size: 60,
blending: THREE.NormalBlending,
transparent:true,
depthWrite: false,
vertexColors: true
// sizeAttenuation: true
});
particlesGeo.colors = particleColors;
var pSystem = new THREE.ParticleSystem( particlesGeo, particleShader);
pSystem.dynamic = true;
splineOutline.add( pSystem );
var vertices = pSystem.geometry.vertices;
var values_size = attributes.size.value;
var values_color = attributes.customColor.value;
for( var v = 0; v < vertices.length; v++ ) {
values_size[ v ] = pSystem.geometry.vertices[v].size;
values_color[ v ] = particleColors[v];
}
pSystem.update = function(){
// var time = Date.now()
for( var i in this.geometry.vertices ){
var particle = this.geometry.vertices[i];
var path = particle.path;
var moveLength = path.length;
particle.lerpN += 0.05;
if(particle.lerpN > 1){
particle.lerpN = 0;
particle.moveIndex = particle.nextIndex;
particle.nextIndex++;
if( particle.nextIndex >= path.length ){
particle.moveIndex = 0;
particle.nextIndex = 1;
}
}
var currentPoint = path[particle.moveIndex];
var nextPoint = path[particle.nextIndex];
particle.copy( currentPoint );
particle.lerp( nextPoint, particle.lerpN );
}
this.geometry.verticesNeedUpdate = true;
};
return splineOutline;
}