Raining outside

Blitz3D Forums/Blitz3D Programming/Raining outside

Neochrome(Posted 2003) [#1]
Has anyone got the code for a simple rain animation on a texture?

re-word: i mean, has anyone got any code that will make rain in 3d OR a animated texture of rain?


_PJ_(Posted 2003) [#2]
There's the ripple-effect thing in the code archives, combined with the Snow/rain/stars code too.
-----------

There's a lot more besides just those too but see what works best for you!


Sunteam Software(Posted 2003) [#3]
I'm not too fussed about sharing this code I've written which will be used in our up and coming RPG. It includes the fogging effect I've been using as well, but you'll need a grayscale image for the fog itself. It's sorta cartoony looking but it can be altered to suit needs, plus it's got splash effects but you'll need a splash image for that too.

You need a 'Cam' object and the water splashes are detected on a collision with EntityType's of 2. Then call init_weather with the required effect (see the constants) to initialise and update_weather for each frame. Call clear_weather to clear all entities but this doesn't clear the actual textures used.

;===================================================
;	<untitled> RPG, (c)2003 Sunteam Software
;	INCLUDE FILE: atmospherics_include.bb
;===================================================

; Weather flags
;----------------
Const w_clear=0	; = clear
Const w_rain=1	; = rain
Const w_fog=2 	; = fog

Const num_droplets%=40	; max number of rain droplets
Const fog_range%=150		; maximum fog range
Const rain_xval%=40
Const rain_zval%=100

Global fog_sphere1%=0		; sphere for fog effect
Global weather_limit%=0		; current weather

; fog texture
Global fog_tex1%=LoadTexture("fx\fog.png")
TextureBlend fog_tex1,2
Global fogt1u#=0.0,fogt1v#=0.0,fogt1a#=0.0
Global fog_tex2%=LoadTexture("fx\fog.png")
Global fogt2u#=0.5,fogt2v#=0.5,fogt2a#=0.0
TextureBlend fog_tex2,2
PositionTexture fog_tex2,0.5,0.5

; rain splash_tex texture
Global splash_tex%=LoadSprite("fx\splash.bmp")
SpriteViewMode splash_tex,2
HideEntity splash_tex
TurnEntity splash_tex,90,0,0

Type rain_droplet
	Field id%,vis%
End Type

Type rain_splash
	Field id%,r#,a#
End Type

Function init_weather(wstart%)
	Local n%,r.rain_droplet
	weather_limit=wstart
	If wstart And w_rain Then 
		rain_precip=100
		num_raindrops=num_droplets
		For n=1 To num_droplets
			r=New rain_droplet
			r\id=CreateCube()
			r\vis=1
			ScaleEntity r\id,0.15,0.5,0.15
			EntityColor r\id,100,100,255
			EntityAlpha r\id,0.3
			PositionEntity r\id,EntityX(Cam,True)+Rnd(-rain_xval,rain_xval),EntityY(Cam,True)+Rnd(-100,40),EntityZ(Cam,True)+Rnd(0,rain_zval)
			TurnEntity r\id,180,0,0
			EntityType r\id,col_rain
			EntityRadius r\id,0.15,0.5
		Next
	EndIf
	If wstart And w_fog Then 
		fog_precip=50
		fog_sphere1=CreateSphere()
		ScaleEntity fog_sphere1,200,40,fog_range/4
		EntityTexture fog_sphere1,fog_tex1,0,0
		EntityTexture fog_sphere1,fog_tex2,0,1
		FlipMesh fog_sphere1
		EntityAlpha fog_sphere1,0.4
		EntityShininess fog_sphere1,0.5
		PositionEntity fog_sphere1,EntityX(Cam,True),EntityY(Cam,True),EntityZ(Cam,True)
		RotateEntity fog_sphere1,-30,180,0
		EntityParent fog_sphere1,Cam
		CameraFogMode Cam,1
		CameraFogRange Cam,10,fog_range
	EndIf
End Function

Function update_weather()
	Local r.rain_droplet,s.rain_splash
	If weather_limit And w_rain Then
		For r=Each rain_droplet
			PositionEntity r\id,EntityX(r\id),EntityY(r\id)-2,EntityZ(r\id)
			If EntityY(r\id,True)<EntityY(Cam,True)-100 Then PositionEntity r\id,EntityX(r\id,True),EntityY(Cam,True)+40,EntityZ(r\id,True),True
			If EntityX(r\id,True)<EntityX(Cam,True)-rain_xval Then PositionEntity r\id,EntityX(Cam,True)+rain_xval,EntityY(r\id,True),EntityZ(r\id,True),True
			If EntityX(r\id,True)>EntityX(Cam,True)+rain_xval Then PositionEntity r\id,EntityX(Cam,True)-rain_xval,EntityY(r\id,True),EntityZ(r\id,True),True
			If EntityZ(r\id,True)<EntityZ(Cam,True) Then PositionEntity r\id,EntityX(r\id,True),EntityY(r\id,True),EntityZ(Cam,True)+rain_zval,True
			If EntityZ(r\id,True)>EntityZ(Cam,True)+rain_zval Then PositionEntity r\id,EntityX(r\id,True),EntityY(r\id,True),EntityZ(Cam,True),True
			If CountCollisions(r\id)>0 Then
				ResetEntity r\id
				s=New rain_splash
				s\id=CopyEntity(splash_tex)
				ShowEntity s\id
				s\r=0.2
				PositionEntity s\id,EntityX(r\id,True),EntityY(r\id,True)+1,EntityZ(r\id,True)
				PositionEntity r\id,EntityX(Cam,True)+Rnd(-rain_xval,rain_xval),EntityY(Cam,True)+Rnd(-100,40),EntityZ(r\id,True),True
				s\a=0.6-((1.0-((EntityY(cam,True)-EntityY(s\id,True))/60.0))/2)
				ScaleSprite s\id,s\a,s\a
				EntityAlpha s\id,s\a
			EndIf
		Next
		For s=Each rain_splash
			s\r=s\r+0.05
			s\a=s\a-0.02
			If s\a<0.1 Then
				FreeEntity s\id
				Delete s
			Else
				ScaleSprite s\id,s\r,s\r
				EntityAlpha s\id,s\a
			EndIf
		Next
	EndIf
	If weather_limit And w_fog Then
		fogt1u=fogt1u+0.0001
		fogt1v=fogt1v+0.0001
		fogt2u=fogt2u-0.0001
		fogt2v=fogt2v-0.0001
		If fogt1u>1.0 Then fogt1u=fogt1u-1.0
		If fogt1v>1.0 Then fogt1v=fogt1v-1.0
		If fogt2u<0.0 Then fogt2u=fogt2u+1.0
		If fogt2v<0.0 Then fogt2v=fogt2v+1.0
		PositionTexture fog_tex1,fogt1u,fogt1v
		PositionTexture fog_tex2,fogt2u,fogt2v
		fogt1a=fogt1a+0.0005
		fogt2a=fogt2a-0.0005
		If fogt1a>360.0 Then fogt1a=fogt1a-360.0
		If fogt2a<0.0 Then fogt2a=fogt2a-360.0
		RotateTexture fog_tex1,fogt1a
		RotateTexture fog_tex2,fogt2a
	EndIf
End Function

Function clear_weather()
	Local r.rain_droplet
	For r=Each rain_droplet
		FreeEntity r\id
		Delete r
	Next
	If weather_limit And w_fog Then CameraFogMode Cam,0
End Function


It probably needs optimising as well but for the moment I get excellent frame rates with it :)

Enjoy.