How z-fighting is resolved in unity

Source: Internet
Author: User

If there is a problem in the screen that the picture flashes. Well, most of the cases are caused by z-fighting,

Solution:


1, in each scene, find the Maincamera, and then on the inspector, find Maincamera Properties, Clipping Planes, need to do is to maximize the near value, minimize the far value. According to my experimental results, the same changes in the near value will be better than the magnitude of the far value. If near from 1 to 20 may have fixed a z-fighting, but far from 1000 to 500 is still useless. This can be noted in practice.

2, if the scene has no way to change the above values, then there is the way to find the model that produces z-fighting, so that the model produces the phenomenon of the two faces as far as possible to leave some distance, how much distance only through the experiment to know.

3, if possible, the program can be used polygon Offset, this is the interface of OpenGL,

[CPP]View PlainCopy
    1. Drawoneplane ();
    2. Draw other plane in the almost same position, before use offset, it'll fighting with the one we have drawn.
    3. Glenable (Gl_polygon_offset_fill);
    4. Glpolygonoffset (G_offsetfactor, g_offsetunit);
    5. Drawotherplane ();
    6. Glpolygonoffset (0.0f, 0.0f);
    7. Gldisable (Gl_polygon_offset_fill);


4, if it is D3D, then this article refers to the 3 way, the last way is the same as the above OpenGL idea.

http://software.intel.com/zh-cn/articles/alternatives-to-using-z-bias-to-fix-z-fighting-issues/

A, Project matrix

[CPP]View PlainCopy
  1. Zfighting Solution #1-projection Matrix
  2. D3dxmatrix Mprojectionmat; //holds default projection matrix
  3. D3dxmatrix Mzbiasedprojectionmat; //Holds zbiased projection matrix
  4. Globals used for Projection matrix
  5. float g_fbasenearclip = 1.0f;
  6. float g_fbasefarclip = 100.0f;
  7. Code indicates no change. IE states ' near and far clipping planes pushed out ' but only far appears pushed
  8. float G_fnearclipbias = 0.0f;
  9. float G_ffarclipbias = 0.5f;
  10. Projection Matrix work around
  11. Best if calculation is done outside of render function.
  12. The "zbiased" projection has it in and far clipping
  13. Planes pushed out ...
  14. D3DXMATRIXPERSPECTIVEFOVLH (&mzbiasedprojectionmat, D3dx_pi/4, (Mprojectionmat._22/mprojectionmat._11),
  15. G_fbasenearclip + G_fnearclipbias,
  16. G_fbasefarclip + G_ffarclipbias);
  17. . . .
  18. Original projection is loaded
  19. M_pd3ddevice->settransform (d3dts_projection, & Mprojectionmat);
  20. Billboards is rendered ...
  21. The "zbiased" projection is loaded ...
  22. M_pd3ddevice->settransform (D3dts_projection, &mzbiasedprojectionmat);
  23. Posters is rendered ...
  24. Original projection is reloaded ...
  25. G_pd3ddevice->settransform (d3dts_projection, & Mprojectionmat);
  26. . . .


b, view port matrix

[CPP]View PlainCopy
  1. Zfighting Solution #2-viewport
  2. D3dviewport9 Mviewport; //Holds viewport data
  3. D3dviewport9 Mnewviewport; //Holds new viewport data
  4. Global used for Viewport
  5. Hard coded for ZBIAS of 1 using the This formula
  6. minz-256/(2^24-1) and
  7. maxz-256/(2^24-1)
  8. 2^24 comes from the amount of zbits and the
  9. For Intel (R) Integrated Graphics, but can is any
  10. multiple of 16.
  11. float G_fviewportbias = 0.0000152588f;
  12. Projection Matrix work around
  13. Viewport work around
  14. M_pd3ddevice->getviewport (&mviewport);
  15. Copy Old Viewport to New
  16. Mnewviewport = Mviewport;
  17. Change by the bias
  18. Mnewviewport.minz-= G_fviewportbias;
  19. MNEWVIEWPORT.MAXZ-= G_fviewportbias;
  20. . . .
  21. Original Viewport is reloaded ...
  22. M_pd3ddevice->setviewport (&mviewport);
  23. Billboards is rendered ...
  24. The new viewport is loaded ...
  25. M_pd3ddevice->setviewport (&mnewviewport);
  26. Posters is rendered ...
  27. Original Viewport is reloaded ...
  28. M_pd3ddevice->setviewport (&mviewport);
  29. . . .


C, Depth-bias

[CPP]View PlainCopy
  1. BOOL M_bdepthbiascap; //TRUE If device has Depthbias Caps
  2. Globals used for Depth Bias
  3. float G_fslopescaledepthbias = 1.0f;
  4. float G_fdepthbias = -0.0005f;
  5. float G_fdefaultdepthbias = 0.0f;
  6. Check for devices which support the new depth bias caps
  7. if ((Pcaps->rastercaps & D3dprastercaps_slopescaledepthbias) &&
  8. (Pcaps->rastercaps & D3dprastercaps_depthbias))
  9. {
  10. M_bdepthbiascap = true; //TRUE, if Depthbias Caps
  11. }
  12. Billboards is rendered ...
  13. Depthbias work around
  14. if (M_BDEPTHBIASCAP) //TRUE, if Depthbias supported
  15. {
  16. //used to determine how much bias can be applied
  17. //To Co-planar primitives to reduce Z fighting
  18. //bias = (max * d3drs_slopescaledepthbias) + D3drs_depthbias,
  19. //Where Max is the maximum depth slope of the triangle being rendered.
  20. M_pd3ddevice->setrenderstate (D3drs_slopescaledepthbias, F2DW (G_fslopescaledepthbias));
  21. M_pd3ddevice->setrenderstate (D3drs_depthbias, F2DW (G_fdepthbias));
  22. }
  23. Posters is rendered ...
  24. if (M_BDEPTHBIASCAP) //TRUE, if Depthbias supported
  25. {
  26. //Depthbias work around
  27. //Set it back to zero (default)
  28. M_pd3ddevice->setrenderstate (D3drs_slopescaledepthbias, F2DW (G_fdefaultdepthbias));
  29. M_pd3ddevice->setrenderstate (D3drs_depthbias, F2DW (G_fdefaultdepthbias));
  30. }
  31. . . .


5, here is another article teaches how to draw the line on the surface, I believe you will encounter the same problem. Of course, the core is still used Depth-bias, but, how to do and do the time to pay attention to the problems are written on (mainly with bias when the value does not take too big, otherwise the wireframe is rendered out, but also more out of the line, because it was originally cut, after the correction may not be cut). Worth a look.

http://www.catalinzima.com/samples/12-months-12-samples-2008/drawing-wireframes-without-z-fighting/

Background principle:


The appearance of z-fighting is that the pixels on the different sides are similar in z-buffer, causing the foreground to take the pixels, and one will take that face. Changing the near and far properties of the camera will involve the precision of the values in the Z-buffer. Because the number of Z-buffer on each platform is different, changing the near and far can give the floating-point portion of the value in Z-buffer as much space as possible, eliminating z-fighting.

This is the wiki on the z-fighting example, you can see the different colors of the face, because the z-buffer of the accuracy of the small, and then two faces placed relatively near, so there has been related to the problem of interspersed display.

How z-fighting is resolved in unity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.