BRGRender.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.Collections.Generic;
  2. using Unity.Collections;
  3. using Unity.Mathematics;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. using Utility;
  7. namespace Core.BRG
  8. {
  9. public class BRGRender<T> : BRGRenderBasic where T : BGRGameObjectInfo
  10. {
  11. public bool isUpdate = true;
  12. public List<T> m_gameObjectInfos = new List<T>();
  13. private List<BatchShaderBind> m_batchShaderBinds = new List<BatchShaderBind>();
  14. private Map<string, BatchShaderBind> m_batchShaderBindMap = new Map<string, BatchShaderBind>();
  15. public int maxCount;
  16. public void InitRender(BRGSamples samples, List<T> gameObjectInfos, int maxCount)
  17. {
  18. this.m_gameObjectInfos = gameObjectInfos;
  19. this.maxCount = maxCount;
  20. for (int i = 0; i < m_gameObjectInfos.Count; i++)
  21. {
  22. m_gameObjectInfos[i].initIndex = i;
  23. }
  24. int maxItemSize = samples.GetAllShaderValueSize();
  25. Init(samples, maxCount, maxItemSize);
  26. UploadGpuData(gameObjectInfos.Count);
  27. }
  28. public BatchShaderBind GetBatchShaderBind(string key)
  29. {
  30. m_batchShaderBindMap.TryGetValue(key, out var bind);
  31. return bind;
  32. }
  33. protected override NativeArray<MetadataValue> ProInitBatchMetadata(int startOffset, int m_maxInstancePerWindow)
  34. {
  35. NativeArray<MetadataValue> metadataValues =
  36. new NativeArray<MetadataValue>(m_samples.AllShaderValues.Count, Allocator.Temp,
  37. NativeArrayOptions.UninitializedMemory);
  38. m_batchShaderBinds.Clear();
  39. // BatchShaderBind transformBind = new BatchShaderBind();
  40. // transformBind.shaderValue = new BRGShaderValue()
  41. // {
  42. // key = "_Object2World",
  43. // };
  44. for (int i = 0; i < m_samples.AllShaderValues.Count; i++)
  45. {
  46. BRGShaderValue shaderValue = m_samples.AllShaderValues[i];
  47. int shaderId = Shader.PropertyToID(shaderValue.key);
  48. BatchShaderBind batchShaderBind = new BatchShaderBind();
  49. metadataValues[i] = CreateMetadataValue(shaderId, startOffset, true);
  50. batchShaderBind.shaderValue = shaderValue;
  51. batchShaderBind.offset = startOffset;
  52. batchShaderBind.InitBuffer(m_maxInstancePerWindow);
  53. startOffset += shaderValue.GetSize() * m_maxInstancePerWindow;
  54. m_batchShaderBinds.Add(batchShaderBind);
  55. m_batchShaderBindMap.Add(shaderValue.key, batchShaderBind);
  56. }
  57. return metadataValues;
  58. }
  59. public void AddBGRGameObjectInfo(T info)
  60. {
  61. m_gameObjectInfos.Add(info);
  62. }
  63. public void RemoveBGRGameObjectInfo(int index)
  64. {
  65. m_gameObjectInfos.RemoveAt(index);
  66. }
  67. public void UpdatePos()
  68. {
  69. if (!isUpdate)
  70. {
  71. return;
  72. }
  73. int totalGpuBufferSize;
  74. int alignedWindowSize;
  75. NativeArray<float3x4> sysmemBuffer =
  76. GetSysmemBuffer(out totalGpuBufferSize, out alignedWindowSize);
  77. // 检查NativeArray是否有效
  78. if (!sysmemBuffer.IsCreated)
  79. {
  80. Debug.LogError("NativeArray has been deallocated or not created properly");
  81. return;
  82. }
  83. int m_itemCount = maxCount;
  84. m_batchShaderBindMap.TryGetValue("_MainColor", out var baseColorBind);
  85. for (int i = 0; i < m_gameObjectInfos.Count; i++)
  86. {
  87. BGRGameObjectInfo info = m_gameObjectInfos[i];
  88. Matrix4x4 matrix4X4 = info.objectToWorld;
  89. float4x4 float3X4 = matrix4X4;
  90. sysmemBuffer[i] = new float3x4(
  91. float3X4.c0.xyz, float3X4.c1.xyz,
  92. float3X4.c2.xyz, float3X4.c3.xyz
  93. );
  94. float4x4 inverse = matrix4X4.inverse;
  95. sysmemBuffer[i + m_itemCount] = new float3x4(
  96. inverse.c0.xyz, inverse.c1.xyz,
  97. inverse.c2.xyz, inverse.c3.xyz
  98. );
  99. if (baseColorBind != null)
  100. {
  101. baseColorBind.SetData(i, info.color);
  102. }
  103. }
  104. // 每帧更新后重新上传数据到GPU
  105. UploadGpuData(m_gameObjectInfos.Count, m_batchShaderBinds);
  106. }
  107. }
  108. }