DielectricSpecularControl.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Globalization;
  3. using System.Reflection;
  4. using UnityEngine;
  5. using UnityEditor.UIElements;
  6. using UnityEngine.UIElements;
  7. namespace UnityEditor.ShaderGraph.Drawing.Controls
  8. {
  9. [AttributeUsage(AttributeTargets.Property)]
  10. class DielectricSpecularControlAttribute : Attribute, IControlAttribute
  11. {
  12. public DielectricSpecularControlAttribute()
  13. {
  14. }
  15. public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
  16. {
  17. return new DielectricSpecularControlView(node, propertyInfo);
  18. }
  19. }
  20. class DielectricSpecularControlView : VisualElement
  21. {
  22. AbstractMaterialNode m_Node;
  23. PropertyInfo m_PropertyInfo;
  24. DielectricSpecularNode.DielectricMaterial m_DielectricMaterial;
  25. VisualElement m_RangePanel;
  26. Slider m_RangeSlider;
  27. FloatField m_RangeField;
  28. VisualElement m_IORPanel;
  29. Slider m_IORSlider;
  30. FloatField m_IORField;
  31. public DielectricSpecularControlView(AbstractMaterialNode node, PropertyInfo propertyInfo)
  32. {
  33. m_Node = node;
  34. m_PropertyInfo = propertyInfo;
  35. styleSheets.Add(Resources.Load<StyleSheet>("Styles/Controls/DielectricSpecularControlView"));
  36. m_DielectricMaterial = (DielectricSpecularNode.DielectricMaterial)m_PropertyInfo.GetValue(m_Node, null);
  37. if (propertyInfo.PropertyType != typeof(DielectricSpecularNode.DielectricMaterial))
  38. throw new ArgumentException("Property must be of type DielectricMaterial.", "propertyInfo");
  39. var enumPanel = new VisualElement { name = "enumPanel" };
  40. enumPanel.Add(new Label("Material"));
  41. var enumField = new EnumField(m_DielectricMaterial.type);
  42. enumField.RegisterValueChangedCallback(OnEnumChanged);
  43. enumPanel.Add(enumField);
  44. Add(enumPanel);
  45. m_RangePanel = new VisualElement { name = "sliderPanel" };
  46. m_RangePanel.Add(new Label("Range"));
  47. m_RangeSlider = new Slider(0.01f, 1) { value = m_DielectricMaterial.range };
  48. m_RangeSlider.RegisterValueChangedCallback((evt) => OnChangeRangeSlider(evt.newValue));
  49. m_RangePanel.Add(m_RangeSlider);
  50. m_RangeField = AddField(m_RangePanel, m_RangeSlider, 0, m_DielectricMaterial);
  51. m_RangePanel.SetEnabled(m_DielectricMaterial.type == DielectricMaterialType.Common);
  52. Add(m_RangePanel);
  53. m_IORPanel = new VisualElement { name = "sliderPanel" };
  54. m_IORPanel.Add(new Label("IOR"));
  55. m_IORSlider = new Slider(1, 2.5f) { value = m_DielectricMaterial.indexOfRefraction };
  56. m_IORSlider.RegisterValueChangedCallback((evt) => OnChangeIORSlider(evt.newValue));
  57. m_IORPanel.Add(m_IORSlider);
  58. m_IORField = AddField(m_IORPanel, m_IORSlider, 1, m_DielectricMaterial);
  59. m_IORPanel.SetEnabled(m_DielectricMaterial.type == DielectricMaterialType.Custom);
  60. Add(m_IORPanel);
  61. }
  62. void OnEnumChanged(ChangeEvent<Enum> evt)
  63. {
  64. if (!evt.newValue.Equals(m_DielectricMaterial.type))
  65. {
  66. m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
  67. m_DielectricMaterial.type = (DielectricMaterialType)evt.newValue;
  68. m_PropertyInfo.SetValue(m_Node, m_DielectricMaterial, null);
  69. switch (m_DielectricMaterial.type)
  70. {
  71. case DielectricMaterialType.Common:
  72. m_RangePanel.SetEnabled(true);
  73. m_IORPanel.SetEnabled(false);
  74. break;
  75. case DielectricMaterialType.Custom:
  76. m_RangePanel.SetEnabled(false);
  77. m_IORPanel.SetEnabled(true);
  78. break;
  79. default:
  80. m_RangePanel.SetEnabled(false);
  81. m_IORPanel.SetEnabled(false);
  82. break;
  83. }
  84. }
  85. }
  86. void OnChangeRangeSlider(float newValue)
  87. {
  88. m_Node.owner.owner.RegisterCompleteObjectUndo("Slider Change");
  89. m_DielectricMaterial.range = newValue;
  90. m_PropertyInfo.SetValue(m_Node, m_DielectricMaterial, null);
  91. if (m_RangeField != null)
  92. m_RangeField.value = newValue;
  93. this.MarkDirtyRepaint();
  94. }
  95. void OnChangeIORSlider(float newValue)
  96. {
  97. m_Node.owner.owner.RegisterCompleteObjectUndo("Slider Change");
  98. m_DielectricMaterial.indexOfRefraction = newValue;
  99. m_PropertyInfo.SetValue(m_Node, m_DielectricMaterial, null);
  100. if (m_IORField != null)
  101. m_IORField.value = newValue;
  102. this.MarkDirtyRepaint();
  103. }
  104. FloatField AddField(VisualElement panel, Slider slider, int index, DielectricSpecularNode.DielectricMaterial initMaterial)
  105. {
  106. float initValue;
  107. if (index == 1)
  108. initValue = initMaterial.indexOfRefraction;
  109. else
  110. initValue = initMaterial.range;
  111. var field = new FloatField { userData = index, value = initValue };
  112. field.RegisterCallback<MouseDownEvent>(Repaint);
  113. field.RegisterCallback<MouseMoveEvent>(Repaint);
  114. field.RegisterValueChangedCallback(evt =>
  115. {
  116. var fieldValue = (float)evt.newValue;
  117. if (index == 1)
  118. m_DielectricMaterial.indexOfRefraction = fieldValue;
  119. else
  120. m_DielectricMaterial.range = fieldValue;
  121. m_PropertyInfo.SetValue(m_Node, m_DielectricMaterial, null);
  122. this.MarkDirtyRepaint();
  123. });
  124. field.Q("unity-text-input").RegisterCallback<FocusOutEvent>(evt =>
  125. {
  126. if (index == 1)
  127. RedrawIORControls(m_DielectricMaterial.indexOfRefraction);
  128. else
  129. RedrawRangeControls(m_DielectricMaterial.range);
  130. this.MarkDirtyRepaint();
  131. });
  132. panel.Add(field);
  133. return field;
  134. }
  135. void RedrawRangeControls(float value)
  136. {
  137. value = Mathf.Max(Mathf.Min(value, 1), 0.01f);
  138. m_RangePanel.Remove(m_RangeSlider);
  139. m_RangeSlider = new Slider(0.01f, 1) { value = value };
  140. m_RangeSlider.RegisterValueChangedCallback((evt) => OnChangeRangeSlider(evt.newValue));
  141. m_RangePanel.Add(m_RangeSlider);
  142. m_RangePanel.Remove(m_RangeField);
  143. m_RangeField.value = value;
  144. m_RangePanel.Add(m_RangeField);
  145. }
  146. void RedrawIORControls(float value)
  147. {
  148. value = Mathf.Max(Mathf.Min(value, 5), 1);
  149. m_IORPanel.Remove(m_IORSlider);
  150. m_IORSlider = new Slider(1, 2.5f) { value = value };
  151. m_IORSlider.RegisterValueChangedCallback((evt) => OnChangeIORSlider(evt.newValue));
  152. m_IORPanel.Add(m_IORSlider);
  153. m_IORPanel.Remove(m_IORField);
  154. m_IORField.value = value;
  155. m_IORPanel.Add(m_IORField);
  156. }
  157. void Repaint<T>(MouseEventBase<T> evt) where T : MouseEventBase<T>, new()
  158. {
  159. evt.StopPropagation();
  160. this.MarkDirtyRepaint();
  161. }
  162. }
  163. }