Files
PlaneBuilding/scripts/modules/TerrainModule.lua
T

143 lines
4.5 KiB
Lua

local is_client = game.Players.LocalPlayer
if is_client then return {} end
local scale = 20 -- * 10
local generation_folder = game.Workspace.Generation
local function triangle(p1,p2,p3,r1,r2) -- simple function, might work?
local s = 0.01
local bottom = {
b1 = nil,
b2 = nil,
dist = 0,
other = nil
}
local function check(point1,point2,other)
local dist = (point1 - point2).Magnitude
if dist > bottom.dist then
bottom.b1 = point1
bottom.b2 = point2
bottom.other = other
bottom.dist = dist
end
end
check(p1,p2,p3) -- check all of the different options for bottom and get the right one?
check(p1,p3,p2)
check(p2,p3,p1)
local view = CFrame.lookAt(bottom.b1,bottom.b2,(bottom.other-bottom.b1).Unit)
local object = view:PointToObjectSpace(bottom.other)
local length = object.Z
local height = object.Y
r1.Size = Vector3.new(0.25,height-s,-length-s)
r1.CFrame = (view * CFrame.Angles(0,math.rad(180),0)) * CFrame.new(0,height/2+s/2,-length/2+s/2)
r2.Size = Vector3.new(0.25,height-s,-(-bottom.dist-length)-s)
r2.CFrame = view * CFrame.new(0,height/2+s/2,-(bottom.dist-length)/2+s/2)
--[[if r1.Position.Y < 0 then
r1.Color = Color3.new(0.764706, 0.756863, 0.478431)
r1.Material = Enum.Material.Sand
end
if r2.Position.Y < 0 then
r2.Color = Color3.new(0.764706, 0.756863, 0.478431)
r2.Material = Enum.Material.Sand
end]]
return r1,r2
end
local editable_mesh_template = Instance.new("EditableMesh")
local editable_mesh_add_vertex = editable_mesh_template.AddVertex
local editable_mesh_add_triangle = editable_mesh_template.AddTriangle
local ta
local tb
local tc
local function ntri(mesh,a,b,c,d)
ta = editable_mesh_add_vertex(mesh,a)
tb = editable_mesh_add_vertex(mesh,b)
tc = editable_mesh_add_vertex(mesh,c)
editable_mesh_add_triangle(mesh,ta,tb,tc)
ta = editable_mesh_add_vertex(mesh,a)
tb = editable_mesh_add_vertex(mesh,c)
tc = editable_mesh_add_vertex(mesh,d)
editable_mesh_add_triangle(mesh,ta,tb,tc)
end
local function part()
local part = Instance.new("Part")
part.Parent = generation_folder
part.Anchored = true
return part
end
local function tri(mat)
local new = mat:Clone()
new.Parent = generation_folder
return new
end
local fidelity = 20
local tree = _G.Storage.Tree
local mat_sand = game.Workspace:WaitForChild("SandTriangle")
local mat_grass = game.Workspace:WaitForChild("GrassTriangle")
local tri_union_pool = {}
local function unionTable(tbl)
if #tbl > 2 then
local start = tbl[#tbl]
table.remove(tbl,#tbl)
start:UnionAsync(tbl).Parent = game.Workspace
for _,item in pairs(tbl) do
item:Destroy()
end
print("union!")
end
end
local function chunk(sx,sy,ox,oy)
--[[for i=18,2,-4 do
local water = _G.Storage.Water:Clone()
water.Size = Vector3.new(sx,0,sy)*scale*scale
water.Position = Vector3.new(ox*sx+sx/2,-2,oy*sy+sy/2)*scale + Vector3.new(0,i,0)
water.Parent = generation_folder
end]]
--[[local object = Instance.new("MeshPart")
object.Parent = generation_folder
object.Name = "MeshObject"
object.Anchored = true
object.Material = Enum.Material.Grass
object.Color = Color3.new(0.254902, 0.403922, 0.133333)
object.Size = Vector3.new(1,1,1)
local edit = Instance.new("EditableMesh")
edit.Parent = object
edit.Name = "VertexObject"
local rows = {}
local last_column = nil
local sumtris = {}
for x=ox*sx,ox*sy+sx,fidelity do
local columns = {}
table.insert(rows,columns)
local last_index = 1
for y=oy*sy,oy*sy+sy,fidelity do
local biome = math.clamp(math.noise(x/1000,y/1000,-6.432),0,1)*50
local island = math.clamp(math.noise(x/200,y/200,-2.132)-0.3,0,1)*30
local height = math.noise(x/10,y/10,0.123) + math.clamp(math.noise(x/50,y/50,0.123),0.2,1)*3 - (0.2-math.clamp(math.abs(math.noise(x/50,y/50,5.213)),0,0.2))*10 + island - 8 + biome
table.insert(columns,Vector3.new(x,height*2,y))
if last_column and last_index > 1 and height > -5 then
local mat = mat_grass
local current = columns[last_index]*scale
local left = columns[last_index-1]*scale
local up_left = last_column[last_index-1]*scale
local up = last_column[last_index]*scale
--ntri(edit,current,up,up_left,left)
local t1,t2 = triangle(current,up,up_left,tri(mat),tri(mat))
local t3,t4 = triangle(current,left,up_left,tri(mat),tri(mat))
table.insert(sumtris,t1)
table.insert(sumtris,t2)
table.insert(sumtris,t3)
table.insert(sumtris,t4)
end
last_index = last_index + 1
end
last_column = columns
end
task.delay(0,function()unionTable(sumtris)end)]]
end
for x=-6,6 do
for y=-6,6 do
wait()
chunk(100,100,x,y)
end
end
return {}