42 lines
1.1 KiB
C#
42 lines
1.1 KiB
C#
using UnityEngine;
|
|
|
|
namespace Net.Project.B.Feel
|
|
{
|
|
public class PreLayerCameraCullingService : MonoBehaviour
|
|
{
|
|
public Camera targetCamera;
|
|
|
|
[System.Serializable]
|
|
public struct LayerDistance
|
|
{
|
|
public LayerMask layer;
|
|
public float distance;
|
|
}
|
|
|
|
public LayerDistance[] layerDistances;
|
|
|
|
private void Start()
|
|
{
|
|
if (targetCamera == null)
|
|
{
|
|
targetCamera = Camera.main;
|
|
}
|
|
|
|
var distances = new float[32]; // Unity支持32个Layer
|
|
foreach (var ld in layerDistances)
|
|
{
|
|
int layerIndex = Mathf.RoundToInt(Mathf.Log(ld.layer.value, 2)); // LayerMask转Layer index
|
|
if (layerIndex is >= 0 and < 32)
|
|
{
|
|
distances[layerIndex] = ld.distance;
|
|
}
|
|
}
|
|
|
|
if (!targetCamera) return;
|
|
targetCamera.layerCullDistances = distances;
|
|
targetCamera.layerCullSpherical = true; // 根据球形距离进行剔除(通常更自然)
|
|
|
|
}
|
|
}
|
|
}
|