Remove Z-Fighting via Intersections (plugin)

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

PluginShowcase

Usage

  1. Click the ‘Z’ plugin icon from the plugin tab in Studio.
  2. Select two parts which overlap
  3. 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)
7 Likes

If you have three parts overlapping, you should union them.

6 Likes

unions can be expensive and also mess up collisions, I think it’s better to avoid them.

4 Likes

It looks like he is trying to make a part with those parts. If you just changed their size to avoid z-fighting, it would look terrible.

2 Likes

To add onto @CCTVStudios, Unions doesn’t work on Meshes, which makes it more difficult to fix Z-Fighting on meshes.

Does changing the size by 0.001 make it look terrible?

1 Like

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.

1 Like

1: ZFighting
2: Size changed
3: Union

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.

2 Likes

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.

1 Like

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.

1 Like

however it locks the material to one, Sure, it merges, but it merges the material of the first selected instance, Here is three parts

  1. Ashpalt
  2. Brick
  3. Concrete

  1. Z-Fighting
  2. Slight Size Change
  3. Union

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

Do not use unions. They have all the costs of meshes and none of the benefits.

1 Like

I don’t know how to make meshes, so I’m going to use unions where I need to. What are these costs, anyways?

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.

1 Like

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.

3 Likes

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.

1 Like

???
image

??? r u just rage baiting? i acknowledge this alrdy?