您的位置:首页 > 移动开发 > Unity3D

z-fighting在unity中的解决方式

2016-09-01 17:37 253 查看
如果在画面中,发现有画面闪烁的问题。那么大多数情况下是z-fighting引起的,

解决方案:

1, 在每个场景中,找到那个MainCamera,然后在Inspector上,找到MainCamera的属性,Clipping Planes,需要做的是尽量放大near的值,尽量减小far的值。根据我的实验结果,同样改动Near值的幅度比Far值的幅度相对来说效果会更好。如Near从1到20可能修正了某个z-fighting,但是Far从1000改到500也还是没有用。这个在实践中可以注意。

2, 如果场景没有办法改动上述的值,那么还有的方式就是找到产生z-fighting的模型,让模型产生这个现象的两个面尽量离开一些距离,究竟多少距离只有通过实验才知道。

3, 如果可能,程序上就可以用Polygon Offset,这个是OpenGL的接口,

[cpp] view plain copy

drawOnePlane();

//Draw other plane in the almost same position, before use offset, it will fighting with the one we has drawn.

glEnable( GL_POLYGON_OFFSET_FILL );

glPolygonOffset( g_OffsetFactor, g_OffsetUnit );

drawOtherPlane();

glPolygonOffset( 0.0f, 0.0f );

glDisable( GL_POLYGON_OFFSET_FILL );

4、如果是D3D,那么这篇文章提到3中方式,最后一种方式是跟上面的OpenGL思路一样的。

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

a, project matrix

[cpp] view plain copy

// 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 are done outside of render function.

// The "zbiased" projection has it near 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 are rendered...

// The "zbiased" projection is loaded ...

m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &mZBiasedProjectionMat);

// Posters are rendered...

// Original projection is reloaded...

g_pd3dDevice->SetTransform( D3DTS_PROJECTION, & mProjectionMat);

. . .

b, view port matrix

[cpp] view plain copy

// 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 this formula

// MinZ - 256/(2^24-1) and

// MaxZ - 256/(2^24-1)

// 2^24 comes from the amount of Zbits and the 256 works

// for Intel (R) Integrated Graphics, but can be 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 are rendered …

// The new viewport is loaded …

m_pd3dDevice->SetViewport(&mNewViewport);

// Posters are rendered …

// Original viewport is reloaded …

m_pd3dDevice->SetViewport(&mViewport);

. . .

c, depth - bias

[cpp] view plain copy

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 are 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 are 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,这里还有一篇文章教到怎么样在面上画出线的,相信你也会遇到同样的问题。当然,核心还是用depth-bias,但是,人家把怎么做和做的时候要注意的问题都写上了(主要就是用bias的时候值不要取的太大,否则线框是渲染出来了,但是也同时多出线来了,因为本来是该本剪切的,经过修正可能就不被剪切了)。值得一看。

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

背景原理:

z-fighting的出现是的不同面上的像素在z-buffer中的值相近,导致前台取像素的时候一会去这个面的,一会取那个面的。改变照相机的near、far属性会涉及到z-buffer中的值的精度。因为在各个平台上z-buffer位数不同,因此改变near和far能给z-buffer中的值的浮点数部分尽量留出空间,消除z-fighting。

这个是wiki上的z-fighting的例子,可以看到不同色彩的面,因为z-buffer的精度设置的小,然后两个面放置的比较近,因此出现了相关的穿插显示的问题。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐