Advertisement
Guest User

Untitled

a guest
Mar 21st, 2022
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.81 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEngine.Rendering;
  4. using UnityEngine.Rendering.Universal;
  5.  
  6.  
  7. public enum CausticsDirection
  8. {
  9. [InspectorName("Light direction")]
  10. DirectionalLight,
  11. [InspectorName("Fixed direction")]
  12. Fixed,
  13. }
  14.  
  15. public enum DebugMode
  16. {
  17. [InspectorName("None")]
  18. Disabled,
  19. [InspectorName("Caustics only")]
  20. CausticsOnly,
  21. [InspectorName("Height and shadow mask")]
  22. Mask,
  23. [InspectorName("World space UVs")]
  24. WorldSpaceUVs,
  25. }
  26.  
  27. public class CausticsFeature : ScriptableRendererFeature
  28. {
  29. #if UNITY_EDITOR
  30. [CustomPropertyDrawer(typeof(MinMaxSliderAttribute))]
  31. public class MinMaxSliderDrawer : PropertyDrawer
  32. {
  33. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  34. {
  35. var minMaxAttribute = (MinMaxSliderAttribute) attribute;
  36. var propertyType = property.propertyType;
  37.  
  38. label.tooltip = minMaxAttribute.min.ToString("F2") + " to " + minMaxAttribute.max.ToString("F2");
  39.  
  40. Rect controlRect = EditorGUI.PrefixLabel(position, label);
  41.  
  42. Rect[] splittedRect = SplitRect(controlRect, 3);
  43.  
  44. if (propertyType == SerializedPropertyType.Vector2)
  45. {
  46. EditorGUI.BeginChangeCheck();
  47.  
  48. Vector2 vector = property.vector2Value;
  49. float minVal = vector.x;
  50. float maxVal = vector.y;
  51.  
  52. minVal = EditorGUI.FloatField(splittedRect[0], float.Parse(minVal.ToString("F2")));
  53. maxVal = EditorGUI.FloatField(splittedRect[2], float.Parse(maxVal.ToString("F2")));
  54.  
  55. EditorGUI.MinMaxSlider(splittedRect[1], ref minVal, ref maxVal,
  56. minMaxAttribute.min, minMaxAttribute.max);
  57.  
  58. if (minVal < minMaxAttribute.min) minVal = minMaxAttribute.min;
  59. if (maxVal > minMaxAttribute.max) maxVal = minMaxAttribute.max;
  60.  
  61. vector = new Vector2(minVal > maxVal ? maxVal : minVal, maxVal);
  62.  
  63. if (EditorGUI.EndChangeCheck()) property.vector2Value = vector;
  64. }
  65. }
  66.  
  67. Rect[] SplitRect(Rect rectToSplit, int n)
  68. {
  69. Rect[] rects = new Rect[n];
  70.  
  71. for (int i = 0; i < n; i++)
  72. rects[i] = new Rect(rectToSplit.position.x + (i * rectToSplit.width / n), rectToSplit.position.y,
  73. rectToSplit.width / n, rectToSplit.height);
  74.  
  75. int padding = (int) rects[0].width - 40;
  76. int space = 5;
  77.  
  78. rects[0].width -= padding + space;
  79. rects[2].width -= padding + space;
  80.  
  81. rects[1].x -= padding;
  82. rects[1].width += padding * 2;
  83.  
  84. rects[2].x += padding + space;
  85.  
  86. return rects;
  87. }
  88. }
  89.  
  90. public class MinMaxSliderAttribute : PropertyAttribute
  91. {
  92. public float min;
  93. public float max;
  94.  
  95. public MinMaxSliderAttribute(float min, float max)
  96. {
  97. this.min = min;
  98. this.max = max;
  99. }
  100. }
  101. #endif
  102.  
  103. [System.Serializable]
  104. public class CausticsSettings
  105. {
  106. [Header("Visuals")] [Range(0f, 3f)] public float strength = 3f;
  107. [Range(0f, 1f)] public float rgbSplit = 0.3f;
  108. [Range(0f, 1f)] public float shadowMask = 1f;
  109.  
  110. [Header("Movement")] public Texture2D texture;
  111. [Range(0.1f, 10f)] public float scale = 5f;
  112. [Range(0f, 0.3f)] public float speed = 0.125f;
  113.  
  114. [Header("Depth")] public float waterLevel = 0f;
  115. #if UNITY_EDITOR
  116. [MinMaxSlider(0, 10)] public Vector2 depth = new Vector2(0f, 4f);
  117. #else
  118. public Vector2 depth = new Vector2(0f, 4f);
  119. #endif
  120. [Range(0f, 1f)] public float fade = 1f;
  121.  
  122. [Header("Rendering")] public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingSkybox;
  123.  
  124. [Header("Caustics Projection")] public CausticsDirection direction = CausticsDirection.DirectionalLight;
  125. public Vector3 fixedDirection = new Vector3(-10f, 30f, -30f);
  126.  
  127. [Header("Debugging")] public DebugMode debug = DebugMode.Disabled;
  128.  
  129. public Material causticsMaterialVolume;
  130. }
  131.  
  132. public CausticsSettings settings = new CausticsSettings();
  133. private CausticsPass causticsPass;
  134.  
  135. [SerializeField, HideInInspector] Shader causticsShader;
  136. private Material causticsMaterial;
  137.  
  138. private static readonly int SrcBlend = Shader.PropertyToID("_SrcBlend");
  139. private static readonly int DstBlend = Shader.PropertyToID("_DstBlend");
  140.  
  141. private static readonly int causticsTextureID = Shader.PropertyToID("_CausticsTexture");
  142. private static readonly int causticsStrengthID = Shader.PropertyToID("_CausticsStrength");
  143. private static readonly int causticsScaleID = Shader.PropertyToID("_CausticsScale");
  144. private static readonly int causticsSpeedID = Shader.PropertyToID("_CausticsSpeed");
  145. private static readonly int causticsSplitID = Shader.PropertyToID("_CausticsSplit");
  146. private static readonly int shadowMaskID = Shader.PropertyToID("_CausticsShadowMask");
  147. private static readonly int causticsFadeID = Shader.PropertyToID("_CausticsFade");
  148.  
  149. private static readonly int waterLevelID = Shader.PropertyToID("_WaterLevel");
  150. private static readonly int causticsStartID = Shader.PropertyToID("_CausticsStart");
  151. private static readonly int causticsEndID = Shader.PropertyToID("_CausticsEnd");
  152.  
  153. public override void Create()
  154. {
  155. causticsPass = new CausticsPass(settings);
  156.  
  157. if (causticsMaterial) DestroyImmediate(causticsMaterial);
  158. causticsShader = Shader.Find("Hidden/Stylized Water/Caustics");
  159. if (causticsShader != null) causticsMaterial = CoreUtils.CreateEngineMaterial(causticsShader);
  160.  
  161. if (!causticsMaterial) return;
  162.  
  163. causticsMaterial.SetTexture(causticsTextureID, settings.texture);
  164. causticsMaterial.SetFloat(causticsStrengthID, settings.strength);
  165. causticsMaterial.SetFloat(causticsScaleID, settings.scale);
  166. causticsMaterial.SetFloat(causticsSpeedID, settings.speed);
  167. causticsMaterial.SetFloat(causticsSplitID, settings.rgbSplit);
  168. causticsMaterial.SetFloat(shadowMaskID, settings.shadowMask);
  169. causticsMaterial.SetFloat(causticsFadeID, settings.fade);
  170.  
  171. causticsMaterial.SetFloat(waterLevelID, settings.waterLevel);
  172. causticsMaterial.SetFloat(causticsStartID, settings.depth.x);
  173. causticsMaterial.SetFloat(causticsEndID, settings.depth.y);
  174.  
  175. settings.causticsMaterialVolume.SetFloat(SrcBlend, 2f);
  176. settings.causticsMaterialVolume.SetFloat(DstBlend, 0f);
  177.  
  178. switch (settings.debug)
  179. {
  180. case DebugMode.Disabled:
  181. causticsMaterial.SetFloat(SrcBlend, 2f);
  182. causticsMaterial.SetFloat(DstBlend, 0f);
  183. causticsMaterial.DisableKeyword("DEBUG_MASK");
  184. causticsMaterial.DisableKeyword("DEBUG_CAUSTICS");
  185. causticsMaterial.DisableKeyword("DEBUG_UVS");
  186. causticsPass.renderPassEvent = settings.renderPassEvent;
  187. break;
  188.  
  189. case DebugMode.CausticsOnly:
  190. causticsMaterial.SetFloat(SrcBlend, 1f);
  191. causticsMaterial.SetFloat(DstBlend, 0f);
  192. causticsMaterial.DisableKeyword("DEBUG_MASK");
  193. causticsMaterial.EnableKeyword("DEBUG_CAUSTICS");
  194. causticsMaterial.DisableKeyword("DEBUG_UVS");
  195. causticsPass.renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
  196. break;
  197.  
  198. case DebugMode.Mask:
  199. causticsMaterial.SetFloat(SrcBlend, 1f);
  200. causticsMaterial.SetFloat(DstBlend, 0f);
  201. causticsMaterial.DisableKeyword("DEBUG_CAUSTICS");
  202. causticsMaterial.DisableKeyword("DEBUG_UVS");
  203. causticsMaterial.EnableKeyword("DEBUG_MASK");
  204. causticsPass.renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
  205. break;
  206.  
  207. case DebugMode.WorldSpaceUVs:
  208. causticsMaterial.SetFloat(SrcBlend, 1f);
  209. causticsMaterial.SetFloat(DstBlend, 0f);
  210. causticsMaterial.DisableKeyword("DEBUG_CAUSTICS");
  211. causticsMaterial.DisableKeyword("DEBUG_MASK");
  212. causticsMaterial.EnableKeyword("DEBUG_UVS");
  213. causticsPass.renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
  214. break;
  215. }
  216.  
  217. causticsPass.causticsMaterial = causticsMaterial;
  218. causticsPass.causticsMaterialVolume = settings.causticsMaterialVolume;
  219. }
  220.  
  221. public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
  222. {
  223. renderer.EnqueuePass(causticsPass);
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement