Unity LOD Study

Unity LOD Study

Begin

LOD : Level Of Detail ,根据物体距离当前渲染摄像机的距离设定不同的level层级,并且根据层级设定当前需要显示的Level模型层级。通过减少远距离的渲染细节,达到减少性能消耗的目的。

As your scenes get larger, performance becomes a bigger consideration. One of the ways to manage this is to have meshes with different levels of detail depending on how far the camera is from the object. This is called Level of Detail (abbreviated as LOD).

Unity LOD Group

LOD Groups are used to manage level of detail (LOD) for GameObjects. Level of Detail is an optimisation technique that uses several meshes for an object; the meshes represent the same object with decreasing detail in the geometry. The idea is that the low-detail meshes are shown when the object is far from the camera and the difference will not be noticed. Since meshes with simpler geometry are less demanding on the hardware, performance can be improved by using LOD judiciously.

  • 创建一个空GameObject作为显示层的Container,如果有对物体的位移、旋转、Collider等逻辑功能,可以挂在在该层。
  • 在Container上添加Unity的LOD Group脚本。设置合适的LOD层级以及生效范围。
    • The different LOD levels are visible in the horizontal bar with the camera icon just above it (LOD: 0, LOD: 1, LOD: 2, etc). The percentages in the LOD bars represent the fraction of the bounding box height relative to screen height where that LOD level becomes active.
    • LOD其实并不是根据距离来确定显示的层级的,而是根据当前模型的Bound的高度与当前视图的Height的比例来判定当前生效的LOG Level
    • 将自定义的不同层级的渲染模型Add到不同的LODLevel中。
    • 通过以上的操作之后,在Scene中滑动鼠标,则可以看到显示层的模型会根据距离的变化而产生变化。在GameView中,通过移动Contaner距离当前渲染摄像机的距离,也可以看到显示出的模型跟着变化。
    • Unity 5.x提供了LOD Group的cross,但是Untiy并没有实现这个功能,只会给你提供单签cross fade,需要自己实现geometry上的blend。
    • Cross-fade: You need to specify a Transition Width value to define a cross-fading zone at the end of the current LOD where it will to transit to the next LOD. In the transition zone, both LOD levels will be rendered. The blend factor goes from 1 to 0 for the current LOD and 0 to 1 for the next LOD.
    • Quality Settings 中可以设置LOD Bias跟Maximum LOD Level
    • LOD Bias 其实就是scale,一个当前distance变化的一个分母比例。 LOD levels are chosen based on the onscreen size of an object. When the size is between two LOD levels, the choice can be biased toward the less detailed or more detailed of the two models available. This is set as a fraction from 0 to +infinity. When it is set between 0 and 1 it favors less detail. A setting of more than 1 favors greater detail. For example, setting LOD Bias to 2 and having it change at 50% distance, LOD actually only changes on 25%.
    • Maximum LOD Level : 当前允许的最大的LOD Level。默认是0,则所有的LOG Level都可以显示,如果采用1的话,那么LOD0的部分都会采用LOD1去代替。低于这个值的LODLevel则没有影响。The highest LOD that will be used by the game. See note below for more Information.
    • Models which have a LOD below the MaximumLOD level will not be used and omitted from the build (which will save storage and memory space). Unity will use the smallest LOD value from all the MaximumLOD values linked with the quality settings for the target platform. If an LOD level is included then models from that LODGroup will be included in the build and always loaded at runtime for that LODGroup, regardless of the quality setting being used. As an example, if LOD level 0 is used in any quality setting then all the LOD levels will be included in the build and all the referenced models loaded at runtime.

LOD Plugin

Simple LOD

  • 模型合并 将当前选中的GameObject下的child的Mesh合并在同一个mesh中。需要注意的是unity允许一个Mesh的顶点数不能超过65536,所以Simple LOD在合并时会自动将超过65536顶点的mesh切换到另外一个mesh,在mesh名后增加增量数字后缀
  • 烘焙Atlas 根据模型的Material自动把不同类型的Texture分类显示出来,在合并的时候,会自动把同类型的Texture合并。也可以手动拖拽合并某一些贴图,但是可能会造成显示错误。合并了贴图之后,可以减少dc。
  • 模型简化 对Skinned mesh动画角色网格提供了很好的支持。
  • Simple LOD提供了自己的LOD Switch。原理跟Unity 的LOD Group一样,都是根据当前的渲染Bounds在屏幕中的scale size去选择相应的LOD Level。只是Simple LOD提供了两种不同的模型替换方式。一种是直接替换当前的mesh。另外一种也是切换显示的GameObejct。
  • 需要评估Simple LOD提供的LOD Swith与LODGroup的性能,再选择选取哪一个。或者根据原理实现自己的LOD Swith,但是原则上不会影响逻辑层。但是都需要读取类似于QulitySetting这样的全局配置。

Shader LOD

在一个Shader当中,可以给不同的subshader指定不同的LOD属性

    SubShader {
        LOD 200
        Pass {
//insert shader pass here
        }
    }
    SubShader {
                LOD 100
        Pass {
//insert shader pass here
        }
    SubShader {
                LOD 0
        Pass {
//insert shader pass here
        }
    }

可以在脚本代码中指定一次全局Shader.globalMaximumLOD值,该值是LOD所执行的最大值,也就是说所有shader中的subshader的LOD值小于该全局值,该subshader才被执行。当然,也可以单独设置Shader.maximumLOD控制个别shader

消耗越大,shader LOD数字越高,可以单独设置每一个Shader对象的maximumLOD

特效的LOD

当特效在距离较远的时候,表现力不需要那么精致,所以可以考虑将特效根据距离进行LOD显示,当然指考虑创建时候的距离,过程中移动不考虑
* 需要美术提供不同LOD的粒子特效。除了根据场景中的距离来设定当前播放的特效对象,还需要根据当前机型选择播放的粒子对象

END

你可能感兴趣的:(Unity游戏开发)