December 03, 2020, 08:58:50 PM

Author Topic: [bb] Full Screen Motion Blur by Jeremy Alessi [ 1+ years ago ]  (Read 635 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Full Screen Motion Blur
Author : Jeremy Alessi
Posted : 1+ years ago

Description : This uses 2D in 3D sprites (thanks to Rob) attached to the camera, and cycles 3 textures to create a full screen motion blur.

The comments should explain how to use the code. Basically call SetupBlurCamera() right after you create your main viewport camera and then call BlurScreen() before RenderWorld. By toying with the blursev you can alter the effect. You can further alter it by changing the blur_magnitude in BlurScreen().  ShowBlur() and HideBlur() have been added so that you control when the blur effect is on or off.  You must call ShowBlur() now before the blur will take effect.


Code :
Code: BlitzBasic
  1. ;====== Full Screen Motion Blur =================================================================
  2. ;====== PROGRAMMED BY JEREMY ALESSI =============================================================
  3. ;====== Requires Rob's Sprite Functions =========================================================
  4.  
  5. ;====== SPRITE FUNCTIONS IF YOU NEED THEM =======================================================
  6. ;====== IF NOT JUST COMMENT THEM OUT ============================================================
  7. ;fov is the same as your CameraZoom.
  8. Function Sprite2D(sprite,x#,y#,fov#)
  9.         PositionEntity sprite,2*(x#-320),-2*(y#-240),fov#*640
  10. End Function
  11.  
  12. ;scale sprite in screen pixels relative to a 640x480 res when used with Sprite2D
  13.  
  14. Function ScaleSprite2(sprite,x,y)
  15.         ScaleEntity sprite,x,y,1
  16. End Function
  17. ;please pass camera to this function Or 0 for a billboard Type with mesh.
  18.  
  19. Function CreateSprite2(parent)
  20.         If parent<>0
  21.                 m=CreateMesh(parent)
  22.         Else
  23.                 m=CreateMesh()
  24.         EndIf
  25.         s=CreateSurface(m)
  26.         AddVertex s,-1,+1,-1,0,0:AddVertex s,+1,+1,-1,1,0
  27.         AddVertex s,+1,-1,-1,1,1:AddVertex s,-1,-1,-1,0,1
  28.         AddTriangle s,0,1,2:AddTriangle s,0,2,3
  29.         ScaleEntity m,100,100,1
  30.         Return m
  31. End Function
  32. ;================================================================================================
  33.  
  34. ;====== GLOBAL VARIABLES ========================================================================
  35. Global blur, blurHud
  36. ;================================================================================================
  37.  
  38. ;====== CALL THIS TO SET UP BLUR CAMERA, CHILDREN, AND TEXTURES =================================
  39. ;====== CALL IT AFTER SETTING UP THE MAIN CAMERA AND THEN PASS THE CAMERA =======================
  40. ;====== BLUR_SEVERITY BECOMES WORSE WITH HIGHER NUMERICAL ALPHA LEVELS ==========================
  41. ;====== SET THE SEVERITY AND THE FALLOFF VALUES SO THAT ALL 3 BLUR HUDS TOGETHER ARE STILL ======
  42. ;====== TRANSPARENT =============================================================================
  43. Function SetupBlurCamera(blurCamera, blurSev# = 0.5)
  44.        
  45.         blur = CreateTexture( Float(GraphicsWidth()), Float(GraphicsWidth()), 256)
  46.        
  47.         widthRatio# = TextureWidth(blur) / Float(GraphicsWidth())
  48.         heightRatio# = TextureHeight(blur) / Float(GraphicsHeight())
  49.        
  50.         ScaleTexture(blur,widthRatio#,heightRatio#)
  51.         blurHud=CreateSprite2(blurCamera)
  52.         NameEntity(blurHud,"blurHud")
  53.         EntityOrder(blurHud,-1000)
  54.         EntityFX(blurHud,1 + 8 + 16)
  55.         ScaleSprite2(blurHud,640,480)
  56.         Sprite2D(blurHud,320,240,1)
  57.         EntityAlpha(blurHud,blurSev#)
  58.         EntityTexture(blurHud, blur)
  59.        
  60. End Function
  61. ;================================================================================================
  62.  
  63. ;====== BLUR FUNCTION, CALL BEFORE RENDERWORLD ==================================================
  64. ;====== PASS THE CAMERA TO IT, HOW MUCH DURATION BETWEEN LATENT IMAGES OR JUST HOW MUCH BLUR ====
  65. ;====== AND WHETHER YOU WANT TO COPY FROM THE FRONT OR BACK BUFFER 1=FRONT 2=BACK ===============
  66. ;====== BE SURE TO CALL ShowBlur() BEFORE USING THIS OR THE BLUR WILL NOT APPEAR ================
  67. ;====== YOU CAN ALSO USE HideBlur() SO THE EFFECT CAN BE CONTROLLED FOR A CERTAIN MODE ==========
  68. Function BlurScreen(blurCamera,blurSev# = 0.5,whichBuffer = 1)
  69.         EntityAlpha(blurHud, blurSev#)
  70.         Select whichBuffer
  71.                 Case 1
  72.                         CopyRect( 0, 0, GraphicsWidth(), GraphicsHeight(), 0, 0, FrontBuffer(), TextureBuffer(blur) )
  73.                
  74.                 Case 2
  75.                         CopyRect( 0 , 0, GraphicsWidth(), GraphicsHeight(), 0, 0, BackBuffer(), TextureBuffer(blur) )
  76.                                        
  77.         End Select
  78.  
  79. End Function
  80. ;================================================================================================
  81.  
  82. ;====== HIDE THE BLUR EFFECTS ===================================================================
  83. Function HideBlur()
  84.         HideEntity(blurHud)
  85. End Function
  86. ;================================================================================================
  87.  
  88. ;====== SHOW THE BLUR EFFECTS ===================================================================
  89. Function ShowBlur()    
  90.         ShowEntity(blurHud)
  91. End Function
  92. ;================================================================================================


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal