OutlineParameters.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. namespace EPOOutline
  6. {
  7. public class MeshPool
  8. {
  9. private Queue<Mesh> freeMeshes = new Queue<Mesh>();
  10. private List<Mesh> allMeshes = new List<Mesh>();
  11. public Mesh AllocateMesh()
  12. {
  13. while (freeMeshes.Count > 0 && freeMeshes.Peek() == null)
  14. freeMeshes.Dequeue();
  15. if (freeMeshes.Count == 0)
  16. {
  17. var mesh = new Mesh();
  18. mesh.MarkDynamic();
  19. allMeshes.Add(mesh);
  20. freeMeshes.Enqueue(mesh);
  21. }
  22. return freeMeshes.Dequeue();
  23. }
  24. public void ReleaseAllMeshes()
  25. {
  26. freeMeshes.Clear();
  27. foreach (var mesh in allMeshes)
  28. freeMeshes.Enqueue(mesh);
  29. }
  30. }
  31. public class OutlineParameters
  32. {
  33. public readonly MeshPool MeshPool = new MeshPool();
  34. public Camera Camera;
  35. public RenderTargetIdentifier Target;
  36. public RenderTargetIdentifier DepthTarget;
  37. public CommandBuffer Buffer;
  38. public DilateQuality DilateQuality = DilateQuality.Base;
  39. public int DilateIterations = 2;
  40. public int BlurIterations = 5;
  41. public Vector2 Scale = Vector2.one;
  42. public Rect? CustomViewport;
  43. public long OutlineLayerMask = -1;
  44. public int TargetWidth;
  45. public int TargetHeight;
  46. public float BlurShift = 1.0f;
  47. public float DilateShift = 1.0f;
  48. public bool UseHDR;
  49. public bool UseInfoBuffer = false;
  50. public bool IsEditorCamera;
  51. public BufferSizeMode PrimaryBufferSizeMode;
  52. public int PrimaryBufferSizeReference;
  53. public float PrimaryBufferScale = 0.1f;
  54. public StereoTargetEyeMask EyeMask;
  55. public int Antialiasing = 1;
  56. public BlurType BlurType = BlurType.Gaussian13x13;
  57. public LayerMask Mask = -1;
  58. public Mesh BlitMesh;
  59. public List<Outlinable> OutlinablesToRender = new List<Outlinable>();
  60. private bool isInitialized = false;
  61. public Vector2Int MakeScaledVector(int x, int y)
  62. {
  63. var fx = (float)x;
  64. var fy = (float)y;
  65. return new Vector2Int(Mathf.FloorToInt(fx * Scale.x), Mathf.FloorToInt(fy * Scale.y));
  66. }
  67. public void CheckInitialization()
  68. {
  69. if (isInitialized)
  70. return;
  71. Buffer = new CommandBuffer();
  72. isInitialized = true;
  73. }
  74. public void Prepare()
  75. {
  76. if (OutlinablesToRender.Count == 0)
  77. return;
  78. UseInfoBuffer = OutlinablesToRender.Find(x => x != null && ((x.DrawingMode & (OutlinableDrawingMode.Obstacle | OutlinableDrawingMode.Mask)) != 0 || x.ComplexMaskingEnabled)) != null;
  79. if (UseInfoBuffer)
  80. return;
  81. foreach (var target in OutlinablesToRender)
  82. {
  83. if ((target.DrawingMode & OutlinableDrawingMode.Normal) == 0)
  84. continue;
  85. if (!CheckDiffers(target))
  86. continue;
  87. UseInfoBuffer = true;
  88. break;
  89. }
  90. }
  91. private static bool CheckDiffers(Outlinable outlinable)
  92. {
  93. if (outlinable.RenderStyle == RenderStyle.Single)
  94. return CheckIfNonOne(outlinable.OutlineParameters);
  95. else
  96. return CheckIfNonOne(outlinable.FrontParameters) || CheckIfNonOne(outlinable.BackParameters);
  97. }
  98. private static bool CheckIfNonOne(Outlinable.OutlineProperties parameters)
  99. {
  100. return parameters.BlurShift != 1.0f || parameters.DilateShift != 1.0f;
  101. }
  102. }
  103. }