-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjetpackFlame.js
228 lines (137 loc) · 5.01 KB
/
jetpackFlame.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
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
var JetpackFlame = function()
{
this.speed = 50;
this.targetRotation = 0;
this.delta = 1;
this.clock = new THREE.Clock();
this.particleCloud = {};
this.sparksEmitter = {};
this.emitterpos = {};
this.timeOnShapePath = 0;
this.rotation = 0;
this.particlesLength = 50000;
var particles = new THREE.Geometry();
this.newpos = function newpos( x, y, z ) {
return new THREE.Vector3( x, y, z );
}
var Pool = {
__pools: [],
// Get a new Vector
get: function() {
if ( this.__pools.length > 0 ) {
return this.__pools.pop();
}
console.log( "pool ran out!" )
return null;
},
// Release a vector back into the pool
add: function( v ) {
this.__pools.push( v );
}
};
for ( i = 0; i < this.particlesLength; i ++ ) {
particles.vertices.push( this.newpos( Math.random() * 200 - 100, Math.random() * 100 + 150, Math.random() * 50 ) );
Pool.add( i );
}
this.generateSprite = function(){
var canvas = document.createElement( 'canvas' );
canvas.width = 32;
canvas.height = 32;
var context = canvas.getContext( '2d' );
context.beginPath();
context.arc( 16, 16, 10, 0, Math.PI * 2, false) ;
context.closePath();
context.lineWidth = 0.5; //0.05
context.stroke();
context.restore();
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.1, 'rgba(200,200,200,1)' );
gradient.addColorStop( 0.3, 'rgba(200,200,200,1)' );
gradient.addColorStop( 1, 'rgba(0,0,0,1)' );
context.fillStyle = gradient;
context.fill();
return canvas;
}
this.attributes = {
size: { type: 'f', value: [] },
pcolor: { type: 'c', value: [] }
};
this.sprite = this.generateSprite() ;
this.texture = new THREE.Texture( this.sprite );
this.texture.needsUpdate = true;
this.uniforms = {
texture: { type: "t", value: 0, texture: this.texture }
};
this.shaderMaterial = new THREE.ShaderMaterial( {
uniforms: this.uniforms,
attributes: this.attributes,
vertexShader: document.getElementById( 'vertexshader' ).textContent,
fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
blending: THREE.AdditiveBlending,
depthWrite: false,
transparent: true
});
particleCloud = new THREE.ParticleSystem( particles, this.shaderMaterial );
this.particleCloud.dynamic = true;
this.system = particleCloud;
//particleCloud.sortParticles = true;
var vertices = particleCloud.geometry.vertices;
var values_size = this.attributes.size.value;
var values_color = this.attributes.pcolor.value;
for( var v = 0; v < vertices.length; v ++ ) {
values_size[ v ] = 10;
values_color[ v ] = new THREE.Color( 0xffffff );
values_color[ v ].setHSL( 0, 0, 0 );
particles.vertices[ v ].set( Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY );
}
var hue = 0;
var setTargetParticle = function() {
var target = Pool.get();
values_size[ target ] = Math.random() * 5 + 5;
return target;
};
var hue = 0;
var onParticleCreated = function( p ) {
var position = p.position;
p.target.position = position;
var target = p.target;
if ( target ) {
//console.log(target,particles.vertices[target]);
//values_size[target]
//values_color[target]
//hue += 0.003 ;
//if ( hue > 1 ) hue -= 1;
particles.vertices[ target ] = p.position;
values_color[ target ].setHSL( 0, .7, .7 );
};
};
var onParticleDead = function( particle ) {
var target = particle.target;
if ( target ) {
// Hide the particle
values_color[ target ].setHSL( 0, 0, 0 );
particles.vertices[ target ].set( Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY );
// Mark particle system as available by returning to pool
Pool.add( particle.target );
}
};
var engineLoopUpdate = function() {
};
this.counter = new SPARKS.SteadyCounter( 1 );
this.sparksEmitter = new SPARKS.Emitter( this.counter );
this.emitterpos = new THREE.Vector3( 100, 0, 0 );
this.sparksEmitter.addInitializer( new SPARKS.Position( new SPARKS.PointZone( this.emitterpos ) ) );
this.sparksEmitter.addInitializer( new SPARKS.Lifetime( 1, 1.5 ));
this.sparksEmitter.addInitializer( new SPARKS.Target( null, setTargetParticle ) );
this.sparksEmitter.addInitializer( new SPARKS.Velocity( new SPARKS.PointZone( new THREE.Vector3( 0, -3, 0 ) ) ) );
// TOTRY Set velocity to move away from centroid
this.sparksEmitter.addAction( new SPARKS.Age() );
this.sparksEmitter.addAction( new SPARKS.Accelerate( 0, 1, 0 ) );
this.sparksEmitter.addAction( new SPARKS.Move() );
this.sparksEmitter.addAction( new SPARKS.RandomDrift( 50, -200, 50) );
this.sparksEmitter.addCallback( "created", onParticleCreated );
this.sparksEmitter.addCallback( "dead", onParticleDead );
this.sparksEmitter.start();
console.log("*** JetpackFlame CREATED *** ")
}