Page 1 of 1

Easy solution to 'volumetric clouds' using particles

Posted: Tue Apr 04, 2017 10:37 pm
by DK_PETER
For PB 5.60+ only
Edit: Use your own images and save them to the same location as the code.
Images should be named as:

1.png, 2.png, 3.png, 4.png (clouds)
flatland.jpg (ground)

Now...Here's the code:

Code: Select all

;Simple and efficient solution to 'volumetric' clouds using particles
;By DK_Peter
;------------------------------------------
UsePNGImageDecoder()
UseJPEGImageDecoder()

InitEngine3D()
InitSprite()
InitKeyboard()

Structure _Object
  id.i
  ma.i
  ms.i
  tx.i
EndStructure
Global sun._Object, earth._Object

Structure _Par
  id.i
  ma.i
  tx.i
EndStructure
Global Dim p._Par(4), x.i, elap.i, elevate.f = -3.5

ExamineDesktops()
OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "Clouds - use arrow keys up and down", #PB_Window_SystemMenu|#PB_Window_BorderLess)
OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0))
Add3DArchive(".", #PB_3DArchive_FileSystem)
WorldShadows(#PB_Shadow_TextureAdditive, 10, $00AAD3, 1024)
CreateCamera(0, 0, 0, 100, 100)
CameraFOV(0, 40)
CameraBackColor(0, $FF9121)
MoveCamera(0, 0, elevate, 0)

For x = 1 To 4
  With p(x)
    \id = CreateParticleEmitter(#PB_Any, 1000, 0, 100, #PB_Particle_Box, 0, 0, -130)
    \tx = LoadTexture(#PB_Any, Str(x) + ".png")
    \ma = CreateMaterial(#PB_Any, TextureID(\tx))
    MaterialFilteringMode(\ma, #PB_Material_Anisotropic, 8)
    MaterialBlendingMode(\ma, #PB_Material_AlphaBlend)
    AddMaterialLayer(\ma, TextureID(\tx), #PB_Material_AlphaBlend)
    AddMaterialLayer(\ma, TextureID(\tx), #PB_Material_Modulate)
    ScrollMaterial(\ma, -0.01, 0, #PB_Material_Animated,2)
    MaterialFog(\ma, $FFFFFF, 200, 0, 300)
    ParticleMaterial(\id, MaterialID(\ma))
    ParticleEmissionRate(\id, 100)
    ParticleEmitterDirection(\id, 0, 0, 1)
    ParticleVelocity(\id, #PB_Particle_MinimumVelocity, 4)
    ParticleVelocity(\id, #PB_Particle_Velocity, 9)
    ParticleVelocity(\id, #PB_Particle_MaximumVelocity, 15)
    ParticleEmitterAngle(\id, 0)
    ParticleColorRange(\id, $00B7E4, $FFFFFF)
    ParticleTimeToLive(\id, 30, 50)
    ParticleColorFader(\id, -0.1, 0.2, 0.2, 0.1)
    ParticleSize(\id, 50, 15)
  EndWith
Next x

earth\ms = CreatePlane(#PB_Any, 1, 1, 1, 1, 1, 1)
earth\tx = LoadTexture(#PB_Any, "flatland.jpg")
earth\ma = CreateMaterial(#PB_Any, TextureID(earth\tx))
MaterialFilteringMode(earth\ma, #PB_Material_Anisotropic, 16)
ScaleMaterial(earth\ma, 0.4, 0.4)
ScrollMaterial(earth\ma, 0, -0.01, #PB_Material_Animated)
earth\id = CreateEntity(#PB_Any, MeshID(earth\ms), MaterialID(earth\ma), 0, -100, -100)
ScaleEntity(earth\id, 3000, 1, 3000)

Repeat
  
  Repeat
    ev = WindowEvent()
  Until ev = 0
  
  MoveCamera(0, 0, elevate, 0, #PB_Absolute)
  
  RenderWorld()
  
  FlipBuffers()
  
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Up) And elevate < 3.5
    elevate + 0.1
  ElseIf KeyboardPushed(#PB_Key_Down) And elevate > -3.5
    elevate - 0.1
  EndIf
Until  KeyboardPushed(#PB_Key_Escape)

Re: Easy solution to 'volumetric clouds' using particles

Posted: Tue Apr 04, 2017 11:33 pm
by falsam
Very cool DK_PETER. Thanks.

Re: Easy solution to 'volumetric clouds' using particles

Posted: Wed Apr 05, 2017 8:24 am
by Mindphazer
Works fine on my Mac
Thanks DK_PETER !

Re: Easy solution to 'volumetric clouds' using particles

Posted: Mon Nov 19, 2018 10:52 am
by Dude
Doesn't work anymore; needs InitEngine3D() with flags of which I know not. Can we get an update, DK_PETER?

Re: Easy solution to 'volumetric clouds' using particles

Posted: Thu Nov 22, 2018 1:25 pm
by DK_PETER
Dude wrote:Doesn't work anymore; needs InitEngine3D() with flags of which I know not. Can we get an update, DK_PETER?
Hey dude.

This one and the ocean by pf shadoko works just fine here. (Windows 7 / Windows 10)
There's no need for any parameter flag in InitEngine()

Two things you could try to make it work for you:
1. Try using 'OpenGL' in as subsystem.
2. Try reinstalling DX9c

Re: Easy solution to 'volumetric clouds' using particles

Posted: Thu Nov 22, 2018 9:58 pm
by Dude
DK_PETER wrote:Try using 'OpenGL' in as subsystem
Thanks, that made it work. :)