Easy solution to 'volumetric clouds' using particles

Everything related to 3D programming
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Easy solution to 'volumetric clouds' using particles

Post 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)
Last edited by DK_PETER on Thu Nov 22, 2018 2:19 pm, edited 1 time in total.
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
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Easy solution to 'volumetric clouds' using particles

Post by falsam »

Very cool DK_PETER. Thanks.

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
Mindphazer
Enthusiast
Enthusiast
Posts: 340
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Easy solution to 'volumetric clouds' using particles

Post by Mindphazer »

Works fine on my Mac
Thanks DK_PETER !
MacBook Pro 14" M1 Pro - 16 Gb - MacOS 14 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Easy solution to 'volumetric clouds' using particles

Post by Dude »

Doesn't work anymore; needs InitEngine3D() with flags of which I know not. Can we get an update, DK_PETER?
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Easy solution to 'volumetric clouds' using particles

Post 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
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.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Easy solution to 'volumetric clouds' using particles

Post by Dude »

DK_PETER wrote:Try using 'OpenGL' in as subsystem
Thanks, that made it work. :)
Post Reply