DepthOfFieldEditor.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using UnityEditor;
  3. using UnityEditor.AnimatedValues;
  4. using UnityEngine;
  5. namespace UnityStandardAssets.ImageEffects
  6. {
  7. [CustomEditor(typeof(DepthOfField))]
  8. class DepthOfFieldEditor : Editor
  9. {
  10. SerializedObject serObj;
  11. SerializedProperty visualizeFocus;
  12. SerializedProperty focalLength;
  13. SerializedProperty focalSize;
  14. SerializedProperty aperture;
  15. SerializedProperty focalTransform;
  16. SerializedProperty maxBlurSize;
  17. SerializedProperty highResolution;
  18. SerializedProperty blurType;
  19. SerializedProperty blurSampleCount;
  20. SerializedProperty nearBlur;
  21. SerializedProperty foregroundOverlap;
  22. SerializedProperty dx11BokehThreshold;
  23. SerializedProperty dx11SpawnHeuristic;
  24. SerializedProperty dx11BokehTexture;
  25. SerializedProperty dx11BokehScale;
  26. SerializedProperty dx11BokehIntensity;
  27. AnimBool showFocalDistance = new AnimBool();
  28. AnimBool showDiscBlurSettings = new AnimBool();
  29. AnimBool showDX11BlurSettings = new AnimBool();
  30. AnimBool showNearBlurOverlapSize = new AnimBool();
  31. bool useFocalDistance { get { return focalTransform.objectReferenceValue == null; } }
  32. bool useDiscBlur { get { return blurType.enumValueIndex < 1; } }
  33. bool useDX11Blur { get { return blurType.enumValueIndex > 0; } }
  34. bool useNearBlur { get { return nearBlur.boolValue; } }
  35. void OnEnable()
  36. {
  37. serObj = new SerializedObject(target);
  38. visualizeFocus = serObj.FindProperty("visualizeFocus");
  39. focalLength = serObj.FindProperty("focalLength");
  40. focalSize = serObj.FindProperty("focalSize");
  41. aperture = serObj.FindProperty("aperture");
  42. focalTransform = serObj.FindProperty("focalTransform");
  43. maxBlurSize = serObj.FindProperty("maxBlurSize");
  44. highResolution = serObj.FindProperty("highResolution");
  45. blurType = serObj.FindProperty("blurType");
  46. blurSampleCount = serObj.FindProperty("blurSampleCount");
  47. nearBlur = serObj.FindProperty("nearBlur");
  48. foregroundOverlap = serObj.FindProperty("foregroundOverlap");
  49. dx11BokehThreshold = serObj.FindProperty("dx11BokehThreshold");
  50. dx11SpawnHeuristic = serObj.FindProperty("dx11SpawnHeuristic");
  51. dx11BokehTexture = serObj.FindProperty("dx11BokehTexture");
  52. dx11BokehScale = serObj.FindProperty("dx11BokehScale");
  53. dx11BokehIntensity = serObj.FindProperty("dx11BokehIntensity");
  54. InitializedAnimBools();
  55. }
  56. void InitializedAnimBools()
  57. {
  58. showFocalDistance.valueChanged.AddListener(Repaint);
  59. showFocalDistance.value = useFocalDistance;
  60. showDiscBlurSettings.valueChanged.AddListener(Repaint);
  61. showDiscBlurSettings.value = useDiscBlur;
  62. showDX11BlurSettings.valueChanged.AddListener(Repaint);
  63. showDX11BlurSettings.value = useDX11Blur;
  64. showNearBlurOverlapSize.valueChanged.AddListener(Repaint);
  65. showNearBlurOverlapSize.value = useNearBlur;
  66. }
  67. void UpdateAnimBoolTargets()
  68. {
  69. showFocalDistance.target = useFocalDistance;
  70. showDiscBlurSettings.target = useDiscBlur;
  71. showDX11BlurSettings.target = useDX11Blur;
  72. showNearBlurOverlapSize.target = useNearBlur;
  73. }
  74. public override void OnInspectorGUI()
  75. {
  76. serObj.Update();
  77. UpdateAnimBoolTargets();
  78. EditorGUILayout.LabelField("Simulates camera lens defocus", EditorStyles.miniLabel);
  79. GUILayout.Label("Focal Settings");
  80. EditorGUILayout.PropertyField(visualizeFocus, new GUIContent(" Visualize"));
  81. EditorGUILayout.PropertyField(focalTransform, new GUIContent(" Focus on Transform"));
  82. if (EditorGUILayout.BeginFadeGroup(showFocalDistance.faded))
  83. {
  84. EditorGUILayout.PropertyField(focalLength, new GUIContent(" Focal Distance"));
  85. }
  86. EditorGUILayout.EndFadeGroup();
  87. EditorGUILayout.Slider(focalSize, 0.0f, 2.0f, new GUIContent(" Focal Size"));
  88. EditorGUILayout.Slider(aperture, 0.0f, 1.0f, new GUIContent(" Aperture"));
  89. EditorGUILayout.Separator();
  90. EditorGUILayout.PropertyField(blurType, new GUIContent("Defocus Type"));
  91. if (!(target as DepthOfField).Dx11Support() && blurType.enumValueIndex > 0)
  92. {
  93. EditorGUILayout.HelpBox("DX11 mode not supported (need shader model 5)", MessageType.Info);
  94. }
  95. if (EditorGUILayout.BeginFadeGroup(showDiscBlurSettings.faded))
  96. {
  97. EditorGUILayout.PropertyField(blurSampleCount, new GUIContent(" Sample Count"));
  98. }
  99. EditorGUILayout.EndFadeGroup();
  100. EditorGUILayout.Slider(maxBlurSize, 0.1f, 2.0f, new GUIContent(" Max Blur Distance"));
  101. EditorGUILayout.PropertyField(highResolution, new GUIContent(" High Resolution"));
  102. EditorGUILayout.Separator();
  103. EditorGUILayout.PropertyField(nearBlur, new GUIContent("Near Blur"));
  104. if (EditorGUILayout.BeginFadeGroup(showNearBlurOverlapSize.faded))
  105. {
  106. EditorGUILayout.Slider(foregroundOverlap, 0.1f, 2.0f, new GUIContent(" Overlap Size"));
  107. }
  108. EditorGUILayout.EndFadeGroup();
  109. EditorGUILayout.Separator();
  110. if (EditorGUILayout.BeginFadeGroup(showDX11BlurSettings.faded))
  111. {
  112. GUILayout.Label("DX11 Bokeh Settings");
  113. EditorGUILayout.PropertyField(dx11BokehTexture, new GUIContent(" Bokeh Texture"));
  114. EditorGUILayout.Slider(dx11BokehScale, 0.0f, 50.0f, new GUIContent(" Bokeh Scale"));
  115. EditorGUILayout.Slider(dx11BokehIntensity, 0.0f, 100.0f, new GUIContent(" Bokeh Intensity"));
  116. EditorGUILayout.Slider(dx11BokehThreshold, 0.0f, 1.0f, new GUIContent(" Min Luminance"));
  117. EditorGUILayout.Slider(dx11SpawnHeuristic, 0.01f, 1.0f, new GUIContent(" Spawn Heuristic"));
  118. }
  119. EditorGUILayout.EndFadeGroup();
  120. serObj.ApplyModifiedProperties();
  121. }
  122. }
  123. }