Hello,
I’ve been fiddling with this theory of breaking up a region made up by the BoundingBox of a model into multiple smaller parts, and these parts must not exceed over 2,000 studs of length, height, or width; and if so, It should cap the size(s) at that amount, and create more smaller boxes to fill in the gap(s).
This is a model created by the code I put together (code is below)
I’m running into a couple issues; the first one is that the fragmentated parts’ sizes are not finely accurate & sometimes protrude out the bounding box (the yellow part in the background), this can be seen easier by analyzing the red & green blocks on the top right and bottom left corners (the model I’m using as reference) which is where they should stop exceeding.
The overlapping issues has been making my head scratch, as well as aligning the part segments according to their respected positions from the origin of the bounding box, & as for the “missing” gap a the bottom right corner, I figured that this is just the remainder part that must be created separately from the nested loop function, but for some reason it also seems inaccurate in size & position when I do create it (this part is at the bottom of the code, commented out)
How would I be able to properly fragmentize the region of the bounding box and shift them to their appropriate positions, but also fill in the bounding box entirely without having the parts overlap from the yellow bounding box in the background? Was my approach accurate or not at all? Where can I look for a better understanding of what I’m trying to do?
Thank you all for the read, sorry in advance for the lengthy post.
My code sample to create the models shown above:
local part = Instance.new("Part")
part.TopSurface, part.BottomSurface = 0, 0
part.Anchored, part.CanCollide = true, false
part.Color = Color3.new(1,1,0)
--part.Material = "Neon"
part.Transparency = 0.5
local debris = game:GetService("Debris")
local maxSize = 2e3 -- on each axis
local model = workspace.Model
local boxOrigin, boxSize = model:GetBoundingBox()
local simulator = part:Clone() do
simulator.Name = "0"
simulator.Color = Color3.new(1,1,0)
simulator.Size = Vector3.new(1,1,1)
simulator.CFrame = boxOrigin
local m = Instance.new("BlockMesh", simulator)
m.Scale = boxSize
simulator.Parent = workspace
debris:AddItem(simulator, 15)
end
local ratio = boxSize / maxSize
local xAspectRatio = ratio.X
local yAspectRatio = ratio.Y
local zAspectRatio = ratio.Z
local segmentsX = math.floor(xAspectRatio)--xAspectRatio > 1 and math.floor(xAspectRatio) or 1
local segmentsY = math.floor(yAspectRatio)--yAspectRatio > 1 and math.floor(yAspectRatio) or 1
local segmentsZ = math.floor(zAspectRatio)--zAspectRatio > 1 and math.floor(zAspectRatio) or 1
local remainderX = xAspectRatio - segmentsX
local remainderY = yAspectRatio - segmentsY
local remainderZ = zAspectRatio - segmentsZ
local remainderVector = Vector3.new(remainderX, remainderY, remainderZ)
--print(segmentsX, remainderX, "\n", segmentsY, remainderY, "\n", segmentsZ, remainderZ)
--print(boxSize)
warn("MAX SIZE", boxSize)
local rng = Random.new()
local count = 0
for x = 0, segmentsX do
for y = 0, segmentsY do
for z = 0, segmentsZ do
local account = 0
for i, v in pairs({x, y, z}) do
if v > 0 then
account += 1
end
end
if account > 0 then
count += 1
local sizeAxis = Vector3.new(
x > 0 and 1 or 0,
y > 0 and 1 or 0,
z > 0 and 1 or 0
)
--print(x, y, z)
local remainderAxis = Vector3.new(
x > 0 and 0 or 1,
y > 0 and 0 or 1,
z > 0 and 0 or 1
)
local size = sizeAxis * maxSize-- + remainderAxis * boxSize
local remainderSize = remainderAxis * Vector3.new(
boxSize.X > maxSize and boxSize.X * (xAspectRatio - 1) or boxSize.X,
boxSize.Y > maxSize and boxSize.Y * (yAspectRatio - 1) or boxSize.Y,
boxSize.Z > maxSize and boxSize.Z * (zAspectRatio - 1) or boxSize.Z
)
--[[local remainderSize = remainderAxis * Vector3.new(
boxSize.X > maxSize and boxSize.X * remainderX or boxSize.X,
boxSize.Y > maxSize and boxSize.Y * remainderY or boxSize.Y,
boxSize.Z > maxSize and boxSize.Z * remainderZ or boxSize.Z
)]]
local actualSize = size + remainderSize
local offsetAxis = Vector3.new(x,y,z)
local offsetSize = offsetAxis * size/2
local remainderOffsetAxis = Vector3.new(
x > 0 and 0 or 1,
y > 0 and 0 or 1,
z > 0 and 0 or 1
)
local remainderOffset = remainderOffsetAxis * remainderSize
local p = part:Clone()
p.Name = count
p.Color = Color3.fromHSV(rng:NextNumber(0,1),1,1)
p.CFrame = boxOrigin * CFrame.new(actualSize/2 - remainderOffset)
p.Size = actualSize
p.Parent = workspace
debris:AddItem(p, 15)
end
end
end
end
--[[local size = Vector3.new(remainderX, remainderY, remainderZ) * maxSize
local remainder = part:Clone()
remainder.Name = count + 1
remainder.Color = Color3.fromHSV(rng:NextNumber(0,1),1,1)
remainder.Size = size
remainder.CFrame = boxOrigin * CFrame.new(-size/2)-- * CFrame.new(remainder.Size)
remainder.Parent = workspace
debris:AddItem(remainder, 15)]]