October 17, 2021, 09:39:33

Author Topic: [bb] Starfox's LOD Mesh System by starfox [ 1+ years ago ]  (Read 571 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Starfox's LOD Mesh System
Author : starfox
Posted : 1+ years ago

Description : This LOD Mesh system supports multiple camera's and unlimited amounts of lod

Code :
Code: BlitzBasic
  1. ;Lod(Level Of Detail) Mesh System By Starfox (aka David Dawkins)
  2. ;What is an Lod mesh?
  3. ;A mesh that changes when you get closer or farther to the mesh, it is
  4. ;sufficent when you have lots of this meshes displayed.
  5. ;This system support unlimited levels of lod
  6. ;and unlimited number of cameras
  7. ;Put the meshes in order from most detail to least detail
  8. ;Only one thing is required if you use this system: credit
  9. ;Have fun
  10.  
  11. ;The Types
  12. Type lodmesh
  13. Field piv,tparts,cloc
  14. Field curpart,active
  15. End Type
  16.  
  17. Type lodpart
  18. Field mesh,nrange,par
  19. Field frange,index
  20. End Type
  21.  
  22. Type lodcam
  23. Field cam
  24. End Type
  25.  
  26. ;Functions For Camera's
  27. Function ActivateLodCamera(cam)
  28. l.lodcam = New lodcam
  29. lcam = cam
  30. End Function
  31.  
  32. Function DeactivateLodCamera(cam)
  33. For l.lodcam = Each lodcam
  34. If lcam = cam Then Delete l : Exit
  35. Next
  36. End Function
  37.  
  38. ;Lod Mesh Functions
  39. Function CreateLodMesh()
  40. l.lodmesh = New lodmesh
  41. lpiv = CreatePivot()
  42. lactive = 1
  43. Return lpiv
  44. End Function
  45.  
  46. Function AddToLodMesh.lodpart(entity,lodpiv,nrang,frang,index)
  47. For l.lodmesh = Each lodmesh
  48.         If lpiv = lodpiv
  49.         lo.lodpart = New lodpart
  50.         lopar = lpiv
  51.         lomesh = entity
  52.         lo
  53. range = nrang : lofrange = frang
  54.         loindex = index
  55.         EntityParent lomesh,lopar,0
  56.         l       parts = l       parts + 1
  57.         If lcurpart > 0
  58.         HideEntity lcurpart
  59.         EndIf
  60.         ShowEntity lomesh
  61.         lcurpart = lomesh
  62.         Return lo
  63.         EndIf
  64. Next
  65. End Function
  66.  
  67. Function ToggleLodActivation(lodpiv)
  68. For l.lodmesh = Each lodmesh
  69. If lpiv = lodpiv
  70.         lactive = 1 - lactive
  71.         Exit
  72. EndIf
  73. Next
  74. End Function
  75.  
  76. Function UpdateLod()
  77. For l.lodcam = Each lodcam
  78.         For lv.lodmesh = Each lodmesh
  79.         If lvactive = 1
  80.         If lvcloc = 0
  81.         lvcloc = lcam
  82.         EndIf
  83.         If EntityDistance(lcam,lvpiv) < EntityDistance(lvpiv,lvcloc)
  84.         lvcloc = lcam
  85.         EndIf
  86.         EndIf
  87.         Next
  88. Next
  89.  
  90. For l.lodcam = Each lodcam
  91.         For lv.lodmesh = Each lodmesh
  92.         If lvactive = 1
  93.         If lvcloc = lcam
  94.         dist = EntityDistance(lvcloc,lvpiv)
  95.                 For lm.lodpart = Each lodpart
  96.                 If lmpar = lvpiv
  97.                
  98.                 If dist => lm
  99. range And dist <= lmfrange
  100.                 If lvcurpart <> lmmesh
  101.                 HideEntity lvcurpart
  102.                 ShowEntity lmmesh
  103.                 lvcurpart = lmmesh
  104.                 EndIf
  105.                 EndIf
  106.                
  107.                 If dist >= lm
  108. range
  109.                         If lmindex = lv parts
  110.                                 If lvcurpart <> lmmesh
  111.                                 HideEntity lvcurpart
  112.                                 ShowEntity lmmesh
  113.                                 lvcurpart = lmmesh
  114.                                 EndIf
  115.                         EndIf
  116.                 EndIf
  117.                
  118.                 EndIf
  119.                 Next
  120.                 lvcloc = 0
  121.         EndIf
  122.         ElseIf lvactive = 0
  123.         For lm.lodpart = Each lodpart
  124.         If lmpar = lvpiv
  125.                 If lmindex = 1
  126.                 If lvcurpart <> lmmesh
  127.                 HideEntity lvcurpart
  128.                 ShowEntity lmmesh
  129.                 lvcurpart = lmmesh
  130.                 EndIf
  131.                 EndIf
  132.                 lvcloc = 0
  133.         EndIf
  134.         Next
  135.         EndIf
  136.         Next
  137. Next
  138. End Function
  139.                        
  140.        
  141. ;Example
  142. Graphics3D 640,480
  143. light = CreateLight() : TurnEntity light,45,45,0
  144. SetBuffer FrontBuffer()
  145. Print "Loading"
  146. SetBuffer BackBuffer()
  147. cam = CreateCamera()
  148. CameraViewport cam,0,0,640,480/2-1
  149. cam2 = CreateCamera()
  150. CameraViewport cam2,0,480/2+1,640,480/2-1
  151. MoveEntity cam,0,0,-100
  152. MoveEntity cam2,-2000,0,100
  153. CameraRange cam,1,5000
  154. CameraRange cam2,1,5000
  155. Local lodon = 1
  156. Local ship[8]
  157. For e = 1 To 8
  158. ship[e] = LoadMesh("tran0"+e+".x")
  159. RotateMesh ship[e],0,180,0
  160. FitMesh ship[e],-1.5,-1,-3,3,2,6
  161. ScaleEntity ship[e],20,20,20
  162. Next
  163. ;Activate Both Cameras
  164. activatelodcamera(cam)
  165. activatelodcamera(cam2)
  166. ;Create the actuall lodmesh
  167. piv = createlodmesh()
  168. ;Add all models to lodmesh and set boundries
  169. addtolodmesh(ship[1],piv,0,200,1)
  170. addtolodmesh(ship[2],piv,201,400,2)
  171. addtolodmesh(ship[3],piv,401,600,3)
  172. addtolodmesh(ship[4],piv,601,800,4)
  173. addtolodmesh(ship[5],piv,801,1000,5)
  174. addtolodmesh(ship[6],piv,1001,1200,6)
  175. addtolodmesh(ship[7],piv,1201,1400,7)
  176. addtolodmesh(ship[8],piv,1401,1600,8)
  177. ;Make sure none of them overlap
  178. PositionEntity piv,3000,0,0 : TurnEntity piv,0,90,0
  179.  
  180. While Not KeyHit(1)
  181. PointEntity cam,piv
  182. PointEntity cam2,piv
  183. MoveEntity piv,0,0,5
  184. ;Update LOD
  185. updatelod()
  186. UpdateWorld
  187. RenderWorld
  188. Text 0,0,"Tris:"+TrisRendered()
  189. Text 640/2,480/2,"Starfox's Dynamic LOD System",1,1
  190. If lodon = 1 Then lodt$ = "On"
  191. If lodon = 0 Then lodt$ = "Off"
  192. Text 640/2,480/2+10,"Space To Toggle LOD: "+lodt,1,1
  193. If KeyHit(57)
  194. togglelodactivation(piv)
  195. lodon = 1 - lodon
  196. EndIf
  197. Flip
  198. Wend
  199. End


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal