z-fighting在unity中的解决方式

如果在画面中,发现有画面闪烁的问题。那么大多数情况下是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

  1. drawOnePlane();
  2. //Draw other plane in the almost same position, before use offset, it will fighting with the one we has 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、如果是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

  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 are done outside of render function.
  12. // The "zbiased" projection has it near 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 are rendered...
  21. // The "zbiased" projection is loaded ...
  22. m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &mZBiasedProjectionMat);
  23. // Posters are rendered...
  24. // Original projection is reloaded...
  25. g_pd3dDevice->SetTransform( D3DTS_PROJECTION, & mProjectionMat);
  26. . . .

b, view port matrix

[cpp] view plain copy

  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 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 256 works
  9. // for Intel (R) Integrated Graphics, but can be 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 are rendered …
  24. // The new viewport is loaded …
  25. m_pd3dDevice->SetViewport(&mNewViewport);
  26. // Posters are rendered …
  27. // Original viewport is reloaded …
  28. m_pd3dDevice->SetViewport(&mViewport);
  29. . . .

c, depth - bias

[cpp] view plain copy

  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 are 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 are 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,这里还有一篇文章教到怎么样在面上画出线的,相信你也会遇到同样的问题。当然,核心还是用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的精度设置的小,然后两个面放置的比较近,因此出现了相关的穿插显示的问题。

时间: 2024-08-03 00:36:36

z-fighting在unity中的解决方式的相关文章

在Unity中定义统一的对象搜索接口

我们经常要在Unity中以各种方式搜索对象.比如按名字搜索.按tag.layer或者是查找名字为xxx开头的对象. 本文是介绍以一种统一的接口来搜索对象. 1.定义统一的搜索接口 /// <summary> /// 游戏对象搜索接口 /// </summary> public interface IGameObjectFinder { /// <summary> /// 搜索 /// </summary> /// <param name="r

SQL安装过程中“针对SQL Server 注册表的一致性验证“出错解决方式

1.打开注册表,查找到[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\0804],分别打开Counter 和Help 2.打开Counter,把滚动条拉倒最后,然后记下最大的那个值(不同计算机不一样的),记下后关闭窗口 3.同样的打开Help,滚动到最后记最大的那个值(你们的值不一定跟我一样的哦!!!),记下后关闭窗口 4,再重新定位到Perflib的节点上, 5.双击Last Counter然后在

MySQL安装过程中出现“APPLY security settings错误”的解决方式

***********************************************声明****************************************************** 原创作品,出自 "晓风残月xj" 博客,欢迎转载,转载时请务必注明出处(http://blog.csdn.net/xiaofengcanyuexj). 因为各种原因.可能存在诸多不足,欢迎斧正. *******************************************

iOS iOS8中 问题&amp;quot;registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later&amp;quot; 解决方式

问题重述: iOS 8中改变了通知注冊的方式,假设App须要同一时候支持iOS 7 和 8 的话,须要首先检查selector. 解决方式:在Xcode 6中 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { //-- Set Notification if ([application respondsToSelector:@s

jsp页面中include静态html出现乱码问题的解决方式

这个问题出现过两次,上次没有做好记录,今天又出现了.不过这两次的情景也不完全一致. 今天通过搜索找到这篇文章的解决方式很好,可供参考.原博客地址http://blog.csdn.net/izgnaw/article/details/6178472. 在web.xml中加入一下代码: <!-- 控制jsp:include的编码 --> <jsp-config> <jsp-property-group> <description> Special propert

Unity中UI界面颤抖解决方法

将Render Mode中属性改为Screen Space - Camera 摄像机挂在Canvas属性下会出现UI界面颤抖的效果. UI界面颤抖解决方式:将Render Mode中属性改为Screen Space - Overlay,如下图所示:

第三篇:开发中的问题及解决方式

1.texarea 如何保存空格.换行? 答:var content1= $("#content").val();     var content =content1.replace(/\n|\r\n/g,"<br>").replace(/\s/g," "); 2.html 内容如何去除html 标签?答:js 截取: function delHtmlTag(str){         // $.parseHTML(str) dom

mysql中limit与in不能同时使用的解决方式.

mysql中limit与in不能同时使用的解决方式. 分类: MySQL2011-10-31 13:53 1277人阅读 评论(0) 收藏 举报 mysqlsubquery MySQL5.1中子查询是不能使用LIMIT的,报错: "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' " 这样的语句是不能正确执行的.select * from message where id i

mysql中采用concat来拼接中文字符乱码解决方式(转)

mysql中采用concat来拼接中文字符乱码解决方式 - fuxuejun的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/fuxuejun/article/details/6284725 mysql concat乱码问题解决 concat(str1,str2) 当concat结果集出现乱码时,大都是由于连接的字段类型不同导致,如concat中的字段参数一个是varchar类型,一个是int类型或doule类型,就会出现乱码. 解决方法:利用mysql的字符