merge 1 mesh with UVs for diffuse and 1 mesh with UVs for lightmap, in 1 mesh

Started by RemiD, October 20, 2019, 10:50:54

Previous topic - Next topic

RemiD

merge 1 mesh with UVs for diffuse and 1 mesh with UVs for lightmap, in 1 mesh with 2 UVs coordsets, one for diffuse, one for lightmap
useful to build maps with premade parts...

;CopyMergeMeshesWithUVCoordsets() + CopyTexture()
;this demonstrates how to load 1 mesh with UVs for diffuse texture and 1 mesh with UVs for lightmap texture,
;and to merge the 2 meshes in 1 new mesh 1 surface with 2 UVs coordsets (1 for diffuse texture, 1 for lightmap texture)
;and also how to copy a texture to a new texture (useful to copy lightmaps if you use premade parts to build your map)
;graphics window
Graphics3D(854,480,32,2)

SeedRnd(MilliSecs())

;PREMADE MESHES TEXTURES
;---------------------------------------------------------------------------------------

Global PixAlpha% = 0
Global PixRed% = 0
Global PixGreen% = 0
Global PixBlue% = 0

;load premade meshes / textures
Global TempCM = LoadMesh("column-CR.b3d") : ScaleMesh(TempCM,0.01,0.01,0.01) ;mesh with uvs for color/diffuse texture
PositionEntity(TempCM,-3.1,0,0,True)

Global TempLSM = LoadMesh("column-LSR.b3d") : ScaleMesh(TempLSM,0.01,0.01,0.01) ;mesh with uvs for lightmap texture
PositionEntity(TempLSM,+3.1,0,0,True)

Global TempCT = LoadTexture("column-CT.png",1) : TextureCoords(TempCT,0) : EntityTexture(TempCM,TempCT,0,0) ;color/diffuse texture

Global TempLST = LoadTexture("column-LST.png",1) : TextureCoords(TempLST,0) : EntityTexture(TempLSM,TempLST,0,0) ;lightmap texture

;merge the 2 separate meshes with 2 different uvs into 1 mesh (1 surface) with 2 uvs coordsets
Global NewM = CopyMergeMeshesWithUVCoordsets(TempCM,TempLSM)
PositionEntity(NewM,0,0,0,True)
;copy the 2 textures
Global NewCT = CopyTexture(TempCT) : TextureCoords(NewCT,0) : EntityTexture(NewM,NewCT,0,0) ;uvs coordset 0, layer 0 (diffuse texture)
Global NewLST = CopyTexture(TempLST) : TextureCoords(NewLST,1) : EntityTexture(NewM,NewLST,0,1) ;uvs coordset 1, layer 1 (lightmap texture)

;delete the loaded meshes/textures
;FreeEntity(TempCM)
;FreeEntity(TempLSM)
;FreeTexture(TempCT)
;FreeTexture(TempLST)

;---------------------------------------------------------------------------------------

;Input
Global MX%
Global MY%
Global MXDiff%
Global MYDiff%

;Origine
Global Origine = CreateCube()
ScaleMesh(Origine,0.01,0.01,0.01)
EntityColor(Origine,255,000,255)
EntityFX(Origine,1)
HideEntity(Origine)

;Camera
Global Camera = CreateCamera()
CameraViewport(Camera,0,0,GraphicsWidth(),GraphicsHeight())
CameraRange(Camera,0.15,150)
CameraClsColor(Camera,000,000,000)

;InteractionMode
Global InteractionMode%
Const C2D% = 1
Const C3D% = 2
InteractionMode = C3D
HidePointer()

;Ghost
Global GhostRoot
Global GhostRootYaw#
Global GhostEyes
Global GhostEyesPitch#
AddGhost()

;

;light
DLight = CreateLight(1)
LightColor(DLight,240,240,240)
PositionEntity(DLight,0,1000,-1000,True)
RotateEntity(DLight,45,0,0,True)

AmbientLight(060,060,060)

PositionEntity(GhostRoot,0,1.65,-5,True)

Global MainLoopTimer = CreateTimer(30)

Main()

End()

Function Main()

Repeat

  MainLoopMilliStart% = MilliSecs()

  MX = MouseX() : MY = MouseY()

  MXDiff = MouseXSpeed() : MYDiff = MouseYSpeed()

  UpdateInteractionMode()
  If( InteractionMode = C2D )
   ;
  Else If( InteractionMode = C3D )
   UpdateGhost()    
  EndIf

  WireFrame(False)
  If( KeyDown(2)=1 )
   WireFrame(True)
  EndIf

  PositionEntity(Camera,EntityX(GhostEyes,True),EntityY(GhostEyes,True),EntityZ(GhostEyes,True),True)
  RotateEntity(Camera,EntityPitch(GhostEyes,True),EntityYaw(GhostEyes,True),EntityRoll(GhostEyes,True),True)

  CameraViewport(Camera,0,0,GraphicsWidth(),GraphicsHeight())
  CameraClsColor(Camera,000,000,000)
  SetBuffer(BackBuffer())
  RenderWorld()

  Color(255,255,255)
  CText("Tris = "+TrisRendered(),0,0)
  CText("FPS = "+FPS,0,15)

  ;Flip(1)
  WaitTimer(MainLoopTimer)
  VWait():Flip(False)

  MainLoopMilliTime = MilliSecs() - MainLoopMilliStart
  If( MainLoopMilliTime < 1 )
   MainLoopMilliTime = 1
  EndIf

  FPS% = 1000.0/MainLoopMilliTime

Until( KeyDown(1)=1 )

End Function

Function CText(TextStr$,PX%,PY%)

Text(PX,PY,TextStr,False,False)

End Function

Function UpdateInteractionMode()

If( KeyHit(15)=1 )
  If( InteractionMode = C2D )
   InteractionMode = C3D
   HidePointer()
  Else If( InteractionMode = C3D )
   InteractionMode = C2D
   ShowPointer()
  EndIf
EndIf

End Function

Function AddGhost()

GhostRoot = CreatePivot()

GhostEyes = CreatePivot()
EntityParent(GhostEyes,GhostRoot,True)

End Function

Function UpdateGhost()

MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2)
GhostEyesPitch = GhostEyesPitch + Float(MYDiff)/10
If( GhostEyesPitch < -89 )
  GhostEyesPitch = -89
Else If( GhostEyesPitch > 89 )
  GhostEyesPitch = 89
EndIf
RotateEntity(GhostEyes,GhostEyesPitch,0,0,False)
GhostRootYaw = GhostRootYaw - Float(MXDiff)/10
RotateEntity(GhostRoot,0,GhostRootYaw,0,False)

If( KeyDown(42) = 0 And KeyDown(29) = 0 )
  Speed# = 0.1
Else If( KeyDown(42) = 1 And KeyDown(29) = 0 )
  Speed# = 1
Else If( KeyDown(42) = 0 And KeyDown(29) = 1 )
  Speed# = 0.01
EndIf

If( KeyDown(17)=1 )
  MoveEntity(GhostRoot,0,0,Speed)
Else If( KeyDown(31)=1 )
  MoveEntity(GhostRoot,0,0,-Speed)
EndIf
If( KeyDown(30)=1 )
  MoveEntity(GhostRoot,-Speed,0,0)
Else If( KeyDown(32)=1 )
  MoveEntity(GhostRoot,Speed,0,0)
EndIf
If( KeyDown(16)=1 )
  MoveEntity(GhostRoot,0,-Speed,0)
Else If( KeyDown(18)=1 )
  MoveEntity(GhostRoot,0,Speed,0)
EndIf

If( KeyDown(44)=1 )
  X# = EntityX(GhostRoot,True) : Z# = EntityZ(GhostRoot,True)
  PositionEntity(GhostRoot,X,1.65,Z,True)
EndIf

End Function

Function CopyMergeMeshesWithUVCoordsets(CMesh,LSMesh)

CSurface = GetSurface(CMesh,1)
LSSurface = GetSurface(LSMesh,1)

XMesh = CreateMesh()
XSurface = CreateSurface(XMesh)

;copy vertices
For VI% = 0 To CountVertices(CSurface)-1 Step 1
  VX# = VertexX(CSurface,VI) : VY# = VertexY(CSurface,VI) : VZ# = VertexZ(CSurface,VI)
  AddVertex(XSurface,VX,VY,VZ)
Next

;copy triangles
For TI% = 0 To CountTriangles(CSurface)-1 Step 1
  V0I% = TriangleVertex(CSurface,TI,0) : V1I% = TriangleVertex(CSurface,TI,1) : V2I% = TriangleVertex(CSurface,TI,2)
  AddTriangle(XSurface,V0I,V1I,V2I)
Next

;copy vertices normals
For VI% = 0 To CountVertices(CSurface)-1 Step 1
  VNX# = VertexNX(CSurface,VI) : VNY# = VertexNY(CSurface,VI) : VNZ# = VertexNZ(CSurface,VI)
  VertexNormal(XSurface,VI,VNX,VNY,VNZ)
Next

;copy vertices UVs of coordset 0 (from CSurface)
For VI% = 0 To CountVertices(CSurface)-1 Step 1
  VU# = VertexU(CSurface,VI,0) : VV# = VertexV(CSurface,VI,0)
  VertexTexCoords(XSurface,VI,VU,VV,0,0)
Next

;copy vertices UVs of coordset 1 (from LSSurface)
For VI% = 0 To CountVertices(LSSurface)-1 Step 1
  VU# = VertexU(LSSurface,VI,0) : VV# = VertexV(LSSurface,VI,0)
  VertexTexCoords(XSurface,VI,VU,VV,0,1)
Next

;DebugLog(CountVertices(XSurface)+"vertices"+" "+CountTriangles(XSurface)+"triangles")

Return XMesh

End Function

Function CopyTexture(InTex)

InWidth% = TextureWidth(InTex) : InHeight% = TextureHeight(InTex)
OutTex = CreateTexture(InWidth,InHeight,1)
CopyRect(0,0,InWidth,InHeight,0,0,TextureBuffer(InTex),TextureBuffer(OutTex))
Return OutTex

End Function

an example with meshes and textures : http://rd-stuff.fr/blitz3d/merge-one-mesh-with-UVs-for-diffuse-and-one-mesh-with-UVs-for-lightmap-in-one-mesh-20191020.7z