<p>The general advice in the OpenSCAD community is to "extend your cuts and embed your joins" (<a href="http://forum.openscad.org/id-tp20439p20460.html" rel="noreferrer">source</a>). The rendering artifacts are one thing but rather just an annoyance; however z-fighting can also cause unexpected errors during STL export (I did not experience that myself so far, just read this somewhere).</p>

<p>So you would change the dimensions of your objects very slightly (<code>0.01</code> mm works fine) so that:</p>

<ul>
<li>for a <code>union()</code>, there is overlap volume between the parts </li>
<li>for a <code>difference()</code>, the intersector has volume both inside and outside of the intersected part</li>
</ul>

<p>Now you could adjust both the size <em>and</em> position of your parts to keep the mathematically exact dimensions for the resulting part. But I found that for the purposes of 3D printing, such accuracy is not worth it because it complicates the formulas so much.</p>

<p>Instead, I adjust <em>either</em> position or size of a part, depending on what is simpler in each case. A measure in the final design will be off by 0.01 mm, which does not matter.</p>

<p>And I keep the 0.01 mm offset in a variable called <code>nothing</code> (picked that up somewhere and liked it …). This keeps the calculations intuitively understandable.</p>

<h2>Example</h2>

<p>To create a cylinder and cut a hole to half of its depth, I would do this:</p>

<pre><code>//!OpenSCAD
nothing=0.01;
height=40;

difference(){
    cylinder(h=height, r=20, center=true);

    translate([0, 0, height/4 + <b>nothing</b>]) 
        cylinder(h=height/2, r=15, center=true);
}
</code></pre>

<p>Now the hole is <code>nothing=0.01</code> less deep than half of the cylinder – that's the inaccuracy I accept.</p>

<p><em>(Note: If you don't have OpenSCAD installed, you can also try the above code online by copy &amp; pasting it into <a href="https://openjscad.org/" rel="noreferrer">OpenJSCAD</a>. Include the magic comment in the first line to switch it to OpenSCAD syntax.)</em></p>

CC BY-SA 4.0
2019-06-25T14:21:00.693Z
This may improve the STL generation.  OpenSCAD has two rendering systems.  The first is used for quick display of the code.  The second is much slower and must be run before generating STL.  In my experience, the first is prone to this problem with zero-thickness layers, but the second is more accurate and is not prone to this problem.   Your experience may be different, but I have not generated bad STL files for images with this display artifact.
cmm
CC BY-SA 4.0
2019-06-25T15:23:31.197Z
@cmm Same here, I also only saw z-fighting in the preview rendering mode so far. The STL issue is something I picked up somewhere else. I made this distinction explicit in the answer now.
tanius
CC BY-SA 4.0
2019-06-28T10:40:43.450Z
You could increase the height of the object by *nothing* to have a perfect part.
Cem Kalyoncu
CC BY-SA 4.0
2019-04-26T20:18:37.673Z
Your answer is the one I'd post for this question. You add the parameter "nothing" while I use "addabit = 0.1" for most z actions.
fred_dot_u
CC BY-SA 4.0
2021-09-27T22:43:12.040Z
I also saw this variable being called `tolerance`, which I personally find less amusing but more descriptive.
user31389
CC BY-SA 4.0
2023-11-06T17:02:30.017Z
I've seen `eps` used to mean `epsilon`--the mathematical arbitrarily small interval used for limits and integration.
Dave X
CC BY-SA 4.0
2019-04-26T17:56:10.070Z
2019-06-25T15:21:12.193Z
2019-06-25T15:21:12.193Z
tanius
<p><a href="https://en.wikipedia.org/wiki/Z-fighting" rel="noreferrer">Z-fighting</a> is a 3D rendering artifact of coplanar surfaces (means, of triangles located in exactly the same plane and overlapping each other).</p>
<p>It can happen in OpenSCAD's preview mode when doing a <code>difference()</code> or <code>union()</code> operation. In case of <code>difference()</code>, the rendering artifacts can prevent seeing into a hole in the object. The &quot;compile and render&quot; mode in OpenSCAD does not have z-fighting issues. But since it can take some time to render an object in that mode, it is not a practical solution during development.</p>
<p>How best to avoid z-fighting?</p>
<p>If the answer involves changes to my OpenSCAD code, I would love to see an idiomatic answer / established convention of OpenSCAD coders, if that exists.</p>

<openscad>
How to prevent z-fighting in OpenSCAD?
tanius
# How to prevent z-fighting in OpenSCAD?

(Asked by: username_0 on 2019-04-26)

[Z-fighting](https://en.wikipedia.org/wiki/Z-fighting) is a 3D rendering artifact of coplanar surfaces (means, of triangles located in exactly the same plane and overlapping each other).

It can happen in OpenSCAD's preview mode when doing a `difference()` or `union()` operation. In case of `difference()`, the rendering artifacts can prevent seeing into a hole in the object. The "compile and render" mode in OpenSCAD does not have z-fighting issues. But since it can take some time to render an object in that mode, it is not a practical solution during development.

How best to avoid z-fighting?

If the answer involves changes to my OpenSCAD code, I would love to see an idiomatic answer / established convention of OpenSCAD coders, if that exists.

**Tags:** openscad


---

## 1 answers

### Answer by username_0 on 2019-04-26. Score: 14 | Accepted answer

The general advice in the OpenSCAD community is to "extend your cuts and embed your joins" ([source](http://forum.openscad.org/id-tp20439p20460.html)). The rendering artifacts are one thing but rather just an annoyance; however z-fighting can also cause unexpected errors during STL export (I did not experience that myself so far, just read this somewhere).

So you would change the dimensions of your objects very slightly (`0.01` mm works fine) so that:

* for a `union()`, there is overlap volume between the parts
* for a `difference()`, the intersector has volume both inside and outside of the intersected part

Now you could adjust both the size *and* position of your parts to keep the mathematically exact dimensions for the resulting part. But I found that for the purposes of 3D printing, such accuracy is not worth it because it complicates the formulas so much.

Instead, I adjust *either* position or size of a part, depending on what is simpler in each case. A measure in the final design will be off by 0.01 mm, which does not matter.

And I keep the 0.01 mm offset in a variable called `nothing` (picked that up somewhere and liked it …). This keeps the calculations intuitively understandable.

## Example

To create a cylinder and cut a hole to half of its depth, I would do this:

```
//!OpenSCAD
nothing=0.01;
height=40;

difference(){
    cylinder(h=height, r=20, center=true);

    translate([0, 0, height/4 + nothing]) 
        cylinder(h=height/2, r=15, center=true);
}

```

Now the hole is `nothing=0.01` less deep than half of the cylinder – that's the inaccuracy I accept.

*(Note: If you don't have OpenSCAD installed, you can also try the above code online by copy & pasting it into [OpenJSCAD](https://openjscad.org/). Include the magic comment in the first line to switch it to OpenSCAD syntax.)*

> **Comments:**
> **username_3**: Your answer is the one I'd post for this question. You add the parameter "nothing" while I use "addabit = 0.1" for most z actions.  
> **username_1**: This may improve the STL generation.  OpenSCAD has two rendering systems.  The first is used for quick display of the code.  The second is much slower and must be run before generating STL.  In my experience, the first is prone to this problem with zero-thickness layers, but the second is more accurate and is not prone to this problem.   Your experience may be different, but I have not generated bad STL files for images with this display artifact.  
> **username_0**: @username_1 Same here, I also only saw z-fighting in the preview rendering mode so far. The STL issue is something I picked up somewhere else. I made this distinction explicit in the answer now.  
> **username_2**: You could increase the height of the object by *nothing* to have a perfect part.  
> **username_4**: I also saw this variable being called `tolerance`, which I personally find less amusing but more descriptive.  
> **username_5**: I've seen `eps` used to mean `epsilon`--the mathematical arbitrarily small interval used for limits and integration.  

---

