How would i further optimize this module script?
local RIGHT = Vector3.new(1, 0, 0)
local UP = Vector3.new(0, 1, 0)
local BACK = Vector3.new(0, 0, 1)
local PartCache = require(game.ServerScriptService:WaitForChild("Cache"))
--
local function vec3Func(f, v)
return Vector3.new(f(v.x), f(v.y), f(v.z))
end
local function slice(part, wPoint, axis)
local pSize, pCF = part.Size, part.CFrame
local pRot = pCF - pCF.p
local length = axis:Dot(pSize)
local mSize = pSize - axis*length
local max, min = length/2, -length/2
local split = axis:Dot(pCF:PointToObjectSpace(wPoint))
if (split >= min and split <= max) then
local l1, l2 = max - split, split - min
local p1, p2 = pCF*(axis*(l1/2 + split)), pCF*(axis*(-l2/2 + split))
local part1 = nil
local part2 = nil
local Size1 = mSize + axis*l1
local Size2 = mSize + axis*l2
if Size1.X < 0.5 or Size1.Y < 0.5 or Size1.Z < 0.5 then
else
part1 = part
part1.CFrame = CFrame.new(p1) * pRot
part1.Size = Size1
end
if Size2.X < 0.5 or Size2.Y < 0.5 or Size2.Z < 0.5 then
elseif part1 ~= nil then
part2 = part:Clone()
part2.CFrame = CFrame.new(p2) * pRot
part2.Size = Size2
part2.Parent = part1.Parent
end
return part1, part2
end
return part
end
local function cut(part, canvas)
local cf, size2 = part.CFrame, part.Size/2
local canvasCF = canvas[1].CFrame
-- slice up the canvas
for _, enum in next, Enum.Axis:GetEnumItems() do
local axis = Vector3.FromAxis(enum)
local p1, p2 = cf*(axis*size2), cf*(-axis*size2)
local sliceAxis = canvasCF:VectorToObjectSpace(cf:VectorToWorldSpace(axis))
sliceAxis = vec3Func(function(c) return math.min(1, math.abs(c)) end, sliceAxis)
for i = 1, #canvas do
local a, b = slice(canvas[i], p1, sliceAxis)
if a then
canvas[#canvas+1] = a
end
if b then
canvas[#canvas+1] = b
end
end
for i = 1, #canvas do
local a, b = slice(canvas[i], p2, sliceAxis)
if a then
canvas[#canvas+1] = a
end
if b then
canvas[#canvas+1] = b
end
end
end
-- remove the canvas slices that overlap with the part
local lookup = {}
for i,v in pairs(canvas) do
lookup[canvas[i]] = true
--if v.Size.X < 0.5 or v.Size.Y < 0.5 or v.Size.Z < 0.5 then
--v:Destroy()
--end
end
part.Size /= Vector3.new(1.5,1.5,1.5)
local t = part.Touched:Connect(function() end)
local touching = part:GetTouchingParts()
part.Size *= Vector3.new(1.5,1.5,1.5)
t:Disconnect()
for i = 1, #touching do
if (lookup[touching[i]]) then
PartCache:StorePart(touching[i])
end
end
-- return the canvas for further cuting (?)
return canvas
end
--
return cut