[B3D] 2D Partikel Engine

Übersicht BlitzBasic Codearchiv

Neue Antwort erstellen

Xaymar

ehemals "Cgamer"

Betreff: [B3D] 2D Partikel Engine

BeitragSa, Jan 24, 2009 17:51
Antworten mit Zitat
Benutzer-Profile anzeigen
Diese Partikel Engine habe ich für Wenan geschrieben.(Spätere Effekte, Feuer, usw).
Sie enthält die Grundlegenen Befehle, wie jede andere Partikel Engine auch, basiert allerdings auf 2D Movement.

Code: [AUSKLAPPEN]
Graphics3D 800,600,32,2
SetBuffer BackBuffer()
Global ParticleCount,WorkingCOunt
Global T#, DeltaT#, Zeit#

Cam = CreateCamera():CameraRange Cam, 0.01, 100
Rain = LoadTexture("rnd.png",2)

PSource = CreateParticleSource(0, 0, 1, Cam)
SetParticleSourceLocked(PSource, 0)
SetParticleSourceTex(PSource, Rain)
SetParticleSourceRot(PSource, -360, 360)
SetParticleSourceColorStart(PSource, 255, 255, 255, 0)
SetParticleSourceColorEnd(PSource, 255, 255, 255, 1)
SetParticleSourceMovement(PSource, 90, 0.001, 90, 0.005)
SetParticleSourceLifetime(PSource, 10000)
SetParticleSourceScale(PSource, 0.05, 0.5)

While Not KeyHit(1)
   Cls
   If a = 10 CreateParticle(PSource)
   a = (A + 1) Mod 11
      
   MSec = MilliSecs()
   UpdateParticles(Cam)
   Msec = MilliSecs()-MSec
   
   MoveParticleSource(PSource, Cos(B), -.5)
   B = (B + 1) Mod 360
   MoveEntity Cam, Cos(B)*.01, 0, 0
   
   RenderWorld
   
   Text 0,0,"Particle Count: "+ParticleCount
   Text 0,15,"Rendered Count: "+(TrisRendered()/2)
   Text 0,30,"Working Count: "+WorkingCount
   Text 0,45,"FPS: "+FPS()
   Text 0,60,"MS: "+MSec
   
   Flip
Wend
End

Global FPS, FPS_temp, FPS_time
Function FPS()
   ctime = MilliSecs()
   FPS_temp = FPS_temp + 1
   If ctime - FPS_time > 1000 Then
      FPS = FPS_temp; * 2
      FPS_temp = 0
      FPS_time = ctime
   EndIf
   Return FPS
End Function

;Particle Engine
Type ParticleSource
   Field Piv,Locked
   Field Tex
   Field ARot, ERot
   Field ACR,ACG,ACB
   Field ECR,ECG,ECB
   Field AAl#,EAl#
   Field Lifetime
   Field AScale#, EScale#
   Field AMovDir#,AMovSpe#
   Field EMovDir#,EMovSpe#
End Type

Type Particle
   Field Spr
   Field ARot, ERot
   Field ACR,ACG,ACB
   Field ECR,ECG,ECB
   Field AAl#,EAl#
   Field Lifetime, Msec, SMsec
   Field AScale#, EScale#, CScale#
   Field AMovDir#,AMovSpe#
   Field EMovDir#,EMovSpe#
End Type

Function GetDelta()
   If T# = 0 Then T# = Zeit#
   DeltaT# = (Zeit# - T#) / 1000 * 60
   T# = Zeit#
End Function

Function CreateParticleSource(X#, Y#, Z#, Parent)
   PS.ParticleSource = New ParticleSource
   PS\Piv = CreatePivot(Parent)
   PositionEntity PS\Piv, X#, Y#, Z#
   Return Handle (PS.ParticleSource)
End Function

Function MoveParticleSource(PSource, X#, Y#)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PositionEntity PS\Piv, X#, Y#, EntityZ(PS\Piv)
End Function

Function DeleteParticleSource(PSource)
   Delete Object.ParticleSourcE(PSource)
End Function

Function SetParticleSourceLocked(PSource, Locked)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\Locked = Locked
End Function

Function SetParticleSourceTex(PSource, Tex)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\Tex = Tex
End Function

Function SetParticleSourceRot(PSource, ARot, ERot)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\ARot = ARot
   PS\ERot = ERot
End Function

Function SetParticleSourceColorStart(PSource, Red, Gre, Blu, Al#)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\ACR = Red
   PS\ACG = Gre
   PS\ACB = Blu
   PS\AAl# = Al#
End Function

Function SetParticleSourceColorEnd(PSource, Red, Gre, Blu, Al#)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\ECR = Red
   PS\ECG = Gre
   PS\ECB = Blu
   PS\EAl# = Al#
End Function

Function SetParticleSourceLifetime(PSource, Lifetime)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\Lifetime = Lifetime
End Function

Function SetParticleSourceScale(PSource, AScale#, EScale#)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\AScale# = AScale#
   PS\EScale# = EScale#
End Function

Function SetParticleSourceMovement(PSource, AMovDir#, AMovSpe#, EMovDir#, EMovSpe#)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\AMovDir# = AMovDir#
   PS\AMovSpe# = AMovSpe#
   PS\EMovDir# = EMovDir#
   PS\EMovSpe# = EMovSpe#
End Function

Function CreateParticle(PSource)
   ParticleCount = ParticleCount + 1
   PS.ParticleSource = Object.ParticleSource(PSource)
   
   ;Main Particle
   P.Particle = New Particle

   ;Settings
   P\ARot = PS\ARot:P\ERot = PS\ERot
   P\ACR = PS\ACR:P\ACG = PS\ACG:P\ACB = PS\ACB
   P\ECR = PS\ECR:P\ECG = PS\ECG:P\ECB = PS\ECB
   P\AAl# = PS\AAl#:P\EAl# = PS\EAl#
   
   P\Lifetime = PS\Lifetime
   
   P\AScale# = PS\AScale#
   P\EScale# = PS\EScale#
   P\CScale# = PS\AScale#
   
   P\AMovDir# = PS\AMovDir#
   P\AMovSpe# = PS\AMovSpe#
   P\EMovDir# = PS\EMovDir#
   P\EMovSpe# = PS\EMovSpe#
   
   ;Sprite
   If PS\Locked = 0
      P\Spr = CreateSprite()
      PositionEntity P\Spr, EntityX(PS\Piv, 1), EntityY(PS\Piv, 1), EntityZ(PS\Piv, 1)
   Else
      P\Spr = CreateSprite(PS\Piv)
   EndIf
   SpriteViewMode P\Spr, 2
   RotateEntity P\Spr, 0, 0, P\ARot
   EntityColor P\Spr, P\ACR, P\ACG, P\ACB
   EntityAlpha P\Spr, P\AAl#
   ScaleEntity P\Spr, P\CScale#, P\CScale#, P\CScale#
   
   If PS\Tex <> 0
      EntityTexture P\Spr, PS\Tex
   EndIf
   
   P\SMsec = MilliSecs()
End Function

Function UpdateParticles(Cam)
   Local Perc#,TMovDir#,TMovSpe#,Red,Gre,Blu
   WorkingCount = 0
   Zeit# = MilliSecs()
   GetDelta()
   For P.Particle = Each Particle
      P\Msec = MilliSecs()-P\SMsec
      If P\Msec > P\Lifetime
         FreeEntity P\Spr
         Delete P.Particle
         ParticleCount = ParticleCount - 1
      Else
         Perc# = P\Msec / Float(P\Lifetime)

         If EntityInView(P\Spr, Cam) = 1
            ;Alpha
            If P\AAl# < P\EAl#:EntityAlpha P\Spr, (P\AAl# + (P\EAl# - P\AAl#)*Perc#):ElseIf P\AAl# > P\EAl#:EntityAlpha P\Spr, 1-(P\EAl# + (P\AAl# - P\EAl#)*Perc#):EndIf
         
            ;Color
            Red = P\ACR:Gre = P\ACG:Blu = P\ACB
            If P\ACR < P\ECR:Red = P\ACR - (P\ACR - P\ECR) * Perc#:ElseIf P\ACR > P\ECR:Red = P\ACR + (P\ECR - P\ACR) * Perc#:EndIf
            If P\ACG < P\ECG:Gre = P\ACG - (P\ACG - P\ECG) * Perc#:ElseIf P\ACG > P\ECG:Gre = P\ACG + (P\ECG - P\ACG) * Perc#:EndIf
            If P\ACB < P\ECB:Blu = P\ACB - (P\ACB - P\ECB) * Perc#:ElseIf P\ACB > P\ECB:Blu = P\ACB + (P\ECB - P\ACB) * Perc#:EndIf
            EntityColor P\Spr, Red, Gre, Blu
            
            ;Scaling
            If 1/P\CScale# <> "Infinity" ScaleEntity P\Spr, 1/P\CScale#, 1/P\CScale#, 1/P\CScale#
            If P\AScale# < P\EScale#:P\CScale# = P\AScale# - (P\AScale# - P\EScale#) * Perc#:ElseIf P\AScale# > P\EScale#:P\CScale# = P\AScale# + (P\EScale# - P\AScale#) * Perc#:EndIf
            ScaleEntity P\Spr, P\CScale#, P\CScale#, P\CScale#
            
            ;Rotating
            If P\ARot < P\ERot:RotateEntity P\Spr, 0, 0, P\ARot + (P\ARot - P\ERot) * Perc#:ElseIf P\ARot > P\ERot:RotateEntity P\Spr, 0, 0, P\ARot - (P\ERot - P\ARot) * Perc#:EndIf
            WorkingCount = WorkingCount + 1
         EndIf
         
         ;Movement
         TMovDir# = P\AMovDir:TMovSpe# = P\AMovSpe#
         If P\AMovDir# < P\EMovDir#:TMovDir# = P\AMovDir# - (P\AMovDir# - P\EMovDir#) * Perc#:ElseIf P\AMovDir# > P\EMovDir#:TMovDir# = P\AMovDir# + (P\EMovDir# - P\AMovDir#) * Perc#:EndIf
         If P\AMovSpe# < P\EMovSpe#:TMovSpe# = P\AMovSpe# - (P\AMovSpe# - P\EMovSpe#) * Perc#:ElseIf P\AMovSpe# > P\EMovSpe#:TMovSpe# = P\AMovSpe# + (P\EMovSpe# - P\AMovSpe#) * Perc#:EndIf
         TranslateEntity P\Spr, (Cos(TMovDir#)*TMovSpe#)*DeltaT#, (Sin(TMovDir#)*TMovSpe#)*DeltaT#, 0.0005
      EndIf
   Next
End Function


Code: [AUSKLAPPEN]
Global ParticleCount
Global T#, DeltaT#, Zeit#

;Particle Engine
Type ParticleSource
   Field Piv,Locked
   Field Tex
   Field ARot, ERot
   Field ACR,ACG,ACB
   Field ECR,ECG,ECB
   Field AAl#,EAl#
   Field Lifetime
   Field AScale#, EScale#
   Field AMovDir#,AMovSpe#
   Field EMovDir#,EMovSpe#
End Type

Type Particle
   Field Spr
   Field ARot, ERot
   Field ACR,ACG,ACB
   Field ECR,ECG,ECB
   Field AAl#,EAl#
   Field Lifetime, Msec, SMsec
   Field AScale#, EScale#, CScale#
   Field AMovDir#,AMovSpe#
   Field EMovDir#,EMovSpe#
End Type

Function GetDelta()
   If T# = 0 Then T# = Zeit#
   DeltaT# = (Zeit# - T#) / 1000 * 60
   T# = Zeit#
End Function

Function CreateParticleSource(X#, Y#, Z#, Parent)
   PS.ParticleSource = New ParticleSource
   PS\Piv = CreatePivot(Parent)
   PositionEntity PS\Piv, X#, Y#, Z#
   Return Handle (PS.ParticleSource)
End Function

Function MoveParticleSource(PSource, X#, Y#)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PositionEntity PS\Piv, X#, Y#, EntityZ(PS\Piv)
End Function

Function DeleteParticleSource(PSource)
   Delete Object.ParticleSourcE(PSource)
End Function

Function SetParticleSourceLocked(PSource, Locked)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\Locked = Locked
End Function

Function SetParticleSourceTex(PSource, Tex)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\Tex = Tex
End Function

Function SetParticleSourceRot(PSource, ARot, ERot)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\ARot = ARot
   PS\ERot = ERot
End Function

Function SetParticleSourceColorStart(PSource, Red, Gre, Blu, Al#)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\ACR = Red
   PS\ACG = Gre
   PS\ACB = Blu
   PS\AAl# = Al#
End Function

Function SetParticleSourceColorEnd(PSource, Red, Gre, Blu, Al#)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\ECR = Red
   PS\ECG = Gre
   PS\ECB = Blu
   PS\EAl# = Al#
End Function

Function SetParticleSourceLifetime(PSource, Lifetime)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\Lifetime = Lifetime
End Function

Function SetParticleSourceScale(PSource, AScale#, EScale#)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\AScale# = AScale#
   PS\EScale# = EScale#
End Function

Function SetParticleSourceMovement(PSource, AMovDir#, AMovSpe#, EMovDir#, EMovSpe#)
   PS.ParticleSource = Object.ParticleSourcE(PSource)
   PS\AMovDir# = AMovDir#
   PS\AMovSpe# = AMovSpe#
   PS\EMovDir# = EMovDir#
   PS\EMovSpe# = EMovSpe#
End Function

Function CreateParticle(PSource)
   PS.ParticleSource = Object.ParticleSource(PSource)
   
   ;Main Particle
   P.Particle = New Particle

   ;Settings
   P\ARot = PS\ARot:P\ERot = PS\ERot
   P\ACR = PS\ACR:P\ACG = PS\ACG:P\ACB = PS\ACB
   P\ECR = PS\ECR:P\ECG = PS\ECG:P\ECB = PS\ECB
   P\AAl# = PS\AAl#:P\EAl# = PS\EAl#
   
   P\Lifetime = PS\Lifetime
   
   P\AScale# = PS\AScale#
   P\EScale# = PS\EScale#
   P\CScale# = PS\AScale#
   
   P\AMovDir# = PS\AMovDir#
   P\AMovSpe# = PS\AMovSpe#
   P\EMovDir# = PS\EMovDir#
   P\EMovSpe# = PS\EMovSpe#
   
   ;Sprite
   If PS\Locked = 0
      P\Spr = CreateSprite()
      PositionEntity P\Spr, EntityX(PS\Piv, 1), EntityY(PS\Piv, 1), EntityZ(PS\Piv, 1)
   Else
      P\Spr = CreateSprite(PS\Piv)
   EndIf
   SpriteViewMode P\Spr, 2
   RotateEntity P\Spr, 0, 0, P\ARot
   EntityColor P\Spr, P\ACR, P\ACG, P\ACB
   EntityAlpha P\Spr, P\AAl#
   ScaleEntity P\Spr, P\CScale#, P\CScale#, P\CScale#
   
   If PS\Tex <> 0
      EntityTexture P\Spr, PS\Tex
   EndIf
   
   P\SMsec = MilliSecs()
End Function

Function UpdateParticles(Cam)
   Local Perc#,TMovDir#,TMovSpe#,Red,Gre,Blu
   Zeit# = MilliSecs()
   GetDelta()
   For P.Particle = Each Particle
      P\Msec = MilliSecs()-P\SMsec
      If P\Msec > P\Lifetime
         FreeEntity P\Spr
         Delete P.Particle
      Else
         Perc# = P\Msec / Float(P\Lifetime)

         If EntityInView(P\Spr, Cam) = 1
            ;Alpha
            If P\AAl# < P\EAl#:EntityAlpha P\Spr, (P\AAl# + (P\EAl# - P\AAl#)*Perc#):ElseIf P\AAl# > P\EAl#:EntityAlpha P\Spr, 1-(P\EAl# + (P\AAl# - P\EAl#)*Perc#):EndIf
         
            ;Color
            If P\ACR < P\ECR:Red = P\ACR - (P\ACR - P\ECR) * Perc#:ElseIf P\ACR > P\ECR:Red = P\ACR + (P\ECR - P\ACR) * Perc#:EndIf
            If P\ACG < P\ECG:Gre = P\ACG - (P\ACG - P\ECG) * Perc#:ElseIf P\ACG > P\ECG:Gre = P\ACG + (P\ECG - P\ACG) * Perc#:EndIf
            If P\ACB < P\ECB:Blu = P\ACB - (P\ACB - P\ECB) * Perc#:ElseIf P\ACB > P\ECB:Blu = P\ACB + (P\ECB - P\ACB) * Perc#:EndIf
            EntityColor P\Spr, Red, Gre, Blu
            
            ;Scaling
            If 1/P\CScale# <> "Infinity" ScaleEntity P\Spr, 1/P\CScale#, 1/P\CScale#, 1/P\CScale#
            If P\AScale# < P\EScale#:P\CScale# = P\AScale# - (P\AScale# - P\EScale#) * Perc#:ElseIf P\AScale# > P\EScale#:P\CScale# = P\AScale# + (P\EScale# - P\AScale#) * Perc#:EndIf
            ScaleEntity P\Spr, P\CScale#, P\CScale#, P\CScale#
            
            ;Rotating
            If P\ARot < P\ERot:RotateEntity P\Spr, 0, 0, P\ARot + (P\ARot - P\ERot) * Perc#:ElseIf P\ARot > P\ERot:RotateEntity P\Spr, 0, 0, P\ARot - (P\ERot - P\ARot) * Perc#:EndIf
         EndIf
         
         ;Movement
         If P\AMovDir# < P\EMovDir#:TMovDir# = P\AMovDir# - (P\AMovDir# - P\EMovDir#) * Perc#:ElseIf P\AMovDir# > P\EMovDir#:TMovDir# = P\AMovDir# + (P\EMovDir# - P\AMovDir#) * Perc#:EndIf
         If P\AMovSpe# < P\EMovSpe#:TMovSpe# = P\AMovSpe# - (P\AMovSpe# - P\EMovSpe#) * Perc#:ElseIf P\AMovSpe# > P\EMovSpe#:TMovSpe# = P\AMovSpe# + (P\EMovSpe# - P\AMovSpe#) * Perc#:EndIf
         TranslateEntity P\Spr, (Cos(TMovDir#)*TMovSpe#)*DeltaT#, (Sin(TMovDir#)*TMovSpe#)*DeltaT#, 0.0005
      EndIf
   Next
End Function


Beschreibung der Befehle:
Zitat:
PartikelQuelle = CreateParticleSource(X, Y, Z, Parent)
-Erstellt eine neue Partikel Quelle
MoveParticleSource(PartikelQuelle, X, Y)
-Bewegt eine erstellte Partikel Quelle
DeleteParticleSource(PartikelQuelle)
-Löscht eine erstellte Partikel Quelle
SetParticleSourceLocked(PartikelQuelle, Fest)
-Setzt fest, ob die Partikel der Quelle folgen sollen
SetParticleSourceTex(PartikelQuelle, Textur)
-Setzt die Textur für die Partikel fest
SetParticleSourceRot(PartikelQuelle, StartRotation, EndRotation)
-Setzt die Rotation für die Partikel
SetParticleSourceColorStart(PartikelQuelle, R, G, B, A#)
-Setzt die Startfarbe der Partikel + Alpha#
SetParticleSourceColorEnd(PartikelQuelle, R, G, B, A#
-Setzt die Endfarbe der Partikel + Alpha#
SetParticleSourceLifetime(PartikelQuelle, Lebenszeit)
-Setzt die Lebenszeit der Partikel in Millisekunden
SetParticleSourceScale(PartikelQuelle, StartGröße, EndGröße)
-Setzt die Größe der Partikel(0.05 ist ideal)
SetParticleSourceMovement(PartikelQuelle, StartGrad, StartGeschwindigkeit, EndGrad, EndGeschwindigkeit)
-Setzt die Geschwindigkeit der Partikel
CreateParticle(PartikelQuelle)
-Erstellt 1 Partikel
UpdateParticles(Kamera)
-Erneuert alle Partikel. Unbedingt vor Renderworld aufrufen. Um eine schnelle Umgebung zu bieten wird eine Kamera benötigt


[Update]mehrere kleine Bugs behoben, ParticleSource kann nun Kamera folgen
Warbseite

Neue Antwort erstellen


Übersicht BlitzBasic Codearchiv

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group