Skip to content

Instantly share code, notes, and snippets.

@grilme99
Last active August 10, 2024 22:38
Show Gist options
  • Save grilme99/8b97cb0b7ca892b0d04202d4d3244eb1 to your computer and use it in GitHub Desktop.
Save grilme99/8b97cb0b7ca892b0d04202d4d3244eb1 to your computer and use it in GitHub Desktop.
Example of Roblox's new DynamicMesh class, generated using ChatGPT. Works at runtime in an ordinary LocalScript.
-- Create a new DynamicMesh instance
local dynamicMesh = Instance.new("DynamicMesh")
-- Function to get perlin noise
local function perlinNoise(x, y, scale, height)
return math.noise(x * scale, y * scale) * height
end
local size = 50 -- Size of the terrain
local scale = 0.1 -- Scale factor for Perlin noise
local height = 5 -- Height factor for Perlin noise
-- Add vertices based on Perlin noise
local vertices = {} -- Stores the vertices
local vertexIds = {} -- Stores the IDs of the vertices
for i = 0, size do
for j = 0, size do
local height = perlinNoise(i, j, scale, height)
local vertex = Vector3.new(i, height, j)
table.insert(vertices, vertex)
local vertexId = dynamicMesh:AddVertex(vertex)
vertexIds[i * (size + 1) + j + 1] = vertexId
dynamicMesh:SetVertexColor(vertexId, BrickColor.new("Bright green").Color)
end
end
-- Calculate normal of a triangle
local function calculateNormal(v1, v2, v3)
local u = v2 - v1
local v = v3 - v1
return u:Cross(v).Unit
end
-- Stores the normals of the vertices
local normals = {}
for i = 0, size - 1 do
for j = 0, size - 1 do
-- Calculate indices
local index0 = i * (size + 1) + j + 1
local index1 = i * (size + 1) + j + 2
local index2 = (i + 1) * (size + 1) + j + 1
local index3 = (i + 1) * (size + 1) + j + 2
-- Get vertices
local v0 = vertices[index0]
local v1 = vertices[index1]
local v2 = vertices[index2]
local v3 = vertices[index3]
-- Calculate normals of the two triangles and average them
local n1 = calculateNormal(v0, v1, v2)
local n2 = calculateNormal(v2, v1, v3)
local normal = (n1 + n2).Unit
-- Store the normal
normals[index0] = normals[index0] and (normals[index0] + normal).Unit or normal
normals[index1] = normals[index1] and (normals[index1] + normal).Unit or normal
normals[index2] = normals[index2] and (normals[index2] + normal).Unit or normal
normals[index3] = normals[index3] and (normals[index3] + normal).Unit or normal
end
end
-- Add normals to the vertices
for i, vertexId in ipairs(vertexIds) do
dynamicMesh:SetVertexNormal(vertexId, normals[i])
end
-- Add triangles to form the terrain
for i = 0, size - 1 do
for j = 0, size - 1 do
-- Calculate indices
local index0 = i * (size + 1) + j + 1
local index1 = i * (size + 1) + j + 2
local index2 = (i + 1) * (size + 1) + j + 1
local index3 = (i + 1) * (size + 1) + j + 2
-- Add triangles
dynamicMesh:AddTriangle(vertexIds[index0], vertexIds[index1], vertexIds[index2])
dynamicMesh:AddTriangle(vertexIds[index2], vertexIds[index1], vertexIds[index3])
end
end
dynamicMesh.Parent = workspace
-- Create a MeshPart from DynamicMesh with Default CollisionFidelity
local meshPart = dynamicMesh:CreateMeshPartAsync(Enum.CollisionFidelity.DynamicPreciseConvexDecomposition)
-- Add the MeshPart to Workspace
meshPart.Parent = workspace
-- Setting some properties of the MeshPart
meshPart.Name = "DynamicMeshPart"
meshPart.Material = Enum.Material.Grass
meshPart.Anchored = true
meshPart.Position = Vector3.new(0, 5, 0)
@grilme99
Copy link
Author

To use:

  1. Using the mod manager, switch to the zintegration branch.
  2. Set the SimEnableDynamicMesh flag to true.
  3. Add the above code to a LocalScript and run!

@Sowd0404
Copy link

such beautiful code

@solarite1797
Copy link

this is awesome thanks

@StevenWinnie
Copy link

StevenWinnie commented Jul 14, 2023

Thanks for sharing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment