unity shader Offset Factor, Units详解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity shader Offset Factor, Units详解相关的知识,希望对你有一定的参考价值。

很久之前查到的各种关于offset的资料,今天群里面又有人问到,我干脆发上来,再有人问我就直接贴地址了!
unity文档上的解释:
Offset Factor, Units
Allows you specify a depth offset with two parameters. factor and units. Factor scales the maximum Z slope, with respect to X or Y of the polygon, and units scale the minimum resolvable depth buffer value. This allows you to force one polygon to be drawn on top of another although they are actually in the same position. For example Offset 0, –1 pulls the polygon closer to the camera ignoring the polygon’s slope, whereas Offset –1, –1 will pull the polygon even closer when looking at a grazing angle.
基本等于没解释。
unity论坛上的解释:
Polygon offset allows the application to specify a depth offset with two parameters, factor and units. factor scales the maximum Z slope, with respect to X or Y of the polygon, and units scales the minimum resolvable depth buffer value. The results are summed to produce the depth offset. This offset is applied in screen space, typically with positive Z pointing into the screen.
The factor parameter is required to ensure correct results for filled primitives that are nearly edge-on to the viewer. In this case, the difference between Z values for the same pixel generated by two coplanar primitives can be as great as the maximum Z slope in X or Y. This Z slope will be large for nearly edge-on primitives, and almost non-existent for face-on primitives. The factor parameter lets you add this type of variable difference into the resulting depth offset.
A typical use might be to set factor and units to 1.0 to offset primitives into positive Z (into the screen) and enable polygon offset for fill mode. Two passes are then made, once with the model‘s solid geometry and once again with the line geometry. Nearly edge-on filled polygons are pushed substantially away from the eyepoint, to minimize interference with the line geometry, while nearly planar polygons are drawn at least one depth buffer unit behind the line geometry.
 
来自opengl的解释:

When GL_POLYGON_OFFSET_FILLGL_POLYGON_OFFSET_LINE, or GL_POLYGON_OFFSET_POINT is enabled, each fragment‘s depth value will be offset after it is interpolated from the depth values of the appropriate vertices. The value of the offset is factor×DZ+r×unitsfactor×DZ+r×units, where DZDZ is a measurement of the change in depth relative to the screen area of the polygon, and rr is the smallest value that is guaranteed to produce a resolvable offset for a given implementation. The offset is added before the depth test is performed and before the value is written into the depth buffer.

glPolygonOffset is useful for rendering hidden-line images, for applying decals to surfaces, and for rendering solids with highlighted edges.
 
有个blog关于这个的解释:

每一个Fragment的深度值都会增加如下所示的偏移量:

offset = (m * factor) + (r * units)

m是多边形的深度的斜率(在光栅化阶段计算得出)中的最大值。这句话难以理解,你只需知道,一个多边形越是与近裁剪面(near clipping plan)平行,m就越接近0。

r是能产生在窗口坐标系的深度值中可分辨的差异的最小值,r是由具体实现OpenGL的平台指定的一个常量。

一个大于0的offset 会把模型推到离你(摄像机)更远一点的位置,相应地,一个小于0的offset 会把模型拉近。
 
http://miffysora.wikidot.com/glpolygonoffset终于,这里有讲一点关于m是怎么计算的了。不过貌似有点错误
技术分享
技术分享

 

技术分享

 

技术分享

以上是关于unity shader Offset Factor, Units详解的主要内容,如果未能解决你的问题,请参考以下文章

Unity3D Shader 按百分比现实模型

unity怎么编辑shader

unity shader怎么混合两张贴图

unity shader有啥好处

unity shader有啥用

unity用脚本怎么让shader重绘