ASEDiffusionProfile.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. #if UNITY_2019_3_OR_NEWER
  4. using UnityEditor;
  5. using UnityEngine;
  6. using System;
  7. using System.Reflection;
  8. namespace AmplifyShaderEditor
  9. {
  10. public class ASEDiffusionProfile : MaterialPropertyDrawer
  11. {
  12. string m_hashField = string.Empty;
  13. public ASEDiffusionProfile( object guidField )
  14. {
  15. m_hashField = guidField.ToString();
  16. }
  17. public override void OnGUI( Rect position, MaterialProperty prop, String label, MaterialEditor editor )
  18. {
  19. var guid = HDUtilsEx.ConvertVector4ToGUID( prop.vectorValue );
  20. var profile = AssetDatabase.LoadAssetAtPath( AssetDatabase.GUIDToAssetPath( guid ), DiffusionProfileSettingsEx.Type );
  21. EditorGUI.BeginChangeCheck();
  22. profile = EditorGUI.ObjectField( position, new GUIContent( label ), profile, DiffusionProfileSettingsEx.Type, false );
  23. if( EditorGUI.EndChangeCheck() )
  24. {
  25. Vector4 newGuid = Vector4.zero;
  26. float hash = 0;
  27. if( profile != null )
  28. {
  29. var guid2 = AssetDatabase.AssetPathToGUID( AssetDatabase.GetAssetPath( profile ) );
  30. newGuid = HDUtilsEx.ConvertGUIDToVector4( guid2 );
  31. hash = HDShadowUtilsEx.Asfloat( DiffusionProfileSettingsEx.Hash( profile ) );
  32. }
  33. prop.vectorValue = newGuid;
  34. var hashField = MaterialEditor.GetMaterialProperty( new UnityEngine.Object[] { editor.target }, m_hashField );
  35. if( hashField != null )
  36. {
  37. hashField.floatValue = hash;
  38. }
  39. }
  40. if( profile == null )
  41. prop.vectorValue = Vector4.zero;
  42. DiffusionProfileMaterialUIEx.DrawDiffusionProfileWarning( profile );
  43. }
  44. private static class DiffusionProfileMaterialUIEx
  45. {
  46. private static System.Type type = null;
  47. public static System.Type Type { get { return ( type == null ) ? type = System.Type.GetType( "UnityEditor.Rendering.HighDefinition.DiffusionProfileMaterialUI, Unity.RenderPipelines.HighDefinition.Editor" ) : type; } }
  48. public static void DrawDiffusionProfileWarning( UnityEngine.Object obj )
  49. {
  50. object[] parameters = new object[] { obj };
  51. MethodInfo method = Type.GetMethod( "DrawDiffusionProfileWarning", BindingFlags.Static | BindingFlags.NonPublic );
  52. method.Invoke( null, parameters );
  53. }
  54. }
  55. private static class HDShadowUtilsEx
  56. {
  57. private static System.Type type = null;
  58. public static System.Type Type { get { return ( type == null ) ? type = System.Type.GetType( "UnityEngine.Rendering.HighDefinition.HDShadowUtils, Unity.RenderPipelines.HighDefinition.Runtime" ) : type; } }
  59. public static float Asfloat( uint val )
  60. {
  61. object[] parameters = new object[] { val };
  62. MethodInfo method = Type.GetMethod( "Asfloat", new Type[] { typeof( uint ) } );
  63. return (float)method.Invoke( null, parameters );
  64. }
  65. public static uint Asuint( float val )
  66. {
  67. object[] parameters = new object[] { val };
  68. MethodInfo method = Type.GetMethod( "Asuint", new Type[] { typeof( float ) } );
  69. return (uint)method.Invoke( null, parameters );
  70. }
  71. }
  72. }
  73. }
  74. #endif //UNITY_2019_3_OR_NEWER