Rain performance

Everything related to 3D programming
Philippe-felixer76-3
User
User
Posts: 45
Joined: Mon Dec 30, 2013 10:12 pm

Rain performance

Post by Philippe-felixer76-3 »

Hi,

I have add some rain to my game but the performance is dropping if i change the amount of rain from 10000 to 20000.

Is there a better more efficiënt way to simulate rain?

Example:

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Billboard
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
;

#CameraSpeed = 2

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY

Structure bb
  bb.l
EndStructure

Global NewList rain.bb()

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/skybox.zip", #PB_3DArchive_Zip)
    Parse3DScripts()
    
    ; First create materials
    ;
    
    GrassMaterial = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any,"grass1.png")))
    MaterialBlendingMode(GrassMaterial, #PB_Material_AlphaBlend)
    DirtMaterial = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any,"Dirt.jpg")))
    
    
    MeshPlane = CreatePlane(#PB_Any, 2000, 2000, 40, 40, 4, 4)
    CreateEntity(#PB_Any, MeshID(MeshPlane), MaterialID(DirtMaterial))
    
    ; Add house
    MeshHouse = LoadMesh(#PB_Any, "tudorhouse.mesh")
    House = CreateEntity(#PB_Any, MeshID(MeshHouse), #PB_Material_None, 0, 280, 0)
    ScaleEntity(House, 0.5, 0.5, 0.5)
    
    ; Rain.. 
    GrassMaterial1 = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any,"grass1.png")))
    MaterialBlendingMode(GrassMaterial1, #PB_Material_AlphaBlend)
    Billboard1 = CreateBillboardGroup(#PB_Any, MaterialID(GrassMaterial1), 4, 10, 0, 0, 0, #PB_All, 1)
    BillboardGroupCommonDirection(Billboard1, 0, 1, 0)    
    
    ; ON MY MACHINE IT RUNS FINE WITH 10000 AND IS VERY SLOW ON 20000
    For i = 0 To 10000
      bx.l = Random(2000)-1000
      bz.l = Random(2000)-1000
      y = Random(2000) 
      AddElement(rain())
      rain()\bb = AddBillboard(Billboard1, bx, y+6, bz)
    Next i
    
    ; create camera
    Camera = CreateCamera(#PB_Any, 0, 0, 100, 100)
    MoveCamera(Camera, 200, 400, 900, #PB_Absolute)
    CameraLookAt(Camera, 0, 100, 0)
    CameraBackColor(Camera, RGB(255,255,255))
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed 
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed 
        Else
          KeyY = 0
        EndIf
        
      EndIf
      
      RotateCamera(Camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (Camera, KeyX, 0, KeyY)
      
      ResetList(rain())
      While NextElement(rain())
        If BillboardY(rain()\bb, Billboard1) - 2.1<0
          bx.l = Random(2000)-1000
          bz.l = Random(2000)-1000
          y = Random(2000)
          RemoveBillboard(rain()\bb, Billboard1)
          rain()\bb = AddBillboard(Billboard1, bx, y, bz)
        Else
          MoveBillboard(rain()\bb, Billboard1, 0, -2.1, 0)
        EndIf
      Wend
        
      RenderWorld()
      Screen3DStats()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Rain performance

Post by DK_PETER »

Any specific reason for not using particles?

Instead of moving billboards, I would use a mesh like this:
Note: Use the flares in the PB textures folders for images for visuals..
viewtopic.php?f=36&t=70132&hilit=mesh

or the particle way:

Way back I made this demo called pouring rain. (Before I began to make 'things big' on a small scale).

Here it is - I've changed the particle's velocity command so the example works with 5.73.

Code: Select all

;Simple example of pouring rain...

EnableExplicit

InitEngine3D()
InitSprite()
InitMouse()
InitKeyboard()

DeclareModule Rain
  Declare.i SetResolution(Width.i = 1024, Height.i = 768)
  Declare.i RunDemo()
EndDeclareModule

Module Rain
  Structure RainData
    id.i
    mat.i
    tex.i[2]
    x.f
    y.f
    z.f
  EndStructure
 
  Structure _MeshData
    mesh.i
    id.i
    mat.i
    tex.i
    id2.i
  EndStructure
 
  Structure _LampsData
    pole._MeshData
    arm._MeshData
    light.i
  EndStructure
 
  Structure ObjectData
    rain.RainData
    splash.RainData
    road._MeshData
    street._MeshData
    clouds._MeshData
    List build._MeshData()
  EndStructure
 
  Declare.i DoPouringRain()
  Declare.i DoStreetAndRoad()
  Declare.i DoBuildings()
  Declare.i DoClouds()
  Declare.i CreateBuildingTexture()
  Declare.i RandomI(min.i, Max.i, Res.i = 100000)
  Declare.i CheckBuild()
 
  Global Win.i, Scr.i, Cam.i, FS.i
  Global ob.ObjectData
 
  Procedure.i SetResolution(Width.i = 1024, Height.i = 768)
    If FullScreen = #False
      Win = OpenWindow(#PB_Any, 0, 0, Width, Height, "Pouring rain", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
      Scr = OpenWindowedScreen(WindowID(Win), 0 ,0 , Width * DesktopResolutionX(), Height * DesktopResolutionY(), #False, 0, 0, #PB_Screen_SmartSynchronization)
    EndIf
   
    Cam = CreateCamera(#PB_Any, 0, 0, 100, 100)
    MoveCamera(Cam, 0, 50, 220)
    CameraBackColor(Cam, $323234)
    Fog($323234, 250, 200, 2000)
  EndProcedure 
 
  Procedure.i RandomI(min.i, Max.i, Res.i = 100000)
    ProcedureReturn (Min + (Max - Min) * Random(Res) / Res)
  EndProcedure
 
  Procedure.i DoPouringRain()
    With ob\rain
      \id = CreateParticleEmitter(#PB_Any, 2000, 600, 2000, #PB_Particle_Box, 0, 0, 0)
     
      \tex[0] = CreateTexture(#PB_Any, 2 , 12)
      StartDrawing(TextureOutput(\tex[0]))
      LineXY(1, 0, 1, 10, $FFFFFF)
      StopDrawing()
     
      \mat = CreateMaterial(#PB_Any, TextureID(\tex[0]))
      ScrollMaterial(\mat, 3 ,0 ,#PB_Material_Animated)
      ParticleMaterial(\id, MaterialID(\mat))
      ParticleTimeToLive(\id, 4, 6)
      ParticleVelocity(\id, #PB_Particle_MinimumVelocity, 200)
      ParticleVelocity(\id, #PB_Particle_MaximumVelocity, 400)
      ParticleVelocity(\id, #PB_Particle_Velocity, 300)
      ParticleEmitterDirection(\id, 0, -1, 0)
      ParticleSize(\id, 0.1, 150)
      ParticleEmissionRate(\id, 1500)
      ParticleSpeedFactor(\id, 4)
    EndWith
   
    With ob\splash
      \id = CreateParticleEmitter(#PB_Any, 2000, 2000, 5, #PB_Particle_Box, 0, 12, 0)
     
      \tex[0] = CreateTexture(#PB_Any, 8 , 10)
      StartDrawing(TextureOutput(\tex[0]))
      LineXY(4,10,2,2,$FFFFFF)
      LineXY(4,10,4,2,$FFFFFF)
      LineXY(4,10,6,2,$FFFFFF)
      StopDrawing()
     
      \mat = CreateMaterial(#PB_Any, TextureID(\tex[0]))
      MaterialBlendingMode(\mat, #PB_Material_Color)
      ParticleMaterial(\id, MaterialID(\mat))
      ParticleTimeToLive(\id, 0.1, 0.1)
      ParticleVelocity(\id, #PB_Particle_MinimumVelocity, 0.01)
      ParticleVelocity(\id, #PB_Particle_MaximumVelocity, 0.01)
      ParticleVelocity(\id, #PB_Particle_Velocity, 0.01)
      ParticleEmitterDirection(\id, 0, 1, 0)
      ParticleSize(\id, 5, 1)
      ParticleEmissionRate(\id, 2700)
      ParticleSpeedFactor(\id, 2)
    EndWith
    ProcedureReturn #True
  EndProcedure
 
  Procedure.i DoStreetAndRoad()
    Protected tex.i
    With ob\road
      \mesh = CreatePlane(#PB_Any, 1000, 1000, 10, 10, 1, 1)
      \tex  = CreateTexture(#PB_Any, 512, 512)
      StartDrawing(TextureOutput(\tex))
      Box(0, 0, 512, 512, $484545)
      For x = 0 To 10000
        Plot(Random(511,1), Random(511,1), $737273)
      Next x
      Box(10,0,20,512, $C1C2BF)
      Box(502,0,20,512, $C1C2BF)
      For y = 0 To 9 Step 100
        Box(256, y, 80, 200, $C1C2BF)
      Next y
      StopDrawing()
      \mat = CreateMaterial(#PB_Any, TextureID(\tex))
      ScaleMaterial(\mat, 0.01, 0.01)
      MaterialFilteringMode(\mat, #PB_Material_Trilinear)
      ScrollMaterial(\mat, 0.6,0, #PB_Material_Fixed)
      ScrollMaterial(\mat, 0,-1, #PB_Material_Animated)
      \id = CreateEntity(#PB_Any, MeshID(\mesh), MaterialID(\mat), 0, 0, 0)
      ScaleEntity(\id, 10, 1, 10)
    EndWith
   
    With ob\street
      \mesh = CreateCube(#PB_Any, 0.5)
      \tex  = CreateTexture(#PB_Any, 1000,1000)
      StartDrawing(TextureOutput(\tex))
      Box(0,0, 50, 1000, $978F8A)
      Box(950, 0, 50, 1000, $978F8A)
      Box(51,0, 900, 1000, $59554C)
      StopDrawing()
      \mat = CreateMaterial(#PB_Any, TextureID(\tex))
      \id  = CreateEntity(#PB_Any, MeshID(\mesh), MaterialID(\mat), -300, -20, 0)
     
      ScaleEntity(\id, 700,100, 10000)
      \id2 = CopyEntity(\id, #PB_Any)
      MoveEntity(\id2, 300,-20,0)
    EndWith
    ProcedureReturn #True
  EndProcedure
 
  Procedure.i CreateBuildingTexture()
    Protected tex.i, ColorGrey.i
    tex = CreateTexture(#PB_Any, 512, 512)
    StartDrawing(TextureOutput(tex))
    ColorGrey = Random(100,40)
    Box(0, 0, 512, 512, RGB(ColorGrey, ColorGrey, ColorGrey))
    Box(0, 0, 512, 30, $8B9075)
    For y = 60 To 460 Step 20
      For x = 0 To 512 Step 20
        ColorGrey = Random(255)
        Box(x,y, 10, 10, RGB(ColorGrey, ColorGrey, ColorGrey))
      Next x
    Next y
    Box(0, 480, 512, 32, $8B9075)
    For x = 10 To 512 Step 80
      Box(x, 484, 15, 30, $555555)
    Next x
    StopDrawing()
    ProcedureReturn tex
  EndProcedure
 
  Procedure.i DoBuildings()
    Protected  scalex.i, scaley.i, scalez.i
    For z = 0 To 5
      AddElement(ob\build())
      With ob\build()
        \mesh = CreateCube(#PB_Any, 150)
        \tex  = CreateBuildingTexture()
        \mat  = CreateMaterial(#PB_Any, TextureID(\tex))
        \id   = CreateEntity(#PB_Any, MeshID(\mesh),MaterialID(\mat), -180, 100, 0-z*300)
        \id2  = CopyEntity(\id, #PB_Any)
        MoveEntity(\id2, 180, 100, 0-z*300)
      EndWith
    Next z
    ProcedureReturn #True
  EndProcedure
 
  Procedure.i DoClouds()
    Protected tex.i, Col.i
    With ob\clouds
      \mesh = CreatePlane(#PB_Any, 5000, 5000, 10, 10, 1, 1)
      \tex  = CreateTexture(#PB_Any, 1024, 1024)
      StartDrawing(TextureOutput(\tex))
      DrawingMode(#PB_2DDrawing_AlphaBlend)
      For y = 0 To 4000
        ypos = Random(922,101)
        xpos = Random(922, 101)
        For x = 0 To 200
          Col.i = Random(100,40)
          Plot(RandomI(xpos-100, xpos+100), RandomI(ypos-100, ypos+100),RGBA(Col,Col,Col, Random(150,50)))
        Next x
      Next y
      StopDrawing()
     
      tex  = CreateTexture(#PB_Any, 1024, 1024)
      StartDrawing(TextureOutput(tex))
      DrawingMode(#PB_2DDrawing_AlphaBlend)
      For y = 0 To 4000
        xpos = Random(922, 101)
        ypos = Random(922, 101)
        For x = 0 To 200
          Col.i = Random(100,40)
          Plot(RandomI(xpos-100, xpos+100), RandomI(ypos-100, ypos+100),RGBA(Col,Col,Col, Random(150,50)))
        Next x
      Next y
      StopDrawing()
     
      \mat = CreateMaterial(#PB_Any, TextureID(\tex))
      ScaleMaterial(\mat, 5, 5)
      ScrollMaterial(\mat, -0.0002, 0.0002, #PB_Material_Animated)
     
      AddMaterialLayer(\mat, TextureID(tex),#PB_Material_Add)
     
      ScaleMaterial(\mat, 20, 20, 1)
      ScrollMaterial(\mat, 0.0002, 0.0002, #PB_Material_Animated, 1)
     
      \id = CreateEntity(#PB_Any, MeshID(\mesh), MaterialID(\mat), 0, 200, 200)
     
      RotateEntity(\id, 180, 0, 0)
    EndWith
    ProcedureReturn #True
  EndProcedure
 
  Procedure.i CheckBuild()
    ForEach ob\build()
      If EntityZ(ob\build()\id,#PB_Absolute) > 180
        MoveEntity(ob\build()\id, -180, 100, -1600, #PB_Absolute)
        MoveEntity(ob\build()\id2, 180, 100, -1600, #PB_Absolute)
      Else
        MoveEntity(ob\build()\id, 0, 0, 1,#PB_Relative)
        MoveEntity(ob\build()\id2, 0, 0, 1,#PB_Relative)
      EndIf
    Next
  EndProcedure
 
  Procedure.i RunDemo()
    Quit = 0
    ret = DoPouringRain()
    ret = DoStreetAndRoad()
    ret = DoBuildings()
    ret = DoClouds()
   
    Repeat
     
      If FS = #False
        Repeat
          ev = WindowEvent()
          If ev = #PB_Event_CloseWindow
            Quit = 1
          EndIf
        Until  ev = 0
      EndIf
     
      ret = CheckBuild()
     
      RenderWorld()
     
      ExamineKeyboard()
     
      FlipBuffers()
     
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
   
  EndProcedure
 
EndModule

Rain::SetResolution(1920,1080)
Rain::RunDemo()
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Philippe-felixer76-3
User
User
Posts: 45
Joined: Mon Dec 30, 2013 10:12 pm

Re: Rain performance

Post by Philippe-felixer76-3 »

DK_PETER wrote:Any specific reason for not using particles?

Instead of moving billboards, I would use a mesh like this:
Note: Use the flares in the PB textures folders for images for visuals..
viewtopic.php?f=36&t=70132&hilit=mesh

or the particle way:

Way back I made this demo called pouring rain. (Before I began to make 'things big' on a small scale).

Here it is - I've changed the particle's velocity command so the example works with 5.73.

Code: Select all

...
Hi DK_PETER i seen your example, but i realy like to use billboards, i want to add enviroment stuff like wind also.

The mesh way works way better performance wise, i can easy add 20000 points, but they are so small. :(

Mabe you can help me with the following, try this example below and then try it again without commeting the FOG command.

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Billboard
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
;

#CameraSpeed = 2

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY

Structure bb
  bb.l
EndStructure

Global NewList rain.bb()

If InitEngine3D()
 
  InitSprite()
  InitKeyboard()
  InitMouse()
 
  If Screen3DRequester()
   
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/skybox.zip", #PB_3DArchive_Zip)
    Parse3DScripts()
   
    ; First create materials
    ;
   
    GrassMaterial = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any,"grass1.png")))
    MaterialBlendingMode(GrassMaterial, #PB_Material_AlphaBlend)
    DirtMaterial = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any,"Dirt.jpg")))
   
   
    MeshPlane = CreatePlane(#PB_Any, 2000, 2000, 40, 40, 4, 4)
    CreateEntity(#PB_Any, MeshID(MeshPlane), MaterialID(DirtMaterial))
   
    ; Add house
    MeshHouse = LoadMesh(#PB_Any, "tudorhouse.mesh")
    House = CreateEntity(#PB_Any, MeshID(MeshHouse), #PB_Material_None, 0, 280, 0)
    ScaleEntity(House, 0.5, 0.5, 0.5)
   
    ; Rain..
    GrassMaterial1 = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any,"basic_droplet.png")))
    RotateMaterial(GrassMaterial1, 180,  #PB_Material_Fixed)
    MaterialBlendingMode(GrassMaterial1, #PB_Material_Color) ;AlphaBlend)
    Billboard1 = CreateBillboardGroup(#PB_Any, MaterialID(GrassMaterial1), 4, 10, 0, 0, 0, #PB_All, 1)
    BillboardGroupCommonDirection(Billboard1, 0, 1, 0)   
   
    ; ON MY MACHINE IT RUNS FINE WITH 10000 AND IS VERY SLOW ON 20000
    For i = 0 To 10000
      bx.l = Random(2000)-1000
      bz.l = Random(2000)-1000
      y = Random(2000)
      AddElement(rain())
      rain()\bb = AddBillboard(Billboard1, bx, y+6, bz)
    Next i
   
    ; create camera
    Camera = CreateCamera(#PB_Any, 0, 0, 100, 100)
    MoveCamera(Camera, 200, 400, 900, #PB_Absolute)
    CameraLookAt(Camera, 0, 100, 0)
    CameraBackColor(Camera, RGB(255,255,255))
    
    ;Fog($ff8888,100,0,800)
    
    Repeat
      Screen3DEvents()
     
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
     
      If ExamineKeyboard()
       
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed
        Else
          KeyX = 0
        EndIf
       
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed
        Else
          KeyY = 0
        EndIf
       
      EndIf
     
      RotateCamera(Camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (Camera, KeyX, 0, KeyY)
     
      ResetList(rain())
      While NextElement(rain())
        If BillboardY(rain()\bb, Billboard1) - 2.1<0
          bx.l = Random(2000)-1000
          bz.l = Random(2000)-1000
          y = Random(20)+1980
          RemoveBillboard(rain()\bb, Billboard1)
          rain()\bb = AddBillboard(Billboard1, bx, y, bz)
        Else
          MoveBillboard(rain()\bb, Billboard1, 0, -2.1, 0)
        EndIf
      Wend
       
      RenderWorld()
      Screen3DStats()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
 
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End
With enabling fog the #PB_Material_Color doesn't work anymore, how can i get that to work right?
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Rain performance

Post by DK_PETER »

Use fog for horizons distances only. Fog interferes with everything you display.
For other purposes take a look at the MaterialFog() command.

Other options:
1. CompositorEffect(s) - Advanced topic.
2. Fake it with particles, entities or spite(s)

Concerning small points for mesh example:
You've got to save the material script to same location as your code.
Change the following lines:

Code: Select all

texture points.png 
and

Code: Select all

point_size 80

AND concerning particles:
Using particles to simulate weather is by far the best way.. Use ParticleEmitterDirection(), ParticleAngle() and ParticleAcceleration() at required intervals.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Rain performance

Post by DK_PETER »

Small particle snow example - could easily be rain:

Code: Select all

;Weather example using particles

InitEngine3D()
InitSprite()
InitKeyboard()

Structure XYZ
  x.f
  y.f
  z.f
EndStructure


Structure Particle
  id.i
  ma.i
  tx.i
  dir.XYZ
  acc.XYZ
EndStructure
Global w.Particle

Structure Objects
  id.i
  ms.i
  ma.i
  tx.i
EndStructure
Global NewList o.Objects()

Declare.i ChangeSimulationParameters()
Declare.i CreateWeather()
Declare.i CreateScene()
Declare.f RandomF(Min.f, Max.f, Res.i = 100000)

Global ev.i, ret.i, el.i, newval.i

AntialiasingMode(#PB_AntialiasingMode_x4)
OpenWindow(0, 0, 0, 800, 600, "Weather", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 800 * DesktopResolutionX(), 600 * DesktopResolutionY())
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
CreateCamera(0, 0, 0, 100, 100)
CameraBackColor(0, $CCCCCC)
;CreateLight(0, $FFFFFFF, -20, 40, 50)
ret = CreateScene()
ret = CreateWeather()


Repeat
  
  Repeat : ev = WindowEvent() : If ev = #PB_Event_CloseWindow : quit = #True : EndIf : Until  ev = 0
  
  ExamineKeyboard()
  
  RenderWorld()
  If ElapsedMilliseconds() - el > newval
    ret = ChangeSimulationParameters()
  EndIf
  FlipBuffers()
  
Until KeyboardPushed(#PB_Key_Escape) Or quit = #True

Procedure.i ChangeSimulationParameters()
  ;parameters to manipulate particles..
  w\acc\x = RandomF(0.01, 0.1)
  w\acc\y = -RandomF(0.01, 0.1)
  w\acc\z = RandomF(0.01, 0.1)
  w\dir\x = RandomF(-1, 1)
  w\dir\y = -RandomF(0.1, 1)
  w\dir\z = RandomF(-1, 1)
  ParticleAcceleration(w\id, w\acc\x, w\acc\y, w\acc\z)
  ParticleEmitterDirection(w\id, w\dir\x, w\dir\y, w\dir\z)
  newval = Random(10000, 2000)
  el = ElapsedMilliseconds()
  ProcedureReturn #True
EndProcedure

Procedure.i CreateWeather()
  w\id = CreateParticleEmitter(#PB_Any, 100, 100, 100, #PB_Particle_Box, 0, 0, -50)
  w\tx = CreateTexture(#PB_Any, 20, 20, "Snow")
  StartDrawing(TextureOutput(w\tx))
  Box(0, 0, OutputWidth(), OutputHeight(), $0)
  DrawingMode(#PB_2DDrawing_Gradient)
  BackColor($FFFFFF) :FrontColor($000000)
  CircularGradient(10, 10, 9)
  Circle(10, 10, 9)
  StopDrawing()
  w\ma = CreateMaterial(#PB_Any, TextureID(w\tx))
  MaterialBlendingMode(w\ma, #PB_Material_Add)
  MaterialFilteringMode(w\ma, #PB_Material_Trilinear)
  MaterialCullingMode(w\ma, #PB_Material_NoCulling)
  ParticleEmissionRate(w\id, 10000)
  ParticleTimeToLive(w\id, 0.8, 10)
  ParticleMaterial(w\id, MaterialID(w\ma))
  ParticleSize(w\id, 0.3, 0.3)
  ChangeSimulationParameters()
  ProcedureReturn 
EndProcedure

Procedure.i CreateScene()
  Protected x, tx, ma
  tx = LoadTexture(#PB_Any, "snow_1024.jpg")
  ma = CreateMaterial(#PB_Any, TextureID(tx))
  SetMaterialColor(ma, #PB_Material_SelfIlluminationColor, $FFFFFF)
  AddElement(o())
  With o()
    \ms = CreatePlane(#PB_Any, 100, 100, 1, 1, 1, 1)
    \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(ma), 0, -0.2, -45)
  EndWith
  For x = 0 To 20
    AddElement(o())
    With o()
      \ms = CreateSphere(#PB_Any, 2, 10, 10)
      TransformMesh(\ms, 0, 0, 0, RandomF(1,10), RandomF(0.1, 5.5), RandomF(1,10), 0, 0, 0)
      \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(ma), RandomF(-150, 150), 0, RandomF(-140, -50))
    EndWith
  Next x
  ProcedureReturn #True
EndProcedure

;Negative and positive extremes allowed 
Procedure.f RandomF(Min.f, Max.f, Res.i = 100000)
  ProcedureReturn (Min + (Max - Min) * Random(Res) / Res)
EndProcedure
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Philippe-felixer76-3
User
User
Posts: 45
Joined: Mon Dec 30, 2013 10:12 pm

Re: Rain performance

Post by Philippe-felixer76-3 »

DK_PETER wrote:Small particle snow example - could easily be rain:

Code: Select all

...
Thank you very much!

I had the mesh option working with the material file, i could resize the point and change the texture but it's not possible to change the width/height, so the particle option is the way, just wanted to start working on it and i only had smoke working with particle's, but now i have this great example! Tnx!

Another question about the mesh option, is it possible to programmatically add a material file?

And also i sometimes get a crash on exit with my program without any error message, or sometimes, when playing with adding 3d stuff, PB crashes on a plane mesh (terrain) saying it's not initialized, i quess it's a memory problem...
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Rain performance

Post by DK_PETER »

Using the mesh option is not a good approach. You'll see if you run it under opengl.
Everything related to the mesh becomes locked at fixed positions.
Might have a go at it again. Haven't tried it in a long time.

Your crash, that's quite different.
Use

Code: Select all

InitEngine3D(#PB_Engine3D_DebugLog)
Examine the ogre.log file to see if you can find a reason the the crash.
Probably at the end of the log.

Edit:
It's true, you can't change the width/height, that's not the purpose of the sprite_Point.
You can, however just modify the image you wish to use as point sprite.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Philippe-felixer76-3
User
User
Posts: 45
Joined: Mon Dec 30, 2013 10:12 pm

Re: Rain performance

Post by Philippe-felixer76-3 »

DK_PETER wrote:Using the mesh option is not a good approach. You'll see if you run it under opengl.
Everything related to the mesh becomes locked at fixed positions.
Might have a go at it again. Haven't tried it in a long time.

Your crash, that's quite different.
Use

Code: Select all

InitEngine3D(#PB_Engine3D_DebugLog)
Examine the ogre.log file to see if you can find a reason the the crash.
Probably at the end of the log.

Edit:
It's true, you can't change the width/height, that's not the purpose of the sprite_Point.
You can, however just modify the image you wish to use as point sprite.
Examine the log didn't gave me any help, i decided to rewrite everything again and when i was busy adding a CreateLine3D the problem started to play again.

I had a long variable eline.l for the function CreateLine3D with a value of 66666666, so my function looked like this:

init:

eline.l = 66666666

And in the main loop somewhere:

CreateLine3D(eline.l, PosCanon\x*1, PosCanon\y*1, PosCanon\z*1, RGB(0,255,0), PosShoot\x - (Direction\x*20), PosShoot\y- (Direction\y*20), PosShoot\z- (Direction\z*20), RGB(255,0,0))

This resulted in a very unstable program that often crashes on exit and made my plane mesh invalid.
User avatar
Psychophanta
Addict
Addict
Posts: 4997
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Rain performance

Post by Psychophanta »

Screen is not centered in PB6.10
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Post Reply