Page 1 of 1

How do I put different textures on the sides of a cube?

Posted: Thu Feb 15, 2024 2:38 am
by jacdelad
Hi,
I want to put different textures on a cube, like a six sided dice. How do I do this? I thought CreateCubeTexture() would do this, but it does not work. Then I tried creating two cubes with different textures and place them next to each other (to realize at least two different sides), but this doesn't work either and is surely totally wrong. Can someone give me a hint?

Shouldn't this example put a different texture on each cube side?

Code: Select all

InitEngine3D() : InitSprite() : InitKeyboard()

  OpenWindow(0, 0,0, 800, 600, "VertexColour - [Esc] quit", #PB_Window_ScreenCentered)
  OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0)

  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/desert.zip", #PB_3DArchive_Zip)
  Parse3DScripts()

  CreateCamera(0, 0, 0, 100, 100)
  MoveCamera(0,0,0,-8)
  CameraLookAt(0,0,0,0)

  CreateLight(0,$ffffff, -100, 100, 50)
  AmbientColor($111111*2)
  CameraBackColor(0,$880044)

  ;SkyBox("desert07.jpg")

  LoadTexture(0,"desert07_RT.jpg")
  LoadTexture(1,"desert07_LF.jpg")
  LoadTexture(2,"desert07_UP.jpg")
  LoadTexture(3,"desert07_DN.jpg")
  LoadTexture(4,"desert07_FR.jpg")
  LoadTexture(5,"desert07_BK.jpg")
  CreateCubicTexture(10, 0, 1, 2, 3, 4, 5)

  LoadTexture(11, "dirt.jpg")
  CreateMaterial(0, TextureID(11))
  AddMaterialLayer(0, TextureID(10), #PB_Material_Add)
  SetMaterialAttribute(0, #PB_Material_EnvironmentMap, #PB_Material_ReflectionMap, 1)

  CreateCube(0,2)
  CreateEntity(0, MeshID(0), MaterialID(0))

  Repeat
    While WindowEvent():Wend
    ExamineKeyboard()
    RotateEntity(0,1,1,1, #PB_Relative)
    RenderWorld()
    FlipBuffers()    
  Until KeyboardReleased(#PB_Key_Escape)

Re: How do I put different textures on the sides of a cube?

Posted: Fri Feb 16, 2024 1:32 am
by box_80
If you want to make a six sided dice. What you want to do is make a mesh in blender or another 3d modeling software, where you can set the uv coordinates for the faces of the mesh with a texture and use LoadMesh. Using CreateCube don't allow much control over the UV coordinates of the mesh. You might be able to use in SetMeshData(). I have not used it myself yet.

Re: How do I put different textures on the sides of a cube?

Posted: Fri Feb 16, 2024 2:15 am
by jacdelad
Hm, thanks. I am totally new to 3D programming, so I've never used blender before. Also the result shall be a virtual PCB, so it is created dynamically.

Also, I already solved...circumvented the problem by glueing two tubes together (for top and bottom side), which works quite good for now.

Re: How do I put different textures on the sides of a cube?

Posted: Fri Feb 16, 2024 11:21 pm
by Comtois
change MeshVertexTextureCoordinate() if you need to rotate texture for each submesh

Code: Select all

InitEngine3D()
InitSprite()
InitKeyboard()

ExamineDesktops()
OpenScreen(DesktopWidth(0), DesktopHeight(0), DesktopDepth(0), "Cube 3D")


Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/desert.zip", #PB_3DArchive_Zip)

Global Angle.f,Pas.f, CameraMode.l


;-Textures
LoadTexture(0,"desert07_RT.jpg")
LoadTexture(1,"desert07_LF.jpg")
LoadTexture(2,"desert07_UP.jpg")
LoadTexture(3,"desert07_DN.jpg")
LoadTexture(4,"desert07_FR.jpg")
LoadTexture(5,"desert07_BK.jpg")


;-Matière
CreateMaterial(0, TextureID(0))
CreateMaterial(1, TextureID(1))
CreateMaterial(2, TextureID(2))
CreateMaterial(3, TextureID(3))
CreateMaterial(4, TextureID(4))
CreateMaterial(5, TextureID(5))



;-Mesh
#Mesh = 0
CreateMesh(#Mesh)

;Dessus 
MeshVertexPosition(-0.5,0.5,-0.5)
MeshVertexNormal(0,1,0)
MeshVertexTextureCoordinate(0,0)

MeshVertexPosition(0.5,0.5,-0.5)
MeshVertexNormal(0,1,0)
MeshVertexTextureCoordinate(1,0)

MeshVertexPosition(0.5,0.5,0.5)
MeshVertexNormal(0,1,0)
MeshVertexTextureCoordinate(1,1)

MeshVertexPosition(-0.5,0.5,0.5)
MeshVertexNormal(0,1,0)
MeshVertexTextureCoordinate(0,1)

MeshFace(2,1,0)
MeshFace(0,3,2)

;Dessous 
AddSubMesh()
MeshVertexPosition(-0.5,-0.5,0.5)
MeshVertexNormal(0,-1,0)
MeshVertexTextureCoordinate(0,0)

MeshVertexPosition(0.5,-0.5,0.5)
MeshVertexNormal(0,-1,0)
MeshVertexTextureCoordinate(1,0)

MeshVertexPosition(0.5,-0.5,-0.5)
MeshVertexNormal(0,-1,0)
MeshVertexTextureCoordinate(1,1)

MeshVertexPosition(-0.5,-0.5,-0.5)
MeshVertexNormal(0,-1,0)
MeshVertexTextureCoordinate(0,1)

MeshFace(2,1,0)
MeshFace(0,3,2)


;Devant 
AddSubMesh()
MeshVertexPosition(-0.5,0.5,0.5)
MeshVertexNormal(0,0,1)
MeshVertexTextureCoordinate(0,0)

MeshVertexPosition(0.5,0.5,0.5)
MeshVertexNormal(0,0,1)
MeshVertexTextureCoordinate(1,0)

MeshVertexPosition(0.5,-0.5,0.5)
MeshVertexNormal(0,0,1)
MeshVertexTextureCoordinate(1,1)

MeshVertexPosition( -0.5,-0.5,0.5)
MeshVertexNormal(0,0,1)
MeshVertexTextureCoordinate(0,1)

MeshFace(2,1,0)
MeshFace(0,3,2)

;Derrière
AddSubMesh()
MeshVertexPosition(0.5,0.5,-0.5)
MeshVertexNormal(0,0,-1)
MeshVertexTextureCoordinate(0,0)

MeshVertexPosition(-0.5,0.5,-0.5)
MeshVertexNormal(0,0,-1)
MeshVertexTextureCoordinate(1,0)

MeshVertexPosition(-0.5,-0.5,-0.5)
MeshVertexNormal(0,0,-1)
MeshVertexTextureCoordinate(1,1)

MeshVertexPosition(0.5,-0.5,-0.5)
MeshVertexNormal(0,0,-1)
MeshVertexTextureCoordinate(0,1)

MeshFace(2,1,0)
MeshFace(0,3,2)

;Cote gauche 
AddSubMesh()
MeshVertexPosition(-0.5,0.5,-0.5)
MeshVertexNormal(-1,0,0)
MeshVertexTextureCoordinate(0,0)

MeshVertexPosition(-0.5,0.5,0.5)
MeshVertexNormal(-1,0,0)
MeshVertexTextureCoordinate(1,0)

MeshVertexPosition(-0.5,-0.5,0.5)
MeshVertexNormal(-1,0,0)
MeshVertexTextureCoordinate(1,1)

MeshVertexPosition(-0.5,-0.5,-0.5)
MeshVertexNormal(-1,0,0)
MeshVertexTextureCoordinate(0,1)

MeshFace(2,1,0)
MeshFace(0,3,2)

;Cote droit
AddSubMesh()
MeshVertexPosition(0.5,0.5,0.5)
MeshVertexNormal(1,0,0)
MeshVertexTextureCoordinate(0,0)

MeshVertexPosition(0.5,0.5,-0.5)
MeshVertexNormal(1,0,0)
MeshVertexTextureCoordinate(1,0)

MeshVertexPosition(0.5,-0.5,-0.5)
MeshVertexNormal(1,0,0)
MeshVertexTextureCoordinate(1,1)

MeshVertexPosition(0.5,-0.5,0.5)
MeshVertexNormal(1,0,0)
MeshVertexTextureCoordinate(0,1)

MeshFace(2,1,0)
MeshFace(0,3,2)

FinishMesh(#True)
NormalizeMesh(#Mesh)

UpdateMeshBoundingBox(#Mesh)

SetMeshMaterial(#Mesh, MaterialID(0), 0)
SetMeshMaterial(#Mesh, MaterialID(1), 1)
SetMeshMaterial(#Mesh, MaterialID(2), 2)
SetMeshMaterial(#Mesh, MaterialID(3), 3)
SetMeshMaterial(#Mesh, MaterialID(4), 4)
SetMeshMaterial(#Mesh, MaterialID(5), 5)

;-Entity
#Entity = 0
CreateEntity(#Entity, MeshID(#Mesh), #PB_Material_None)
ScaleEntity(#Entity, 90, 90, 90) ; Agrandi l'entity

;-Camera
#Camera = 0
CreateCamera(#Camera, 0, 0 , 100 , 100)
MoveCamera(#Camera, 0, 0, -400)
CameraLookAt(#Camera, EntityX(#Entity), EntityY(#Entity), EntityZ(#Entity))


;-Light
AmbientColor(RGB(255,255,255)) ; Réduit la lumière ambiante pour mieux voir les lumières


;Modifier la vitesse de rotation en changeant la valeur de 'pas'
pas = 0.8

Repeat
  
  Angle + Pas
  RotateEntity(0,angle,angle/2,-Angle)
  
  If ExamineKeyboard()
    If KeyboardReleased(#PB_Key_F1)
      CameraMode=1-CameraMode
      CameraRenderMode(#Camera, CameraMode)
    EndIf
  EndIf
  
  RenderWorld()
  
  
  FlipBuffers()
  
Until KeyboardPushed(#PB_Key_Escape)

Re: How do I put different textures on the sides of a cube?

Posted: Sat Feb 17, 2024 12:55 am
by jacdelad
Oooooh, great. I will be to work with that. Thanks very much!

Re: How do I put different textures on the sides of a cube?

Posted: Mon Feb 19, 2024 6:24 pm
by pf shadoko
hi comtois
there is a slightly shorter syntax (meshvertex(), meshface() with the 4th parameter)

Code: Select all

InitEngine3D()
InitSprite()
InitKeyboard()

ExamineDesktops()
OpenScreen(DesktopWidth(0), DesktopHeight(0), DesktopDepth(0), "Cube 3D")


Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Packs/desert.zip", #PB_3DArchive_Zip)

Global Angle.f,Pas.f, CameraMode.l


;-Textures
LoadTexture(0,"desert07_RT.jpg")
LoadTexture(1,"desert07_LF.jpg")
LoadTexture(2,"desert07_UP.jpg")
LoadTexture(3,"desert07_DN.jpg")
LoadTexture(4,"desert07_FR.jpg")
LoadTexture(5,"desert07_BK.jpg")


;-Matière
CreateMaterial(0, TextureID(0))
CreateMaterial(1, TextureID(1))
CreateMaterial(2, TextureID(2))
CreateMaterial(3, TextureID(3))
CreateMaterial(4, TextureID(4))
CreateMaterial(5, TextureID(5))


;-Mesh
#Mesh = 0
CreateMesh(#Mesh)

;Dessus 
MeshVertex(-0.5,0.5,-0.5,0,0,0,0,1,0)
MeshVertex(0.5,0.5,-0.5,1,0,0,0,1,0)
MeshVertex(0.5,0.5,0.5,1,1,0,0,1,0)
MeshVertex(-0.5,0.5,0.5,0,1,0,0,1,0)
MeshFace(3,2,1,0)

;Dessous 
AddSubMesh()
MeshVertex(-0.5,-0.5,0.5,0,0,0,0,-1,0)
MeshVertex(0.5,-0.5,0.5,1,0,0,0,-1,0)
MeshVertex(0.5,-0.5,-0.5,1,1,0,0,-1,0)
MeshVertex(-0.5,-0.5,-0.5,0,1,0,0,-1,0)
MeshFace(3,2,1,0)

;Devant 
AddSubMesh()
MeshVertex(-0.5,0.5,0.5,0,0,0,0,0,1)
MeshVertex(0.5,0.5,0.5,1,0,0,0,0,1)
MeshVertex(0.5,-0.5,0.5,1,1,0,0,0,1)
MeshVertex( -0.5,-0.5,0.5,0,1,0,0,0,1)
MeshFace(3,2,1,0)

;Derrière
AddSubMesh()
MeshVertex(0.5,0.5,-0.5,0,0,0,0,0,-1)
MeshVertex(-0.5,0.5,-0.5,1,0,0,0,0,-1)
MeshVertex(-0.5,-0.5,-0.5,1,1,0,0,0,-1)
MeshVertex(0.5,-0.5,-0.5,0,1,0,0,0,-1)
MeshFace(3,2,1,0)

;Cote gauche 
AddSubMesh()
MeshVertex(-0.5,0.5,-0.5,0,0,0,-1,0,0)
MeshVertex(-0.5,0.5,0.5,1,0,0,1,0,0)
MeshVertex(-0.5,-0.5,0.5,1,1,0,-1,0,0)
MeshVertex(-0.5,-0.5,-0.5,0,1,0,-1,0,0)
MeshFace(3,2,1,0)

;Cote droit
AddSubMesh()
MeshVertex(0.5,0.5,0.5,0,0,0,1,0,0)
MeshVertex(0.5,0.5,-0.5,1,0,0,1,0,0)
MeshVertex(0.5,-0.5,-0.5,1,1,0,1,0,0)
MeshVertex(0.5,-0.5,0.5,0,1,0,1,0,0)
MeshFace(3,2,1,0)

FinishMesh(#True)
NormalizeMesh(#Mesh)

UpdateMeshBoundingBox(#Mesh)

SetMeshMaterial(#Mesh, MaterialID(0), 0)
SetMeshMaterial(#Mesh, MaterialID(1), 1)
SetMeshMaterial(#Mesh, MaterialID(2), 2)
SetMeshMaterial(#Mesh, MaterialID(3), 3)
SetMeshMaterial(#Mesh, MaterialID(4), 4)
SetMeshMaterial(#Mesh, MaterialID(5), 5)

;-Entity
#Entity = 0
CreateEntity(#Entity, MeshID(#Mesh), #PB_Material_None)
ScaleEntity(#Entity, 90, 90, 90) ; Agrandi l'entity

;-Camera
#Camera = 0
CreateCamera(#Camera, 0, 0 , 100 , 100)
MoveCamera(#Camera, 0, 0, -400)
CameraLookAt(#Camera, EntityX(#Entity), EntityY(#Entity), EntityZ(#Entity))


;-Light
AmbientColor(RGB(255,255,255)) ; Réduit la lumière ambiante pour mieux voir les lumières


;Modifier la vitesse de rotation en changeant la valeur de 'pas'
pas = 0.8

Repeat
  
  Angle + Pas
  RotateEntity(0,angle,angle/2,-Angle)
  
  If ExamineKeyboard()
    If KeyboardReleased(#PB_Key_F1)
      CameraMode=1-CameraMode
      CameraRenderMode(#Camera, CameraMode)
    EndIf
  EndIf
  
  RenderWorld()
  
  
  FlipBuffers()
  
Until KeyboardPushed(#PB_Key_Escape)

Re: How do I put different textures on the sides of a cube?

Posted: Tue Mar 26, 2024 3:06 am
by jacdelad
Oh my, it took me a long time to adapt this tiny codes, but it works now. Thanks very much, Comtois and pf shadoko!!!
Image

Re: How do I put different textures on the sides of a cube?

Posted: Tue Mar 26, 2024 3:40 am
by marc_256
Hi jacdelad,

Very, very nice, well done.
Love your work here.

marc

Re: How do I put different textures on the sides of a cube?

Posted: Tue Mar 26, 2024 3:55 am
by jacdelad
Thanks very much. Unfortunately the board is still missing the parts, but it slowly evolves. I'll release the source when it's done (the Gerber module just needs one more feature to be 100% compatible with Gerber-X (minus the attributes, which don't affect drawing)).

Re: How do I put different textures on the sides of a cube?

Posted: Tue Mar 26, 2024 9:25 am
by marc_256
Hi,
jacdelad wrote: Tue Mar 26, 2024 3:06 am Oh my, it took me a long time to adapt this tiny codes, but it works now.
I love to quote ...
IdeasVacuum
If it sounds simple, you have not grasped the complexity.