Ooops
October 28, 2020, 06:18:29 AM

Author Topic: [bb] Lens Flares by Barliesque [ 1+ years ago ]  (Read 1397 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Lens Flares by Barliesque [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Lens Flares
Author : Barliesque
Posted : 1+ years ago

Description : ***DOWNLOAD THE DEMO*** complete with source code and media here:
<a href="http://game.barliesque.com/blitz3d/sun_flares_demo.zip" target="_blank">http://game.barliesque.com/blitz3d/sun_flares_demo.zip[/url] (1.7 MB)

This system produces one of the best lens flares I've seen, IMHO.  It's not quite perfect yet, but I'm working on making it perfect!  :)  
For now, I think this is a good start.

Features:
- Flares change size and intensity based on how directly they're viewed
- Flares are tinted, based on the colour of the light source (great for stained glass windows!)



All code and media included here are available for you to use as you please.  If you do use it in a project, a small credit would be quite nice.  Also, if you produce your own flares image or come up with some good changes, please post them here.

Special Thanks:
- SyntaxError for "Sprite Control"
- Fredborg for the little landscape that appears in this demo
- BloodLocust for the simple lens flare code, which provided a start


Code :
Code: BlitzBasic
  1. ;=======================================================================================
  2. ;  "SUN FLARES"
  3. ;  by David Barlia (a.k.a. Barliesque)
  4. ;  david[at]barliesque[dot]com
  5. ;
  6. ;  Please feel free to make use of this code as you wish.
  7. ;  If you do use it in a project, a little credit would be nice.
  8. ;  Also, if you should happen to make some exciting improvements,
  9. ;  don't hesitate to post your changes (or alternate flares image?)
  10. ;  to the code archives.
  11. ;
  12. ;  This code makes use of a modified version of "Sprite COntrol" by SyntaxError.
  13. ;  If you are already using "Sprite Control" in your project, and want to add
  14. ;  this code, you will probably have no problem changing to this modified version.
  15. ;  See "Sprite Control.bb" for further info.
  16. ;
  17. ;=======================================================================================
  18.  
  19. AppTitle = "Sun Flares Demo"
  20. HidePointer
  21.  
  22.  
  23. graphics3D 640,480,32,1
  24.  
  25.  
  26. Include "Sprite Control.bb"
  27.  
  28.  
  29. Global camera, player
  30. Global cam_pitch#, cam_yaw#
  31. Global mvx#,mvy#,mvz#,targetpitch#,targetyaw#
  32.  
  33. Global playerheight#=32         ;height of collision sphere
  34.  
  35. Global FrameTime, FPS#=80.0
  36.  
  37. ;-------------------------
  38. ; Set up the Camera/Player
  39. ;-------------------------
  40. setbuffer Backbuffer()
  41. player=CreatePivot()
  42. camera=CreateCamera(player)
  43. RotateEntity player,0,180,0
  44. MoveEntity camera,0,playerheight,0
  45.  
  46. camerarange camera,1,5000
  47. CameraFogMode camera,0
  48. CameraFogColor camera,210,200,150
  49. CameraFogRange camera,200,1000
  50. AmbientLight 35,30,40
  51.  
  52. CameraClsMode camera,True,True
  53.  
  54. ClearTextureFilters
  55.  
  56. MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
  57.  
  58.  
  59. ;-------------------------
  60. ; Set up Sprite Control
  61. ; and our flares
  62. ;-------------------------
  63. Global ViewX=GraphicsWidth(),ViewY=GraphicsHeight()
  64. Global ViewAspect# = float(viewx)/float(viewy)
  65.  
  66. global FlareRed, FlareGreen, FlareBlue
  67. dim Flare(15)
  68.  
  69. spritecamera = camera
  70. spritepivot = CreateSpritePivot(spritecamera,1.01)
  71. SetupFlares("Medialens-flares.jpg")
  72.  
  73.  
  74. ;-------------------------
  75. ;  Load the Scene
  76. ;-------------------------
  77. Scene = LoadAnimMesh("MediaScene.b3d")
  78. Sun = FindChild(Scene,"Sun")
  79. Sky = FindChild(Scene,"Sky")
  80. SkyTurn# = 0.0
  81.  
  82.  
  83. ;----------------------------------------------
  84. ;  Set PickModes...
  85. ;    Use 1 for flare sources
  86. ;    Use 2 for objects that can block the sun
  87. ;----------------------------------------------
  88. restore PickSettings
  89. repeat
  90.                 read ChildName$, PickMode
  91.                 if ChildName<>"" then EntityPickMode FindChild(scene,ChildName$),PickMode
  92. until ChildName=""
  93.  
  94.  
  95.  
  96. .PickSettings
  97. data "Sun", 1
  98. data "Landscape", 2
  99. data "Stand 1 Upper", 2
  100. data "Stand 1 Lower", 2
  101. ;data "Stand 1 Frame", 2
  102. data "Stand 2 Upper", 2
  103. data "Stand 2 Lower", 2
  104. ;data "Stand 2 Frame", 2
  105. data "Stand 3 Upper", 2
  106. data "Stand 3 Lower", 2
  107. ;data "Stand 3 Frame", 2
  108. data "Stand 4 Upper", 2
  109. data "Stand 4 Lower", 2
  110. ;data "Stand 4 Frame", 2
  111. data "", 0
  112.  
  113.  
  114.  
  115. ;MAIN LOOP * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  116.  
  117. While Not KeyHit(1)
  118.         PlayerControls()
  119.  
  120.         rate# = RatePerFrame#(0.0005, 240.0)   
  121.         skyTurn = SkyTurn + rate
  122.         if SkyTurn>=360.0 then skyTurn=SkyTurn-360.0
  123.         TurnEntity Sky,0,SkyTurn,0
  124.  
  125.         PointEntity Sun, Camera
  126.  
  127.         UpdateFlare(camera, Sun)
  128.  
  129.         Sync()
  130.  
  131. Wend
  132.  
  133. End
  134.  
  135. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  136.  
  137. function Sync()
  138.  
  139.    UpdateWorld
  140.         RenderWorld
  141.  
  142.         time = millisecs() - FrameTime
  143.         FrameTime = millisecs()
  144.         FPS = 1000.0/float(time)
  145.  
  146.         text 50,0,"FPS: " + FPS
  147.        
  148.         vwait : Flip false
  149.  
  150. end function
  151.  
  152.  
  153. function RatePerFrame#(delta# = 1.0, secs# = 1.0)
  154.         rate# = delta / float(secs * FPS)
  155.         return rate#
  156. end function
  157.  
  158. ;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  159.  
  160.  
  161. Function PlayerControls()
  162.  
  163.         ;***
  164.         ;***  Free Look
  165.         ;***
  166.  
  167.         mxspd# = MouseXSpeed()*0.25
  168.         myspd# = MouseYSpeed()*0.25
  169.  
  170.         MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
  171.  
  172.         targetpitch = targetpitch + myspd
  173.         targetpitch = clampvaLUE(targetpitch, -85,85)
  174.         targetyaw = targetyaw - mxspd  
  175.  
  176.         cam_pitch = cam_pitch + (targetpitch - cam_pitch)/8.0
  177.         cam_yaw = cam_yaw + (targetyaw - cam_yaw)/8.0
  178.        
  179.         RotateEntity player,0,cam_yaw,0
  180.         RotateEntity camera,cam_pitch,0,0
  181.  
  182.         ;***
  183.         ;***  Movement
  184.         ;***
  185.        
  186.         If KeyDown(203) Then mvx=mvx-.25
  187.         If KeyDown(205) Then mvx=mvx+.25
  188.         If KeyDown(200) Then mvz=mvz+.25
  189.         If KeyDown(208) Then mvz=mvz-.25
  190.        
  191.         If KeyDown(30) Then mvx=mvx-.25
  192.         If KeyDown(32) Then mvx=mvx+.25
  193.         If KeyDown(17) Then mvz=mvz+.25
  194.         If KeyDown(31) Then mvz=mvz-.25
  195.        
  196.         TranslateEntity player,0,mvy,0
  197.         MoveEntity player,mvx,0,mvz
  198.  
  199.         mvx=mvx/1.2
  200.         mvy=mvy/1.2
  201.         mvz=mvz/1.2
  202.        
  203. End Function
  204.  
  205.  
  206. ;-----------------------------------------------------
  207.  
  208. function ClampValue(Original#, low#, high#)
  209.         if Original<low  then return low
  210.         if Original>high then return high
  211.         return Original
  212. end function
  213.  
  214. ;-----------------------------------------------------
  215.  
  216. Function UpdateFlare(cam_entity,source)
  217.  
  218.         cameraProject cam_entity,EntityX(source,True),EntityY(source,True),EntityZ(source,True)
  219.         SourceX# = ProjectedX#()
  220.         SourceY# = ProjectedY#()
  221.         x# = SourceX/viewx
  222.         y# = SourceY/viewy
  223.  
  224.         text 40,0,"X,Y:  " + X + ", " + Y
  225.  
  226.         SeeSource = camerapick(cam_entity,SourceX,SourceY)
  227.         if ((SeeSource = Source) or (SeeSource = 0)) and entityinview(source,cam_entity) and (x>0 And x<=1) And (y>0 And y<=1)
  228.  
  229.       GetFlareColor(cam_entity, source, SourceX, SourceY)
  230.      
  231.                 scale# = ViewX/800.0
  232.                 restore FlareData
  233.                 read TotalFlares               
  234.                 for f=1 to TotalFlares
  235.                         read Distance, FlareSize, ColorInfluence#, Alpha#, Frame
  236.                         flare_x# = SourceX - (((x-0.5)*2.0)*Distance)
  237.                         flare_y# = SourceY - (((y-0.5)*2.0)*(Distance/ViewAspect))
  238.  
  239.                         r = (ColorInfluence * FlareRed   + 255.0*(1.0-ColorInfluence))
  240.                         g = (ColorInfluence * FlareGreen + 255.0*(1.0-ColorInfluence))
  241.                         b = (ColorInfluence * FlareBlue  + 255.0*(1.0-ColorInfluence))
  242.                         entitycolor Flare(f), r,g,b
  243.  
  244.  
  245.  
  246.                         FlareSize = FlareSize * scale * (x + y + (cos(Distance*0.45)/2.0) + 0.5)
  247.                 ResizeImage3D Flare(f),FlareSize,FlareSize
  248.  
  249.                         if lowest#(x,y)<0.1 then
  250.                                 EntityAlpha Flare(f),Alpha * lowest#(x,y)/0.2
  251.                         else
  252.                                 EntityAlpha Flare(f),Alpha
  253.                         endif
  254.  
  255.                         DrawImage3D Flare(f),flare_x,flare_y,Frame
  256.                         ShowEntity Flare(f)
  257.                 next
  258.                
  259.    else
  260.            restore FlareData
  261.            read TotalFlares
  262.            for f=1 to TotalFlares
  263.                         HideEntity Flare(f)
  264.            next
  265.  
  266.    endif
  267.  
  268. End Function
  269.  
  270.  
  271.  
  272. ; FLARE DATA:   Distance, FlareSize, ColorInfluence#, Alpha#, Frame
  273. ;
  274. ; Distance       - Maximum offset from source
  275. ; FlareSize      - Maximum size of flare
  276. ; ColorInfluence - How strongly colour of source affects the flare's colour
  277. ; Alpha          - Maximum alpha level (0.0 to 1.0)
  278. ; Frame          - Frame of the lens flare texture (1 to 16)
  279.  
  280. .FlareData
  281. Data 15        
  282. Data -100, 400, 1.00, 0.40,  1  ;Red Crescent (50% to 0%)
  283. Data  -95,  35, 0.80, 0.40,  2  ;Orange/Yellow Gradient (80% to 0%)
  284. Data    0, 130, 0.60, 1.00,  3  ;Bright Flare Center (100%)
  285. Data   95,  35, 0.35, 0.50,  4  ;Purple Disc (60%)
  286. Data  150,  45, 0.50, 0.30,  5  ;Blue Disc (60%)
  287. Data  120,  70, 0.60, 0.40,  6  ;Blue Gradient (50% to 20%)
  288. Data  200,  20, 0.90, 0.40,  7  ;Orange Disc (80%)
  289. Data  250,   8, 0.10, 1.00, 15  ;Sharp point (100%)
  290. Data  280,  15, 0.15, 1.00,  8  ;Fuzzy Star (100%)
  291. Data  345,  80, 0.90, 0.20,  7  ;Orange Disc (50%)
  292. Data  390,  60, 0.80, 0.60,  7  ;Orange Disc (80%)
  293. Data  395,  30, 0.40, 0.40,  9  ;Green Disc (80%)
  294. Data  460, 100, 0.90, 0.40, 10  ;Orange Gradiant (90% to 0%)
  295. Data  550, 160, 0.70, 0.50, 11  ;Yellow Ring (90%) with Green Gradient (60% to 0%)
  296. Data  800, 350, 0.80, 0.30, 12  ;Rainbow Halo (outside to in:  Red,Orange/Yellow,Violet) (50%)
  297.  
  298. ;-----------------------------------------------------------
  299.  
  300. function SetupFlares(filename$)
  301.  
  302.         FirstFlare = LoadAnimImage3D(filename$,4,4,1)
  303.         restore FlareData
  304.         read TotalFlares
  305.         for i=1 to TotalFlares
  306.                  read Distance, FlareSize, ColorInfluence#, Alpha#, Frame
  307.  
  308.                  Flare(i) = CopyImage3D(FirstFlare)
  309.                
  310.                  ResizeImage3D Flare(i),FlareSize,FlareSize
  311.                  EntityAlpha Flare(i),Alpha
  312.                  EntityBlend Flare(i),3
  313.                  EntityOrder Flare(i),-100-i
  314.                  midhandle3D Flare(i)
  315.                  entitycolor Flare(i),255,255,255
  316.                  HideEntity Flare(i)
  317.         next
  318.         FreeImage3D(FirstFlare)
  319.  
  320. end function
  321.  
  322. ;-----------------------------------------------------------
  323.  
  324. function GetFlareColor(cam_entity, source, SourceX, SourceY)
  325.  
  326.         ;  This function is responsible for the majority of
  327.         ;  resource drain.  For just the sun, it may be an acceptable
  328.         ;  slowdown.  For things like candles and indoor lights
  329.         ;  this feature should probably not be used.
  330.  
  331.         CameraProjMode cam_entity, 0
  332.  
  333.         sample_cam = createcamera()
  334.  
  335.         positionentity sample_cam, EntityX(player,true),playerheight,EntityZ(player,true)
  336.         pointentity sample_cam, source
  337.         CameraProjMode sample_cam, 2
  338.        
  339.         if SourceX<0 then SourceX=0
  340.         if SourceY<0 then SourceY=0
  341.         cameraviewport sample_cam, SourceX, SourceY, 1,1
  342.        
  343.         for f=1 to 15 : hideentity Flare(f) : next
  344.                                        
  345.         updateworld
  346.         RenderWorld
  347.        
  348.         getcolor SourceX,SourceY
  349.         FlareRed   = ColorRed()
  350.         FlareGreen = ColorGreen()
  351.         FlareBlue  = ColorBlue()
  352.         color 255,255,255
  353.  
  354.         CameraProjMode cam_entity, 1
  355.         FreeEntity sample_cam
  356.        
  357. end function
  358.  
  359.  
  360. ;-----------------------------------------------------------
  361.  
  362. function lowest#(val1#, val2#)
  363.  
  364.    if val1<val2 then
  365.                 return val1
  366.    else
  367.       return val2
  368.         endif
  369.        
  370. end function


Comments :


Filax(Posted 1+ years ago)

 Very nice lenflare !!! thanks to release code !!


Barliesque(Posted 1+ years ago)

 Thank you!  :)I just made a small change to correct an error in GetFlareColor() where it was off a little bit from the centre of the flare.positionentity sample_cam, EntityX(camera,true),EntityY(camera,true),EntityZ(camera,true)...changed to:positionentity sample_cam, EntityX(player,true),playerheight,EntityZ(player,true)...though I'm not clear why this would fix it!  :S


napole0n(Posted 1+ years ago)

 Very nice effect!


elseano(Posted 1+ years ago)

 Best lens flare effect I have seen. Ever.


aCiD2(Posted 1+ years ago)

 This is incredible.... Wow *Attempts to shut gob smacked mouth* I can't believe this is open source :) Funkkkyyyy


Bot Builder(Posted 1+ years ago)

 Nice!!!


carpman(Posted 1+ years ago)

 Hi Barliesque,It didn't take you long to get used to Blitz then!!A really nice piece of work.  I've given it a test in one of my space themes and it looks stunning.I'm going to try and write it into my TrainSim, it should give some nice effects for early morning and late evening running.I've managed to get a locomotive working and looking quite good on the Port Daniel line but no coaches yet, so passengers are still a bit irate!!!Return tickets holders are now certain of being returned to where they started from because they never leave in the first place!!!Thanks again.   Carpman


Kel(Posted 1+ years ago)

 Woah, i get 90 fps almost constantly on my pIII 800, GF MX440. quite impressive... i like the effect of the blending colors, superb stuff!


Pedro(Posted 1+ years ago)

 It is brillant.I will use it and I will credit you.thanks


WendellM(Posted 1+ years ago)

 Wow, *very* pretty - thanks for sharing!


Red(Posted 1+ years ago)

 Oh ! Another sweet effect ! - reallistic lens FX- paper pen FX- black light FX- sand of time FX- motion blur FX- glow FX


Barliesque(Posted 1+ years ago)

 Thank you all!  Really great to read all your comments!I look forward to seeing it in use in your projects.<div class="quote"> Woah, i get 90 fps almost constantly on my pIII 800, GF MX440. quite impressive... i like the effect of the blending colors, superb stuff!  </div>Excellent news.  I was wondering how well the flare colouring effects would work with other systems.  I was amazed I could get away with rendering the scene a second time (even if it is just for a single pixel) and still manage a strong frame rate.  I love Blitz3D!!!<div class="quote"> Return tickets holders are now certain of being returned to where they started from because they never leave in the first place!!! </div>Just like the real thing!  You are a stickler for realism, Carpman.  ;)


Barliesque(Posted 1+ years ago)

 Recently, a user was having difficulty integrating the code into his project.  So here are some notes I wrote up to help him solve the trouble.  Maybe others will find this helpful:1)  Be sure to include the file "Sprite Control.bb"2)  Two globals are being used by GetFlareColor()
Code: [Select]
     Global player
      Global playerheight#=32
They'll need to be defined in your main program (or you may prefer to make them parameters of that function, rather than relying on globals.  Looking back at the code now, I'm not sure why I did it the way I did!)3)  The following bit of code from the demo contains global declarations that are essential.  This code should come AFTER your graphics3D command, so that ViewX and ViewY are correctly set.
Code: [Select]
       ;-------------------------
        ; Set up Sprite Control
        ; and our flares
        ;-------------------------
        Global ViewX=GraphicsWidth(),ViewY=GraphicsHeight()
        Global ViewAspect# = float(viewx)/float(viewy)

        global FlareRed, FlareGreen, FlareBlue
        dim Flare(15)

        spritecamera = camera
        spritepivot = CreateSpritePivot(spritecamera,1.01)
        SetupFlares("Medialens-flares.jpg")
4) Of course, the last line above needs to be changed if the lens flare image is in a different location.5)  In your main program loop, you need only one command:
Code: [Select]
       UpdateFlare(camera, Sun)  ...where "camera" is the camera entity, and "Sun" is the entity of a pivot where the sun should be.  Note alse these lines from the intialisation part of the demo:
Code: [Select]
       Scene = LoadAnimMesh("MediaScene.b3d")
        Sun = FindChild(Scene,"Sun")
An object within the hierarchy of the sample scene model was named "Sun."  This statement finds that entity so that the flare functions can refer to it as a "light source"


shamanenCoder(Posted 1+ years ago)

 great! thx a lot!


Viperfish(Posted 1+ years ago)

 Awesome!Here it is at work in my project;


Barliesque(Posted 1+ years ago)

 Ahhh, great!  Nice to see it in use.  Thanks for posting the pretty screenshot. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal