源战役客户端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

125 行
3.3 KiB

  1. #if UNITY_EDITOR
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. /// <summary>
  7. /// 主要用于计算overdraw像素
  8. /// </summary>
  9. public class EffectEvla
  10. {
  11. public Camera _camera;
  12. SingleEffectEvla singleEffectEvla;
  13. public float time = 0;
  14. //采集特效数据的区域大小
  15. int rtSizeWidth = 512;
  16. int rtSizeHeight = 512;
  17. RenderTexture rt;
  18. public EffectEvla(Camera camera)
  19. {
  20. SetCamera(camera);
  21. rt = new RenderTexture(rtSizeWidth, rtSizeHeight, 0, RenderTextureFormat.ARGB32);
  22. singleEffectEvla = new SingleEffectEvla(1);
  23. }
  24. public void SetCamera(Camera camera)
  25. {
  26. _camera = camera;
  27. camera.SetReplacementShader(Shader.Find("ParticleEffectProfiler/OverDraw"), "");
  28. }
  29. public void Update()
  30. {
  31. time += Time.deltaTime;
  32. RecordOverDrawData(singleEffectEvla);
  33. }
  34. public EffectEvlaData[] GetEffectEvlaData()
  35. {
  36. return singleEffectEvla.GetEffectEvlaDatas();
  37. }
  38. #region overdraw
  39. public void RecordOverDrawData(SingleEffectEvla singleEffectEvla)
  40. {
  41. long pixTotal = 0;
  42. long pixActualDraw = 0;
  43. GetCameraOverDrawData(out pixTotal, out pixActualDraw);
  44. // 往数据+1
  45. singleEffectEvla.UpdateOneData(pixTotal, pixActualDraw);
  46. }
  47. public void GetCameraOverDrawData(out long pixTotal, out long pixActualDraw)
  48. {
  49. //记录当前激活的渲染纹理
  50. RenderTexture activeTextrue = RenderTexture.active;
  51. //渲染指定范围的rt,并记录范围内所有rgb像素值
  52. _camera.targetTexture = rt;
  53. _camera.Render();
  54. RenderTexture.active = rt;
  55. Texture2D texture = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false);
  56. texture.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
  57. GetOverDrawData(texture, out pixTotal, out pixActualDraw);
  58. //恢复之前激活的渲染纹理
  59. RenderTexture.active = activeTextrue;
  60. Texture2D.DestroyImmediate(texture);
  61. rt.Release();
  62. _camera.targetTexture = null;
  63. }
  64. public void GetOverDrawData(Texture2D texture, out long pixTotal, out long pixActualDraw)
  65. {
  66. var texw = texture.width;
  67. var texh = texture.height;
  68. var pixels = texture.GetPixels();
  69. int index = 0;
  70. pixTotal = 0;
  71. pixActualDraw = 0;
  72. for (var y = 0; y < texh; y++)
  73. {
  74. for (var x = 0; x < texw; x++)
  75. {
  76. float r = pixels[index].r;
  77. float g = pixels[index].g;
  78. float b = pixels[index].b;
  79. bool isEmptyPix = IsEmptyPix(r, g, b);
  80. if (!isEmptyPix)
  81. {
  82. pixTotal++;
  83. }
  84. int drawThisPixTimes = DrawPixTimes(r, g, b);
  85. pixActualDraw += drawThisPixTimes;
  86. index++;
  87. }
  88. }
  89. }
  90. //计算单像素的绘制次数
  91. public int DrawPixTimes(float r, float g, float b)
  92. {
  93. //在OverDraw.Shader中像素每渲染一次,g值就会叠加0.04
  94. return Convert.ToInt32(g / 0.04);
  95. }
  96. public bool IsEmptyPix(float r, float g, float b)
  97. {
  98. return r == 0 && g == 0 && b == 0;
  99. }
  100. #endregion
  101. }
  102. #endif