local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local Workspace = game:GetService("Workspace")
local Points = {}
local Color = BrickColor.new("Medium stone grey")
local Material = Enum.Material.SmoothPlastic
local Align = "Normal"
local Thickness = 1
local Tris = {}
function spawnTrianglePart(col,mat)
local P = Instance.new("WedgePart")
P.Anchored = true
P.Parent = Workspace
P.BrickColor = col
P.Material = mat
P.FrontSurface = Enum.SurfaceType.SmoothNoOutlines
P.BackSurface = Enum.SurfaceType.SmoothNoOutlines
P.LeftSurface = Enum.SurfaceType.SmoothNoOutlines
P.RightSurface = Enum.SurfaceType.SmoothNoOutlines
P.TopSurface = Enum.SurfaceType.SmoothNoOutlines
P.BottomSurface = Enum.SurfaceType.SmoothNoOutlines
return P
end
function drawTriangle(a,b,c,n,ad)
local len_AB = (b - a).magnitude
local len_BC = (c - b).magnitude
local len_CA = (a - c).magnitude
if (len_AB > len_BC) and (len_AB > len_CA) then
a,c = c,a
b,c = c,b
elseif (len_CA > len_AB) and (len_CA > len_BC) then
a,b = b,a
b,c = c,b
end
local dot = (a - b):Dot(c - b)
local split = b + (c-b).unit*dot/(c - b).magnitude
local xA = ad[3]
local yA = (split - a).magnitude
local zA = (split - b).magnitude
local xB = ad[3]
local yB = (split - a).magnitude
local zB = (split - c).magnitude
local diry = (a - split).unit
local dirz = (c - split).unit
local dirx = diry:Cross(dirz).unit
local posA = split + diry*yA/2 - dirz*zA/2
local posB = split + diry*yB/2 + dirz*zB/2
local partA = spawnTrianglePart(ad[1],ad[2])
partA.Name = "TrianglePart"
partA.Size = Vector3.new(xA,math.min(yA,2048),math.min(zA,2048))
local mA = Instance.new("SpecialMesh",partA)
mA.MeshType = Enum.MeshType.Wedge
mA.Scale = Vector3.new(xA,yA,zA)/partA.Size
mA.Offset = Vector3.new(-n*(partA.Size.x-xA)/2,0,0)
if mA.Scale == Vector3.new(1,1,1) then mA:Destroy() end
partA.CFrame = CFrame.new(posA.x,posA.y,posA.z, dirx.x,diry.x,dirz.x, dirx.y,diry.y,dirz.y, dirx.z,diry.z,dirz.z)
partA.CFrame = partA.CFrame:toWorldSpace(CFrame.new(n*math.max(.2,xA)/2,0,0))
dirx = dirx * -1
dirz = dirz * -1
local partB = spawnTrianglePart(ad[1],ad[2])
partB.Name = "TrianglePart"
partB.Size = Vector3.new(xB,yB,zB)
local mB = Instance.new("SpecialMesh",partB)
mB.MeshType = Enum.MeshType.Wedge
mB.Scale = Vector3.new(xB,math.min(yB,2048),math.min(zB,2048))/partB.Size
mB.Offset = Vector3.new(n*(partB.Size.x-xB)/2,0,0)
if mB.Scale == Vector3.new(1,1,1) then mB:Destroy() end
partB.CFrame = CFrame.new(posB.x,posB.y,posB.z, dirx.x,diry.x,dirz.x, dirx.y,diry.y,dirz.y, dirx.z,diry.z,dirz.z)
partB.CFrame = partB.CFrame:toWorldSpace(CFrame.new(-n*math.max(.2,xB)/2,0,0))
return partA,partB
end
local function CreateTri()
local Target = Mouse.Target
if (Target ~= nil and Target.Locked == false and not Target:IsA("WedgePart")) then
local Icheck = true
for i,v in next,Points do
if (v[1] == Target) then
Icheck = false
break
end
end
if (Icheck) then
table.insert(Points,{Target,Target.Position})
if (#Points >= 3) then
local Pts = {{Points[1][1],Points[1][2]},{Points[2][1],Points[2][2]},{Points[3][1],Points[3][2]}}
local Data = {Color,Material,Thickness}
local Check = true
for i,v in next,Tris do
local Chcked1 = Pts[1][1] == v[2][1] or Pts[1][1] ==v [3][1] or Pts[1][1] == v[4][1]
local Chcked2 = Pts[2][1] == v[2][1] or Pts[2][1] ==v [3][1] or Pts[2][1] == v[4][1]
local Chcked3 = Pts[3][1] == v[2][1] or Pts[3][1] ==v [3][1] or Pts[3][1] == v[4][1]
if (Chcked1 and Chcked2 and Chcked3) then
for _,a in pairs(v[1]) do
a:Destroy()
end
for _,a in pairs(v[5]) do
a:disconnect()
end
table.remove(Tris,i)
break
end
end
local N =- 1
if (Align == "Center") then
N = 0
elseif (Align == "Inverted") then
N = 1
end
local nTris={}
nTris[1],nTris[2]=drawTriangle(Points[1][2],Points[2][2],Pts[3][2],N,Data)
local nFunc={}
table.insert(Tris,{nTris,Pts[1],Pts[2],Pts[3],nFunc,Data})
for i=1,3 do
table.insert(nFunc,Points[i][1].Changed:connect(function(property)
if (property=="Position") then
Pts[i][2] = Pts[i][1].Position
for i,v in next,nTris do
v:Destroy()
end
nTris[1],nTris[2]=drawTriangle(Pts[1][2],Pts[2][2],Pts[3][2],N,Data)
end
end))
end
Points={}
end
end
end
end
Mouse.Button1Down:connect(CreateTri)
Change Align to one of this
Center or Inversed