private bool InitData(GPUInstanceData instanceData) { if (instanceData == null || !instanceData.HasData) { lastInstanceID = 0; return(false); } if (lastInstanceID == instanceData.GetInstanceID()) { return(true); } lastInstanceID = instanceData.GetInstanceID(); GPUInstanceData.InstanceTile[] tiles = instanceData.InstanceTiles; int countPerInstance = instanceData.CountPerInstance; instanceMaterial = instanceData.InstanceMaterial; instanceMesh = instanceData.InstanceMesh; castShadow = instanceData.CastShadow; TileDatas = new TileData[tiles.Length]; for (int i = 0; i < TileDatas.Length; i++) { TileDatas[i].bounds = tiles[i].bounds; TileDatas[i].matrix4X4sList = new List <Matrix4x4[]>(); if (tiles[i].batchCount == 1) { TileDatas[i].matrix4X4sList.Add(tiles[i].localToWorld); } else { //tile内的总mesh数量可能大于合批数量 所以先拆分数组 //在这里拆分而不是烘焙时拆分是因为 List<Matrix4x4[]> 无法序列化 //前几批的数组 int j; for (j = 0; j < tiles[i].batchCount - 1; j++) { Matrix4x4[] matrix4x4s = new Matrix4x4[countPerInstance]; Array.Copy(tiles[i].localToWorld, j * countPerInstance, matrix4x4s, 0, countPerInstance); TileDatas[i].matrix4X4sList.Add(matrix4x4s); } //最后一个批次的矩阵数组 int totalCount = tiles[i].localToWorld.Length; int lastCount = totalCount - (tiles[i].batchCount - 1) * countPerInstance; if (lastCount > 0) { Matrix4x4[] lastMatrix4x4s = new Matrix4x4[lastCount]; Array.Copy(tiles[i].localToWorld, j * countPerInstance, lastMatrix4x4s, 0, lastCount); TileDatas[i].matrix4X4sList.Add(lastMatrix4x4s); } } } return(true); }
private void OnDisable() { Instance = null; }
public static GPUInstanceData Instance; //全局唯一 便于render feature调用 private void OnEnable() { Instance = this; this.gameObject.name = "GPU Instanced Data"; }