[Unity] 利用Culling Group实现LOD和剔除逻辑

[Unity] 利用Culling Group实现LOD和剔除逻辑

注: 至于性能没有做过测试!!!  如果认为有问题那Unity的LodGroup 都没必要用!

官方手册: 

https://docs.unity3d.com/2018.2/Documentation/Manual/CullingGroupAPI.html

https://docs.unity3d.com/ScriptReference/CullingGroup.html

https://docs.unity3d.com/ScriptReference/CullingGroupEvent.html

 

 

该API于Unity 5.2和之后版本出现

官方文档中提到一个优化:

Mass object movement & CullingGroups

As mentioned in the section on Transform Manipulation, moving large Transform hierarchies has a relatively high CPU cost due to the propagation of change messages. However, in real development environments, it is often impossible to collapse a hierarchy to a modest number of GameObjects.

At the same time, it is good development practice to only run enough behavior to maintain the believability of the game world while eliminating behavior the user will not notice – for example, in a Scene with a large number of characters, it is always more optimal to only run Mesh-skinning and animation-driven Transform movement for characters that are on-screen. There is no reason to waste CPU time calculating purely visual elements of the simulation for characters that are off-screen.

Both of these problems can be neatly addressed with an API first introduced in Unity 5.1: CullingGroups.

Instead of directly manipulating a large group of GameObjects in the scene, change the system to manipulate the Vector3 parameters of a group of BoundingSpheres within a CullingGroup. Each BoundingSphere serves as the authoritative repository for a single game-logical entity’s world-space position, and receives callbacks when the entity moves near/within the frustum of the CullingGroup’s main camera. These callbacks can then be used to activate/deactivate code or components (such as Animators) governing behavior that should only run while the entity is visible.

大量对象移动和剔除群组

如同 Transform Manipulation 那节所述,移动有超大层级结构的 Transform 对象会造成很大的 CPU 消耗。但在现实的环境中,通常不可能将对象结构精简到最少的 GameObjects。

同时,如果可以最好在玩家不发现的前提下,删除玩家看不到的行为。例如,在有大量角色的场景时,只针对屏幕可见范围内的角色计算网格蒙皮(Mesh-skinning)和处理角色动作等等。不需要浪费CPU的资源在计算屏幕外看不到的角色行为。

这两个问题都可以透过 Unity 5.1 导入的 API 来完美解决:CullingGroups。

与其直接操作场景中一大群的 GameObject,而是改变系统操作 CullingGroup 里的一组 BoundingSpheres 的Vector3 参数。每个 BoundingSphere 作为这些 GameObject 在游戏世界中的代表,当 CullingGroup 接近或进入CullingGroup 设定的主镜头的锥体范围内时成员才会收到 callback。然后这些 callback 就可以用来执行启用/停用的程序代码或组件(例如Animators)让物体执行在可见范围内该有的行为。

https://docs.unity3d.com/2018.2/Documentation/Manual/BestPracticeUnderstandingPerformanceInUnity8.html

 

https://docs.unity3d.com/ScriptReference/CullingGroup.html

https://docs.unity3d.com/2018.2/Documentation/Manual/CullingGroupAPI.html

https://unitycoder.com/blog/2018/10/31/find-nearby-objects-using-cullinggroup/

有两种方式来触发CullingGroup的事件:

  • 相机裁剪
  • 相对距离(相机或主主角相对于目标物体的距离)

这两种方式可以混用,也可以只使用其中一个

应用场景有:

  • 粒子当前不可见时,将其暂停
  • 粒子与相机或主角在不同距离阶段时,使用不同的简化版粒子。
  • ai不在视野内时停止更新
  • 15
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现这个功能,你需要使用Unity中的LOD(Level of Detail)系统,同时使用LODGroup组件来控制星球和星图之间的切换。 首先,你需要创建两个模型:星球和星图。这两个模型需要具有相同的中心点和比例,以便无缝切换。然后,将这两个模型分别命名为“Planet”和“StarMap”。 接下来,将这两个模型导入到Unity中,并将它们放置在场景中。然后,将“Planet”模型添加到一个空对象中,并将其命名为“Planet_LOD”. 然后,将“StarMap”模型添加到另一个空对象中,并将其命名为“StarMap_LOD". 接下来,你需要为这两个空对象添加LODGroup组件。在“Planet_LOD”空对象中,设置两个LOD级别,分别是0和1。在LOD 0级别下,将“Planet”模型设置为可见,将“StarMap”模型设置为不可见。在LOD 1级别下,将“Planet”模型设置为不可见,将“StarMap”模型设置为可见,并且将“StarMap”模型的Mesh Renderer的Material替换为星图材质。 在“StarMap_LOD”空对象中,同样设置两个LOD级别,分别是0和1。在LOD 0级别下,将“StarMap”模型设置为可见,将“Planet”模型设置为不可见。在LOD 1级别下,将“StarMap”模型设置为不可见,将“Planet”模型设置为可见,并且将“Planet”模型的Mesh Renderer的Material替换为星球材质。 最后,在场景中创建一个空对象,将“Planet_LOD”和“StarMap_LOD”作为其子对象,并将其命名为“Planet”. 然后,将这个空对象作为相机的跟随目标,并通过代码控制相机的距离来实现星球和星图之间的切换。 通过这种方式,你就可以实现星球模型和星图模型之间的切换了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值