Ooops
November 24, 2020, 07:15:03 AM

Author Topic: [bb] Realtime Color-filter by Kryzon [ 1+ years ago ]  (Read 685 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Realtime Color-filter by Kryzon [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Realtime Color-filter
Author : Kryzon
Posted : 1+ years ago

Description : By using an overlay mesh with a Multiply blend mode you can select which colors are visible on screen and which aren't, just like a filter in real life.

You control how much value of a certain RGB channel is able to reach the viewer's eye depending on the way you color the filter mesh.
Something like [255,0,0] would only allow the red color to pass, and thus this could be called a "red filter" RGB.
You can use other RGB combinations to allow other shades of filter.

Note this demo uses a full-screen filter, but you can scale the quad to fit any portion of it and it will still filter out colors - say, for a GUI element that has a small camera viewport, like an inventory viewer.

PS: VariousOtherThings() function by Floyd.

PS.2: This filter effect is most likely ported to BMax using the SHADEBLEND blend flag.


Code :
Code: BlitzBasic
  1. Graphics3D 800,600,0,2
  2.  
  3. Local camera = CreateCamera()
  4.         PositionEntity camera,0,5,-5
  5.         RotateEntity camera,45,0,0
  6.        
  7. Local filterQuad = CreateFilterQuad(camera)
  8.        
  9. LightRange CreateLight(2),3
  10. AmbientLight 100,100,100
  11.  
  12. VariousOtherThings 30
  13.  
  14. Local fpsTimer = CreateTimer(60)
  15. Const FILTER_SPEED# = 0.01
  16. Local filterStrength# = 1.0
  17. Local activeColors$ = ""
  18.  
  19. Local redPass% = 1, greenPass% = 0, bluePass% = 0
  20.  
  21. While Not KeyHit(1)
  22.         WaitTimer(fpsTimer)
  23.  
  24.         filterStrength = filterStrength + (KeyDown(30)*FILTER_SPEED) - (KeyDown(44)*FILTER_SPEED)
  25.         If filterStrength > 1.0 Then filterStrength = 1.0
  26.         If filterStrength < 0.0 Then filterStrength = 0.0
  27.  
  28.         ;Possible filter colors:
  29.        
  30.         ;RED: 255,0,0
  31.         ;YELLOW: 255,255,0
  32.         ;GREEN: 0,255,0 (Great for night-vision scenes.)
  33.         ;CYAN: 0,255,255
  34.         ;BLUE: 0,0,255
  35.         ;PINK: 255,0,255
  36.        
  37.         ;(You can use any values lower than 255, but that may darken the screen a bit.)
  38.        
  39.         activeColors = ""
  40.         If (KeyHit(2) + KeyHit(79)) Then redPass = 1 - redPass
  41.         If redPass Then activeColors  = activeColors + "RED"
  42.        
  43.         If (KeyHit(3) + KeyHit(80)) Then greenPass = 1 - greenPass
  44.         If greenPass Then activeColors = activeColors + " GREEN"
  45.        
  46.         If (KeyHit(4) + KeyHit(81)) Then bluePass = 1 - bluePass
  47.         If bluePass Then activeColors = activeColors + " BLUE"
  48.        
  49.         If (redPass+greenPass+bluePass = 0) Then activeColors = "NONE"
  50.         If (redPass+greenPass+bluePass = 3) Then activeColors = "ALL"
  51.  
  52.         ;Update filter color.
  53.         SetFilterColor(filterQuad, filterStrength, 255*redPass, 255*greenPass, 255*bluePass)
  54.  
  55.         RenderWorld()
  56.         Text 10,10,"Use the A or Z keys to control the intensity of the filter."
  57.         Text 10,30,"Use the 1, 2 and 3 keys to toggle the colors of the filter."
  58.         Text 10,60,"Active colors: "+activeColors
  59.         Text 10,80,"Filter intensity: ["+Int(filterStrength*100)+" %]"
  60.        
  61.         Flip
  62. Wend
  63.  
  64. ;Creates a full screen quad.
  65. Function CreateFilterQuad(parent=0)
  66.         Local mesh = CreateMesh(parent)
  67.         Local surf = CreateSurface(mesh)
  68.        
  69.         AddVertex surf,-0.5,0.5,0
  70.         AddVertex surf,0.5,0.5,0
  71.         AddVertex surf,-0.5,-0.5,0
  72.         AddVertex surf,0.5,-0.5,0
  73.        
  74.         AddTriangle surf,0,1,3
  75.         AddTriangle surf,0,3,2
  76.        
  77.         EntityColor mesh,255,255,255
  78.        
  79.         EntityFX mesh,1 ;Fullbright FX
  80.         EntityBlend mesh,2 ;Multiply
  81.        
  82.         ScaleMesh mesh,4,3,1 ;Scale the quad mesh with a 4:3 ratio, to fill the entire screen.
  83.         MoveEntity mesh,0,0,2 ;Move quad mesh so it fills the entire screen of the 4:3 camera.
  84.        
  85.         Return mesh
  86. End Function
  87.  
  88. Function SetFilterColor(filterMesh, fVal#=1.0, r#=0, g#=0, b#=0)
  89.         ;Interpolate from [255,255,255] (which is white, no filter) to [r,g,b] supplied by the user.
  90.         EntityColor filterMesh, 255.0 - (255.0-r)*fVal, 255.0 - (255.0-g)*fVal, 255.0 - (255.0-b)*fVal
  91. End Function
  92.  
  93. ;Aesthetics. Function by Floyd.
  94. Function VariousOtherThings( quantity )
  95.         For n = 1 To quantity
  96.                 Select Rand( 1, 4 )
  97.                         Case 1 : temp = CreateSphere()
  98.                         Case 2 : temp = CreateCone()
  99.                         Case 3 : temp = CreateCylinder()
  100.                         Case 4 : temp = CreateCube()
  101.                 End Select
  102.                 ScaleEntity temp, Rnd( 0.6, 1.5 ), Rnd( 0.6, 1.5 ), Rnd( 0.6, 1.5 )
  103.                 EntityColor temp, Rand( 100, 255 ), Rand( 100, 255 ), Rand( 100, 255 )
  104.                 RotateEntity temp, Rnd( -20, 20 ), Rnd( -50, 50 ), Rnd( -20, 20 )
  105.                 angle# = Rnd( -45, 225)
  106.                 dist#  = Rnd( 2.5, 6 )
  107.                 PositionEntity temp, dist * Cos( angle ), Rnd( - 3, 3 ), dist * Sin( angle )
  108.         Next
  109. End Function
  110.  
  111. End


Comments :


BlitzSupport(Posted 1+ years ago)

 Nice one!


Kryzon(Posted 1+ years ago)

 I was thinking of possible uses for this. Mainly color grading, narratively: using the filter in a subtle manner, to convey certain sensations to the player.


Bobysait(Posted 1+ years ago)

 Simple and average, well done !To be sure you won't get into trouble using some blitz3d effects, like fog, you should enable the "+8" on the filter mesh
Code: [Select]
EntityFX mesh,1+8 ;Fullbright FX
And to assert your mesh is drawn in front of everything that could be in the "near plane" of the camera :
Code: [Select]
EntityOrder mesh, -999
and by the way :<div class="quote"> ScaleMesh mesh,4,3,1 ;Scale the quad mesh with a 4:3 ratio, to fill the entire screen.   MoveEntity mesh,0,0,2 ;Move quad mesh so it fills the entire screen of the 4:3 camera. </div>actually, you just need a quad that fullfill the screen, so, no matter if it is larger than the screen ...Then, if you really want generic stuff :- just enlarge the mesh, no matter if it's larger than the screen.- or use the Float(GraphicsHeight())/GraphicsWidth() ratio to define the height of the mesh to really fit the screen (then you won't need to take care of the screen ratio) [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal