January 26, 2021, 11:43:10 AM

Author Topic: [bb] Weather effects by Klaas [ 1+ years ago ]  (Read 584 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Weather effects by Klaas [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Weather effects
Author : Klaas
Posted : 1+ years ago

Description : nothing spectacular .. this is a simple particle system that keeps the particle density on a constant amount when you travel through the snow
This could be used for other effects as well, like rain or something like it.


Code :
Code: BlitzBasic
  1. Graphics3D 800,600,32,2
  2. SetBuffer(BackBuffer())
  3.  
  4. ;------ camera stuff
  5. Global ML_cam_pivot = CreatePivot()
  6. Global ML_cam_pivot2 = CreatePivot(ML_cam_pivot)
  7. Global ML_camera = CreateCamera(ML_cam_pivot2)
  8.  
  9. ;cameracone for the topview
  10. cone = CreateCone(12,1,ml_camera)
  11. EntityColor(cone,255,0,0)
  12. TurnEntity cone,-90,0,0
  13.  
  14. Global x_speed#,y_speed#,z_speed#
  15.  
  16. ;------ load the snowflake
  17. extractdata("snow.jpg")
  18. Global snow = LoadSprite("snow.jpg",2)
  19. ScaleSprite(snow,0.5,0.5)
  20. DeleteFile("snow.jpg")
  21. HideEntity(snow)
  22.  
  23. ;----- the particle type
  24. Type snow
  25.         Field entity
  26.         Field x#,y#,z#
  27.         Field vx#,vy#,vz#
  28. End Type
  29.  
  30. ;topview camera
  31. topview = CreateCamera(ml_cam_pivot)
  32. CameraViewport topview,10,10,320,240
  33. CameraProjMode(topview,2)
  34. CameraZoom(topview,0.02)
  35. MoveEntity(topview,0,10,10)
  36. TurnEntity(topview,90,0,0)
  37.  
  38. ;a plane
  39. plane = CreatePlane()
  40. MoveEntity(plane,0,-8,0)
  41. EntityColor(plane,30,30,30)
  42.  
  43. ;----- the dimension of the weather box
  44. Global weather_x# = 25
  45. Global weather_y# = 20
  46. Global weather_z# = 40
  47.  
  48. ;----- particles must only be created once on startup ... they will be recycled
  49. createSnow(200)
  50.  
  51. ;main loop
  52. While Not KeyHit(1)
  53.         mousemove()
  54.         mouselook()
  55.         doSnow()
  56.  
  57.         RenderWorld()
  58.        
  59.         ;border for topview
  60.         Color 255,255,255
  61.         Rect 10,10,320,240,0
  62.        
  63.         Flip()
  64. Wend
  65. End
  66.  
  67. Function doSnow()
  68.  
  69.         For s.snow = Each snow
  70.                
  71.                 ;this is just the tumbling of the flakes ...
  72.                 ;all movements should be applied To the virtual position
  73.                 svx = svx + Rnd(-0.01,0.01)
  74.                 If svx > 0.051
  75.                         svx = 0.051
  76.                 ElseIf svx < -0.051
  77.                         svx = - 0.051
  78.                 EndIf
  79.                
  80.                 svz = svz + Rnd(-0.01,0.01)
  81.                 If svz > 0.051
  82.                         svz = 0.051
  83.                 ElseIf svz < -0.051
  84.                         svz = - 0.051
  85.                 EndIf
  86.                
  87.                 ;---- now lets correct the position of the flake
  88.                 PositionEntity sentity,sx,sy,sz,1
  89.                
  90.                 ;---- if the flake leaves the weatherbox ... move them to the other side of the box
  91.                 If EntityX(sentity) < -weather_x
  92.                         TranslateEntity sentity,weather_x*2,0,0
  93.                 ElseIf EntityX(sentity) > weather_x
  94.                         TranslateEntity sentity,-weather_x*2,0,0
  95.                 EndIf
  96.  
  97.                 If EntityY(sentity) < -weather_y
  98.                         TranslateEntity sentity,0,weather_y*2,0
  99.                 ElseIf EntityY(sentity) > weather_y
  100.                         TranslateEntity sentity,0,-weather_y*2,0
  101.                 EndIf
  102.  
  103.                 If EntityZ(sentity) < 0
  104.                         TranslateEntity sentity,0,0,weather_z
  105.                 ElseIf EntityZ(sentity) > weather_z
  106.                         TranslateEntity sentity,0,0,-weather_z
  107.                 EndIf
  108.                
  109.                 ;---- now correct the virtual position of the flake and aplly the movements
  110.                 sx = EntityX(sentity,1) + svx
  111.                 sy = EntityY(sentity,1) - 0.051
  112.                 sz = EntityZ(sentity,1) + svz
  113.         Next
  114. End Function
  115.  
  116. Function createSnow(amount)
  117.         For i=1 To amount
  118.                 s.snow = New snow
  119.                
  120.                 ;the snow must be parented to the camera
  121.                 sentity = CopyEntity(snow,ML_camera)
  122.                
  123.                 sx = Rnd(-weather_x,weather_x)
  124.                 sy = Rnd(-weather_y,weather_y)
  125.                 sz = Rnd(0,weather_z)
  126.                 PositionEntity(sentity,sx,sy,sz)
  127.                 EntityAutoFade(sentity,weather_z/2,weather_z)
  128.                 RotateSprite(sentity,Rnd(0,360))
  129.         Next
  130. End Function
  131.  
  132. ;----------------- navigation
  133. Function mouselook()
  134.         x_speed# = x_speed * 0.3 + MouseXSpeed() * 0.3
  135.         y_speed# = y_speed * 0.3 + MouseYSpeed() * 0.3
  136.         z_speed# = z_speed * 0.3 + MouseZSpeed() * 0.3
  137.  
  138.         MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
  139.                
  140.         TurnEntity ML_cam_pivot,0,-x_speed,0    ;turn player left/right
  141.         TurnEntity ML_cam_pivot2,y_speed,0,0    ;tilt camera
  142. End Function
  143.  
  144. Function mousemove()
  145.         If KeyDown(200) ;forward
  146.                 MoveEntity ML_cam_pivot,0,0,0.5
  147.         End If
  148.         If KeyDown(208) ;backward
  149.                 MoveEntity ML_cam_pivot,0,0,-0.5
  150.         End If
  151.         If KeyDown(203) ;left
  152.                 MoveEntity ML_cam_pivot,-0.5,0,0
  153.         End If
  154.         If KeyDown(205) ;right
  155.                 MoveEntity ML_cam_pivot,0.5,0,0
  156.         End If
  157.         If KeyDown(30) ;UP
  158.                 MoveEntity ML_cam_pivot,0,0.5,0
  159.         End If
  160.         If KeyDown(44) ;DOWN
  161.                 MoveEntity ML_cam_pivot,0,-0.5,0
  162.         End If
  163. End Function
  164.  
  165. ;--------- the data for the snowflake
  166. Function extractdata(filename$)
  167.         file = WriteFile(filename)
  168.         d = True
  169.         While d > -1
  170.                 Read d
  171.                 WriteByte(file,d)
  172.         Wend
  173.         CloseFile(file)
  174. End Function
  175.  
  176. .snowData
  177. Data 255,216,255,224,0,16,74,70,73,70,0,1,2,0,0,100,0,100,0,0,255
  178.  
  179. Data 236,0,17,68,117,99,107,121,0,1,0,4,0,0,0,50,0,0,255,238,0,33
  180. Data 65,100,111,98,101,0,100,192,0,0,0,1,3,0,16,3,3,6,9,0,0,1,235
  181. Data 0,0,2,53,0,0,2,140,255,219,0,132,0,8,6,6,6,6,6,8,6,6,8,12,8
  182. Data 7,8,12,14,10,8,8,10,14,16,13,13,14,13,13,16,17,12,14,13,13
  183. Data 14,12,17,15,18,19,20,19,18,15,24,24,26,26,24,24,35,34,34,34
  184. Data 35,39,39,39,39,39,39,39,39,39,39,1,9,8,8,9,10,9,11,9,9,11,14
  185. Data 11,13,11,14,17,14,14,14,14,17,19,13,13,14,13,13,19,24,17,15
  186. Data 15,15,15,17,24,22,23,20,20,20,23,22,26,26,24,24,26,26,33,33
  187. Data 32,33,33,39,39,39,39,39,39,39,39,39,39,255,194,0,17,8,0,32
  188. Data 0,32,3,1,34,0,2,17,1,3,17,1,255,196,0,141,0,1,0,3,1,0,0,0,0
  189. Data 0,0,0,0,0,0,0,0,8,4,6,7,5,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  190. Data 0,16,0,2,2,2,3,1,0,0,0,0,0,0,0,0,0,0,3,4,1,2,5,6,0,32,17,18
  191. Data 17,0,2,2,2,2,3,1,0,0,0,0,0,0,0,0,0,0,1,17,2,18,3,33,49,65,81
  192. Data 97,34,18,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,19,1,1,1,1,0,3
  193. Data 1,0,0,0,0,0,0,0,0,0,1,17,0,33,32,49,65,97,255,218,0,12,3,1
  194. Data 0,2,17,3,17,0,0,0,194,111,18,17,65,206,130,214,47,28,37,88
  195. Data 214,244,111,199,168,149,99,255,218,0,8,1,2,0,1,5,1,233,255
  196. Data 218,0,8,1,3,0,1,5,1,233,255,218,0,8,1,1,0,1,5,1,10,229,61,177
  197. Data 250,237,152,171,122,193,71,7,5,215,190,164,64,85,165,214,23
  198. Data 133,24,190,54,129,64,220,195,22,4,240,93,164,137,220,152,151
  199. Data 30,125,218,56,90,218,107,40,236,37,90,175,230,8,228,204,204
  200. Data 243,255,218,0,8,1,2,2,6,63,1,31,255,218,0,8,1,3,2,6,63,1,31
  201. Data 255,218,0,8,1,1,1,6,63,1,199,85,114,102,91,83,79,209,58,211
  202. Data 29,46,161,142,187,123,125,18,171,8,106,11,66,225,154,219,234
  203. Data 74,226,252,13,221,153,208,149,217,141,255,0,71,195,147,255
  204. Data 218,0,8,1,2,3,1,63,16,240,255,218,0,8,1,3,3,1,63,16,240,255
  205. Data 218,0,8,1,1,3,1,63,16,178,30,196,59,157,254,12,153,115,192
  206. Data 105,210,31,184,165,17,149,162,153,59,132,130,243,57,65,20,153
  207. Data 215,144,174,25,39,143,88,130,217,234,226,169,123,220,77,224
  208. Data -1


Comments :


wedoe(Posted 1+ years ago)

 Pretty good snow imitation this one, :)


puki(Posted 1+ years ago)

 "Wedoe" aint lying this time - (just joking "Wedoe")That is a good snow effect, in fact, I don't recall seeing a 3D one before - good stuff "Klass".


_PJ_(Posted 1+ years ago)

 Grrrr! TTook me ages to spot the 'DeleteFile 'Snow.jpg' line!Apart from that little hiccup, it's really good!


Danny(Posted 1+ years ago)

 Excellent bit of code!


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal