January 26, 2021, 06:39:41 AM

Author Topic: [bb] LOD handling code by Rob [ 1+ years ago ]  (Read 1283 times)

Offline BlitzBot

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

Description : Have fun with it.

Code :
Code: BlitzBasic
  1. ; Lod handling code. Loads three meshes and dynamically
  2. ; swaps between all three depending on the distance.
  3. ; Useful For games containing high polygon objects.
  4. ; (Use up/down arrow to move camera.)
  5. ;
  6. ; By Rob Cummings
  7.  
  8.  
  9. Graphics3D 640,480,16,2
  10. camera=CreateCamera()
  11. light=CreateLight(2)
  12. PositionEntity light,1000,1000,1000
  13. PositionEntity camera,0,0,-20
  14.  
  15. ; For the purposes of code archives, I have provided a simple function to mimic
  16. ; LoadLodMesh (createlodmesh)
  17.  
  18. ;player = LoadLodMesh("lowdetail.3ds","mediumdetail.3ds","highdetail.3ds")
  19. player = CreateLodMesh()
  20.  
  21.  
  22. While Not KeyHit(1)
  23.  
  24.         If KeyDown(200) Then MoveEntity camera,0,0,1
  25.         If KeyDown(208) Then MoveEntity camera,0,0,-1
  26.         If KeyHit(17) Then wire=1-wire : WireFrame wire
  27.  
  28.         UpdateWorld
  29.         updatelod(player,camera,100) ;<< disbias (last parameter) controls lod changes.
  30.         RenderWorld
  31.         Text 0,0,"Polycount: "+TrisRendered()
  32.         Text 0,16,"Press W to toggle wireframe."
  33.         Text 0,32,"Up/Down arrows to move camera."
  34.         Flip
  35. Wend
  36. End
  37.  
  38.  
  39. ;lod "distbias" controls the amount of distance between each lod. Divide your
  40. ;camerarange by three in order to get a useful approximation, but you may want to
  41. ;use a lot less. Experiment!
  42.  
  43. Function UpdateLod(lodpivot,lodcamera,distbias#=250)
  44.         If Not EntityInView(lodpivot,lodcamera) Return 0
  45.         distlod#=EntityDistance(lodpivot,lodcamera)
  46.         HideEntity lodpivot
  47.         If distlod<1*distbias
  48.                 ShowEntity GetChild(lodpivot,1)
  49.         ElseIf distlod<2*distbias
  50.                 ShowEntity GetChild(lodpivot,2)
  51.         ElseIf distlod<3*distbias
  52.                 ShowEntity GetChild(lodpivot,3)
  53.         EndIf
  54. End Function
  55.  
  56. Function LoadLodMesh(lodfile1$,lodfile2$,lodfile3$)
  57.         temppivot=CreatePivot()
  58.         templod=LoadAnimMesh(lodfile3$):EntityParent templod,temppivot
  59.         templod=LoadAnimMesh(lodfile2$):EntityParent templod,temppivot
  60.         templod=LoadAnimMesh(lodfile1$):EntityParent templod,temppivot
  61.         Return temppivot
  62. End Function
  63.  
  64. Function CreateLodMesh() ; for testing purposes
  65.         temppivot=CreatePivot()
  66.         templod=CreateSphere(32):EntityParent templod,temppivot
  67.         templod=CreateSphere(16):EntityParent templod,temppivot
  68.         templod=CreateSphere(8):EntityParent templod,temppivot
  69.         ScaleEntity temppivot,10,10,10
  70.         Return temppivot
  71. End Function


Comments :


GQ(Posted 1+ years ago)

 This no longer works as is in v1.88(due to changes in showentity behavior?)Showentity will not do anything if the parent entity is not visible.Function UpdateLod(lodpivot,lodcamera,distbias#=250)   If Not EntityInView(lodpivot,lodcamera) Return 0   distlod#=EntityDistance(lodpivot,lodcamera)   ;HideEntity lodpivotHideEntity GetChild(lodpivot,1)HideEntity GetChild(lodpivot,2)HideEntity GetChild(lodpivot,3)   If distlod<1*distbias      ShowEntity GetChild(lodpivot,1)   ElseIf distlod<2*distbias      ShowEntity GetChild(lodpivot,2)   ElseIf distlod<3*distbias      ShowEntity GetChild(lodpivot,3)   EndIfEnd Function


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal