You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
8.4 KiB

  1. using UnityEngine;
  2. using UnityEditor;
  3. [CustomEditor(typeof(WaterBase))]
  4. public class WaterBaseEditor : Editor
  5. {
  6. public GameObject oceanBase;
  7. private WaterBase waterBase;
  8. private Material oceanMaterial = null;
  9. private SerializedObject serObj;
  10. private SerializedProperty sharedMaterial;
  11. public SerializedProperty waterQuality;
  12. public SerializedProperty edgeBlend;
  13. public void OnEnable ()
  14. {
  15. serObj = new SerializedObject (target);
  16. sharedMaterial = serObj.FindProperty("sharedMaterial");
  17. waterQuality = serObj.FindProperty("waterQuality");
  18. edgeBlend = serObj.FindProperty("edgeBlend");
  19. }
  20. public override void OnInspectorGUI ()
  21. {
  22. serObj.Update();
  23. waterBase = (WaterBase)serObj.targetObject;
  24. oceanBase = ((WaterBase)serObj.targetObject).gameObject;
  25. if(!oceanBase)
  26. return;
  27. GUILayout.Label ("This script helps adjusting water material properties", EditorStyles.miniBoldLabel);
  28. EditorGUILayout.PropertyField(sharedMaterial, new GUIContent("Material"));
  29. oceanMaterial = (Material)sharedMaterial.objectReferenceValue;
  30. if (!oceanMaterial) {
  31. sharedMaterial.objectReferenceValue = (Object)WaterEditorUtility.LocateValidWaterMaterial(oceanBase.transform);
  32. serObj.ApplyModifiedProperties();
  33. oceanMaterial = (Material)sharedMaterial.objectReferenceValue;
  34. if (!oceanMaterial)
  35. return;
  36. }
  37. EditorGUILayout.Separator ();
  38. GUILayout.Label ("Overall Quality", EditorStyles.boldLabel);
  39. EditorGUILayout.PropertyField(waterQuality, new GUIContent("Quality"));
  40. EditorGUILayout.PropertyField(edgeBlend, new GUIContent("Edge blend?"));
  41. if(waterQuality.intValue > (int)WaterQuality.Low && !SystemInfo.supportsRenderTextures)
  42. EditorGUILayout.HelpBox("Water features not supported", MessageType.Warning);
  43. if(edgeBlend.boolValue && !SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.Depth))
  44. EditorGUILayout.HelpBox("Edge blend not supported", MessageType.Warning);
  45. EditorGUILayout.Separator ();
  46. bool hasShore = oceanMaterial.HasProperty("_ShoreTex");
  47. GUILayout.Label ("Main Colors", EditorStyles.boldLabel);
  48. GUILayout.Label ("Alpha values define blending with realtime textures", EditorStyles.miniBoldLabel);
  49. WaterEditorUtility.SetMaterialColor("_BaseColor", EditorGUILayout.ColorField("Refraction", WaterEditorUtility.GetMaterialColor("_BaseColor", oceanMaterial)), oceanMaterial);
  50. WaterEditorUtility.SetMaterialColor("_ReflectionColor", EditorGUILayout.ColorField("Reflection", WaterEditorUtility.GetMaterialColor("_ReflectionColor", oceanMaterial)), oceanMaterial);
  51. EditorGUILayout.Separator ();
  52. GUILayout.Label ("Main Textures", EditorStyles.boldLabel);
  53. GUILayout.Label ("Used for small waves (bumps), foam and white caps", EditorStyles.miniBoldLabel);
  54. WaterEditorUtility.SetMaterialTexture("_BumpMap",(Texture)EditorGUILayout.ObjectField("Normals", WaterEditorUtility.GetMaterialTexture("_BumpMap", waterBase.sharedMaterial), typeof(Texture), false), waterBase.sharedMaterial);
  55. if (hasShore)
  56. WaterEditorUtility.SetMaterialTexture("_ShoreTex", (Texture)EditorGUILayout.ObjectField("Shore & Foam", WaterEditorUtility.GetMaterialTexture("_ShoreTex", waterBase.sharedMaterial), typeof(Texture), false), waterBase.sharedMaterial);
  57. Vector4 animationTiling;
  58. Vector4 animationDirection;
  59. Vector2 firstTiling;
  60. Vector2 secondTiling;
  61. Vector2 firstDirection;
  62. Vector2 secondDirection;
  63. animationTiling = WaterEditorUtility.GetMaterialVector("_BumpTiling", oceanMaterial);
  64. animationDirection = WaterEditorUtility.GetMaterialVector("_BumpDirection", oceanMaterial);
  65. firstTiling = new Vector2(animationTiling.x*100.0F,animationTiling.y*100.0F);
  66. secondTiling = new Vector2(animationTiling.z*100.0F,animationTiling.w*100.0F);
  67. firstTiling = EditorGUILayout.Vector2Field("Tiling 1", firstTiling);
  68. secondTiling = EditorGUILayout.Vector2Field("Tiling 2", secondTiling);
  69. //firstTiling.x = EditorGUILayout.FloatField("1st Tiling U", firstTiling.x);
  70. //firstTiling.y = EditorGUILayout.FloatField("1st Tiling V", firstTiling.y);
  71. //secondTiling.x = EditorGUILayout.FloatField("2nd Tiling U", secondTiling.x);
  72. //secondTiling.y = EditorGUILayout.FloatField("2nd Tiling V", secondTiling.y);
  73. firstDirection = new Vector2(animationDirection.x,animationDirection.y);
  74. secondDirection = new Vector2(animationDirection.z,animationDirection.w);
  75. //firstDirection.x = EditorGUILayout.FloatField("1st Animation U", firstDirection.x);
  76. //firstDirection.y = EditorGUILayout.FloatField("1st Animation V", firstDirection.y);
  77. //secondDirection.x = EditorGUILayout.FloatField("2nd Animation U", secondDirection.x);
  78. //secondDirection.y = EditorGUILayout.FloatField("2nd Animation V", secondDirection.y);
  79. firstDirection = EditorGUILayout.Vector2Field("Direction 1", firstDirection);
  80. secondDirection = EditorGUILayout.Vector2Field("Direction 2", secondDirection);
  81. animationTiling = new Vector4(firstTiling.x/100.0F,firstTiling.y/100.0F, secondTiling.x/100.0F,secondTiling.y/100.0F);
  82. animationDirection = new Vector4(firstDirection.x,firstDirection.y, secondDirection.x,secondDirection.y);
  83. WaterEditorUtility.SetMaterialVector("_BumpTiling", animationTiling, oceanMaterial);
  84. WaterEditorUtility.SetMaterialVector("_BumpDirection", animationDirection, oceanMaterial);
  85. Vector4 displacementParameter = WaterEditorUtility.GetMaterialVector("_DistortParams", oceanMaterial);
  86. Vector4 fade = WaterEditorUtility.GetMaterialVector("_InvFadeParemeter", oceanMaterial);
  87. EditorGUILayout.Separator ();
  88. GUILayout.Label ("Normals", EditorStyles.boldLabel);
  89. GUILayout.Label ("Displacement for fresnel, specular and reflection/refraction", EditorStyles.miniBoldLabel);
  90. float gerstnerNormalIntensity = WaterEditorUtility.GetMaterialFloat("_GerstnerIntensity", oceanMaterial);
  91. gerstnerNormalIntensity = EditorGUILayout.Slider("Per Vertex", gerstnerNormalIntensity, -2.5F, 2.5F);
  92. WaterEditorUtility.SetMaterialFloat("_GerstnerIntensity", gerstnerNormalIntensity, oceanMaterial);
  93. displacementParameter.x = EditorGUILayout.Slider("Per Pixel", displacementParameter.x, -4.0F, 4.0F);
  94. displacementParameter.y = EditorGUILayout.Slider("Distortion", displacementParameter.y, -0.5F, 0.5F);
  95. // fade.z = EditorGUILayout.Slider("Distance fade", fade.z, 0.0f, 0.5f);
  96. EditorGUILayout.Separator ();
  97. GUILayout.Label ("Fresnel", EditorStyles.boldLabel);
  98. GUILayout.Label ("Defines reflection to refraction relation", EditorStyles.miniBoldLabel);
  99. if(!oceanMaterial.HasProperty("_Fresnel")) {
  100. if(oceanMaterial.HasProperty("_FresnelScale")) {
  101. float fresnelScale = EditorGUILayout.Slider("Intensity", WaterEditorUtility.GetMaterialFloat("_FresnelScale", oceanMaterial), 0.1F, 4.0F);
  102. WaterEditorUtility.SetMaterialFloat("_FresnelScale", fresnelScale, oceanMaterial);
  103. }
  104. displacementParameter.z = EditorGUILayout.Slider("Power", displacementParameter.z, 0.1F, 10.0F);
  105. displacementParameter.w = EditorGUILayout.Slider("Bias", displacementParameter.w, -3.0F, 3.0F);
  106. }
  107. else
  108. {
  109. Texture fresnelTex = (Texture)EditorGUILayout.ObjectField(
  110. "Ramp",
  111. (Texture)WaterEditorUtility.GetMaterialTexture("_Fresnel",
  112. oceanMaterial),
  113. typeof(Texture),
  114. false);
  115. WaterEditorUtility.SetMaterialTexture("_Fresnel", fresnelTex, oceanMaterial);
  116. }
  117. EditorGUILayout.Separator ();
  118. WaterEditorUtility.SetMaterialVector("_DistortParams", displacementParameter, oceanMaterial);
  119. if (edgeBlend.boolValue)
  120. {
  121. GUILayout.Label ("Fading", EditorStyles.boldLabel);
  122. fade.x = EditorGUILayout.Slider("Edge fade", fade.x, 0.001f, 3.0f);
  123. if(hasShore)
  124. fade.y = EditorGUILayout.Slider("Shore fade", fade.y, 0.001f, 3.0f);
  125. fade.w = EditorGUILayout.Slider("Extinction fade", fade.w, 0.0f, 2.5f);
  126. WaterEditorUtility.SetMaterialVector("_InvFadeParemeter", fade, oceanMaterial);
  127. }
  128. EditorGUILayout.Separator ();
  129. if(oceanMaterial.HasProperty("_Foam")) {
  130. GUILayout.Label ("Foam", EditorStyles.boldLabel);
  131. Vector4 foam = WaterEditorUtility.GetMaterialVector("_Foam", oceanMaterial);
  132. foam.x = EditorGUILayout.Slider("Intensity", foam.x, 0.0F, 1.0F);
  133. foam.y = EditorGUILayout.Slider("Cutoff", foam.y, 0.0F, 1.0F);
  134. WaterEditorUtility.SetMaterialVector("_Foam", foam, oceanMaterial);
  135. }
  136. serObj.ApplyModifiedProperties();
  137. }
  138. }