UniversalRenderPipelineLightEditor.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using UnityEditor.AnimatedValues;
  2. using UnityEngine;
  3. using UnityEngine.Rendering.Universal;
  4. using UnityEngine.Rendering;
  5. namespace UnityEditor.Rendering.Universal
  6. {
  7. [CanEditMultipleObjects]
  8. [CustomEditorForRenderPipeline(typeof(Light), typeof(UniversalRenderPipelineAsset))]
  9. class UniversalRenderPipelineLightEditor : LightEditor
  10. {
  11. AnimBool m_AnimSpotOptions = new AnimBool();
  12. AnimBool m_AnimPointOptions = new AnimBool();
  13. AnimBool m_AnimDirOptions = new AnimBool();
  14. AnimBool m_AnimAreaOptions = new AnimBool();
  15. AnimBool m_AnimRuntimeOptions = new AnimBool();
  16. AnimBool m_AnimShadowOptions = new AnimBool();
  17. AnimBool m_AnimShadowAngleOptions = new AnimBool();
  18. AnimBool m_AnimShadowRadiusOptions = new AnimBool();
  19. AnimBool m_AnimLightBounceIntensity = new AnimBool();
  20. class Styles
  21. {
  22. public readonly GUIContent SpotAngle = EditorGUIUtility.TrTextContent("Spot Angle", "Controls the angle in degrees at the base of a Spot light's cone.");
  23. public readonly GUIContent BakingWarning = EditorGUIUtility.TrTextContent("Light mode is currently overridden to Realtime mode. Enable Baked Global Illumination to use Mixed or Baked light modes.");
  24. public readonly GUIContent DisabledLightWarning = EditorGUIUtility.TrTextContent("Lighting has been disabled in at least one Scene view. Any changes applied to lights in the Scene will not be updated in these views until Lighting has been enabled again.");
  25. public readonly GUIContent ShadowsNotSupportedWarning = EditorGUIUtility.TrTextContent("Realtime shadows for point lights are not supported. Either disable shadows or set the light mode to Baked.");
  26. public static readonly GUIContent ShadowRealtimeSettings = EditorGUIUtility.TrTextContent("Realtime Shadows", "Settings for realtime direct shadows.");
  27. public static readonly GUIContent ShadowStrength = EditorGUIUtility.TrTextContent("Strength", "Controls how dark the shadows cast by the light will be.");
  28. public static readonly GUIContent ShadowNearPlane = EditorGUIUtility.TrTextContent("Near Plane", "Controls the value for the near clip plane when rendering shadows. Currently clamped to 0.1 units or 1% of the lights range property, whichever is lower.");
  29. public static GUIContent shadowBias = EditorGUIUtility.TrTextContent("Bias", "Select if the Bias should use the settings from the Pipeline Asset or Custom settings.");
  30. public static int[] optionDefaultValues = { 0, 1 };
  31. public static GUIContent[] displayedDefaultOptions =
  32. {
  33. new GUIContent("Custom"),
  34. new GUIContent("Use Pipeline Settings")
  35. };
  36. }
  37. static Styles s_Styles;
  38. public bool typeIsSame { get { return !settings.lightType.hasMultipleDifferentValues; } }
  39. public bool shadowTypeIsSame { get { return !settings.shadowsType.hasMultipleDifferentValues; } }
  40. public bool lightmappingTypeIsSame { get { return !settings.lightmapping.hasMultipleDifferentValues; } }
  41. public Light lightProperty { get { return target as Light; } }
  42. public bool spotOptionsValue { get { return typeIsSame && lightProperty.type == LightType.Spot; } }
  43. public bool pointOptionsValue { get { return typeIsSame && lightProperty.type == LightType.Point; } }
  44. public bool dirOptionsValue { get { return typeIsSame && lightProperty.type == LightType.Directional; } }
  45. public bool areaOptionsValue { get { return typeIsSame && (lightProperty.type == LightType.Rectangle || lightProperty.type == LightType.Disc); } }
  46. // Point light realtime shadows not supported
  47. public bool runtimeOptionsValue { get { return typeIsSame && (lightProperty.type != LightType.Rectangle && lightProperty.type != LightType.Point && !settings.isCompletelyBaked); } }
  48. public bool bakedShadowRadius { get { return typeIsSame && (lightProperty.type == LightType.Point || lightProperty.type == LightType.Spot) && settings.isBakedOrMixed; } }
  49. public bool bakedShadowAngle { get { return typeIsSame && lightProperty.type == LightType.Directional && settings.isBakedOrMixed; } }
  50. public bool shadowOptionsValue { get { return shadowTypeIsSame && lightProperty.shadows != LightShadows.None; } }
  51. #pragma warning disable 618
  52. public bool bakingWarningValue { get { return !UnityEditor.Lightmapping.bakedGI && lightmappingTypeIsSame && settings.isBakedOrMixed; } }
  53. #pragma warning restore 618
  54. public bool showLightBounceIntensity { get { return true; } }
  55. public bool isShadowEnabled { get { return settings.shadowsType.intValue != 0; } }
  56. public bool realtimeShadowsWarningValue
  57. {
  58. get
  59. {
  60. return typeIsSame && lightProperty.type == LightType.Point &&
  61. shadowTypeIsSame && isShadowEnabled &&
  62. lightmappingTypeIsSame && !settings.isCompletelyBaked;
  63. }
  64. }
  65. UniversalAdditionalLightData m_AdditionalLightData;
  66. SerializedObject m_AdditionalLightDataSO;
  67. SerializedProperty m_UseAdditionalDataProp;
  68. protected override void OnEnable()
  69. {
  70. m_AdditionalLightData = lightProperty.gameObject.GetComponent<UniversalAdditionalLightData>();
  71. settings.OnEnable();
  72. init(m_AdditionalLightData);
  73. UpdateShowOptions(true);
  74. }
  75. void init(UniversalAdditionalLightData additionalLightData)
  76. {
  77. if(additionalLightData == null)
  78. return;
  79. m_AdditionalLightDataSO = new SerializedObject(additionalLightData);
  80. m_UseAdditionalDataProp = m_AdditionalLightDataSO.FindProperty("m_UsePipelineSettings");
  81. settings.ApplyModifiedProperties();
  82. }
  83. public override void OnInspectorGUI()
  84. {
  85. if (s_Styles == null)
  86. s_Styles = new Styles();
  87. settings.Update();
  88. // Update AnimBool options. For properties changed they will be smoothly interpolated.
  89. UpdateShowOptions(false);
  90. settings.DrawLightType();
  91. EditorGUILayout.Space();
  92. // When we are switching between two light types that don't show the range (directional and area lights)
  93. // we want the fade group to stay hidden.
  94. using (var group = new EditorGUILayout.FadeGroupScope(1.0f - m_AnimDirOptions.faded))
  95. if (group.visible)
  96. settings.DrawRange(m_AnimAreaOptions.target);
  97. // Spot angle
  98. using (var group = new EditorGUILayout.FadeGroupScope(m_AnimSpotOptions.faded))
  99. if (group.visible)
  100. DrawSpotAngle();
  101. // Area width & height
  102. using (var group = new EditorGUILayout.FadeGroupScope(m_AnimAreaOptions.faded))
  103. if (group.visible)
  104. settings.DrawArea();
  105. settings.DrawColor();
  106. EditorGUILayout.Space();
  107. CheckLightmappingConsistency();
  108. using (var group = new EditorGUILayout.FadeGroupScope(1.0f - m_AnimAreaOptions.faded))
  109. if (group.visible)
  110. {
  111. Light light = target as Light;
  112. if (light.type != LightType.Disc)
  113. {
  114. settings.DrawLightmapping();
  115. }
  116. }
  117. settings.DrawIntensity();
  118. using (var group = new EditorGUILayout.FadeGroupScope(m_AnimLightBounceIntensity.faded))
  119. if (group.visible)
  120. settings.DrawBounceIntensity();
  121. ShadowsGUI();
  122. settings.DrawRenderMode();
  123. settings.DrawCullingMask();
  124. EditorGUILayout.Space();
  125. if (SceneView.lastActiveSceneView != null )
  126. {
  127. #if UNITY_2019_1_OR_NEWER
  128. var sceneLighting = SceneView.lastActiveSceneView.sceneLighting;
  129. #else
  130. var sceneLighting = SceneView.lastActiveSceneView.m_SceneLighting;
  131. #endif
  132. if (!sceneLighting)
  133. EditorGUILayout.HelpBox(s_Styles.DisabledLightWarning.text, MessageType.Warning);
  134. }
  135. serializedObject.ApplyModifiedProperties();
  136. }
  137. void CheckLightmappingConsistency()
  138. {
  139. //Universal render-pipeline only supports baked area light, enforce it as this inspector is the universal one.
  140. if (settings.isAreaLightType && settings.lightmapping.intValue != (int)LightmapBakeType.Baked)
  141. {
  142. settings.lightmapping.intValue = (int)LightmapBakeType.Baked;
  143. serializedObject.ApplyModifiedProperties();
  144. }
  145. }
  146. void SetOptions(AnimBool animBool, bool initialize, bool targetValue)
  147. {
  148. if (initialize)
  149. {
  150. animBool.value = targetValue;
  151. animBool.valueChanged.AddListener(Repaint);
  152. }
  153. else
  154. {
  155. animBool.target = targetValue;
  156. }
  157. }
  158. void UpdateShowOptions(bool initialize)
  159. {
  160. SetOptions(m_AnimSpotOptions, initialize, spotOptionsValue);
  161. SetOptions(m_AnimPointOptions, initialize, pointOptionsValue);
  162. SetOptions(m_AnimDirOptions, initialize, dirOptionsValue);
  163. SetOptions(m_AnimAreaOptions, initialize, areaOptionsValue);
  164. SetOptions(m_AnimShadowOptions, initialize, shadowOptionsValue);
  165. SetOptions(m_AnimRuntimeOptions, initialize, runtimeOptionsValue);
  166. SetOptions(m_AnimShadowAngleOptions, initialize, bakedShadowAngle);
  167. SetOptions(m_AnimShadowRadiusOptions, initialize, bakedShadowRadius);
  168. SetOptions(m_AnimLightBounceIntensity, initialize, showLightBounceIntensity);
  169. }
  170. void DrawSpotAngle()
  171. {
  172. settings.DrawInnerAndOuterSpotAngle();
  173. }
  174. void DrawAdditionalShadowData()
  175. {
  176. bool hasChanged = false;
  177. int selectedUseAdditionalData;
  178. if (m_AdditionalLightDataSO == null)
  179. {
  180. selectedUseAdditionalData = 1;
  181. }
  182. else
  183. {
  184. m_AdditionalLightDataSO.Update();
  185. selectedUseAdditionalData = !m_AdditionalLightData.usePipelineSettings ? 0 : 1;
  186. }
  187. Rect controlRectAdditionalData = EditorGUILayout.GetControlRect(true);
  188. if(m_AdditionalLightDataSO != null)
  189. EditorGUI.BeginProperty(controlRectAdditionalData, Styles.shadowBias, m_UseAdditionalDataProp);
  190. EditorGUI.BeginChangeCheck();
  191. selectedUseAdditionalData = EditorGUI.IntPopup(controlRectAdditionalData, Styles.shadowBias, selectedUseAdditionalData, Styles.displayedDefaultOptions, Styles.optionDefaultValues);
  192. if (EditorGUI.EndChangeCheck())
  193. {
  194. hasChanged = true;
  195. }
  196. if(m_AdditionalLightDataSO != null)
  197. EditorGUI.EndProperty();
  198. if (selectedUseAdditionalData != 1 && m_AdditionalLightDataSO != null)
  199. {
  200. EditorGUI.indentLevel++;
  201. EditorGUILayout.Slider(settings.shadowsBias, 0f, 10f, "Depth");
  202. EditorGUILayout.Slider(settings.shadowsNormalBias, 0f, 10f, "Normal");
  203. EditorGUI.indentLevel--;
  204. m_AdditionalLightDataSO.ApplyModifiedProperties();
  205. }
  206. if (hasChanged)
  207. {
  208. if (m_AdditionalLightDataSO == null)
  209. {
  210. lightProperty.gameObject.AddComponent<UniversalAdditionalLightData>();
  211. m_AdditionalLightData = lightProperty.gameObject.GetComponent<UniversalAdditionalLightData>();
  212. UniversalRenderPipelineAsset asset = GraphicsSettings.renderPipelineAsset as UniversalRenderPipelineAsset;
  213. settings.shadowsBias.floatValue = asset.shadowDepthBias;
  214. settings.shadowsNormalBias.floatValue = asset.shadowNormalBias;
  215. init(m_AdditionalLightData);
  216. }
  217. m_UseAdditionalDataProp.intValue = selectedUseAdditionalData;
  218. m_AdditionalLightDataSO.ApplyModifiedProperties();
  219. }
  220. }
  221. void ShadowsGUI()
  222. {
  223. // Shadows drop-down. Area lights can only be baked and always have shadows.
  224. float show = 1.0f - m_AnimAreaOptions.faded;
  225. settings.DrawShadowsType();
  226. EditorGUI.indentLevel += 1;
  227. show *= m_AnimShadowOptions.faded;
  228. // Baked Shadow radius
  229. using (var group = new EditorGUILayout.FadeGroupScope(show * m_AnimShadowRadiusOptions.faded))
  230. if (group.visible)
  231. settings.DrawBakedShadowRadius();
  232. // Baked Shadow angle
  233. using (var group = new EditorGUILayout.FadeGroupScope(show * m_AnimShadowAngleOptions.faded))
  234. if (group.visible)
  235. settings.DrawBakedShadowAngle();
  236. // Runtime shadows - shadow strength, resolution and near plane offset
  237. // Bias is handled differently in UniversalRP
  238. using (var group = new EditorGUILayout.FadeGroupScope(show * m_AnimRuntimeOptions.faded))
  239. {
  240. if (group.visible)
  241. {
  242. EditorGUILayout.LabelField(Styles.ShadowRealtimeSettings);
  243. EditorGUI.indentLevel += 1;
  244. EditorGUILayout.Slider(settings.shadowsStrength, 0f, 1f, Styles.ShadowStrength);
  245. DrawAdditionalShadowData();
  246. // this min bound should match the calculation in SharedLightData::GetNearPlaneMinBound()
  247. float nearPlaneMinBound = Mathf.Min(0.01f * settings.range.floatValue, 0.1f);
  248. EditorGUILayout.Slider(settings.shadowsNearPlane, nearPlaneMinBound, 10.0f, Styles.ShadowNearPlane);
  249. EditorGUI.indentLevel -= 1;
  250. }
  251. }
  252. EditorGUI.indentLevel -= 1;
  253. if (bakingWarningValue)
  254. EditorGUILayout.HelpBox(s_Styles.BakingWarning.text, MessageType.Warning);
  255. if (realtimeShadowsWarningValue)
  256. EditorGUILayout.HelpBox(s_Styles.ShadowsNotSupportedWarning.text, MessageType.Warning);
  257. EditorGUILayout.Space();
  258. }
  259. protected override void OnSceneGUI()
  260. {
  261. if (!(GraphicsSettings.currentRenderPipeline is UniversalRenderPipelineAsset))
  262. return;
  263. Light light = target as Light;
  264. switch (light.type)
  265. {
  266. case LightType.Spot:
  267. using (new Handles.DrawingScope(Matrix4x4.TRS(light.transform.position, light.transform.rotation, Vector3.one)))
  268. {
  269. CoreLightEditorUtilities.DrawSpotLightGizmo(light);
  270. }
  271. break;
  272. case LightType.Point:
  273. using (new Handles.DrawingScope(Matrix4x4.TRS(light.transform.position, Quaternion.identity, Vector3.one)))
  274. {
  275. CoreLightEditorUtilities.DrawPointLightGizmo(light);
  276. }
  277. break;
  278. case LightType.Rectangle:
  279. using (new Handles.DrawingScope(Matrix4x4.TRS(light.transform.position, light.transform.rotation, Vector3.one)))
  280. {
  281. CoreLightEditorUtilities.DrawRectangleLightGizmo(light);
  282. }
  283. break;
  284. case LightType.Disc:
  285. using (new Handles.DrawingScope(Matrix4x4.TRS(light.transform.position, light.transform.rotation, Vector3.one)))
  286. {
  287. CoreLightEditorUtilities.DrawDiscLightGizmo(light);
  288. }
  289. break;
  290. case LightType.Directional:
  291. using (new Handles.DrawingScope(Matrix4x4.TRS(light.transform.position, light.transform.rotation, Vector3.one)))
  292. {
  293. CoreLightEditorUtilities.DrawDirectionalLightGizmo(light);
  294. }
  295. break;
  296. default:
  297. base.OnSceneGUI();
  298. break;
  299. }
  300. }
  301. }
  302. }