Ooops
November 25, 2020, 05:14:20 AM

Author Topic: [bb] Textured Spotlight by fredborg [ 1+ years ago ]  (Read 673 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Textured Spotlight by fredborg [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Textured Spotlight
Author : fredborg
Posted : 1+ years ago

Description : A nice little effect, that could be used for car headlights or flashlights. It shouldn't be too dificult to understand how it works...

It's not optimized, so you probably need to change some stuff if you want to use it in a game...

You will need this image:
[img]www.frecle.net/misc/SpotLight2.html">

Have fun!


Code :
Code: BlitzBasic
  1. ;
  2. ;
  3. ; Textured spotlight thingy
  4. ;
  5. ; Created by Mikkel Fredborg
  6. ; Use as you please!
  7. ;
  8.  
  9. Type dl_receiver
  10.         Field mesh
  11. End Type
  12.  
  13. Type dl_light
  14.         Field entity
  15.         Field range#
  16.         Field scale#
  17.         Field intensity#
  18.         Field flicker#
  19.         Field flickerrange#
  20.         Field r#,g#,b#
  21. End Type
  22.  
  23. Global dl_brush
  24. Global dl_tex
  25.  
  26. Function DL_Init()
  27.  
  28.         ClearTextureFilters
  29.         dl_tex = LoadTexture("spotlight2.png",1+16+32)
  30.  
  31.         dl_brush = CreateBrush()
  32.         BrushBlend dl_brush,3
  33.         BrushFX dl_brush,1+2
  34.         BrushTexture dl_brush,dl_tex   
  35.  
  36. End Function
  37.  
  38. Function DL_Free()
  39.  
  40.         For dlr.dl_receiver = Each dl_receiver
  41.                 FreeEntity dlrmesh
  42.                 Delete dlr
  43.         Next
  44.  
  45.         For dll.dl_light = Each dl_light
  46.                 Delete dll
  47.         Next
  48.  
  49.         If dl_tex       Then FreeTexture dl_tex
  50.         If dl_brush     Then FreeBrush dl_brush
  51.  
  52.         dl_tex          = 0
  53.         dl_brush        = 0
  54.  
  55. End Function
  56.  
  57. Function DL_SetReceiver(mesh)
  58.  
  59.         dlr.dl_receiver = New dl_receiver
  60.         dlrmesh = CopyMesh(mesh)
  61.         PaintMesh dlrmesh,dl_brush
  62.        
  63. End Function
  64.  
  65. Function DL_SetLight(entity,range#=500.0,scale#=0.75,intensity#=2.0,flicker#=0.05,flickerrange#=0.5,r#=200,g#=220,b#=255)
  66.  
  67.         dll.dl_light = First dl_light
  68.  
  69.         If dll = Null
  70.                 dll.dl_light = New dl_light
  71.         End If
  72.        
  73.         dllentity        = entity
  74.         dll
  75. ange     = range
  76.         dllscale                = scale
  77.         dllintensity = intensity
  78.         dllflicker       = flicker
  79.         dllflickerrange = flickerrange
  80.        
  81.         dll
  82.                 = r
  83.         dllg            = g
  84.         dll            = b
  85.        
  86. End Function
  87.  
  88. Function DL_Update()
  89.  
  90.         dll.dl_light = First dl_light
  91.         If dll = Null Then Return
  92.  
  93.         If Rnd(0.0,1.0)<dllflicker
  94.                 intensity# = dllintensity*Rnd(dllflickerrange,1.0)
  95.         Else
  96.                 intensity# = dllintensity
  97.         End If
  98.  
  99.         For dlr.dl_receiver = Each dl_receiver
  100.                 mesh    = dlrmesh
  101.                 n_surfs = CountSurfaces(mesh)
  102.                 For s = 1 To n_surfs
  103.                         surf = GetSurface(mesh,s)
  104.                         n_verts = CountVertices(surf)-1
  105.                         For v = 0 To n_verts
  106.                                 TFormPoint VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v),mesh,dllentity
  107.                                 x# = TFormedX()
  108.                                 y# = TFormedY()
  109.                                 z# = TFormedZ()
  110.                                
  111.                                 dist# = Sqr(x*x + y*y + z*z)*dllscale
  112.                                 tu# = (x/dist)+0.5
  113.                                 tv# = 1.0-((y/dist)+0.5)
  114.                                
  115.                                 VertexTexCoords surf,v,tu,tv
  116.  
  117.                                 If z>dll
  118. ange Then z = dll
  119. ange
  120.                                 falloff# = 1.0-(z/dll
  121. ange)
  122.                                 If falloff<0.0 Then falloff = 0.0
  123.                                 If falloff>1.0 Then falloff = 1.0
  124.                                
  125.                                 TFormNormal VertexNX(surf,v),VertexNY(surf,v),VertexNZ(surf,v),mesh,dllentity
  126.                                 dot# = -TFormedZ()*falloff*intensity
  127.                                 If dot>0.0
  128.                                         VertexColor surf,v,dot*dll
  129. ,dot*dllg,dot*dll
  130.                                 Else
  131.                                         VertexColor surf,v,0,0,0
  132.                                 End If
  133.                         Next
  134.                 Next           
  135.         Next
  136.  
  137. End Function
  138.  
  139.  
  140.  
  141. ;
  142. ; Example
  143. ;
  144. Graphics3D 640,480,0,2
  145. SetBuffer BackBuffer()
  146.  
  147. HidePointer
  148. AmbientLight 12,24,50
  149. SeedRnd MilliSecs()
  150.  
  151. light = CreateLight()
  152. RotateEntity light,70,40,0
  153. LightColor light,50,50,50
  154.  
  155. camera = CreateCamera()
  156. scene = CreateMesh()
  157.  
  158. For i = 0 To 100
  159.  
  160.         cube = CreateCube()
  161.         ScaleMesh cube,Rnd(1,10),Rnd(1,10),Rnd(1,10)
  162.         RotateMesh cube,Rnd(-90,90),Rnd(-180,180),Rnd(-180,180)
  163.         PositionMesh cube,Rnd(-100,100),Rnd(-100,100),Rnd(-100,100)
  164.        
  165.         AddMesh cube,scene
  166.        
  167.         FreeEntity cube
  168.        
  169. Next
  170.  
  171. cube = CreateCube()
  172. ScaleMesh cube,150,150,150
  173. FlipMesh cube
  174. AddMesh cube,scene
  175. FreeEntity cube
  176.  
  177. DL_Init()
  178. DL_SetReceiver(scene)
  179. DL_SetLight(camera)
  180.  
  181. spd# = 2.0
  182.  
  183. Repeat
  184.  
  185.         MoveEntity camera,(KeyDown(205)-KeyDown(203))*spd,0,(KeyDown(200)-KeyDown(208))*spd
  186.         TurnEntity camera,-MouseYSpeed()*0.25,-MouseXSpeed()*0.25,0
  187.         RotateEntity camera,EntityPitch(camera,True),EntityYaw(camera,True),0
  188.  
  189.         MoveMouse 320,240
  190.  
  191.         DL_Update()
  192.  
  193.         RenderWorld
  194.         Flip
  195.  
  196. Until KeyHit(1)
  197.  
  198. DL_Free()
  199.  
  200. End


Comments :


Kozmi(Posted 1+ years ago)

 Thank's Fredborg!!! :)This is exactly the type of effect I was searching for my torch for my player character of my game im' designing!!!This should be of great use to me!Thank's again!!! ;)


HNPhan(Posted 1+ years ago)

 wicked effect :Dthx alot


Sir Gak(Posted 1+ years ago)

 I like it.  Thanks.


Mr. Bean(Posted 1+ years ago)

 Nice Effect.Thanks!!!!!


Kryzon(Posted 1+ years ago)

 This is very nice, but how can you apply that projected texture inside an already textured mesh? I have tried that and it only disrupted the other textures. Is there a way to separate different coordinates for each texture (as in, 1 set for a diffuse map and another one for this light texture)?EDIT: Yes there is... <a href="http://blitzbasic.com/b3ddocs/command.php?name=TextureCoords&ref=3d_cat" target="_blank">TextureCoords()[/url].


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal