forked from jeromeetienne/threex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreex.sparks.js
More file actions
232 lines (181 loc) · 6.55 KB
/
Copy paththreex.sparks.js
File metadata and controls
232 lines (181 loc) · 6.55 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// This THREEx helper makes it even easier to use spark.js with three.js
// * FIXME This is currently only with WebGL
//
// # Code
//
var THREEx = THREEx || {};
THREEx.Sparks = function(opts)
{
opts = opts || {};
this._maxParticles = opts.maxParticles || console.assert(false);
this._texture = opts.texture || this._buildDefaultTexture();
var counter = opts.counter || console.assert(false);
// TODO replace this by objectpool.js
var vertexIndexPool = {
__pools: [],
// Get a new Vector
get: function() {
if( this.__pools.length > 0 ) return this.__pools.pop();
console.assert(false, "pool ran out!")
return null;
},
// Release a vector back into the pool
add: function(v){ this.__pools.push(v); }
};
var particles = new THREE.Geometry();
var vertices = particles.vertices;
for ( i = 0; i < this._maxParticles; i++ ) {
var position = new THREE.Vector3(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
vertices.push(new THREE.Vertex(position));
vertexIndexPool.add(i);
}
// to handle window resize
this._$onWindowResize = this._onWindowResize.bind(this);
window.addEventListener('resize', this._$onWindowResize, false);
var attributes = this._attributes = {
size : { type: 'f', value: [] },
aColor : { type: 'c', value: [] }
};
var uniforms = this._uniforms = {
texture : { type: "t", texture: this._texture },
color : { type: "c", value: new THREE.Color(0xffffff) },
sizeRatio : { type: "f", value: this._computeSizeRatio() }
};
// fill attributes array
var valuesSize = this._attributes.size.value;
var valuesColor = this._attributes.aColor.value;
for(var v = 0; v < particles.vertices.length; v++ ){
valuesSize[v] = 99;
valuesColor[v] = new THREE.Color( 0x000000 );
}
var material = new THREE.ShaderMaterial( {
uniforms : this._uniforms,
attributes : this._attributes,
vertexShader : THREEx.Sparks.vertexShaderText,
fragmentShader : THREEx.Sparks.fragmentShaderText,
blending : THREE.AdditiveBlending,
depthWrite : false,
//depthTest : false,
transparent : true
});
this._group = new THREE.ParticleSystem( particles, material );
this._group.dynamic = true;
this._group.sortParticles = true; // TODO is this needed ?
//// EMITTER STUFF
var setTargetParticle = function() {
var vertexIdx = vertexIndexPool.get();
var target = {
vertexIdx : vertexIdx,
size : function(value){
if( value !== undefined ) valuesSize[vertexIdx] = value;
return valuesSize[vertexIdx];
},
color : function(value){
if( value !== undefined ) valuesColor[vertexIdx] = value;
return valuesColor[vertexIdx];
}
};
return target;
};
var onParticleCreated = function(particle) {
var vertexIdx = particle.target.vertexIdx;
// copy particle position into three.js geometry
vertices[vertexIdx].position = particle.position;
};
var onParticleDead = function(particle) {
var vertexIdx = particle.target.vertexIdx;
// Hide the particle
valuesColor[vertexIdx].setHex( 0x000000 );
vertices[vertexIdx].position.set(Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
// Mark particle system as available by returning to pool
vertexIndexPool.add( vertexIdx );
};
var emitter = this._emitter = new SPARKS.Emitter(counter);
emitter.addInitializer(new SPARKS.Target(null, setTargetParticle));
emitter.addCallback("created" , onParticleCreated );
emitter.addCallback("dead" , onParticleDead );
}
THREEx.Sparks.prototype.destroy = function()
{
window.removeEventListener('resize', this._$onWindowResize);
if( this._emitter.isRunning() ) this._emitter.stop();
}
//////////////////////////////////////////////////////////////////////////////////
// //
//////////////////////////////////////////////////////////////////////////////////
THREEx.Sparks.prototype.container = function()
{
return this._group;
}
THREEx.Sparks.prototype.emitter = function()
{
return this._emitter;
}
THREEx.Sparks.prototype.update = function()
{
this._group.geometry.__dirtyVertices = true;
this._group.geometry.__dirtyColors = true;
this._attributes.size.needsUpdate = true;
this._attributes.aColor.needsUpdate = true;
}
//////////////////////////////////////////////////////////////////////////////////
// handle window resize //
//////////////////////////////////////////////////////////////////////////////////
THREEx.Sparks.prototype._onWindowResize = function()
{
this._uniforms.sizeRatio.value = this._computeSizeRatio();
this._uniforms.sizeRatio.needsUpdate = true;
}
THREEx.Sparks.prototype._computeSizeRatio = function()
{
return window.innerHeight / 1024;
}
//////////////////////////////////////////////////////////////////////////////////
// Shader Text //
//////////////////////////////////////////////////////////////////////////////////
THREEx.Sparks.vertexShaderText = [
"attribute float size;",
"attribute vec4 aColor;",
"uniform float sizeRatio;",
"varying vec4 vColor;",
"void main() {",
"vec4 mvPosition= modelViewMatrix * vec4( position, 1.0 );",
// FIXME is that the proper formula for size ? doesnt this depends on the fov ?
"gl_PointSize = size * sizeRatio * ( 150.0 / length( mvPosition.xyz ) );",
"gl_Position = projectionMatrix * mvPosition;",
"vColor = aColor;",
"}"
].join('\n');
THREEx.Sparks.fragmentShaderText = [
"uniform vec3 color;",
"uniform sampler2D texture;",
"varying vec4 vColor;",
"void main() {",
"vec4 outColor = texture2D( texture, gl_PointCoord );",
"gl_FragColor = outColor * vec4( color * vColor.xyz, 1.0 );",
"}"
].join('\n');
//////////////////////////////////////////////////////////////////////////////////
// Texture //
//////////////////////////////////////////////////////////////////////////////////
THREEx.Sparks.prototype._buildDefaultTexture = function(size)
{
size = size || 128;
var canvas = document.createElement( 'canvas' );
var context = canvas.getContext( '2d' );
canvas.width = canvas.height = size;
var gradient = context.createRadialGradient( canvas.width/2, canvas.height /2, 0, canvas.width /2, canvas.height /2, canvas.width /2 );
gradient.addColorStop( 0 , 'rgba(255,255,255,1)' );
gradient.addColorStop( 0.2, 'rgba(255,255,255,1)' );
gradient.addColorStop( 0.4, 'rgba(128,128,128,1)' );
gradient.addColorStop( 1 , 'rgba(0,0,0,1)' );
context.beginPath();
context.arc(size/2, size/2, size/2, 0, Math.PI*2, false);
context.closePath();
context.fillStyle = gradient;
//context.fillStyle = 'rgba(128,128,128,1)';
context.fill();
var texture = new THREE.Texture( canvas );
texture.needsUpdate = true;
return texture;
}