Hi! I made a quick plugin for fixing Z-Fighting between two parts.
Disclaimer
If you have three parts overlapping, this will do little to help. You may find this plugin nice in combination with the brick Cutter plugin.
Showcase
Usage
Click the ‘Z’ plugin icon from the plugin tab in Studio.
Select two parts which overlap
Press Y
Your parts should be fixed
Link
Source
The plugin is free, this is the source code. Feel free to republish with more features (main missing thing is Ctrl-Z functionality)
Code
--!strict
local Selection = game:GetService("Selection")
local CAS = game:GetService("ContextActionService")
local function getCommonAncestor(a: Instance?, b: Instance?): Instance?
while a ~= b do
a = a and a.Parent
b = b and b.Parent
end
return a
end
local function fixAsync(a: BasePart, b: BasePart)
local io = a:IntersectAsync({ b }) :: IntersectOperation
if not io then
return false
end
local parent = getCommonAncestor(a, b)
if not parent then
return false
end
io.Parent = parent
local newA = a:SubtractAsync({ io })
newA.Name = a.Name
newA.Parent = a.Parent
a:Destroy()
local newB = b:SubtractAsync({ io })
newB.Name = b.Name
newB.Parent = b.Parent
b:Destroy()
return true
end
local function setupBind()
CAS:BindAction("FixZFight_Plugin", function(_, state)
local sel = Selection:Get()
local a = sel[1]
local b = sel[2]
if not a or not a:IsA("BasePart") then
return
end
if not b or not b:IsA("BasePart") then
return
end
pcall(fixAsync, a, b)
end, false, Enum.KeyCode.Y)
end
local function clearBind()
CAS:UnbindAction("FixZFight_Plugin")
end
local toolbar = plugin:CreateToolbar("Z-Fighting")
local pluginButton = toolbar:CreateButton(
"Enable",
"Toggles the plugin's functionality. Use Y to fix parts",
"rbxassetid://16314034881"
) :: PluginToolbarButton
pluginButton.ClickableWhenViewportHidden = false
local isOn = false
pluginButton.Click:Connect(function()
isOn = not isOn
if isOn then
setupBind()
else
clearBind()
end
end)
So the thing I get with this method is that it works well for floors and roofs, since the player willl never notice it, but once you get to walls being joined together, yes it does looks horrendous. Unions work for that case, but they lead you with performance issues if you overuse them, and like you mentioned it doesn’t works on meshes (at least for now as roblox does has plans for meshpart CSG).
I think solving Z fighting is a tricky issue with Roblox’s mapping tools.
The union is the prettiest because the materials merge. With the middle one, it may look good if you ignore how there are clearly different parts making up the path, which doesn’t look good in my opinion. Bland textures like plastic may get away with it as long as they’re the same color.
Unions only mess up collision if your CollisionFidelity is set to Automatic: this is a non-issue. Also, key word on can be expensive. Unions aren’t evil.
Even if your collision fidelity is set to precise it’s not 100% acurrate on complex unions, also they do take up more performance because it’s easier and faster to calculate a collision on a defined shape and a convex hull than it is on a meshpart or union.
Unions are not evil, but don’t use them to create stuff you can do with normal parts.
overall, Changing the size by 0.001 would sort of do a good job, and there is a fine line between the size of the red parts and the size of the green parts
Enum.Material.Plastic has a slightly noticeable material viewing, which was why I overused Enum.Material.SmoothPlastic years ago, but I stopped overusing it
You can quickly convert a union to a mesh by exporting it as an OBJ- then just reimporting the exact file.
The structure of unions poly’s are extremely unoptimized, the geometry as a whole is terrible, so obviously making the mesh from the ground up would be better, but oh well.
As for the benefits- meshes as a whole support level of detailing and different rendering fidelities (iirc unions do not, could be wrong). Meshes also don’t have any information aside from their collisions, makeups, etc… whereas unions also store it’s part “makeup”, for lack of a better term, which is how you can separate them, and restore former states, etc… There are a handful more pro’s, as well as cons, but these are the major ones. These costs add up a good bit.
Converting a union to a mesh that way will not fix the horrible unnecessary geometry, you will need to manually clean it on a program like blender and import it back.
Only use unions if you need destructuble environments and live CSG, otherwise use meshes like Node said.
I mention that within my response. The overall geometry will not be improved, but you get the added benefits of render fidelities, and the overheads of union-csg being removed.