it is possible to access and manipulate any part of the mesh after you make it first. you want to texture only the cube top using the uv numbers even it is the last defined cube face.
the solution is to copy the cube mesh to a special structured array with function GetMeshData, and in the structured array change anything (positions, uv, etc) and save it again to the mesh with function SetMeshData.
1- press 'D' you will see the cube top go up 1 unit, press D again and again. look procedure deform() to see how it is done.
2- press 'U' all the faces textures will be textured white and then will texture again the top face as smiley face texture , look procedure change_uv()
MeshDataInd(30)\Index refer to vertex 20. and we access vertex 20 y data either with:
the array MeshDataInd(..)\Index contains the vertices numbers sequence in which every triple of points define a triangle. it is a tricky concept but structured and working okay.
Code: Select all
Declare DrawCube()
Declare triangle(num.l)
Declare change_uv()
Declare deform()
Global Dim MeshData.PB_MeshVertex(0)
Global Dim MeshDataInd.PB_MeshFace(0)
Define.f keyX, keyY, MouseX, MouseY, CameraSpeed = 0.5
Enumeration
#mesh
#entity
#light
#camera
#tex2
#cube
#sphere
EndEnumeration
InitEngine3D() ;#PB_Engine3D_DebugLog
InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
OpenWindow(0,0,0,DesktopWidth(0), DesktopHeight(0),"'W' toggle wire/solid .... 'U' call change_uv ...'D' deform cube top ... 'space' .. tell what triangle the sphere on .. arrows+mouse Cam ",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,DesktopWidth(0), DesktopHeight(0),1,0,0,#PB_Screen_WaitSynchronization)
Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Parse3DScripts()
Global monitor=CreateSprite(#PB_Any,120,40)
;Global texture = GetScriptMaterial(#PB_Any, "Texture1")
Global texture = CreateMaterial(#PB_Any, LoadTexture(222, "texture1.png"))
;MaterialShadingMode(texture, #PB_Material_Wireframe)
MaterialCullingMode(texture, #PB_Material_NoCulling)
KeyboardMode(#PB_Keyboard_AllowSystemKeys)
CreateMaterial(#tex2, LoadTexture(#tex2, "Wood.jpg"))
MaterialCullingMode(#tex2, #PB_Material_NoCulling)
CreatePlane(500, 30, 30, 1, 1, 1, 1)
CreateEntity(500, MeshID(500), MaterialID(#tex2), 0,-10,0)
CreateLight(0,RGB(255,255,255),6,13,0)
;Create Camera
CreateCamera(0,0,0,100,100)
MoveCamera(0,0,20, 50,#PB_Absolute)
CameraLookAt(0, 0, 0, 0)
CameraBackColor(0, RGB(0,200,200))
AmbientColor(RGB(255,255,255))
CreateSphere(#sphere, 0.5)
CreateEntity(#sphere, MeshID(#sphere), #PB_Material_None ,0,2,0)
;-Mesh
DrawCube()
GetMeshData(#mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Color | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate | #PB_Mesh_Tangent , 0, MeshVertexCount(#Mesh)-1)
GetMeshData(#mesh,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(#mesh, 0)-1)
;Debug MeshIndexCount(#mesh)/3
wireFrame = 1 : rot=0
;WorldShadows(#PB_Shadow_Additive)
Global tri=0
triangle(tri)
SetMeshData(#mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Color | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate | #PB_Mesh_Tangent, 0, MeshVertexCount(#mesh)-1)
UpdateMeshBoundingBox(#mesh)
Global deform = 0
Repeat
Repeat
Until WindowEvent() = 0
If ExamineMouse()
MouseX = -MouseDeltaX() * CameraSpeed * 0.2
MouseY = -MouseDeltaY() * CameraSpeed * 0.2
EndIf
ShowCursor_(0)
; Use arrow keys and mouse to rotate camera and fly in/out
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Left)
KeyX.f = -CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX.f = CameraSpeed
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = CameraSpeed
Else
KeyY = 0
EndIf
If KeyboardReleased(#PB_Key_W)
If wireFrame
MaterialShadingMode(texture, #PB_Material_Wireframe)
wireFrame ! 1
Else
MaterialShadingMode(texture, #PB_Material_Solid)
wireFrame ! 1
EndIf
EndIf
If KeyboardReleased(#PB_Key_D)
deform()
EndIf
If KeyboardReleased(#PB_Key_U)
change_uv()
EndIf
If KeyboardReleased(#PB_Key_Space)
tri+1
If tri*3 > MeshIndexCount(#mesh)-1
tri=0
EndIf
triangle(tri)
EndIf
RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
MoveCamera (0, KeyX, 0, KeyY)
RenderWorld()
StartDrawing(SpriteOutput(monitor))
DrawText(5,5,"the ball in Triangle " )
DrawText(5,20,"Triangle= " +" ")
DrawText(5,20,"Triangle= " +Str(tri))
StopDrawing()
DisplaySprite(monitor,0,0)
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow
;main drawing routine
Procedure DrawCube()
CreateMesh(#mesh, #PB_Mesh_TriangleList, #PB_Mesh_Dynamic)
;back
MeshVertexPosition(-5, 5, -5)
MeshVertexNormal(0,0,-1)
MeshVertexTextureCoordinate(1/6, 0)
MeshVertexPosition(5, 5, -5)
MeshVertexNormal(0,0,-1)
MeshVertexTextureCoordinate(0,0)
MeshVertexPosition(5, -5, -5)
MeshVertexNormal(0,0,-1)
MeshVertexTextureCoordinate(0,1)
MeshVertexPosition(-5, -5, -5)
MeshVertexNormal(0,0,-1)
MeshVertexTextureCoordinate(1/6, 1)
;front
MeshVertexPosition(-5, 5, 5)
MeshVertexNormal(0,0,1)
MeshVertexTextureCoordinate(1/6, 0)
MeshVertexPosition(5, 5, 5)
MeshVertexNormal(0,0,1)
MeshVertexTextureCoordinate(2/6, 0)
MeshVertexPosition(5, -5, 5)
MeshVertexNormal(0,0,1)
MeshVertexTextureCoordinate(2/6, 1)
MeshVertexPosition(-5, -5, 5)
MeshVertexNormal(0,0,1)
MeshVertexTextureCoordinate(1/6, 1)
;left
MeshVertexPosition(-5, 5, -5)
MeshVertexNormal(-1,0,0)
MeshVertexTextureCoordinate(4/6, 0)
MeshVertexPosition(-5, 5, 5)
MeshVertexNormal(-1,0,0)
MeshVertexTextureCoordinate(5/6, 0)
MeshVertexPosition(-5, -5, 5)
MeshVertexNormal(-1,0,0)
MeshVertexTextureCoordinate(5/6, 1)
MeshVertexPosition(-5, -5, -5)
MeshVertexNormal(-1,0,0)
MeshVertexTextureCoordinate(4/6, 1)
;right
MeshVertexPosition(5, 5, -5)
MeshVertexNormal(1,0,0)
MeshVertexTextureCoordinate(4/6, 0)
MeshVertexPosition(5, -5, -5)
MeshVertexNormal(1,0,0)
MeshVertexTextureCoordinate(4/6, 1)
MeshVertexPosition(5, 5, 5)
MeshVertexNormal(1,0,0)
MeshVertexTextureCoordinate(3/6, 0)
MeshVertexPosition(5, -5, 5)
MeshVertexNormal(1,0,0)
MeshVertexTextureCoordinate(3/6, 1)
;bottom
MeshVertexPosition(-5, -5, -5)
MeshVertexNormal(0,1,0)
MeshVertexTextureCoordinate(5/6, 1)
MeshVertexPosition(5, -5, -5)
MeshVertexNormal(0,1,0)
MeshVertexTextureCoordinate(6/6, 1)
MeshVertexPosition(5, -5, 5)
MeshVertexNormal(0,1,0)
MeshVertexTextureCoordinate(6/6, 0)
MeshVertexPosition(-5, -5, 5)
MeshVertexNormal(0,1,0)
MeshVertexTextureCoordinate(5/6, 0)
;top
MeshVertexPosition(-5, 5, -5)
MeshVertexNormal(0,1,0)
MeshVertexTextureCoordinate(2/6,0)
MeshVertexPosition(5, 5, -5)
MeshVertexNormal(0,1,0)
MeshVertexTextureCoordinate(3/6,0)
MeshVertexPosition(5, 5, 5)
MeshVertexNormal(0,1,0)
MeshVertexTextureCoordinate(3/6,1)
MeshVertexPosition(-5, 5, 5)
MeshVertexNormal(0,1,0)
MeshVertexTextureCoordinate(2/6,1)
; connecting vertices together
MeshFace(0, 2, 3)
MeshFace(0, 1, 2)
MeshFace(4, 6, 5)
MeshFace(4, 7, 6)
MeshFace(8, 10, 9)
MeshFace(8, 11, 10)
MeshFace(12, 14, 13)
MeshFace(13, 14, 15)
MeshFace(16, 17, 18)
MeshFace(16, 18, 19)
MeshFace(20, 23, 21)
MeshFace(21, 23, 22)
FinishMesh(#True)
SetMeshMaterial(#mesh, MaterialID(texture))
CreateEntity(#mesh, MeshID(#mesh), MaterialID(texture) ,0,0,0)
EndProcedure
Procedure triangle(num.l)
i = 3*num
x1.f = MeshData(MeshDataInd(i)\Index)\x
y1.f = MeshData(MeshDataInd(i)\Index)\y
z1.f = MeshData(MeshDataInd(i)\Index)\z
x2.f = MeshData(MeshDataInd(i+1)\Index)\x
y2.f = MeshData(MeshDataInd(i+1)\Index)\y
z2.f = MeshData(MeshDataInd(i+1)\Index)\z
x3.f = MeshData(MeshDataInd(i+2)\Index)\x
y3.f = MeshData(MeshDataInd(i+2)\Index)\y
z3.f = MeshData(MeshDataInd(i+2)\Index)\z
If deform ; to deform the triangle vertices or not
MeshData(MeshDataInd(i)\Index)\z - 2
MeshData(MeshDataInd(i+1)\Index)\z - 2
MeshData(MeshDataInd(i+2)\Index)\z - 2
EndIf
SetMeshData(#mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Color | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate | #PB_Mesh_Tangent, 0, MeshVertexCount(#mesh)-1)
;position a sphere at the center of the triangle
MoveEntity(#sphere, (x1+x2+x3)/3,(y1+y2+y3)/3,(z1+z2+z3)/3, #PB_Absolute)
EndProcedure
Procedure change_uv()
i=0
;the uv numbers below refer to a white point on the texture, you can make it 0,0
While i<=32
MeshData(MeshDataInd(i)\Index)\u = 180/510
MeshData(MeshDataInd(i)\Index)\v = 6/74 : i+1
MeshData(MeshDataInd(i)\Index)\u = 180/510
MeshData(MeshDataInd(i)\Index)\v = 6/74 : i+1
MeshData(MeshDataInd(i)\Index)\u = 180/510
MeshData(MeshDataInd(i)\Index)\v = 6/74 : i+1
MeshData(MeshDataInd(i)\Index)\u = 180/510
MeshData(MeshDataInd(i)\Index)\v = 6/74 : i+1
Wend
;Debug MeshDataInd(34)\Index
;cube top
MeshData(MeshDataInd(30)\Index)\u = 2/6
MeshData(MeshDataInd(30)\Index)\v = 0
MeshData(MeshDataInd(33)\Index)\u = 3/6
MeshData(MeshDataInd(33)\Index)\v = 0
MeshData(MeshDataInd(34)\Index)\u = 2/6
MeshData(MeshDataInd(34)\Index)\v = 1
MeshData(MeshDataInd(35)\Index)\u = 3/6
MeshData(MeshDataInd(35)\Index)\v = 1
SetMeshData(#mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Color | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate | #PB_Mesh_Tangent, 0, MeshVertexCount(#mesh)-1)
EndProcedure
Procedure deform()
GetMeshData(#mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Color | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate | #PB_Mesh_Tangent , 0, MeshVertexCount(#Mesh)-1)
;GetMeshData(#mesh,0, MeshDataInd(), #PB_Mesh_Face, 0, MeshIndexCount(#mesh, 0)-1)
MeshData(20)\y + 1 ; the same as: MeshData(MeshDataInd(30)\Index)\y + 1
MeshData(21)\y + 1
MeshData(22)\y + 1
MeshData(23)\y + 1
SetMeshData(#mesh,0, MeshData(), #PB_Mesh_Vertex | #PB_Mesh_Color | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate | #PB_Mesh_Tangent, 0, MeshVertexCount(#mesh)-1)
EndProcedure