ripple effect

Blitz3D Forums/Blitz3D Programming/ripple effect

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here's something I threw together real quick. It's a function for doing ripples. You give it mesh, frequency, wave size (height), phase, center point, and the normal of the wave, and it takes care of the rest of it for you. Look at the example to see how exactly it works. To make the wave actually "wave" you just increment the phase, 0 to 360. That doesn't work in the example, but it should get you started.

Graphics3D 800,600,32,2
SetBuffer BackBuffer()

Camera=CreateCamera()
PositionEntity Camera,0,2,-5

Light=CreateLight(2)
AmbientLight 60,60,60
LightRange Light,8
LightColor Light,255,200,150
PositionEntity Light,2,4,0

Mesh=CreateQuad(40)
EntityColor Mesh,0,60,180
EntityPickMode Mesh,2
EntityShininess Mesh,0.2

Type Point
	Field x#,y#,z#
End Type

Pt.Point=Null

While Not KeyDown(1)
Cls

	Count#=Count#+90

	PointEntity Camera,Mesh
	If Pt.Point=Null
		If CameraPick(Camera,MouseX(),MouseY())
			If MouseDown(1)
				Pt.Point=New Point	;Wave Center
				Pt\x#=PickedX()
				Pt\y#=PickedY()
				Pt\z#=PickedZ()
				Nm.Point=New Point	;Wave Normal
				Nm\y#=1
				Ripple(Mesh,200,0.2,Count#,Pt,Nm)
				UpdateNormals Mesh				
			EndIf
		EndIf
	EndIf

UpdateWorld
RenderWorld

	If OldTime < MilliSecs()
		OldTime=MilliSecs()+1000
		Fps=FrameCount
		FrameCount=0
	Else
		FrameCount=FrameCount+1
	EndIf
	
	Text 16,16,Fps+" FPS"

Flip
Wend

Function Ripple(Mesh,Freq#,Height#,Phase#,Ct.Point,Nm.Point)
	For Sf=1 To CountSurfaces(Mesh)
		Surf=GetSurface(Mesh,Sf)
		For Vert=0 To CountVertices(Surf)-1
			Vx#=VertexX(Surf,Vert)
			Vy#=VertexY(Surf,Vert)
			Vz#=VertexZ(Surf,Vert)
			TFormPoint Vx#,Vy#,Vz#,Mesh,0
			Vx#=TFormedX()
			Vy#=TFormedY()
			Vz#=TFormedZ()
			Dx#=Ct\x#-Vx#
			Dy#=Ct\y#-Vy#
			Dz#=Ct\z#-Vz#
			Dd#=Sqr((Dx#*Dx#)+(Dy#*Dy#)+(Dz#*Dz#))
			Vx#=Vx#+(Sin(-Phase#+(Dd#*Freq#))*Height#)*Nm\x#
			Vy#=Vy#+(Cos(-Phase#+(Dd#*Freq#))*Height#)*Nm\y#
			Vz#=Vz#+(Sin(-Phase#+(Dd#*Freq#))*Height#)*Nm\z#
			TFormPoint Vx#,Vy#,Vz#,0,Mesh
			Vx#=TFormedX()
			Vy#=TFormedY()
			Vz#=TFormedZ()
			VertexCoords(Surf,Vert,Vx#,Vy#,Vz#)
		Next
	Next
End Function

Function CreateQuad(msDt#,msParent=0)
	Mesh=CreateMesh(msParent)
	Surf=CreateSurface(Mesh)
	For x#=0 To msDt#
	For y#=0 To msDt#
		x2#=x#-(msDt#/2)
		y2#=y#-(msDt#/2)
		v0=AddVertex(Surf,x2#/msDt#,0,y2#/msDt#,(x#)/msDt#,(msDt#-y#)/msDt#)
		v1=AddVertex(Surf,(x2#+1)/msDt#,0,y2#/msDt#,(x#)/msDt#,(msDt#-y#)/msDt#)
		v2=AddVertex(Surf,x2#/msDt#,0,(y2#+1)/msDt#,(x#)/msDt#,(msDt#-y#)/msDt#)
		v3=AddVertex(Surf,(x2#+1)/msDt#,0,(y2#+1)/msDt#,(x#)/msDt#,(msDt#-y#)/msDt#)
		AddTriangle(Surf,v2,v1,v0)
		AddTriangle(Surf,v2,v3,v1)
	Next
	Next
	ScaleMesh Mesh,4,4,4
	UpdateNormals Mesh
	Return Mesh
End Function