So, I’ve been making a model pixelator that makes use of raycasting. But there is this one annoying issue that is stopping this system from being great. And that is these annoying line-shaped holes in the pixelated model.
even with regular parts and bricks the issue still occurs.
Its most likely to do a math error of mine, but i cannot confirm that. Some help will be greatly appreciated
PLACE FILE:
Model pixelater.rbxl (93.4 KB)
SCRIPT:
--== SETTINGS ==--
local ModelToPixelate = workspace:WaitForChild("Cube") -- The model to pixelate
local ScanningSize = 100 -- The area to scan in all 3 axis
local ScanDistance = 500 -- How far the scanning ray is casted
local KeepMaterials = true -- Use the same materials that the model has
local KeepTransparency = true -- Use the same transparent areas that the model has
local DebugMode = true -- Show the scanning ray's Origin position
--== FUNCTIONS ==--
local BlockCount = 0
local BlockSize = 1 -- do not change
local DebugBrick1
local function round(n)
return math.floor(n + 0.5)
end
local PixelsGroup = Instance.new("Model")
PixelsGroup.Name = "Pixelated " .. ModelToPixelate.Name
PixelsGroup.Parent = ModelToPixelate
function CreateBlock(PartColour, PartMaterial, PartPosition, PartTransparency)
print("Created block")
local Block = Instance.new("Part")
Block.Name = "PixelBlock"
Block.Anchored = true
Block.Size = Vector3.new(BlockSize, BlockSize, BlockSize)
Block.TopSurface = Enum.SurfaceType.Smooth
Block.BottomSurface = Enum.SurfaceType.Smooth
Block.Color = PartColour
Block.Position = Vector3.new(round(PartPosition.X), round(PartPosition.Y), round(PartPosition.Z))
if PartMaterial and KeepMaterials then
Block.Material = PartMaterial
else
Block.Material = Enum.Material.SmoothPlastic
end
if PartTransparency and KeepTransparency then
Block.Transparency = PartTransparency
end
Block.Parent = PixelsGroup
BlockCount = BlockCount + 1
return Block
end
function ScanFaceAndPixelate(Origin, Direction)
if DebugBrick1 then
DebugBrick1.Position = Origin
end
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Whitelist
Params.FilterDescendantsInstances = {ModelToPixelate}
local ScanRay = workspace:Raycast(Origin, Direction, Params)
if ScanRay then
local Hit = ScanRay.Instance
if Hit and Hit:IsA("BasePart") and Hit.Name ~= "PixelBlock" then
CreateBlock(Hit.Color, Hit.Material, ScanRay.Position)
end
end
end
--== MAIN ==--
if ModelToPixelate and ModelToPixelate.PrimaryPart then
local OriginalModelToPixelateCFrame = ModelToPixelate:GetPrimaryPartCFrame()
local CurrentPos = ModelToPixelate.PrimaryPart.Position + Vector3.new(ScanningSize / 2, ScanningSize / 2, ScanningSize / 2)
if DebugMode then
DebugBrick1 = CreateBlock(Color3.fromRGB(255, 0, 0), nil, CurrentPos)
DebugBrick1.Shape = Enum.PartType.Ball
DebugBrick1.Name = "DebugPart"
DebugBrick1.Transparency = 0.5
DebugBrick1.CanCollide = false
end
-- Begin pixelating the model
function ScanModel()
local CurrentPos = ModelToPixelate.PrimaryPart.Position + Vector3.new(ScanningSize / 2, ScanningSize / 2, ScanningSize / 2)
for index = 1, ScanningSize/BlockSize do
wait()
for index = 1, ScanningSize/BlockSize do
--print(CurrentPos)
ScanFaceAndPixelate(CurrentPos, CurrentPos - Vector3.new(0, ScanDistance, 0))
CurrentPos = CurrentPos - Vector3.new(BlockSize, 0, 0)
end
CurrentPos = Vector3.new(ScanningSize / 2, ScanningSize / 2, CurrentPos.Z) - Vector3.new(0, 0, BlockSize)
end
end
print("Scanning side 1 out of 6 | Front")
ModelToPixelate:SetPrimaryPartCFrame(CFrame.Angles(0, 0, 0))
ScanModel()
print("Scanning side 2 out of 6 | Right")
ModelToPixelate:SetPrimaryPartCFrame(CFrame.Angles(math.rad(90), 0, 0))
ScanModel()
print("Scanning side 3 out of 6 | Back")
ModelToPixelate:SetPrimaryPartCFrame(CFrame.Angles(math.rad(180), 0, 0))
ScanModel()
print("Scanning side 4 out of 6 | Left")
ModelToPixelate:SetPrimaryPartCFrame(CFrame.Angles(math.rad(270), 0, 0))
ScanModel()
print("Scanning side 5 out of 6 | Top")
ModelToPixelate:SetPrimaryPartCFrame(CFrame.Angles(0, 0, math.rad(90)))
ScanModel()
print("Scanning side 6 out of 6 | Bottom")
ModelToPixelate:SetPrimaryPartCFrame(CFrame.Angles(0, 0, math.rad(270)))
ScanModel()
ModelToPixelate:SetPrimaryPartCFrame(OriginalModelToPixelateCFrame)
if DebugBrick1 then
DebugBrick1:Destroy()
end
PixelsGroup.Parent = workspace
print(ModelToPixelate.Name .. " has been successfully pixelated! | " .. BlockCount .. " parts created")
else
error("'ModelToPixelate' is nil or 'ModelToPixelate' has no set PrimaryPart")
end