Ooops
May 30, 2020, 03:31:29 AM

Author Topic: [bb] AddMeshToSurface by sswift [ 1+ years ago ]  (Read 1129 times)

Offline BlitzBot

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

Description : This function allows you to add a single surface mesh to a specific surface of another mesh.

This is extremely useful for optimizing your levels, because in Blitz, each surface you have affects your framerate significantly.

A surface is a collection of vertices within a mesh, and each mesh always has one surface.

So say you have a forest.  If each tree were a seprate entity, you might have 100 surfaces!  That's bad.  In fact, if each tree uses two textures, one for the trunk, and one alpha mapped for the leaves, each tree might have TWO surfaces!  That's even worse.

So what you should do is you should combine surfaces from diffrent entities which use the same texture into a single surface in a single entity.

For example, let's use that forest example again.  If your trees are made from a solid trunk, and an alpha mapped top, then you should combine all your trees into one mesh with two surfaces, and put all the trunks in the first surface, and all the tops in the second.

However, because of the way alpha is sorted in Blitz, it might be better to keep the tops of the trees as seprate entites and just combine the trunks.  You'll have to decide what is best for you.

You can also combine CLUMPS of trees into meshes, that way you can autofade out whole sections of forest for additional speedus.

Using that last method I was able to render a world with 50,000 2 poly trees on a terrain on realtively crappy hardware at a decent rate of speed.  Such a feat would be impossible if they were seprate entities.

Anyhow, here is the code.  Hope you find it useful!


Code :
Code: BlitzBasic
  1. ; -------------------------------------------------------------------------------------------------------------------
  2. ; This function copies a mesh with a single surface to a specific surface of another mesh.
  3. ;
  4. ; The function can optionally offset the mesh within the destination mesh as it copies it.
  5. ; The last six parameters may be omitted when calling the function if no transformation is desired.
  6. ; -------------------------------------------------------------------------------------------------------------------
  7. Function AddMeshToSurface(SrcMesh, DestMesh, DestSurface, Voffsetx#=0, Voffsety#=0, Voffsetz#=0, Vpitch#=0, Vyaw#=0, Vroll#=0, Vscalex#=1, Vscaley#=1, VscaleZ#=1)
  8.  
  9.         ; Determine if we can optimize the mesh copying.
  10.         TransformVertices = True
  11.         If (Vpitch#=0) And (Vyaw#=0) And (Vroll#=0)
  12.                 TransformVertices = False
  13.         EndIf
  14.  
  15.  
  16.         ; Make sure there's a surface to copy, because the mesh might be empty.
  17.         If CountSurfaces(SrcMesh) > 0
  18.  
  19.                 SrcSurface = GetSurface(SrcMesh, 1)
  20.  
  21.                 DestVerts  = CountVertices(DestSurface)
  22.                 SrcVerts   = CountVertices(SrcSurface)
  23.                        
  24.                 ; If we need to transform the vertices in a complex way...
  25.                 If TransformVertices
  26.  
  27.                         ; Do slower copy method because we need to rotate the vertices.
  28.  
  29.                         ; Create a pivot to do the transformations with.
  30.                         ThisPivot = CreatePivot()
  31.                         PositionEntity ThisPivot, Voffsetx#, Voffsety#, Voffsetz#, True
  32.                         RotateEntity ThisPivot, Vpitch#, Vyaw#, Vroll#, True
  33.                         ScaleEntity ThisPivot, Vscalex#, Vscaley#, Vscalez#
  34.  
  35.                         ; Copy all the vertices from the source mesh to the destination surface.
  36.                         For VertLoop = 0 To SrcVerts-1
  37.                                        
  38.                                 Vu#  = VertexU#(SrcSurface, VertLoop)
  39.                                 Vv#  = VertexV#(SrcSurface, VertLoop)          
  40.                                 Vw#  = VertexW#(SrcSurface, VertLoop)
  41.                                 Vr   = VertexRed(SrcSurface, VertLoop)
  42.                                 Vg   = VertexGreen(SrcSurface, VertLoop)
  43.                                 Vb   = VertexBlue(SrcSurface, VertLoop)
  44.                        
  45.                                 TFormPoint VertexX#(SrcSurface, VertLoop), VertexY#(SrcSurface, VertLoop), VertexZ#(SrcSurface, VertLoop), ThisPivot, 0
  46.                                 Vx# = TFormedX#()
  47.                                 Vy# = TFormedY#()
  48.                                 Vz# = TFormedZ#()
  49.  
  50.                                 TFormNormal VertexNX#(SrcSurface, VertLoop), VertexNY#(SrcSurface, VertLoop), VertexNZ#(SrcSurface, VertLoop), ThisPivot, 0
  51.                                 Vnx# = TFormedX#()
  52.                                 Vny# = TFormedY#()
  53.                                 Vnz# = TFormedZ#()
  54.                        
  55.                                 AddVertex(DestSurface, Vx#, Vy#, Vz#, Vu#, Vv#, Vw#)
  56.                                 VertexNormal(DestSurface, VertLoop+DestVerts, Vnx#, Vny#, Vnz#)
  57.                                 VertexColor(DestSurface, VertLoop+DestVerts, Vr, Vg, Vb)
  58.        
  59.                         Next
  60.  
  61.                         FreeEntity ThisPivot
  62.  
  63.                 Else
  64.  
  65.                         ; Do the fast copy.
  66.                         ; Fast copy can do offset and scaling, but not rotation.
  67.  
  68.                         ; Copy all the vertices from the source mesh to the destination surface.
  69.                         For VertLoop = 0 To SrcVerts-1
  70.  
  71.                                        
  72.                                 Vx#  = VertexX#(SrcSurface, VertLoop)
  73.                                 Vy#  = VertexY#(SrcSurface, VertLoop)
  74.                                 Vz#  = VertexZ#(SrcSurface, VertLoop)
  75.                                 Vu#  = VertexU#(SrcSurface, VertLoop)
  76.                                 Vv#  = VertexV#(SrcSurface, VertLoop)          
  77.                                 Vw#  = VertexW#(SrcSurface, VertLoop)
  78.                                 Vnx# = VertexNX#(SrcSurface, VertLoop)
  79.                                 Vny# = VertexNY#(SrcSurface, VertLoop)
  80.                                 Vnz# = VertexNZ#(SrcSurface, VertLoop)                                         
  81.                                 Vr   = VertexRed(SrcSurface, VertLoop)
  82.                                 Vg   = VertexGreen(SrcSurface, VertLoop)
  83.                                 Vb   = VertexBlue(SrcSurface, VertLoop)
  84.                
  85.                                 AddVertex(DestSurface, (Vx#*Vscalex#)+Voffsetx#, (Vy#*Vscaley#)+Voffsety#, (Vz#*Vscalez#)+Voffsetz#, Vu#, Vv#, Vw#)
  86.                                 VertexNormal(DestSurface, VertLoop+DestVerts, Vnx#, Vny#, Vnz#)
  87.                                 VertexColor(DestSurface, VertLoop+DestVerts, Vr, Vg, Vb)
  88.        
  89.                         Next
  90.  
  91.                 EndIf
  92.  
  93.  
  94.                 ; Copy all triangles from the source surface to the destination surface.       
  95.                 SrcTris  = CountTriangles(SrcSurface)
  96.                 For TriLoop = 0 To SrcTris-1
  97.        
  98.                         V0 = TriangleVertex(SrcSurface, TriLoop, 0)
  99.                         V1 = TriangleVertex(SrcSurface, TriLoop, 1)
  100.                         V2 = TriangleVertex(SrcSurface, TriLoop, 2)
  101.                
  102.                         AddTriangle(DestSurface, V0+DestVerts, V1+DestVerts, V2+DestVerts)
  103.        
  104.                 Next
  105.                
  106.         EndIf
  107.  
  108.                        
  109. End Function


Comments :


ryan scott(Posted 1+ years ago)

 >I m a little confused about AddMeshToSurface.>>If I have 20 enemies on screen at once that are identical to each >other,>can I use AddMeshToSurface to speed up rendering?>>If thats the case, Im pretty unclear how I would do that with your >code.   any hints?  Or, is this more for static objects, because they end >up being tied to each other somehow?You should only really combine meshes if you need to, and generally you only need to for static objects.  I would not recommend doing it for enemies, unless you are doing something crazy and have a hundred onscreen at once, and they're all low poly. Particles are an exception to the rule, as there are generally tons of them, but you should use a custom particle system for that as there's no sense in recreating triangles and you don't need to copy normals and stuff.And keep in mind that combining meshes removes the ability to do individual collision detection on them.  You could use a pivot as an "impostor" for that though.>Is it sort of like you could have 1 master entity with its mesh, and >the>other entities rather than having their own mesh, point to the master entity?You have one mesh, and if your other entities are made from the same mesh you'd just add as many copies of it as you need to the main mesh.  But keep in mind that meshes take up video ram, whereas copies of entities do not.  Copies of entities use just one copy of a mesh in video ram.I recommend you only use addmesh to surface for combining things like trees or buildings that are near eachother.


GW(Posted 1+ years ago)

 Why would this command be more usefull that Addmesh() which is 100X faster?


GW(Posted 1+ years ago)

 ..


sswift(Posted 1+ years ago)

 Because addmesh does not allow one to add a mesh to a specific surface in a mesh, and may create additional surfaces.


Stevie G(Posted 1+ years ago)

 Also, Sswifts code allows you to rotate and position the mesh prior to adding it - which is quite handy.


sswift(Posted 1+ years ago)

 Yes, for example, if you wanted to create a clump of trees with addmesh, you would have to:meshcopytranslatemeshaddmeshfreeentityIs that faster than my function?  I'm not sure.  Now add random rotation and scaling to the mix.  Still faster?  Again, I'm not sure.  But it seems likely that creating and freeing hundreds of meshes just might not be faster than simply adding new vertices to a mesh.


Wings(Posted 1+ years ago)

 This is awsome thanks.To bad i dint find this 2 yars ago.Now i can have 32000 trees loaded into gpu at same time.and still have 60 fps.


_PJ_(Posted 1+ years ago)

 <div class="quote"> Yes, for example, if you wanted to create a clump of trees with addmesh, you would have to:meshcopytranslatemeshaddmeshfreeentityIs that faster than my function? I'm not sure. Now add random rotation and scaling to the mix. Still faster? Again, I'm not sure. But it seems likely that creating and freeing hundreds of meshes just might not be faster than simply adding new vertices to a mesh.  </div>Thanks, Sswift. I find your code is certainly a lot easier to work with and is more manageable than Copying AddMesh and Freeing. Speedwise, it seemed to perform adequately but I've only used it for <30 meshes, Couldn't comment on which way is faster yet it works :)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal