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
- Drawoneplane ();
- Draw other plane in the almost same position, before use offset, it'll fighting with the one we have drawn.
- Glenable (Gl_polygon_offset_fill);
- Glpolygonoffset (G_offsetfactor, g_offsetunit);
- Drawotherplane ();
- Glpolygonoffset (0.0f, 0.0f);
- 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
- Zfighting Solution #1-projection Matrix
- D3dxmatrix Mprojectionmat; //holds default projection matrix
- D3dxmatrix Mzbiasedprojectionmat; //Holds zbiased projection matrix
- Globals used for Projection matrix
- float g_fbasenearclip = 1.0f;
- float g_fbasefarclip = 100.0f;
- Code indicates no change. IE states ' near and far clipping planes pushed out ' but only far appears pushed
- float G_fnearclipbias = 0.0f;
- float G_ffarclipbias = 0.5f;
- Projection Matrix work around
- Best if calculation is done outside of render function.
- The "zbiased" projection has it in and far clipping
- Planes pushed out ...
- D3DXMATRIXPERSPECTIVEFOVLH (&mzbiasedprojectionmat, D3dx_pi/4, (Mprojectionmat._22/mprojectionmat._11),
- G_fbasenearclip + G_fnearclipbias,
- G_fbasefarclip + G_ffarclipbias);
- . . .
- Original projection is loaded
- M_pd3ddevice->settransform (d3dts_projection, & Mprojectionmat);
- Billboards is rendered ...
- The "zbiased" projection is loaded ...
- M_pd3ddevice->settransform (D3dts_projection, &mzbiasedprojectionmat);
- Posters is rendered ...
- Original projection is reloaded ...
- G_pd3ddevice->settransform (d3dts_projection, & Mprojectionmat);
- . . .
b, view port matrix
[CPP]View PlainCopy
- Zfighting Solution #2-viewport
- D3dviewport9 Mviewport; //Holds viewport data
- D3dviewport9 Mnewviewport; //Holds new viewport data
- Global used for Viewport
- Hard coded for ZBIAS of 1 using the This formula
- minz-256/(2^24-1) and
- maxz-256/(2^24-1)
- 2^24 comes from the amount of zbits and the
- For Intel (R) Integrated Graphics, but can is any
- multiple of 16.
- float G_fviewportbias = 0.0000152588f;
- Projection Matrix work around
- Viewport work around
- M_pd3ddevice->getviewport (&mviewport);
- Copy Old Viewport to New
- Mnewviewport = Mviewport;
- Change by the bias
- Mnewviewport.minz-= G_fviewportbias;
- MNEWVIEWPORT.MAXZ-= G_fviewportbias;
- . . .
- Original Viewport is reloaded ...
- M_pd3ddevice->setviewport (&mviewport);
- Billboards is rendered ...
- The new viewport is loaded ...
- M_pd3ddevice->setviewport (&mnewviewport);
- Posters is rendered ...
- Original Viewport is reloaded ...
- M_pd3ddevice->setviewport (&mviewport);
- . . .
C, Depth-bias
[CPP]View PlainCopy
- BOOL M_bdepthbiascap; //TRUE If device has Depthbias Caps
- Globals used for Depth Bias
- float G_fslopescaledepthbias = 1.0f;
- float G_fdepthbias = -0.0005f;
- float G_fdefaultdepthbias = 0.0f;
- Check for devices which support the new depth bias caps
- if ((Pcaps->rastercaps & D3dprastercaps_slopescaledepthbias) &&
- (Pcaps->rastercaps & D3dprastercaps_depthbias))
- {
- M_bdepthbiascap = true; //TRUE, if Depthbias Caps
- }
- Billboards is rendered ...
- Depthbias work around
- if (M_BDEPTHBIASCAP) //TRUE, if Depthbias supported
- {
- //used to determine how much bias can be applied
- //To Co-planar primitives to reduce Z fighting
- //bias = (max * d3drs_slopescaledepthbias) + D3drs_depthbias,
- //Where Max is the maximum depth slope of the triangle being rendered.
- M_pd3ddevice->setrenderstate (D3drs_slopescaledepthbias, F2DW (G_fslopescaledepthbias));
- M_pd3ddevice->setrenderstate (D3drs_depthbias, F2DW (G_fdepthbias));
- }
- Posters is rendered ...
- if (M_BDEPTHBIASCAP) //TRUE, if Depthbias supported
- {
- //Depthbias work around
- //Set it back to zero (default)
- M_pd3ddevice->setrenderstate (D3drs_slopescaledepthbias, F2DW (G_fdefaultdepthbias));
- M_pd3ddevice->setrenderstate (D3drs_depthbias, F2DW (G_fdefaultdepthbias));
- }
- . . .
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