Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore.coffee/threejs/Particles.coffee
4627 views
1
###
2
Particle system wrapper/helper
3
4
@class bkcore.threejs.Particles
5
@author Thibaut 'BKcore' Despoulain <http://bkcore.com>
6
###
7
class Particles
8
9
###
10
Creates a new particle system using given parameters
11
12
@param {Object{max, spawnRate, spawn, velocity, randomness,
13
force, spawnRadius, life, friction, color, color2, tint,
14
texture, size, blending, depthTest, transparent, opacity}} opts
15
###
16
constructor: (opts)->
17
18
@black = new THREE.Color(0x000000)
19
@white = new THREE.Color(0xffffff)
20
21
@material = new THREE.ParticleBasicMaterial(
22
color: opts.tint ? 0xffffff
23
map: opts.texture ? null
24
size: opts.size ? 4
25
blending: opts.blending ? THREE.AdditiveBlending
26
depthTest: opts.depthTest ? false
27
transparent: opts.transparent ? true
28
vertexColors: true
29
opacity: opts.opacity ? 1.0
30
sizeAttenuation: true
31
)
32
33
@max = opts.max ? 1000
34
@spawnRate = opts.spawnRate ? 0
35
36
@spawn = opts.spawn ? new THREE.Vector3()
37
@velocity = opts.velocity ? new THREE.Vector3()
38
@randomness = opts.randomness ? new THREE.Vector3()
39
@force = opts.force ? new THREE.Vector3()
40
@spawnRadius = opts.spawnRadius ? new THREE.Vector3()
41
@life = opts.life ? 60
42
@ageing = 1 / @life
43
@friction = opts.friction ? 1.0
44
@color = new THREE.Color(opts.color ? 0xffffff)
45
@color2 = if opts.color2? then new THREE.Color(opts.color2) else null
46
47
@position = opts.position ? new THREE.Vector3()
48
@rotation = opts.rotation ? new THREE.Vector3()
49
@sort = opts.sort ? false
50
51
@pool = []
52
@buffer = []
53
@geometry = null
54
@system = null
55
56
@build()
57
58
###
59
Emits given number of particles
60
@param int count
61
###
62
emit: (count)->
63
64
emitable = Math.min(count, @pool.length)
65
66
for i in [0..emitable]
67
68
p = @pool.pop()
69
p.available = false
70
71
p.position.copy(@spawn).addSelf(
72
@randomVector().multiplySelf(@spawnRadius)
73
)
74
p.velocity.copy(@velocity).addSelf(
75
@randomVector().multiplySelf(@randomness)
76
)
77
p.force.copy(@force)
78
p.basecolor.copy(@color)
79
80
if @color2?
81
p.basecolor.lerpSelf(@color2, Math.random())
82
83
p.life = 1.0
84
85
###
86
@private
87
###
88
build: ()->
89
90
@geometry = new THREE.Geometry()
91
@geometry.dynamic = true
92
93
@pool = []
94
@buffer = []
95
96
for i in [0..@max]
97
98
p = new bkcore.threejs.Particle()
99
@pool.push(p)
100
@buffer.push(p)
101
@geometry.vertices.push(p.position)
102
@geometry.colors.push(p.color)
103
104
@system = new THREE.ParticleSystem(@geometry, @material)
105
@system.position = @position
106
@system.rotation = @rotation
107
@system.sort = @sort
108
109
###
110
@private
111
###
112
randomVector: ()->
113
114
return new THREE.Vector3(
115
Math.random()*2-1,
116
Math.random()*2-1,
117
Math.random()*2-1
118
)
119
120
###
121
Updates particles (should be call in a RAF loop)
122
@param float dt time delta ~1.0
123
###
124
update: (dt)->
125
126
df = new THREE.Vector3()
127
dv = new THREE.Vector3()
128
129
for i in [0..@buffer.length]
130
131
p = @buffer[i]
132
continue if p.available
133
134
p.life -= @ageing
135
136
if p.life <= 0
137
p.reset()
138
@pool.push(p)
139
continue
140
141
142
l = if p.life > 0.5 then 1.0 else p.life + 0.5
143
p.color.setRGB(
144
l * p.basecolor.r,
145
l * p.basecolor.g,
146
l * p.basecolor.b
147
)
148
149
if @friction != 1.0
150
p.velocity.multiplyScalar(@friction)
151
152
df.copy(p.force).multiplyScalar(dt)
153
p.velocity.addSelf(df)
154
155
dv.copy(p.velocity).multiplyScalar(dt)
156
p.position.addSelf(dv)
157
158
if @spawnRate > 0
159
@emit(@spawnRate)
160
161
@geometry.verticesNeedUpdate = true
162
@geometry.colorsNeedUpdate = true
163
164
###
165
Particle sub class
166
167
@class bkcore.threejs.Particle
168
@author Thibaut 'BKcore' Despoulain <http://bkcore.com>
169
###
170
class Particle
171
172
constructor: ()->
173
174
@position = new THREE.Vector3(-10000,-10000,-10000)
175
@velocity = new THREE.Vector3()
176
@force = new THREE.Vector3()
177
@color = new THREE.Color(0x000000)
178
@basecolor = new THREE.Color(0x000000)
179
@life = 0.0
180
@available = true
181
182
reset: ()->
183
@position.set(0,-100000,0)
184
@velocity.set(0,0,0)
185
@force.set(0,0,0)
186
@color.setRGB(0,0,0)
187
@basecolor.setRGB(0,0,0)
188
@life = 0.0
189
@available = true
190
191
###
192
Exports
193
@package bkcore.threejs
194
###
195
exports = exports ? @
196
exports.bkcore ||= {}
197
exports.bkcore.threejs ||= {}
198
exports.bkcore.threejs.Particle = Particle
199
exports.bkcore.threejs.Particles = Particles
200
201