Unity HybridRender 视锥体裁剪

这篇博客介绍了如何在Unity中利用Unity.Rendering.FrustumPlanes结构体进行视锥体裁剪。通过从相机获取视锥体6个平面,并使用Intersect函数判断物体是否在视锥体内,实现逻辑层的裁剪判断。该方法适用于HybridRenderSystem,并提供了相应的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Unity HybridRender 使用Unity.Rendering.FrustumPlanes 结构体来实现视锥体裁剪。这个结构体基本都为静态函数,其中使用NativeArray<PlanePacket4>作为参数的函数是HybridRenderSystem中进行裁剪的函数。从试验结果看,如果需要在逻辑层判断是否在视锥体内,可以考虑使用FromCamera(Camera camera, NativeArray<float4> planes)构造视锥体6个平面,然后使用Intersect(NativeArray<float4> cullingPlanes, AABB a)这个函数获取判断结果。判断结果是个enum:Out,In, Partial。

using FrustumPlanes = Unity.Rendering.FrustumPlanes;

public class FrustumCut : SystemBase
    {
        Camera mainCam = null;
        string camTag = "MainCamera";
        protected override void OnCreate()
        {
            base.OnCreate();
            mainCam = FindCamera(camTag);
        }
        [BurstCompile]
        protected override void OnUpdate()
        {
            if (mainCam == null)
            {
                mainCam = FindCamera(camTag);
                return;
            }
            var plans = new NativeArray<float4>(6, Allocator.TempJob);
            FrustumPlanes.FromCamera(mainCam, plans);
            Dependency = Entities
                .ForEach((Entity e, int entityInQueryIndex,in WorldRenderBounds bounds ) =>
                {
                    var r = FrustumPlanes.Intersect(plans, bounds.Value);
                    //DO ANYTHING HERE...
                }).Schedule(Dependency);
            plans.Dispose(Dependency);
        }

        Camera FindCamera(string tag)
        {
            var go = GameObject.FindGameObjectWithTag(tag);
            if (go == null)
                return null;
            return go.GetComponent<Camera>();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值