Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Erroneous surface coloration shows position of elements that were "differenced" out. #4985

Closed
MethylBromide opened this issue Feb 14, 2024 · 1 comment

Comments

@MethylBromide
Copy link

Describe the bug
The preview panel occasionally displays entire faces of entities that were used as second and later operands in a difference operation, despite that parts of these faces are not adjacent to any occupied volume.

To Reproduce
Steps to reproduce the behavior:

  1. Open OpenSCAD and create a new script.
  2. Paste in and execute the script below.
  3. Observe the preview display shows crosshatching indicating the presence of elements that are not actually part of an entity (see screenshot).

These green crosshatch rectangles indicate the position of a cube entity that was subtracted from the lenticular "ikea" objects to flatten one edge. The edge of the large cube is exactly aligned with the cube used to remove part of the lens shape -- their faces touch but there is no overlap between them. A portion of that face remains as the flattened edge surface of the ikea object, but the whole face should not be shown.

Expected behavior
Entities used as second and subsequent operands in a difference operation, should not be visible in the preview panel at all. This "ghost face" of a missing cube is confusing.

Code reproducing the issue

$fa = 1;
$fs = 0.4; 
module ikea(r = 5, h = 3) {
   difference() {
                union() {
                    translate([0, 0, h/2]) cylinder(h/2, r, r-h/2);
                    cylinder(h/2, r-h/2, r);
                    translate([0,0,h/2-.001]) cylinder(.002,r);
                }
                // trim one side
                translate([-r, -r-h/2, -.5]) cube([r+r, h, h+1]);
    }
}

difference() {
    cube([25, 12, 5]);
    translate([16,-1,1]) cube([10, 11, 3]);
}
translate([6,3.5,1]) ikea();
translate([6,3.5,6]) ikea();
translate([21,3.5,1]) ikea();

Screenshots
Screenshot_171

Environment and Version info (please complete the following information):

  • OS: Windows 11
  • System: Intel PC 64-bit
  • OpenSCAD Version: 2024.02.11 (git dd2da9e) downloaded from OpenSCAD website.
@jordanbrown0
Copy link
Contributor

See also #4286 and many others (#4442, #4780, #4546, #4335, et cetera).

The simple answer is that yes, the previewer gets Z-fighting artifacts any time that a face of a negative object aligns with a face of a positive object. There can be similar issues when faces that have been subtracted away align with positive faces of a different color.

Here's the FAQ entry: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/FAQ#What_are_those_strange_flickering_artifacts_in_the_preview?

There are roughly two strategies for addressing this problem:

  • Arrange that those cases don't happen. The major case there is where you are subtracting a negative object from a positive one and cutting into its surface. Arrange that the negative object extends outside the positive object. (But that doesn't directly help your particular scenario.)
  • Wrap strategic pieces with render(), which forces OpenSCAD to fully evaluate the geometry of that subassembly.

In your particular case, wrapping the difference() in ikea() in a render() appears to address the problem.

...
module ikea(r = 5, h = 3) {
   render() difference() {
                union() {
...

Why does it do this, and why doesn't somebody fix it? Because the previewer does black magic to get its performance, and it's a wonder that it works as well as it does, and nobody has a better answer. Recent work to dramatically improve render performance may provide a viable replacement, though it's still slower than the previewer and more limited in other ways like color and transparency support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants