How to view UV of an EditableMesh and apply textures to individual faces

I’m trying to use an EditableMesh to create a cube, and then render textures on each face (using EditableImage)

This creates a cube, with all 6 sides. When I print GetUVs, it’s printing 8. Idk much about modelling, but a cube has 6 faces, I assumed the UVs would be the 6 faces of a cube? And then from there, how can I apply individual textures onto each individual face?

local BLOCK_SIZE = 4

local Cube = {}
Cube.__index = Cube

local AssetService = game:GetService("AssetService")

local function AddSharpQuad(editableMesh, vertex0, vertex1, vertex2, vertex3)
	-- Add a new normal for this face
	local Normal = editableMesh:AddNormal()

	-- Create two triangles to form the quad
	local Triangle1 = editableMesh:AddTriangle(vertex0, vertex1, vertex2)
	editableMesh:SetFaceNormals(Triangle1, { Normal, Normal, Normal })

	local Triangle2 = editableMesh:AddTriangle(vertex0, vertex2, vertex3)
	editableMesh:SetFaceNormals(Triangle2, { Normal, Normal, Normal })
end

function Cube.new()
	local self = setmetatable({}, Cube)
		
	return self
end

function Cube:CreateCube(position)
	-- Create empty EditableMesh
	local EditableMesh = AssetService:CreateEditableMesh()

	-- Add vertices for a unit cube
	local Vertex1 = EditableMesh:AddVertex(Vector3.new(0, 0, 0))
	local Vertex2 = EditableMesh:AddVertex(Vector3.new(1, 0, 0))
	local Vertex3 = EditableMesh:AddVertex(Vector3.new(0, 1, 0))
	local Vertex4 = EditableMesh:AddVertex(Vector3.new(1, 1, 0))
	local Vertex5 = EditableMesh:AddVertex(Vector3.new(0, 0, 1))
	local Vertex6 = EditableMesh:AddVertex(Vector3.new(1, 0, 1))
	local Vertex7 = EditableMesh:AddVertex(Vector3.new(0, 1, 1))
	local Vertex8 = EditableMesh:AddVertex(Vector3.new(1, 1, 1))

	-- Add the six faces of the cube
	AddSharpQuad(EditableMesh, Vertex5, Vertex6, Vertex8, Vertex7) -- front
	AddSharpQuad(EditableMesh, Vertex1, Vertex3, Vertex4, Vertex2) -- back
	AddSharpQuad(EditableMesh, Vertex1, Vertex5, Vertex7, Vertex3) -- left
	AddSharpQuad(EditableMesh, Vertex2, Vertex4, Vertex8, Vertex6) -- right
	AddSharpQuad(EditableMesh, Vertex1, Vertex2, Vertex6, Vertex5) -- bottom
	AddSharpQuad(EditableMesh, Vertex3, Vertex7, Vertex8, Vertex4) -- top

	-- Clean up unused normals
	EditableMesh:RemoveUnused()
	
	print(EditableMesh:GetUVs())
	
	-- Create the preview MeshPart
	local PreviewMesh = AssetService:CreateMeshPartAsync(Content.fromObject(EditableMesh))
	PreviewMesh.Anchored = true
	PreviewMesh.Position = Vector3.new(position.X, position.Y, position.Z)
	PreviewMesh.Size = Vector3.new(BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
	PreviewMesh.Parent = workspace
	
	PreviewMesh.TextureContent = Content.fromObject(Texture)
	
	return PreviewMesh, EditableMesh
end

return Cube
1 Like

UV is on vertices. The cube has 8 vertices therefore 8 UVs.
We have an example place file which has an EditableImage applied on EditableMesh. Check out MeshPainting module. I hope this would help.

1 Like

I’ve tried looking over that file, but it’s just wayy too complex for me to understand :smiling_face_with_tear:

I’m wanting to just do a cube, not a complex mesh. The example also doesn’t seem to deal with textures, just drawing rectangles onto a mesh. I’m wanting a specific texture id to be put on a specific face of the mesh