ReflectionsEditor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. namespace ShinySSRR {
  5. [CustomEditor(typeof(Reflections))]
  6. public class ReflectionsEditor : Editor {
  7. SerializedProperty ignore, scope, layerMask, nameFilter, subMeshMask;
  8. SerializedProperty smoothness, perSubMeshSmoothness, subMeshSettings;
  9. SerializedProperty useMaterialSmoothness, materialSmoothnessMapPropertyName, materialSmoothnessIntensityPropertyName;
  10. SerializedProperty useMaterialNormalMap, materialNormalMapPropertyName;
  11. SerializedProperty fresnel, fuzzyness, contactHardening;
  12. SerializedProperty overrideGlobalSettings;
  13. SerializedProperty sampleCount, maxRayLength, thickness, binarySearchIterations, refineThickness, thicknessFine, decay, jitter;
  14. private void OnEnable() {
  15. ignore = serializedObject.FindProperty("ignore");
  16. scope = serializedObject.FindProperty("scope");
  17. layerMask = serializedObject.FindProperty("layerMask");
  18. nameFilter = serializedObject.FindProperty("nameFilter");
  19. subMeshMask = serializedObject.FindProperty("subMeshMask");
  20. smoothness = serializedObject.FindProperty("smoothness");
  21. useMaterialSmoothness = serializedObject.FindProperty("useMaterialSmoothness");
  22. materialSmoothnessMapPropertyName = serializedObject.FindProperty("materialSmoothnessMapPropertyName");
  23. materialSmoothnessIntensityPropertyName = serializedObject.FindProperty("materialSmoothnessIntensityPropertyName");
  24. perSubMeshSmoothness = serializedObject.FindProperty("perSubMeshSmoothness");
  25. subMeshSettings = serializedObject.FindProperty("subMeshSettings");
  26. useMaterialNormalMap = serializedObject.FindProperty("useMaterialNormalMap");
  27. materialNormalMapPropertyName = serializedObject.FindProperty("materialNormalMapPropertyName");
  28. fresnel = serializedObject.FindProperty("fresnel");
  29. fuzzyness = serializedObject.FindProperty("fuzzyness");
  30. contactHardening = serializedObject.FindProperty("contactHardening");
  31. overrideGlobalSettings = serializedObject.FindProperty("overrideGlobalSettings");
  32. sampleCount = serializedObject.FindProperty("sampleCount");
  33. maxRayLength = serializedObject.FindProperty("maxRayLength");
  34. binarySearchIterations = serializedObject.FindProperty("binarySearchIterations");
  35. thickness = serializedObject.FindProperty("thickness");
  36. refineThickness = serializedObject.FindProperty("refineThickness");
  37. thicknessFine = serializedObject.FindProperty("thicknessFine");
  38. decay = serializedObject.FindProperty("decay");
  39. jitter = serializedObject.FindProperty("jitter");
  40. }
  41. public override void OnInspectorGUI() {
  42. UnityEngine.Rendering.Universal.UniversalRenderPipelineAsset pipe = UnityEngine.Rendering.GraphicsSettings.currentRenderPipeline as UnityEngine.Rendering.Universal.UniversalRenderPipelineAsset;
  43. if (pipe == null) {
  44. EditorGUILayout.HelpBox("Universal Rendering Pipeline asset is not set in 'Project Settings / Graphics or Quality' !", MessageType.Error);
  45. EditorGUILayout.Separator();
  46. GUI.enabled = false;
  47. } else if (!ShinySSRR.installed) {
  48. EditorGUILayout.HelpBox("Shiny SSRR Render Feature must be added to the rendering pipeline renderer.", MessageType.Error);
  49. if (GUILayout.Button("Go to Universal Rendering Pipeline Asset")) {
  50. Selection.activeObject = pipe;
  51. }
  52. EditorGUILayout.Separator();
  53. GUI.enabled = false;
  54. } else {
  55. if (!pipe.supportsCameraDepthTexture) {
  56. EditorGUILayout.HelpBox("Depth Texture option is required. Check Universal Rendering Pipeline asset!", MessageType.Warning);
  57. if (GUILayout.Button("Go to Universal Rendering Pipeline Asset")) {
  58. Selection.activeObject = pipe;
  59. }
  60. EditorGUILayout.Separator();
  61. GUI.enabled = false;
  62. }
  63. EditorGUILayout.BeginVertical(GUI.skin.box);
  64. if (GUILayout.Button("Show Global Settings")) {
  65. if (pipe != null) {
  66. var so = new SerializedObject(pipe);
  67. var prop = so.FindProperty("m_RendererDataList");
  68. if (prop != null && prop.arraySize > 0) {
  69. var o = prop.GetArrayElementAtIndex(0);
  70. if (o != null) {
  71. Selection.SetActiveObjectWithContext(o.objectReferenceValue, null);
  72. GUIUtility.ExitGUI();
  73. }
  74. }
  75. }
  76. }
  77. EditorGUILayout.EndVertical();
  78. }
  79. bool isForwardPath = true;
  80. if (ShinySSRR.isDeferredActive) {
  81. EditorGUILayout.HelpBox("In deferred mode, material properties like smoothness and normal map plus global SSR settings are used.", MessageType.Warning);
  82. isForwardPath = false;
  83. GUI.enabled = false;
  84. }
  85. // ensure submesh array size matches materials count
  86. Reflections refl = (Reflections)target;
  87. if (refl.ssrRenderers != null && refl.ssrRenderers.Count == 1 && refl.ssrRenderers[0].originalMaterials != null) {
  88. List<Material> materials = refl.ssrRenderers[0].originalMaterials;
  89. if (refl.subMeshSettings == null) {
  90. refl.subMeshSettings = new SubMeshSettingsData[materials.Count];
  91. } else if (refl.subMeshSettings.Length < materials.Count) {
  92. System.Array.Resize(ref refl.subMeshSettings, materials.Count);
  93. }
  94. }
  95. serializedObject.Update();
  96. EditorGUILayout.PropertyField(ignore);
  97. if (!ignore.boolValue) {
  98. if (refl.renderers?.Count == 0) {
  99. if (scope.intValue == (int)Scope.OnlyThisObject) {
  100. EditorGUILayout.HelpBox("No renderers found on this gameobject. Switch to 'Include Children' or add this script to another object which contains a renderer.", MessageType.Warning);
  101. } else {
  102. EditorGUILayout.HelpBox("No renderers found under this gameobject.", MessageType.Warning);
  103. }
  104. }
  105. EditorGUILayout.PropertyField(scope);
  106. if (scope.intValue == (int)Scope.IncludeChildren) {
  107. EditorGUI.indentLevel++;
  108. EditorGUILayout.PropertyField(layerMask);
  109. EditorGUILayout.PropertyField(nameFilter);
  110. EditorGUILayout.PropertyField(subMeshMask);
  111. EditorGUI.indentLevel--;
  112. }
  113. EditorGUILayout.PropertyField(useMaterialSmoothness);
  114. GUI.enabled = !useMaterialSmoothness.boolValue;
  115. EditorGUILayout.PropertyField(perSubMeshSmoothness);
  116. if (perSubMeshSmoothness.boolValue) {
  117. EditorGUILayout.PropertyField(subMeshSettings, new GUIContent("Smoothness Values"), true);
  118. } else {
  119. EditorGUILayout.PropertyField(smoothness);
  120. }
  121. GUI.enabled = isForwardPath;
  122. EditorGUILayout.PropertyField(useMaterialNormalMap);
  123. if (useMaterialSmoothness.boolValue || perSubMeshSmoothness.boolValue || useMaterialNormalMap.boolValue) {
  124. EditorGUILayout.Separator();
  125. EditorGUILayout.LabelField("Material Property Names", EditorStyles.miniBoldLabel);
  126. if (useMaterialSmoothness.boolValue || perSubMeshSmoothness.boolValue) {
  127. EditorGUILayout.PropertyField(materialSmoothnessMapPropertyName, new GUIContent("Smoothness Map", "The material property name for the smoothness map"));
  128. EditorGUILayout.PropertyField(materialSmoothnessIntensityPropertyName, new GUIContent("Smoothness Intensity", "The material property name for the smoothness intensity"));
  129. }
  130. if (useMaterialNormalMap.boolValue) {
  131. EditorGUILayout.PropertyField(materialNormalMapPropertyName, new GUIContent("NormalMap", "The material property name for the normal map"));
  132. }
  133. }
  134. EditorGUILayout.PropertyField(overrideGlobalSettings);
  135. if (overrideGlobalSettings.boolValue) {
  136. EditorGUI.indentLevel++;
  137. EditorGUILayout.BeginHorizontal();
  138. EditorGUILayout.LabelField("Apply Quality Preset:", GUILayout.Width(EditorGUIUtility.labelWidth));
  139. if (GUILayout.Button("Fast")) {
  140. ApplyRaytracingPreset(RaytracingPreset.Fast);
  141. }
  142. if (GUILayout.Button("Medium")) {
  143. ApplyRaytracingPreset(RaytracingPreset.Medium);
  144. }
  145. if (GUILayout.Button("High")) {
  146. ApplyRaytracingPreset(RaytracingPreset.High);
  147. }
  148. if (GUILayout.Button("Superb")) {
  149. ApplyRaytracingPreset(RaytracingPreset.Superb);
  150. }
  151. if (GUILayout.Button("Ultra")) {
  152. ApplyRaytracingPreset(RaytracingPreset.Ultra);
  153. }
  154. EditorGUILayout.EndHorizontal();
  155. EditorGUILayout.PropertyField(sampleCount);
  156. EditorGUILayout.PropertyField(maxRayLength);
  157. EditorGUILayout.PropertyField(thickness);
  158. EditorGUILayout.PropertyField(binarySearchIterations);
  159. EditorGUILayout.PropertyField(refineThickness);
  160. if (refineThickness.boolValue) {
  161. EditorGUILayout.PropertyField(thicknessFine);
  162. }
  163. EditorGUILayout.PropertyField(jitter);
  164. EditorGUILayout.PropertyField(fresnel);
  165. EditorGUILayout.PropertyField(decay);
  166. EditorGUILayout.PropertyField(fuzzyness, new GUIContent("Fuzziness"));
  167. EditorGUILayout.PropertyField(contactHardening);
  168. EditorGUI.indentLevel--;
  169. }
  170. }
  171. serializedObject.ApplyModifiedProperties();
  172. }
  173. void ApplyRaytracingPreset(RaytracingPreset preset) {
  174. Reflections ssr = (Reflections)target;
  175. ssr.ApplyRaytracingPreset(preset);
  176. EditorUtility.SetDirty(ssr);
  177. }
  178. }
  179. }