| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 | 
							- using System.Collections.Generic;
 
- using Unity.Collections;
 
- using Unity.Mathematics;
 
- using UnityEngine;
 
- using UnityEngine.Rendering;
 
- using Utility;
 
- namespace Core.BRG
 
- {
 
-     public class BRGRender<T> : BRGRenderBasic where T : BGRGameObjectInfo
 
-     {
 
-         public bool isUpdate = true;
 
-         public List<T> m_gameObjectInfos = new List<T>();
 
-         private List<BatchShaderBind> m_batchShaderBinds = new List<BatchShaderBind>();
 
-         private Map<string, BatchShaderBind> m_batchShaderBindMap = new Map<string, BatchShaderBind>();
 
-         public int maxCount;
 
-         public void InitRender(BRGSamples samples, List<T> gameObjectInfos, int maxCount)
 
-         {
 
-             this.m_gameObjectInfos = gameObjectInfos;
 
-             this.maxCount = maxCount;
 
-             for (int i = 0; i < m_gameObjectInfos.Count; i++)
 
-             {
 
-                 m_gameObjectInfos[i].initIndex = i;
 
-             }
 
-             int maxItemSize = samples.GetAllShaderValueSize();
 
-             Init(samples, maxCount, maxItemSize);
 
-             UploadGpuData(gameObjectInfos.Count);
 
-         }
 
-         public BatchShaderBind GetBatchShaderBind(string key)
 
-         {
 
-             m_batchShaderBindMap.TryGetValue(key, out var bind);
 
-             return bind;
 
-         }
 
-         protected override NativeArray<MetadataValue> ProInitBatchMetadata(int startOffset, int m_maxInstancePerWindow)
 
-         {
 
-             NativeArray<MetadataValue> metadataValues =
 
-                 new NativeArray<MetadataValue>(m_samples.AllShaderValues.Count, Allocator.Temp,
 
-                     NativeArrayOptions.UninitializedMemory);
 
-             m_batchShaderBinds.Clear();
 
-             for (int i = 0; i < m_samples.AllShaderValues.Count; i++)
 
-             {
 
-                 BRGShaderValue shaderValue = m_samples.AllShaderValues[i];
 
-                 int shaderId = Shader.PropertyToID(shaderValue.key);
 
-                 BatchShaderBind batchShaderBind = new BatchShaderBind();
 
-                 metadataValues[i] = CreateMetadataValue(shaderId, startOffset, true);
 
-                 batchShaderBind.shaderValue = shaderValue;
 
-                 batchShaderBind.offset = startOffset;
 
-                 batchShaderBind.InitBuffer(m_maxInstancePerWindow);
 
-                 startOffset += shaderValue.GetSize() * m_maxInstancePerWindow;
 
-                 m_batchShaderBinds.Add(batchShaderBind);
 
-                 m_batchShaderBindMap.Add(shaderValue.key, batchShaderBind);
 
-             }
 
-             return metadataValues;
 
-         }
 
-         public void AddBGRGameObjectInfo(T info)
 
-         {
 
-             m_gameObjectInfos.Add(info);
 
-         }
 
-         public void RemoveBGRGameObjectInfo(int index)
 
-         {
 
-             m_gameObjectInfos.RemoveAt(index);
 
-         }
 
-         public void UpdatePos()
 
-         {
 
-             if (!isUpdate)
 
-             {
 
-                 return;
 
-             }
 
-           
 
-             int totalGpuBufferSize;
 
-             int alignedWindowSize;
 
-             NativeArray<float3x4> sysmemBuffer =
 
-                 GetSysmemBuffer(out totalGpuBufferSize, out alignedWindowSize);
 
-             // 检查NativeArray是否有效
 
-             if (!sysmemBuffer.IsCreated)
 
-             {
 
-                 Debug.LogError("NativeArray has been deallocated or not created properly");
 
-                 return;
 
-             }
 
-             int m_itemCount = maxCount;
 
-             m_batchShaderBindMap.TryGetValue("_MainColor", out var baseColorBind);
 
-             for (int i = 0; i < m_gameObjectInfos.Count; i++)
 
-             {
 
-                 BGRGameObjectInfo info = m_gameObjectInfos[i];
 
-                 Matrix4x4 matrix4X4 = info.objectToWorld;
 
-                 float4x4 float3X4 = matrix4X4;
 
-                 sysmemBuffer[i] = new float3x4(
 
-                     float3X4.c0.xyz, float3X4.c1.xyz,
 
-                     float3X4.c2.xyz, float3X4.c3.xyz
 
-                 );
 
-                 float4x4 inverse = matrix4X4.inverse;
 
-                 sysmemBuffer[i + m_itemCount] = new float3x4(
 
-                     inverse.c0.xyz, inverse.c1.xyz,
 
-                     inverse.c2.xyz, inverse.c3.xyz
 
-                 );
 
-                 if (baseColorBind != null)
 
-                 {
 
-                     baseColorBind.SetData(i, info.color);
 
-                 }
 
-             }
 
-             // 每帧更新后重新上传数据到GPU
 
-             UploadGpuData(m_gameObjectInfos.Count, m_batchShaderBinds);
 
-         }
 
-     }
 
- }
 
 
  |