-
Notifications
You must be signed in to change notification settings - Fork 0
/
soulparticles.py
415 lines (244 loc) · 11.6 KB
/
soulparticles.py
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
from direct.particles.ParticleEffect import ParticleEffect
from direct.particles import Particles
from direct.particles import ForceGroup # Correct import for ForceGroup
from direct.particles import ParticleManagerGlobal
from direct.particles import Particles
from panda3d.physics import RectangleEmitter
from panda3d.physics import BaseParticleEmitter, BaseParticleRenderer
from panda3d.physics import PointParticleFactory, SpriteParticleRenderer
from panda3d.physics import LinearNoiseForce, DiscEmitter
import glob
from panda3d.core import Filename, LVector3, LVector4, LPoint3, LPoint3f, CardMaker
import random
from panda3d.core import Point3
class SoulParticles:
def __init__(self, texture_directory, parent_node):
self.texture_directory = texture_directory
self.parent_node = parent_node
self.effect = None
self.p = ParticleEffect()
self.colors = [
LVector4(1.0, 0.0, 0.0, 1.0), # Red
LVector4(1.0, 0.5, 0.0, 1.0), # Orange
LVector4(1.0, 1.0, 0.0, 1.0), # Yellow
LVector4(0.0, 1.0, 0.0, 1.0), # Green
LVector4(0.0, 0.0, 1.0, 1.0), # Blue
LVector4(0.29, 0.0, 0.51, 1.0), # Indigo
LVector4(0.93, 0.51, 0.93, 1.0), # Violet
]
self.color_index = 0
self.time_since_last_change = 0.0
self.color_change_interval = 0.5 # Change color every 0.5 seconds
self.dt = globalClock.get_dt()
self.angular_velocities = []
def load_textures(self, directory):
"""Load all textures from the specified directory."""
print(f"Loading textures from directory: {directory}")
texture_files = glob.glob(f"{directory}/*.png")
textures = []
for file in texture_files:
texture = loader.loadTexture(Filename.fromOsSpecific(file))
if texture:
textures.append(texture)
print(f"Loaded texture: {file}")
else:
print(f"Failed to load texture: {file}")
print(f"Total textures loaded: {len(textures)}")
return textures
def create_matrix_effect(self, t):
print("Creating matrix effect...")
self.p.cleanup()
self.p = ParticleEffect()
# Reset the effect and set scale
self.p.reset()
self.p.setScale(1, 1, 1)
# Create a new particle system
self.p0 = Particles.Particles("particles-1")
# Particles parameters
self.p0.setFactory("PointParticleFactory")
self.p0.setRenderer("SpriteParticleRenderer")
self.p0.setEmitter("BoxEmitter")
self.p0.setPoolSize(4096) # Number of particles
self.p0.setBirthRate(0.00002) # Rate of particle creation
self.p0.setLitterSize(256) # Number of particles per burst
self.p0.setLitterSpread(128) # Spread of particles within each burst
self.p0.setSystemLifespan(
20
) # Lifespan of the entire particle system (in seconds)
self.p0.setLocalVelocityFlag(1) # Local velocity flag
self.p0.setSystemGrowsOlderFlag(0) # Particles should not grow older
# Factory parameters (control particle behavior)
self.p0.factory.setLifespanBase(1) # Base lifespan of each particle
self.p0.factory.setLifespanSpread(5) # Spread in particle lifespan
self.p0.factory.setMassBase(10.0) # Mass of each particle
self.p0.factory.setMassSpread(9) # Variation in particle mass
self.p0.factory.setTerminalVelocityBase(
1000.0
) # Terminal velocity of particles
self.p0.factory.setTerminalVelocitySpread(500) # Spread in terminal velocity
# Renderer parameters (control appearance)
self.p0.renderer.setAlphaMode(BaseParticleRenderer.PRALPHAUSER)
self.p0.renderer.setUserAlpha(0.5) # Full opacity
self.p0.renderer.setTexture(
base.loader.loadTexture("stars/00041-2773312458.png")
) # Texture for particles
self.p0.renderer.setColor(LVector4(1.0, 1.0, 1.0, 1.0)) # White color (RGBA)
self.p0.renderer.setXScaleFlag(True) # Disable scaling along X-axis
self.p0.renderer.setYScaleFlag(True) # Disable scaling along Y-axis
self.p0.renderer.setAnimAngleFlag(False) # No animation on particle angle
self.p0.renderer.setInitialXScale(0.025) # Initial size of particles (X)
self.p0.renderer.setFinalXScale(0.0025) # Final size of particles (X)
self.p0.renderer.setInitialYScale(0.025) # Initial size of particles (Y)
self.p0.renderer.setFinalYScale(0.0025) # Final size of particles (Y)
self.p0.renderer.setNonanimatedTheta(180.0) # No rotation on the particles
self.p0.renderer.setAlphaBlendMethod(
BaseParticleRenderer.PPBLENDLINEAR
) # Linear blend for transparency
# Set blending to additive
self.p0.renderer.setAlphaBlendMethod(
BaseParticleRenderer.PR_ALPHA_IN
) # Additive blending mode
self.p0.renderer.setAlphaDisable(False)
# Emitter parameters (control emission behavior)
self.p0.emitter.setEmissionType(
BaseParticleEmitter.ETCUSTOM
) # Emit particles in a radiating pattern
self.p0.emitter.setAmplitude(
100.0
) # No amplitude (adjust if you need more force)
self.p0.emitter.setAmplitudeSpread(100.0) # No spread in amplitude
self.p0.emitter.setOffsetForce(
LVector3(0.0, 0.0, -90.2)
) # Apply a slight downward force
self.p0.emitter.setExplicitLaunchVector(
LVector3(0.0, 0.0, -90.0)
) # Emit particles downward
self.p0.emitter.setRadiateOrigin(
LPoint3(0.0, 0.0, 0.0)
) # Emit from the origin (0,0,0)
self.p0.emitter.setMinBound(
LPoint3(-1000, -1000, -1000)
) # Minimum bounds of the emitter area
self.p0.emitter.setMaxBound(
LPoint3f(1000, 1000, 1000)
) # Maximum bounds of the emitter area
# New part: Apply angular velocity to particles for spinning effect
for i in range(self.p0.getLivingParticles()):
angular_velocity = random.uniform(
-18, 18
) # Random angular velocity (in degrees)
self.p0.factory.setAngularVelocityBase(angular_velocity)
# Add the particles to the effect
self.p.addParticles(self.p0)
# Apply force groups (e.g., gravity or other forces)
f0 = ForceGroup.ForceGroup("default")
self.p.addForceGroup(f0)
# Load and add textures
textures = self.load_textures(self.texture_directory)
if not textures:
raise FileNotFoundError(
f"No textures found in directory: {self.texture_directory}"
)
for texture in textures:
self.p0.renderer.addTexture(texture)
print(f"Added {len(textures)} textures to renderer.")
self.t = t
self.p.reparentTo(self.t)
print("Starting particle effect...")
base.taskMgr.add(self.update, "star_update")
self.p.start(self.t)
def loadParticleConfig(self, filename):
# Start of the code from steam.ptf
self.p.cleanup()
self.p = ParticleEffect()
self.p.loadConfig(Filename(filename))
# Sets particles to birth relative to the teapot, but to render at
# toplevel
self.p.start(self.t)
self.p.setPos(0, 0.000, 0)
def load_font_glyphs(self, font_path):
"""Loads glyphs from the font and returns them as textures."""
print(f"Loading glyphs from font: {font_path}")
glyph_textures = []
text_node = TextNode("glyph")
text_node.setFont(loader.loadFont(font_path))
text_node.setTextColor(1, 1, 1, 1)
text_node.setAlign(TextNode.A_center)
for char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789":
text_node.setText(char)
texture = self.create_texture_from_text(text_node)
if texture:
glyph_textures.append(texture)
print(f"Created texture for glyph: {char}")
else:
print(f"Failed to create texture for glyph: {char}")
print(f"Total glyph textures created: {len(glyph_textures)}")
return glyph_textures
def create_texture_from_text(self, text_node):
"""Creates a texture from the rendered text node."""
print(f"Creating texture from text node with text: {text_node.getText()}")
cm = CardMaker("text_card")
card = base.render.attachNewNode(cm.generate())
text_node_path = card.attachNewNode(text_node)
text_node_path.setScale(1) # Ensure proper scaling for the render
# Capture image from the text node
img = PNMImage()
card.node().setIntoCollideMask(0) # Avoid interference with collision
base.graphicsEngine.renderFrame()
base.win.getScreenshot(img)
texture = Texture()
texture.load(img)
card.removeNode() # Clean up the temporary card
print("Texture created successfully.")
return texture
def update(self, task):
"""
Update the particle system with a color cycle.
:param task: The task object, which provides information about the elapsed time.
"""
self.randomize_color() # Randomize color in every frame
dt = task.dt # Access the elapsed time from the task
self.time_since_last_change += dt
if self.time_since_last_change >= self.color_change_interval:
# Cycle to the next color in the ROYGBIV array
self.color_index = (self.color_index + 1) % len(self.colors)
if (
self.p0 and self.p0.renderer
): # Check if self.p0 and its renderer are valid
self.p0.renderer.setColor(self.colors[self.color_index])
# print(f"Cycled to color: {self.colors[self.color_index]}")
else:
# print("Error: self.p0 or self.p0.renderer is None during color cycling.")
self.time_since_last_change = 0.0 # Reset the timer
return task.cont # Continue the task
def randomize_color(self):
"""
Set the particle color to a random color from the ROYGBIV list.
"""
if self.p0 and self.p0.renderer: # Check if self.p0 and its renderer are valid
random_color = random.choice(self.colors)
self.p0.renderer.setColor(random_color)
# print(f"Randomized color: {random_color}")
else:
# print("Error: self.p0 or self.p0.renderer is None during random color update.")
pass
def cleanup(self):
"""Clean up the particle system and associated resources."""
print("Cleaning up particle effect...")
# Stop the particle effect
if self.p:
self.p.cleanup() # Clean up the particle effect resources
self.p = None # Set the particle effect reference to None to release it
# Reset any textures or other resources
self.p0 = (
None # Remove the particle system (optional, as cleanup is already done)
)
# Optional: Reset color cycling state
self.color_index = 0
self.time_since_last_change = 0.0
# Optional: Remove any additional tasks if needed
if "star_update" in base.taskMgr.getTasks():
base.taskMgr.remove("star_update")
print("Cleanup completed.")
def reCenter(self, player=None):
self.p0.emitter.setRadiateOrigin(player.getPos())