【个人学习记录-实装GPU实例化渲染的AABB包围盒】

个人学习记录-实装GPU实例化渲染的AABB包围盒

数据序列化

我的GPU实例化系统围绕字典类型:Dictionary<Bounds, List>展开,Bounds对应预先计算好的AABB包围盒,List对应包围盒内的对象的变换矩阵,而在此步骤中,我先进行数据的存储与读取。

只有一些简单类型才可以被直接序列化,Dictionary、Bounds、Matrix4x4等复杂类型无法直接序列化,因此我们需要为这个类型编写对应的映射类,把它们分解成简单类型。

例如:

// 我们需要序列化这个字典
Dictionary<Bounds, List<Matrix4x4>> d;

// 则对应的映射类应该是:
    [Serializable]
    public class SerializableBounds
    {
        public Vector3 center = new Vector3();
        public Vector3 size = new Vector3();

        public Bounds ToBounds()
        {
            return new Bounds(center, size);
        }
    }
    [Serializable]
    public class SerializableMatrix4X4
    {
        public float[] values = new float[16];

        public Matrix4x4 ToMatrix4X4()
        {
            Matrix4x4 matrix = new Matrix4x4();
            for (int i = 0; i < 16; i++)
                matrix[i] = values[i];
            return matrix;
        }
    }
    [Serializable]
    public class SerializableData
    {
        public SerializableBounds bounds = new SerializableBounds();
        public List<SerializableMatrix4X4> matrixLists = new List<SerializableMatrix4X4>();
    }
    [Serializable]
    public class SerializableDataList
    {
        public List<SerializableData> data = new List<SerializableData>();
    }
    
    // Dictionary<Bounds, List<Matrix4x4>>对应的类就是SerializableDataList

将这个数据转化成映射类的类型,就可以通过数据文件的API进行转化了。

同理,当我们试图读取数据文件为代码中的数据类型时,需要先反序列化,即把数据文件加载为映射类后,将映射类转换为我们需要的数据类型。

// 字典转换为映射类
	static SerializableDataList DictionaryToJson(Dictionary<Bounds, List<Matrix4x4>> dictionary)
        {
            SerializableDataList serializableDataList = new SerializableDataList();
            foreach (KeyValuePair<Bounds,List<Matrix4x4>> entry in dictionary)
            {
                SerializableData tmpData = new SerializableData();
                tmpData.bounds = new SerializableBounds();
                tmpData.bounds.center = entry.Key.center;
                tmpData.bounds.size = entry.Key.size;
                tmpData.matrixLists = new List<SerializableMatrix4X4>();
                foreach (Matrix4x4 matrix in entry.Value)
                {
                    SerializableMatrix4X4 tmpMatrix = new SerializableMatrix4X4();
                    for (int i = 0; i < 16; i++)
                    {
                        tmpMatrix.values[i] = matrix[i];
                    }
                    tmpData.matrixLists.Add(tmpMatrix);
                }
                serializableDataList.data.Add(tmpData);
            }
            return serializableDataList;
        }
// 存储为JSON文件
static void SaveData<T>(string path, T data)
    {
        string currentPath = DuplicationOfNameRename(Application.dataPath + path, ".json");
        string json = JsonUtility.ToJson(data, true);
        File.WriteAllText(currentPath, json);
        AssetDatabase.Refresh();
    }


// 映射类转换为字典
	static Dictionary<Bounds, List<Matrix4x4>> ToDictionary(SerializableDataList serializableDataList)
        {
            Dictionary<Bounds, List<Matrix4x4>> dictionary = new Dictionary<Bounds, List<Matrix4x4>>();
            for (int i = 0; i < serializableDataList.data.Count; i++)
            {
                Bounds bounds = serializableDataList.data[i].bounds.ToBounds();
                List<Matrix4x4> matrices = new List<Matrix4x4>();
                foreach (var serializableMatrix in serializableDataList.data[i].matrixLists)
                    matrices.Add(serializableMatrix.ToMatrix4X4());
                dictionary[bounds] = matrices;
            }
            return dictionary;
        }

// 读取JSON文件
	static Dictionary<Bounds, List<Matrix4x4>> LoadData(string path)
        {
            if (File.Exists(path))
            {
                string json = File.ReadAllText(path);
                SerializableDataList serializableDataList = JsonUtility.FromJson<SerializableDataList>(json);
                Dictionary<Bounds, List<Matrix4x4>> data = ToDictionary(serializableDataList);
                return data;
            }
            return default;
        }

视锥体AABB包围盒

注意:Graphics.DrawMeshInstancedProcedural中会接收一个Bounds值,其作用是使实例化物体坐标空间转换至相对Bounds的空间。(根本不是内置的AABB裁切,屁用都没有,被骗了!)

unity引擎提供一些很方便的方法用于计算AABB盒

Plane[] cameraFrustumPlanes = new Plane[6];// 存储AABB盒的六个面
GeometryUtility.CalculateFrustumPlanes(cam, cameraFrustumPlanes);// 计算AABB盒的六个面
//or
//Plane[] cameraFrustumPlanes = GeometryUtility.CalculateFrustumPlanes(cam);

//计算单元对象的包围盒
Bounds bound = new Bounds(centerPosWS, sizeWS);

bool isInPlane = GeometryUtility.TestPlanesAABB(cameraFrustumPlanes, bound);// 判断bound是否在cameraFrustumPlanes内,返回一个布尔值

而具体代码中,基于上节讲述的反序列化方法将数据写入GrassInfos中,并对其裁切即可。

Dictionary<Bounds, List<Matrix4x4>> GrassInfos
private void UpdateGrassBuffer()
{
    Profiler.BeginSample("GPU ====== performance test");
    List<Matrix4x4> cullingGrassBuffer = new List<Matrix4x4>();
    Plane[] planes = GeometryUtility.CalculateFrustumPlanes(Camera.main);
    foreach (var t0 in GrassInfos)
    {
        if (GeometryUtility.TestPlanesAABB(planes, t0.Key))
        {
            cullingGrassBuffer.AddRange(t0.Value);
        }
    }
    Profiler.EndSample();
    _grassBuffer?.Release();
    _grassBuffer = new ComputeBuffer(cullingGrassBuffer.Count, 64);
    _grassBuffer.SetData(cullingGrassBuffer);
    grassCount = cullingGrassBuffer.Count;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值