Ooops
July 28, 2021, 05:49:47

Author Topic: [bb] True 3D Visibility / Line of Sight code (pseudo LOS) by OpticEvIl [ 1+ years ago ]  (Read 896 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : True 3D Visibility / Line of Sight code (pseudo LOS)
Author : OpticEvIl
Posted : 1+ years ago

Description : This is an example of how to check if any part of an entity is visible to the camera. It even returns how much of an entity is visible. Visibility can even be masked through 3 channels. Super fast, does 80 fps at 640x480 and supports a unlimited number of entities with very little slowdown. Useful for games like Splinter cell and SOCOM ect.

Code :
Code: BlitzBasic
  1. ;True 3D Visibility V1.0
  2. ;Visibility in native B3D
  3. ;by OpticEvIl
  4. ;manntec@msn.com
  5.  
  6. ;if you use this in your code, give me a little credit
  7.  
  8. AppTitle "True 3D Visibility v1.0- OpticEvIl"
  9. ;Example in 640x480
  10. Graphics3D 640,480   ;change this only after an understanding of the code
  11.                                          ;screen w&h are hard coded in the version
  12. SetBuffer BackBuffer()
  13.  
  14.                                  ;xstep & ystep can be increased to increase speed
  15.                                  ;at the cost of resolution
  16. Const xstep = 5  ;check every 5th pixel on the x-axis (2D)
  17. Const ystep = 4  ;check every 4th pixel on the y-axis (2D)
  18. Const nos = 2    ;number of objects
  19. Const now = 10   ;number of obstructions
  20.  
  21. Type Visstruct
  22.         Field ent
  23.         Field child
  24.         Field rc,gc,bc
  25.         Field vis_type1,vis_type2,vis_type3
  26.         Field ret_type1,ret_type2,ret_type3
  27. End Type
  28.  
  29. Dim s(nos)
  30. Dim w(now)
  31.  
  32. Global tex = LoadTexture ("./tex.jpg") ;replace with your own tex!
  33. Global cap,v1,v2 ;cap will contain screen step val
  34.  
  35. Global camera=CreateCamera()
  36. PositionEntity camera,-0,0,-8
  37.  
  38. ;creates obstructions, and assign them to blue channel for visibility check
  39. ;the green and blue channels are not use in the example
  40. For q = 1 To now
  41.         w(q) = CreateCube()
  42.         EntityColor w(q),128,128,128
  43.         ScaleEntity w(q),1,.2,.2
  44.         PositionEntity w(q),q-(now/2)-1,0,-4
  45.         RotateEntity w(q),0,0,90
  46.         RotateEntity w(q),0,0,Rand(0,359)
  47.        
  48.         targetvis.VisStruct = New VisStruct
  49.         targetvisent = w(q)    
  50.         targetvis
  51. c  = 128
  52.         targetvisgc  = 128
  53.         targetvisc  = 128
  54.         targetvisvis_type1 = 0
  55.         targetvisvis_type2      = 0
  56.         targetvisvis_type3 = 1
  57. Next
  58.  
  59.  
  60. s(1)=CreateSphere()
  61. EntityColor s(1),120,50,50
  62. PositionEntity s(1),2,0,0
  63. EntityTexture s(1),tex
  64.  
  65.  
  66. s(2)=CreateSphere()
  67. EntityColor s(2),120,50,50
  68. PositionEntity s(2),2,-1.8,-6
  69.  
  70. For q = 1 To nos
  71.         targetvis.VisStruct = New VisStruct
  72.         targetvisent = s(q)    
  73.         targetvis
  74. c  = 120
  75.         targetvisgc  = 50
  76.         targetvisc  = 50
  77.         targetvisvis_type1 = q
  78.         targetvisvis_type2      = 0
  79.         targetvisvis_type3 = 0
  80. Next
  81.  
  82.  
  83. AmbientLight 128,128,128
  84. light = CreateLight()
  85.  
  86. Hmills#=MilliSecs()
  87. While Not KeyDown(1)
  88.  
  89.         ;controls
  90.         TurnEntity camera,0,(KeyDown(203)-KeyDown(205)),0              
  91.         MoveEntity camera,0,0,(KeyDown(200)-KeyDown(208))*0.1
  92.        
  93.         cap = cap + 96
  94.         If cap > 480-(96) Then                  ; -(96) is for TS only!
  95.                 cap = 0
  96.         End If
  97.        
  98.         UpdateVis()    
  99.        
  100.         Color 255,255,255
  101.         If Not (frames Mod 5) Then
  102.                 ov1 = v1
  103.                 ov2 = v2
  104.                 v1 = 0
  105.                 v2 = 0
  106.         End If
  107.        
  108.         fps = frames/(MilliSecs()-Hmills)*1000         
  109.         Text 10,20,"Object 1 optical strength:"+ov1
  110.         Text 10,40,"Object 2 optical strength:"+ov2
  111.         Text 10,60,"FPS:"+fps
  112.                
  113.         Flip
  114.         frames = frames + 1
  115. Wend
  116.  
  117. End
  118.  
  119. Function UpdateVis()
  120.        
  121.         AmbientLight 0,0,0
  122.         PrepVis()
  123.        
  124.         RenderWorld
  125.  
  126.         LockBuffer BackBuffer()
  127.         For x = 0 To 639 Step xstep     ;change step number to change resolution
  128.  
  129.                 y = cap
  130.                 While y < 96+cap
  131.                         Select xRed(ReadPixelFast (x,y))
  132.                        
  133.                                 Case 0
  134.                                 Case 1
  135.                                         v1 = v1 + 1
  136.                                 Case 2
  137.                                         v2 = v2 + 1
  138.                         End Select
  139.                         y = y + ystep                   ;change step number to change resolution
  140.                 Wend
  141.         Next
  142.         UnlockBuffer BackBuffer()
  143.  
  144.         AmbientLight 128,128,128
  145.         Retvis()
  146.  
  147.         UpdateWorld    
  148.         RenderWorld
  149.  
  150. End Function
  151.  
  152. Function PrepVis()
  153.  
  154.         ;prepare all entities for visibility checking
  155.         For targetvis.VisStruct = Each VisStruct
  156.                 EntityFX targetvisent,5
  157.                 EntityColor targetvisent,targetvisvis_type1,targetvisvis_type2,targetvisvis_type3
  158.         Next
  159.  
  160. End Function
  161.  
  162. Function RetVis()
  163.  
  164.         ;reassign original values
  165.         For targetvis.VisStruct = Each VisStruct
  166.                 EntityFX targetvisent,0        
  167.                 EntityColor targetvisent,targetvis
  168. c,targetvisc,targetvisgc
  169.         Next
  170.  
  171. End Function
  172.  
  173. Function xRed(rgb)      
  174.         Return rgb Shr 16 And $ff
  175. End Function
  176.  
  177. Function xGreen(rgb)
  178.         Return rgb Shr 8 And $ff
  179. End Function
  180.  
  181. Function xBlue(rgb)
  182.         Return rgb And $ff
  183. End Function


Comments :


Q(Posted 1+ years ago)

 I get a 'Memory Access Violation' when I try to run this..


Subirenihil(Posted 1+ years ago)

 You didn't put in your own texture.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal