1
0

StyledDiffusionMaterialDrawer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Cristian Pop - https://boxophobic.com/
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System;
  5. namespace Boxophobic.StyledGUI
  6. {
  7. public class StyledDiffusionMaterialDrawer : MaterialPropertyDrawer
  8. {
  9. public string propName;
  10. //GUIStyle styleCenteredHelpBox;
  11. public StyledDiffusionMaterialDrawer(string propName)
  12. {
  13. this.propName = propName;
  14. }
  15. public override void OnGUI(Rect position, MaterialProperty prop, String label, MaterialEditor materialEditor)
  16. {
  17. //SetGUIStyles();
  18. Material material = materialEditor.target as Material;
  19. UnityEngine.Object materialAsset = null;
  20. GUILayout.Space(5);
  21. if (material.GetInt(propName) == 0)
  22. {
  23. EditorGUILayout.HelpBox("Diffusion profile values not set! Due to the current HDRP architecture the diffusion profiles are not directly supported. You will need to create an HDRP Lit material and assign a Diffusion Profile to it, drag this HDRP material to the " + label + " slot to allow the profile values to be copied to the material. The HDRP material will not be saved to the property field! Please refer to the documentation for more information.", MessageType.Warning);
  24. }
  25. else
  26. {
  27. EditorGUILayout.HelpBox("Diffusion profile values set! Due to the current HDRP architecture the diffusion profiles are not directly supported. You will need to create an HDRP Lit material and assign a Diffusion Profile to it, drag this HDRP material to the " + label + " slot to allow the profile values to be copied to the material. The HDRP material will not be saved to the property field! Please refer to the documentation for more information.", MessageType.Info);
  28. }
  29. GUILayout.Space(10);
  30. materialAsset = (Material)EditorGUILayout.ObjectField(label, materialAsset, typeof(Material), false);
  31. Material materialObject = AssetDatabase.LoadAssetAtPath<Material>(AssetDatabase.GetAssetPath(materialAsset));
  32. if (materialAsset != null)
  33. {
  34. if (materialObject.HasProperty("_DiffusionProfileAsset") && materialObject.HasProperty("_DiffusionProfileHash"))
  35. {
  36. var diffusionProfileAsset = materialObject.GetVector("_DiffusionProfileAsset");
  37. var diffusionProfileHash = materialObject.GetFloat("_DiffusionProfileHash");
  38. if (diffusionProfileAsset.x != 0 && diffusionProfileHash != 0)
  39. {
  40. material.SetVector(propName + "_asset", diffusionProfileAsset);
  41. material.SetFloat(propName, diffusionProfileHash);
  42. Debug.Log("Diffusion Profile settings copied from " + materialObject.name + "!");
  43. materialAsset = null;
  44. }
  45. else
  46. {
  47. material.SetVector(propName + "_asset", Vector4.zero);
  48. material.SetFloat(propName, 0.0f);
  49. Debug.Log("Diffusion Profile settings set to None because " + materialObject.name + " has no Diffusion Profile asset!");
  50. materialAsset = null;
  51. }
  52. }
  53. else
  54. {
  55. Debug.Log("The Material used to copy the Diffusion Profile does not a valid Diffusion Profile!");
  56. }
  57. }
  58. //EditorGUI.HelpBox(new Rect(position.x, position.y + top, position.width, position.height), message, mType);
  59. }
  60. public override float GetPropertyHeight(MaterialProperty prop, string label, MaterialEditor editor)
  61. {
  62. return -2;
  63. }
  64. //void SetGUIStyles()
  65. //{
  66. // styleCenteredHelpBox = new GUIStyle(GUI.skin.GetStyle("HelpBox"))
  67. // {
  68. // alignment = TextAnchor.MiddleCenter,
  69. // };
  70. //}
  71. }
  72. }