Code archives/3D Graphics - Effects/Realistically Modeled Motion Blur Effect

This code has been declared by its author to be Public Domain code.

Download source code

Realistically Modeled Motion Blur Effect by ClayPigeon2011
I noticed that a lot of the motion blur effects in the code archives are actually just trail effects. I made this motion blur effect in order to simulate how motion blur is caused in real life. Motion blur is the byproduct of an object of interest moving during a camera's exposure. I simulate this by overlaying several renders of different RenderWorld tween states. Change the "layers" constant to change the nubmer of blur passes (quality). The higher the number, the faster objects can move without artifacts appearing. Change buffer_size to any power-of-2 number that is smaller than both the width and height of your graphics mode in order to increase the resolution of the blur effect. You can also change the EntityAlpha of the blur quads to make the effect more prominent. I can get a decent framerate on my laptop with these settings, and greater than 60 FPS on my PC with the same settings. Enjoy!

-If you want to pause it to examine the effect, just grab the top bar of the window and if will freeze. :)
AppTitle "Motion Blur"
Graphics3D 640,480,0,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Const layers% = 8

Global buffer_size% = 256

Dim blurtex%(layers-1)
Dim blurquad%(layers-1)

For i = 0 To layers-1
	blurtex(i) = CreateTexture(buffer_size,buffer_size)
Next

Global objecttex = CreateTexture(128,128)
SetBuffer TextureBuffer(objecttex)
Color 255,255,0
Rect 0,0,128,128
Color 0,127,255
Rect 0,0,64,64,True
Rect 64,64,64,64,True
SetBuffer BackBuffer()

Global camera% = CreateCamera()
CameraRange camera,0.1,1000

Global cube% = CreateCube()
PositionEntity cube,0,0,3
EntityTexture cube,objecttex

Global clear% = CreateMesh()
Global surf% = CreateSurface(clear)
AddVertex(surf,-1,1,0)
AddVertex(surf,1,1,0)
AddVertex(surf,1,-1,0)
AddVertex(surf,-1,-1,0)
AddTriangle(surf,0,1,2)
AddTriangle(surf,0,2,3)
EntityColor clear,0,0,0
PositionEntity clear,0,0,1
EntityOrder clear,-1

For i = 0 To layers-1
	blurquad(i) = CreateMesh()
	surf = CreateSurface(blurquad(i))
	AddVertex(surf,-1,1,0,0,0)
	AddVertex(surf,1,1,0,1,0)
	AddVertex(surf,1,-1,0,1,1)
	AddVertex(surf,-1,-1,0,0,1)
	AddTriangle(surf,0,1,2)
	AddTriangle(surf,0,2,3)
	EntityTexture blurquad(i),blurtex(i)
	PositionEntity blurquad(i),0,0,1
	EntityFX blurquad(i),1
	EntityAlpha blurquad(i),0.125
	EntityOrder blurquad(i),-1-i
Next

While Not KeyHit(1)
	Cls
	
	CaptureWorld
	
	TurnEntity cube,4,8,12
	
	UpdateWorld
	CameraViewport camera,0,0,buffer_size,buffer_size
	HideEntity clear
	For i = 0 To layers-1
		HideEntity blurquad(i)
	Next
	CameraViewport camera,0,0,buffer_size,buffer_size
	For i = 0 To layers-1
		RenderWorld (i+1)/Float(layers)
		CopyRect 0,0,buffer_size,buffer_size,0,0,BackBuffer(),TextureBuffer(blurtex(i))
	Next
	CameraViewport camera,0,0,GraphicsWidth(),GraphicsHeight()
	ShowEntity clear
	For i = 0 To layers-1
		ShowEntity blurquad(i)
	Next
	RenderWorld
	
	Flip True
Wend

End

Comments

Subirenihil2011
EntityAlpha blurquad(i),0.125

Should be
EntityAlpha blurquad(i),1.0/layers
Very nicely done.


ClayPigeon2011
I know, I this is just a bare-bones version of it. I made this change after I uploaded it as well as several other changes. If enough people want it, I'll upload a better updated version.


Code Archives Forum