October 19, 2021, 09:32:30

Author Topic: [bb] MultiTextureModel->SingleTextureFile Convertor by Banshee [ 1+ years ago ]  (Read 643 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : MultiTextureModel->SingleTextureFile Convertor
Author : Banshee
Posted : 1+ years ago

Description : This program converts a multi-surface multi-textured model into a single surface single textured model.

It supports a model of up to 4 textures and outputs two files with 4in1 in the name. The .x exporter is David Bird's from the code archives.

The 4in1 texture will need to be re-applied to the mesh.

Written because I needed it, shared because it actually worked.


Code :
Code: BlitzBasic
  1. Graphics3D 640,480,0,2
  2. Global source$="source.3ds"
  3. Global model=LoadMesh(source)
  4. Global vQ=0
  5.  
  6. Type vert
  7.         Field index
  8.         Field surface
  9.         Field x#
  10.         Field y#
  11.         Field z#
  12.         Field u#
  13.         Field v#
  14.         Field w#
  15.         Field nx#
  16.         Field ny#
  17.         Field nz#
  18. End Type
  19. Type tri
  20.         Field vertA,vertB,vertC
  21. End Type
  22.  
  23. compileMesh
  24.  
  25. Function compileMesh()
  26.         texture=CreateImage(512,512)
  27.         For s=1 To CountSurfaces(model)
  28.                 surface=GetSurface(model,s)
  29.                
  30.                 brush=GetSurfaceBrush(surface)
  31.                 tex=GetBrushTexture(brush)
  32.                 FreeBrush brush
  33.                
  34.                 uOff=0 : vOff=0
  35.                 If s=2 Then uOff=256
  36.                 If s=3 Then vOff=256
  37.                 If s=4 Then uOff=256 : vOff=256
  38.                 CopyRect 0,0,256,256,uOff,vOff,TextureBuffer(tex),ImageBuffer(texture)
  39.                 FreeTexture tex
  40.                
  41.                 For tr=0 To CountTriangles(surface)
  42.                         t.tri=New tri
  43.                         tvertA=TriangleVertex(surface,tr,0)
  44.                         tvertB=TriangleVertex(surface,tr,1)
  45.                         tvertC=TriangleVertex(surface,tr,2)
  46.                        
  47.                         If tvertA>CountVertices(surface) Or tvertB>CountVertices(surface) Or tvertC>CountVertices(surface)
  48.                                 Delete t
  49.                         Else
  50.                                 tvertA=checkVertice(surface,tvertA,s)
  51.                                 tvertB=checkVertice(surface,tvertB,s)
  52.                                 tvertC=checkVertice(surface,tvertC,s)
  53.                         EndIf
  54.                 Next
  55.         Next
  56.  
  57.         mesh=CreateMesh()
  58.         surface=CreateSurface(mesh)
  59.                
  60.         For v.vert=Each vert
  61.                 AddVertex surface,vx,vy,vz,vu,vv
  62.                 Delete v
  63.         Next   
  64.         For t.tri=Each tri
  65.                 AddTriangle surface,tvertA,tvertB,tvertC
  66.                 Delete t
  67.         Next
  68.        
  69.         SaveImage(texture,Left(source,Len(source)-4)+"4in1.bmp")
  70.         saveMeshX(mesh,Left(source,Len(source)-4)+"4in1.x")
  71. End Function
  72. Function checkVertice(surface,vertice,s)
  73.         offU#=0.0
  74.         offV#=0.0
  75.        
  76.         If s=2 Then offU=.5
  77.         If s=3 Then offV=.5
  78.         If s=4 Then offU=.5 : offV=.5
  79.  
  80.         vertExist=False
  81.         For v.vert=Each vert
  82.                 If vsurface=surface
  83.                         If vx=VertexX(surface,vertice)
  84.                                 If vy=VertexY(surface,vertice)
  85.                                         If vz=VertexZ(surface,vertice)
  86.                                                 If vu=(VertexU(surface,vertice)/2.0)+offU
  87.                                                         If vv=(VertexV(surface,vertice)/2.0)+offV
  88.                                                                 If vw=VertexW(surface,vertice)
  89.                                                                         If v
  90. x=VertexNX(surface,vertice)
  91.                                                                                 If v
  92. y=VertexNY(surface,vertice)
  93.                                                                                         If v
  94. z=VertexNZ(surface,vertice)
  95.                                                                                                 vertExist=True
  96.                                                                                                 foundVert=vindex
  97.                                                                                         EndIf
  98.                                                                                 EndIf
  99.                                                                         EndIf
  100.                                                                 EndIf
  101.                                                         EndIf
  102.                                                 EndIf
  103.                                         EndIf
  104.                                 EndIf
  105.                         EndIf
  106.                 EndIf
  107.         Next
  108.  
  109.         If Not vertExist
  110.                 v.vert=New vert
  111.                 vindex=vQ
  112.                 vsurface=surface
  113.                 vx=VertexX(surface,vertice)                                                    
  114.                 vy=VertexY(surface,vertice)
  115.                 vz=VertexZ(surface,vertice)                                                    
  116.                 vu=(VertexU(surface,vertice)/2.0)+offU
  117.                 vv=(VertexV(surface,vertice)/2.0)+offV 
  118.                 vw=VertexW(surface,vertice)                            
  119.                 v
  120. x=VertexNX(surface,vertice)                                                    
  121.                 v
  122. y=VertexNY(surface,vertice)                                                    
  123.                 v
  124. z=VertexNZ(surface,vertice)                                                    
  125.                 vQ=vQ+1
  126.                 foundVert=vindex
  127.         EndIf
  128.  
  129.         Return foundVert
  130. End Function
  131.  
  132.  
  133. ;    X File Saving Function (c)2001 David Bird
  134. ;    dave@birdie72.freeserve.co.uk
  135. ;    www.birdie72.freeserve.co.uk
  136.  
  137. Function SaveMeshX(mesh,file$)
  138. ;single surface at the moment
  139.     cnt=CountSurfaces(mesh)
  140.     If cnt=0 Then Return
  141.    
  142.     ;TODO Multi surface support
  143.     surf=GetSurface(mesh,1)
  144.    
  145.     out=WriteFile(file$)
  146.     WriteLine out,"xof 0302txt 0064"
  147.     WriteLine out,""
  148.     WriteLine out,"Header {"
  149.     WriteLine out," 1;"
  150.     WriteLine out," 0;"
  151.     WriteLine out," 1;"
  152.     WriteLine out,"}"
  153.     WriteLine out,""
  154.     WriteLine out,"Frame frm_Scene_Root {"
  155.     WriteLine out," FrameTransformMatrix {"
  156.     WriteLine out," 1.000000,0.000000,0.000000,0.000000,"
  157.     WriteLine out," 0.000000,1.000000,0.000000,0.000000,"
  158.     WriteLine out," 0.000000,0.000000,1.000000,0.000000,"
  159.     WriteLine out," 0.000000,0.000000,0.000000,1.000000;"
  160.     WriteLine out,"}"
  161.     WriteLine out,""    
  162.     name$=EntityName$(mesh)
  163.     If name$="" Then name$="Unknown"
  164.     WriteLine out,"Frame frm_"+name$+" {"
  165.     WriteLine out," FrameTransformMatrix {"
  166.     WriteLine out," 1.000000,0.000000,0.000000,0.000000,"
  167.     WriteLine out," 0.000000,1.000000,0.000000,0.000000,"
  168.     WriteLine out," 0.000000,0.000000,1.000000,0.000000,"
  169.     WriteLine out," 0.000000,0.000000,0.000000,1.000000;"
  170.     WriteLine out,"}"
  171.     WriteLine out,""
  172.     WriteLine out," Mesh frm_"+name$+" {"
  173.     WriteLine out,CountVertices(surf)+";"
  174.     For a=0 To CountVertices(surf)-1
  175.         WriteLine out,VertexX(surf,a)+";"+VertexY(surf,a)+";"+VertexZ(surf,a)+";,"
  176.     Next
  177.     WriteLine out,""
  178.     WriteLine out," "+CountTriangles(surf)+";"
  179.     For a=0 To CountTriangles(surf)-1
  180.         in1=TriangleVertex(surf,a,0)
  181.         in2=TriangleVertex(surf,a,1)
  182.         in3=TriangleVertex(surf,a,2)
  183.         ln$=" 3;"+in1+","+in2+","+in3+";"
  184.         If a=CountTriangles(surf)-1 Then ln$=ln$+";" Else ln$=ln$+","
  185.         WriteLine out,ln$
  186.     Next
  187.     WriteLine out,""
  188.  
  189.     WriteLine out,"MeshMaterialList {"
  190.     WriteLine out,"1;"
  191.     WriteLine out,"1;"
  192.     WriteLine out,"0;;"
  193.     WriteLine out,""
  194.     WriteLine out,"Material {"
  195.  
  196.     WriteLine out," 1.000000,1.000000,1.000000,1.000000;;";rgba
  197.     WriteLine out," 1.000000;"
  198.     WriteLine out," 0.500000,0.500000,0.500000;;"
  199.     WriteLine out," 0.000000,0.000000,0.000000;;"
  200.     WriteLine out,"}"
  201.     WriteLine out,"}"
  202.     WriteLine out,""
  203.     WriteLine out,"MeshNormals {"
  204.     WriteLine out,CountVertices(surf)+";"
  205.     For a=0 To CountVertices(surf)-1
  206.         WriteLine out,VertexNX(surf,a)+";"+VertexNY(surf,a)+";"+VertexNZ(surf,a)+";,"
  207.     Next
  208.     WriteLine out," "+CountTriangles(surf)+";"
  209.     For a=0 To CountTriangles(surf)-1
  210.         in1=TriangleVertex(surf,a,0)
  211.         in2=TriangleVertex(surf,a,1)
  212.         in3=TriangleVertex(surf,a,2)
  213.         ln$=" 3;"+in1+","+in2+","+in3+";"
  214.         If a=CountTriangles(surf)-1 Then ln$=ln$+";" Else ln$=ln$+","
  215.         WriteLine out,ln$
  216.     Next
  217.     WriteLine out,"}"
  218.     WriteLine out,""
  219.  
  220.     WriteLine out,"MeshTextureCoords {"
  221.     WriteLine out,CountVertices(surf)+";"
  222.     For a=0 To CountVertices(surf)-1
  223.         ln$=VertexU(surf,a)+";"+VertexV(surf,a)+";"
  224.         If a=CountVertices(surf)-1 Then ln$=ln$+";" Else ln$=ln$+","
  225.         WriteLine out,ln$
  226.     Next
  227.     WriteLine out," }"
  228.     WriteLine out," }"
  229.     WriteLine out," }"
  230.     WriteLine out,"}"
  231.     CloseFile out
  232. End Function


Comments :


Damien Sturdy(Posted 1+ years ago)

 Hey, now THIS could come in very very handy indeed!


Banshee(Posted 1+ years ago)

 I should also say the code fixes the texture at 512x512 and expects the source textures to be 256x256 as that's what the model I was converting had, but it shouldn't be too hard to tweek.


Damien Sturdy(Posted 1+ years ago)

 Oops, yup. I'll have to tweak. I want to use it on a large chunk of an entire level :P Still, good work!BTW, I just got my first pair (kay okay, second pair) of... "Spectacles"....


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal