November 28, 2020, 02:07:49 AM

Author Topic: [bb] Smoke Particle Engine by _PJ_ [ 1+ years ago ]  (Read 662 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Smoke Particle Engine by _PJ_ [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Smoke Particle Engine
Author : _PJ_
Posted : 1+ years ago

Description : The Functions and Types allow you to attach a particle emitter to any existing entity (Meshes, Sprites, Pivots, Lights etc) The emitters, once placed, will continuously emit particles each loop.

Code :
Code: BlitzBasic
  1. ;************************
  2. ;GALACTIC ALLEGIANCE v3.2
  3. ;************************
  4.  
  5. ;******* FUNCTIONS *****
  6.  
  7. ;Coded By PJ Chowdhury 2006
  8. ;For the Galactic Allegiance Open-Source Project
  9.  
  10. ;These Functions Released as Full Public Domain
  11.  
  12.  
  13. ;The functions are used to create a particle emitter which will
  14. ;Then continue To emit smoke-like particles of the chosen specifications
  15.  
  16. ;************************************************************************************************
  17. ;                                         EXAMPLE
  18. ;************************************************************************************************
  19.  
  20.  
  21. Graphics3D 800,600,32,2
  22. SetBuffer BackBuffer()
  23.  
  24. cam=CreateCamera()
  25. CameraViewport cam,0,0,800,600
  26. CameraRange cam,1,100
  27.  
  28. MoveEntity cam,0,0,-10
  29.  
  30. tex=CreateTexture(128,128,3)
  31.  
  32. SetBuffer TextureBuffer(tex)
  33.  
  34.        
  35.        
  36.         ClsColor 0,0,0
  37.         Cls
  38.                 Color 255,255,255
  39.                 Oval 64,64,64,64,1
  40. SetBuffer BackBuffer()
  41.  
  42. Global SmokeSprite=CreateSprite() ;*YOU WILL NEED A GLOBAL SPRITE WITH THE HANDLE SmokeSprite FOR THE FUNCTIONS TO WORK
  43. EntityFX SmokeSprite,1
  44.  
  45. EntityTexture SmokeSprite,tex
  46.  
  47. EntityAlpha SmokeSprite,0              
  48.                
  49. AmbientLight 180,180,180
  50. sun=CreateLight(1)
  51. PositionEntity sun,40,20,60
  52.  
  53. PointEntity sun,cam
  54.  
  55. cube=CreateCube()
  56. EntityColor cube,180,0,0
  57. MoveEntity cube,-5,0,2
  58. EntityPickMode cube,2
  59.  
  60. sphere=CreateSphere(20)
  61. EntityColor sphere,0,180,0
  62. MoveEntity sphere,5,0,2
  63. EntityPickMode sphere,2
  64.  
  65. cylinder=CreateCylinder(20,1)
  66. MoveEntity cylinder,0,4,10
  67. RotateMesh cylinder,90,0,45
  68. EntityColor cylinder,0,0,180
  69. EntityPickMode cylinder,2
  70.  
  71. piv=CreatePivot()
  72.  
  73. pyra=CreateCone(4,1)
  74. EntityColor pyra,180,180,0
  75. MoveEntity pyra,0,-2,5
  76. EntityPickMode pyra,2
  77.  
  78. EntityParent pyra,piv
  79.  
  80. While Not KeyDown(1)
  81.         TurnEntity piv,0,0.5,0
  82.         TurnEntity cylinder,1,5,0
  83.         If MouseHit(1)
  84.                 msh=CameraPick(cam,MouseX(),MouseY())
  85.                
  86.                 If msh>0
  87.                         p=msh
  88.                         s#=Rnd#(0.001,0.5)
  89.                         d=Rand(1,5)
  90.                         c=Rand(64,192)
  91.                         f#=Rnd#(0.01,0.1)
  92.                        
  93.                         Emit_Smoke(p,s#,d,c,c,c,f#,Rand(0,20))
  94.                 EndIf
  95.                
  96.         EndIf
  97.  
  98. Update_Smoke()
  99.  
  100.  
  101.  
  102.  
  103.        
  104. UpdateWorld
  105.  
  106. RenderWorld
  107.  
  108. Text 0,0,"CLICK ON A PRIMITIVE MESH TO APPLY A RANDOMISED SMOKE EMITTER"
  109. Text 0,100,"LAST EMITTER: "+msh
  110.  
  111. Flip
  112.  
  113. Wend
  114.  
  115.  
  116.  
  117.  
  118. ;*************************************************************************************************
  119. ;                                   FUNCTIONS AND TYPES                                          
  120. ;*************************************************************************************************
  121.  
  122.  
  123.  
  124. Type Smoke_Particles
  125.         Field Sprite
  126.         Field Alpha#
  127.         Field Fade#
  128. End Type
  129.  
  130. Type Smoke_Emitter
  131.         Field Pivot
  132.         Field SP_Red
  133.         Field SP_Green
  134.         Field SP_Blue
  135.         Field SP_Dens
  136.         Field SP_Scale#
  137.         Field SP_Fade#
  138.         Field SP_Var
  139. End Type
  140.  
  141.  
  142.  
  143.  
  144.  
  145. ;The main user-defining function is Emit_Smoke,
  146. ;Call this Function Whenever you wish To Add an emitter To your world
  147.  
  148. ;Parent_Entity is the entity to which you wish to attach the emitter
  149. ;Sm_Scale# is the size of both the particles and the spread of the smoke effect
  150. ;Sm_Density changes the number of particles created per frame. Integers only
  151. ;Smoke_Red, Green and Blue determine the colour of smoke emitted by that emitter.
  152. ;Sm_fade# is the rate at which the particles fade out (Floats between 0.005 and 0.1 are best)          
  153. ;Sm_Colour_Var is an amount (0 to 50 is a good range) to vary the colour of the smoke by
  154.  
  155. Function Emit_Smoke(Parent_Entity,Sm_Scale#,Sm_Density,Smoke_Red,Smoke_Green,Smoke_Blue,Sm_Fade#,Sm_Colour_Var)
  156.  
  157.         Sm_emit.Smoke_Emitter= New Smoke_Emitter
  158.                 Sm_emitpivot=CreatePivot()      ;This pivot is in effect The particle emitter
  159.  
  160.                         PositionEntity Sm_emitpivot,EntityX#(Parent_Entity,1),EntityY#(Parent_Entity,1),EntityZ#(Parent_Entity,1),1
  161.                         EntityParent Sm_emitpivot,Parent_Entity ;Attach pivot to Parent Entity
  162.  
  163.  
  164.                         Sm_emitSP_Dens=Sm_Density                       ;These lines feed
  165.                         Sm_emitSP_Red=Smoke_Red                 ;the relevant data
  166.                         Sm_emitSP_Green=Smoke_Green             ;to the emitter
  167.                         Sm_emitSP_Blue=Smoke_Blue                       ;
  168.                         Sm_emitSP_Scale#=Sm_Scale#                      ;ensuring a record
  169.                         Sm_emitSP_Fade#=Sm_Fade#                        ;is kept of the
  170.                         Sm_emitSP_Var=Sm_Colour_Var             ;particle criteria
  171.  
  172. End Function
  173.  
  174.  
  175.  
  176. Function Update_smoke()         ;This function should be called each loop.
  177.        
  178.         For upd_Smoke.Smoke_Emitter=Each Smoke_Emitter
  179.  
  180.                 For f=1 To upd_SmokeSP_Dens             ;sets a small loop to generate more particles for higher densities
  181.  
  182.                         SmPart.Smoke_Particles = New Smoke_Particles    ;Generates New Smoke Particles
  183.                                 SmPartSprite=CopyEntity (SmokeSprite)   ;Copies the generic Smoke Sprite
  184.                                 SmPartAlpha#=1                                          ;Sets the particles to be initially visible
  185.                                 SmPartFade#=upd_SmokeSP_Fade#                   ;Gives this Particle a fade index
  186.  
  187.                                 part_ScaleSmall#=((upd_smokeSP_Scale#)/10)                                              ;
  188.                                 scl#=Rnd#(part_Scale#,upd_smokeSP_Scale#)                                               ;                                                                               ;
  189.                                 ScaleSprite SmPartSprite,scl#,scl#                                                              ;Scales the particle
  190.        
  191.                                 EntityColor SmPartSprite,upd_smokeSP_Red+(Rand(0-upd_smokeSP_var,upd_smokeSP_var)),upd_smokeSP_Blue+(Rand(0-upd_smokeSP_var,upd_smokeSP_var)),upd_smokeSP_Green+(Rand(0-upd_smokeSP_var,upd_smokeSP_var))
  192.                                 ;Colours the particle
  193.                                 PositionEntity SmPartSprite,EntityX#(upd_SmokePivot,1),EntityY#(upd_SmokePivot,1),EntityZ#(upd_SmokePivot,1),1
  194.                                
  195.                                 part_ScaleSmall#=Part_ScaleSmall#*5
  196.        
  197.                                 MoveEntity SmPartSprite,Rnd#(0-Part_ScaleSmall#,Part_ScaleSmall#),Rnd#(0-Part_ScaleSmall#,Part_ScaleSmall#),Rnd#(0-Part_ScaleSmall#,Part_ScaleSmall#)
  198.                                 ;positions the particle
  199.                        
  200.                        
  201.                 Next
  202.                
  203.         Next
  204.        
  205.        
  206.         For Upd_Parts.Smoke_Particles = Each Smoke_Particles ;This should also be called each loop
  207.  
  208.                                 EntityAlpha Upd_PartsSprite,Upd_PartsAlpha#                     ;      
  209.                                 Upd_PartsAlpha#=Upd_PartsAlpha# - Upd_PartsFade#                ; Fades the particle
  210.                                
  211.                                 TranslateEntity Upd_PartsSprite,0,0.1,0                         ;Makes the particles rise
  212.                                
  213.  
  214.                                
  215.                                         If Upd_PartsAlpha#<=0 Then Delete Upd_Parts             ;Destroy fully-faded articles
  216.                                                                
  217.         Next
  218.        
  219. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal