local MeshCreation = {}
local chunkSize = 8
local bs = 2.8
local cubeVertices = {
-- Left face
Vector3.new(-1, -1, -1),
Vector3.new(-1, -1, 1),
Vector3.new(-1, 1, 1),
Vector3.new(-1, 1, -1),
-- Right face
Vector3.new( 1, -1, 1),
Vector3.new( 1, -1, -1),
Vector3.new( 1, 1, -1),
Vector3.new( 1, 1, 1),
-- Bottom face
Vector3.new(-1, -1, -1),
Vector3.new( 1, -1, -1),
Vector3.new( 1, -1, 1),
Vector3.new(-1, -1, 1),
-- Top face
Vector3.new(-1, 1, -1),
Vector3.new(-1, 1, 1),
Vector3.new( 1, 1, 1),
Vector3.new( 1, 1, -1),
-- Front face
Vector3.new( 1, -1, -1),
Vector3.new(-1, -1, -1),
Vector3.new(-1, 1, -1),
Vector3.new( 1, 1, -1),
-- Back face
Vector3.new(-1, -1, 1),
Vector3.new( 1, -1, 1),
Vector3.new( 1, 1, 1),
Vector3.new(-1, 1, 1)
}
local cubeFaces = {
Left = {1, 2, 3, 4},
Right = {5, 6, 7, 8},
Bottom = {9, 10, 11, 12},
Top = {13, 14, 15, 16},
Front = {17, 18, 19, 20},
Back = {21, 22, 23, 24}
}
local function getBiomeColor(x, z)
-- This is a placeholder implementation. In a real game, you'd have a more
-- complex biome system. For now, we'll use a simple noise function to
-- generate different colors.
local noise = math.noise(x / 10, z / 10)
return Color3.new(0.2 + noise * 0.3, 0.7 + noise * 0.3, 0.2 + noise * 0.1)
end
local function getUVsForBlock(blockX, blockY)
local textureSize = 16
local u1 = blockX * textureSize
local v1 = blockY * textureSize
local u2 = u1 + textureSize
local v2 = v1 + textureSize
return {
Vector2.new(u1, v2) / 256, -- Top-left
Vector2.new(u2, v2) / 256, -- Top-right
Vector2.new(u2, v1) / 256, -- Bottom-right
Vector2.new(u1, v1) / 256 -- Bottom-left
}
end
local function getTextureCoords(blockType, face)
local textureMap = {
[1] = {all = {0, 0}},
[2] = {all = {1,0}},
[7] = {all = {6, 0}},
[8] = {top = {7,0}, bottom = {6,0}, side = {8,0}}
}
local coords = textureMap[blockType]
if coords.all then
return coords.all
elseif face == "Top" then
return coords.top or coords.all
elseif face == "Bottom" then
return coords.bottom or coords.all
else
return coords.side or coords.all
end
end
local lightlevel = 0
local maxlightlevel = 15
local minbrightness = 0.1
function MeshCreation.generateChunkMesh(x, y, z, chunks)
local meshpart = Instance.new("MeshPart")
meshpart.Name = string.format("%s.%s.%s", x, y, z)
meshpart.Size = Vector3.new(1, 1, 1) * bs
meshpart.Position = Vector3.new(x, y, z) * bs * chunkSize
meshpart.CanCollide = false
meshpart.Anchored = true
local em = Instance.new("EditableMesh")
em.Parent = meshpart
local editableImage = game:GetService("AssetService"):CreateEditableImageAsync("rbxassetid://94341675161722")
editableImage.Parent = meshpart
editableImage:Resize(Vector2.new(256, 256))
local vertexOffset = 0
local function getBlockType(wx, wy, wz)
local cx, cy, cz = math.floor(wx / chunkSize), math.floor(wy / chunkSize), math.floor(wz / chunkSize)
local bx, by, bz = wx % chunkSize, wy % chunkSize, wz % chunkSize
if chunks[cx] and chunks[cx][cy] and chunks[cx][cy][cz] then
return buffer.readu8(chunks[cx][cy][cz].buffer, bx + by * chunkSize + bz * chunkSize * chunkSize)
end
return 0 -- Return air if chunk doesn't exist
end
for nx = 0, chunkSize - 1 do
for ny = 0, chunkSize - 1 do
for nz = 0, chunkSize - 1 do
local wx, wy, wz = x * chunkSize + nx, y * chunkSize + ny, z * chunkSize + nz
local blockType = getBlockType(wx, wy, wz)
if blockType ~= 0 then
for direction, face in pairs(cubeFaces) do
local newpos = Vector3.new(wx, wy, wz) + Vector3.FromNormalId(Enum.NormalId[direction])
local adjBlock = getBlockType(newpos.X, newpos.Y, newpos.Z)
if adjBlock == 0 then
local currentOffset = vertexOffset * 4 -- 4 vertices per face
local textureCoords = getTextureCoords(blockType, direction)
local uvs = getUVsForBlock(textureCoords[1], textureCoords[2])
-- Get biome color for this block
local biomeColor = getBiomeColor(wx, wz)
for i, vertexIndex in ipairs(face) do
local vertex = cubeVertices[vertexIndex]
local position = vertex * 0.5 + Vector3.new(nx, ny, nz) + Vector3.new(0.5, 0.5, 0.5)
local vx = em:AddVertex(position, Vector3.FromNormalId(Enum.NormalId[direction]))
em:SetUV(vx, uvs[i])
-- Apply biome color to vertex
if blockType == 8 and (direction == "Top") then
em:SetVertexColor(vx, biomeColor)
else
em:SetVertexColor(vx, Color3.new(0.5, 0.5, 0.5)) -- Default white for other blocks/faces
end
end
em:AddTriangle(currentOffset + 1, currentOffset + 2, currentOffset + 3)
em:AddTriangle(currentOffset + 1, currentOffset + 3, currentOffset + 4)
vertexOffset = vertexOffset + 1
end
end
end
end
end
end
meshpart.Parent = workspace
return meshpart
end
return MeshCreation
This is my (pretty bad) code for generating chunk meshes for my voxel game right now, but Roblox has changed the API for EditableMeshes in a way that I literally can’t even begin to make it work with the updated API. Can anyone show me how I could make it work for the new API (if even anyone understands it)?