How to avoid Z-fighting

Suppose that you have two overlapped regions and you want to avoid Z-fighting.

Capture.PNG

If you want to prevent Z-fighting, you can follow the approach reported below.

First of all, you should decide which entity you prefer to display on top (in this case I choose CircularRegion).

Next, you should create a new class "MyRegion" that derives from CircularRegion, and override Draw() and Render() methods.

class MyRegion : CircularRegion 
{ 
   public MyRegion(double x, double y, double radius)
      : base(x, y, radius) 
   { 
   } 

   protected override void Draw(DrawParams data)
   { 
      data.RenderContext.PushRasterizerState(); 
      data.RenderContext.SetState(rasterizerStateType.CCW_PolygonFill_NoCullFace_PolygonOffset_Minus3Minus2);

      base.Draw(data); 

      data.RenderContext.PopRasterizerState(); 
   } 

   protected override void Render(RenderParams data)
   { 
      data.RenderContext.PushRasterizerState(); 
      data.RenderContext.SetState(rasterizerStateType.CCW_PolygonFill_NoCullFace_PolygonOffset_Minus3Minus2);

      base.Render(data); 

      data.RenderContext.PopRasterizerState(); 
   } 
}

Finally, when you draw the two entities:

MyRegion circ = new MyRegion(80, 80, 30); 
circ.ColorMethod = colorMethodType.byEntity; 
circ.Color = Color.Red; 
viewportLayout1.Entities.Add(circ); 

RectangularRegion rect = new RectangularRegion(200, 200); 
rect.Color = Color.Gray; 
rect.ColorMethod = colorMethodType.byEntity; 
viewportLayout1.Entities.Add(rect);

you will obtain the following result:

Capture1.PNG

Previous versions of this article: Eyeshot 7

Was this article helpful?
3 out of 3 found this helpful
Have more questions? Submit a request

Comments

2 comments
  • If a "Surface" is only single sided, and the drawing it done on the underside (i.e. derived class from `LinearPath`) facing outwards, Then this method will force the appearance to be flipped to the inside.

     

    0
  • This is the solution I am currently using:

    private void BringCloserToEye()
    {
    var viewNormal = ActiveViewport.Camera.ViewNormal;
    viewNormal *= 70; // Move the points 70 mm closer to the observer so that they appear in front of the object drawn upon
    Transformation transform = new Translation(viewNormal);
    // Check to see if the overhead of parallel is going to be worth it.
    if (points.Count > 16)
    {
    Parallel.ForEach(points, pt => { pt.TransformBy(transform); });
    }
    else
    {
    foreach (Point3D pt in points)
    {
    pt.TransformBy(transform);
    }
    }
    }
    0

Please sign in to leave a comment.