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

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

Comments

0 comments

Please sign in to leave a comment.