PigmentMapGeneratorInspector.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // Fantasy Adventure Environment
  2. // Copyright Staggart Creations
  3. // staggart.xyz
  4. using UnityEngine;
  5. using System.Collections;
  6. using UnityEditor;
  7. using UnityEditor.Callbacks;
  8. using FAE;
  9. using System;
  10. namespace FAE
  11. {
  12. [CustomEditor(typeof(PigmentMapGenerator))]
  13. public class PigmentMapGeneratorInspector : Editor
  14. {
  15. PigmentMapGenerator pmg;
  16. new SerializedObject serializedObject;
  17. SerializedProperty isMultiTerrain;
  18. SerializedProperty isMegaSplat;
  19. SerializedProperty terrainObjects;
  20. SerializedProperty manualInput;
  21. SerializedProperty resIdx;
  22. SerializedProperty useCustomPigmentMap;
  23. SerializedProperty inputPigmentMap;
  24. SerializedProperty layerMask;
  25. public static string[] reslist = new string[] { "64x64", "128x128", "256x256", "512x512", "1024x1024", "2048x2048", "4096x4096" };
  26. // Use this for initialization
  27. void OnEnable()
  28. {
  29. pmg = (PigmentMapGenerator)target;
  30. GetProperties();
  31. }
  32. private void GetProperties()
  33. {
  34. serializedObject = new SerializedObject(pmg);
  35. isMultiTerrain = serializedObject.FindProperty("isMultiTerrain");
  36. isMegaSplat = serializedObject.FindProperty("isMegaSplat");
  37. terrainObjects = serializedObject.FindProperty("terrainObjects");
  38. manualInput = serializedObject.FindProperty("manualInput");
  39. resIdx = serializedObject.FindProperty("resIdx");
  40. useCustomPigmentMap = serializedObject.FindProperty("useCustomPigmentMap");
  41. inputPigmentMap = serializedObject.FindProperty("customPigmentMap");
  42. layerMask = serializedObject.FindProperty("layerMask");
  43. }
  44. //Meta
  45. private string terrainInfo;
  46. private bool showHelp;
  47. public override void OnInspectorGUI()
  48. {
  49. DoHeader();
  50. #if UNITY_2019_3_OR_NEWER
  51. if (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset)
  52. {
  53. EditorGUILayout.HelpBox("Not available in the Universal Render Pipeline.\n\nThe Stylized Grass Shader package is URP compatible", MessageType.Warning);
  54. GUILayout.Space(-32);
  55. using (new EditorGUILayout.HorizontalScope())
  56. {
  57. GUILayout.FlexibleSpace();
  58. if (GUILayout.Button("Open Asset Store", GUILayout.Width(120)))
  59. {
  60. Application.OpenURL("com.unity3d.kharma:content/143830");
  61. }
  62. GUILayout.Space(8);
  63. }
  64. GUILayout.Space(11);
  65. }
  66. #endif
  67. EditorGUI.BeginChangeCheck();
  68. serializedObject.Update();
  69. DrawTerrainInfo();
  70. EditorGUILayout.PropertyField(useCustomPigmentMap, new GUIContent("Custom pigment map"));
  71. if (showHelp) EditorGUILayout.HelpBox("This option allows you to assign a custom pigment map, rather than rendering one.", MessageType.Info);
  72. EditorGUILayout.Space();
  73. //Custom pigment map
  74. if (useCustomPigmentMap.boolValue)
  75. {
  76. EditorGUILayout.PropertyField(inputPigmentMap, new GUIContent("Input"));
  77. if (showHelp) EditorGUILayout.HelpBox("Grass heightmap should be stored in the alpha channel.", MessageType.Info);
  78. if (pmg.customPigmentMap)
  79. {
  80. //Check if input heightmap is readable
  81. try
  82. {
  83. pmg.customPigmentMap.GetPixel(0, 0);
  84. }
  85. catch (UnityException e)
  86. {
  87. if (e.Message.StartsWith("Texture '" + pmg.customPigmentMap.name + "' is not readable"))
  88. {
  89. EditorGUILayout.HelpBox("Please enable the Read/Write option on texture \"" + pmg.customPigmentMap.name + "\"\n\nIt accessed be read otherwise.", MessageType.Error);
  90. }
  91. }
  92. }
  93. EditorGUILayout.Space();
  94. }
  95. //If terrain list is empty, expand it
  96. if (isMultiTerrain.boolValue && terrainObjects.arraySize == 0 && !manualInput.boolValue)
  97. {
  98. terrainObjects.isExpanded = true;
  99. }
  100. DoTerrainList();
  101. EditorGUILayout.Space();
  102. if (!useCustomPigmentMap.boolValue)
  103. {
  104. //Safety check, required for preventing errors when updating from 1.1.2 to 1.2.0
  105. //if (pmg.terrains == null || pmg.terrains.Length == 0) return;
  106. //If single terrain without any textures
  107. if (!isMegaSplat.boolValue)
  108. {
  109. #if UNITY_2018_3_OR_NEWER
  110. if (!isMultiTerrain.boolValue && pmg.workflow == TerrainUVUtil.Workflow.Terrain && pmg.terrains[0].terrainData.terrainLayers.Length == 0)
  111. #else
  112. if (!isMultiTerrain.boolValue && pmg.workflow == TerrainUVUtil.Workflow.Terrain && pmg.terrains[0].terrainData.splatPrototypes.Length == 0)
  113. #endif
  114. {
  115. EditorGUILayout.HelpBox("Assign at least one texture to your terrain", MessageType.Error);
  116. return;
  117. }
  118. }
  119. DoHeightMap();
  120. DoTexTransforms();
  121. DoRenderer();
  122. }
  123. EditorGUILayout.Space();
  124. //Button
  125. //If multi terrain, with no objects assigned, don't show Generate button
  126. if (isMultiTerrain.boolValue && terrainObjects.arraySize == 0 && !manualInput.boolValue)
  127. {
  128. EditorGUILayout.HelpBox("Assign at least one terrain object", MessageType.Error);
  129. }
  130. EditorGUI.BeginDisabledGroup(isMultiTerrain.boolValue && terrainObjects.arraySize == 0 && !manualInput.boolValue);
  131. string buttonLabel = (useCustomPigmentMap.boolValue) ? "Assign" : "Generate";
  132. if (GUILayout.Button(buttonLabel, GUILayout.Height(40f)))
  133. {
  134. pmg.Generate();
  135. }
  136. EditorGUI.EndDisabledGroup();
  137. EditorGUILayout.Space();
  138. DoPreview();
  139. //GUIHelper.DrawFooter();
  140. serializedObject.ApplyModifiedProperties();
  141. if (GUI.changed || EditorGUI.EndChangeCheck())
  142. {
  143. EditorUtility.SetDirty((PigmentMapGenerator)target);
  144. EditorUtility.SetDirty(this);
  145. }
  146. }
  147. private void DoHeader()
  148. {
  149. EditorGUILayout.BeginHorizontal();
  150. showHelp = GUILayout.Toggle(showHelp, "Toggle help", "Button");
  151. GUILayout.Label("FAE Pigmentmap Generator", GUIHelper.Header);
  152. EditorGUILayout.EndHorizontal();
  153. if (showHelp) EditorGUILayout.HelpBox("This renders a color map from your terrain, which is used by the \"FAE/Grass\" shader to blend the grass color with the terrain.", MessageType.Info);
  154. if (showHelp) EditorGUILayout.HelpBox("If you'd like some objects to be included, like cliffs, parent them under your terrain object.", MessageType.Info);
  155. if (showHelp) EditorGUILayout.HelpBox("For a multi-terrain setup, add this script to the parent object of your terrain tiles/chunks.", MessageType.Info);
  156. EditorGUILayout.Space();
  157. }
  158. private void DrawTerrainInfo()
  159. {
  160. EditorGUILayout.PropertyField(manualInput, new GUIContent("Manual input"));
  161. if (showHelp) EditorGUILayout.HelpBox("Allows you to manually set the terrain's size, center and corner values. Particularly useful if your terrain(s) sits in a different scene.", MessageType.Info);
  162. if (manualInput.boolValue)
  163. {
  164. pmg.targetCenterPosition = EditorGUILayout.Vector3Field("Center", pmg.targetCenterPosition);
  165. pmg.targetSize = EditorGUILayout.Vector3Field("Size", pmg.targetSize);
  166. pmg.targetOriginPosition = EditorGUILayout.Vector3Field("Corner/Origin", pmg.targetOriginPosition);
  167. EditorGUILayout.Space();
  168. using (new EditorGUILayout.HorizontalScope())
  169. {
  170. GUILayout.FlexibleSpace();
  171. if (GUILayout.Button("Apply", EditorStyles.miniButton))
  172. {
  173. Shader.SetGlobalVector("_TerrainUV", new Vector4(pmg.targetSize.x, pmg.targetSize.z, Mathf.Abs(pmg.targetOriginPosition.x), Mathf.Abs(pmg.targetOriginPosition.z)));
  174. }
  175. }
  176. return;
  177. }
  178. terrainInfo = string.Format("Size: ({0},{1},{2}) \nCenter: ({3},{4}) \nCorner: ({5},{6})",
  179. Mathf.Round(pmg.targetSize.x),
  180. Mathf.Round(pmg.targetSize.y),
  181. Mathf.Round(pmg.targetSize.z),
  182. Mathf.Round(pmg.targetCenterPosition.x),
  183. Mathf.Round(pmg.targetCenterPosition.z),
  184. Mathf.Round(pmg.targetOriginPosition.x),
  185. Mathf.Round(pmg.targetOriginPosition.z)
  186. );
  187. EditorGUILayout.HelpBox(terrainInfo, MessageType.Info);
  188. GUILayout.Space(-32);
  189. using (new EditorGUILayout.HorizontalScope())
  190. {
  191. GUILayout.FlexibleSpace();
  192. pmg.showArea = GUILayout.Toggle(pmg.showArea, new GUIContent(" Show area", EditorGUIUtility.IconContent("Prefab Icon").image), "Button", GUILayout.MaxHeight(17f));
  193. GUILayout.Space(8);
  194. }
  195. GUILayout.Space(11);
  196. }
  197. private void DoTerrainList()
  198. {
  199. if (isMultiTerrain.boolValue && !manualInput.boolValue)
  200. {
  201. EditorGUILayout.PropertyField(terrainObjects, new GUIContent("Terrain objects"), true);
  202. if (terrainObjects.isExpanded)
  203. {
  204. EditorGUILayout.HelpBox("The first object in the array must be the corner chunk, where the terrain continues on the postive X and Z axis", MessageType.Warning);
  205. using (new EditorGUILayout.HorizontalScope())
  206. {
  207. if (GUILayout.Button("Assign all child objects"))
  208. {
  209. pmg.GetChildTerrainObjects(pmg.transform);
  210. }
  211. if (GUILayout.Button("Add active terrains"))
  212. {
  213. AssignActiveTerrains();
  214. }
  215. }
  216. }
  217. }
  218. else
  219. {
  220. //Single terrain, don't show array
  221. }
  222. }
  223. private void DoHeightMap()
  224. {
  225. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  226. //If is single terrain
  227. if (!isMultiTerrain.boolValue && pmg.workflow == TerrainUVUtil.Workflow.Terrain && !isMegaSplat.boolValue && !manualInput.boolValue)
  228. {
  229. EditorGUILayout.LabelField("Grass heightmap", EditorStyles.boldLabel);
  230. EditorGUILayout.Space();
  231. pmg.heightmapChannel = (PigmentMapGenerator.HeightmapChannel)EditorGUILayout.EnumPopup("Height source material", pmg.heightmapChannel);
  232. if (showHelp) EditorGUILayout.HelpBox("This is the texture whose painted weight will determine the grass height \n\nThe effect can be controlled through the \"Heightmap influence\" parameter on the FAE/Grass shader", MessageType.None);
  233. /*
  234. if (pmg.heightmapChannel > 0)
  235. {
  236. using (new EditorGUILayout.HorizontalScope())
  237. {
  238. GUILayout.FlexibleSpace();
  239. EditorGUILayout.LabelField(pmg.terrains[0].terrainData.splatPrototypes[(int)pmg.heightmapChannel - 1].texture.name);
  240. }
  241. }
  242. */
  243. }
  244. //If mesh or multi terrain
  245. else
  246. {
  247. //Field to assign heightmap texture
  248. EditorGUILayout.LabelField("Input grass heightmap (optional)", EditorStyles.boldLabel);
  249. EditorGUILayout.Space();
  250. pmg.inputHeightmap = EditorGUILayout.ObjectField("Heightmap", pmg.inputHeightmap, typeof(Texture2D), false) as Texture2D;
  251. if (showHelp) EditorGUILayout.HelpBox("This information is used in the \"FAE/Grass\" shader to make the grass shorter where desired", MessageType.Info);
  252. if (pmg.inputHeightmap)
  253. {
  254. //Check if input heightmap is readable
  255. try
  256. {
  257. pmg.inputHeightmap.GetPixel(0, 0);
  258. }
  259. catch (UnityException e)
  260. {
  261. if (e.Message.StartsWith("Texture '" + pmg.inputHeightmap.name + "' is not readable"))
  262. {
  263. EditorGUILayout.HelpBox("Please enable Read/Write on texture \"" + pmg.inputHeightmap.name + "\"", MessageType.Error);
  264. }
  265. }
  266. }
  267. }
  268. EditorGUILayout.Space();
  269. EditorGUILayout.EndVertical();
  270. }
  271. private void DoTexTransforms()
  272. {
  273. if (pmg.workflow == TerrainUVUtil.Workflow.Terrain) return;
  274. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  275. EditorGUILayout.LabelField("Texture transformation", EditorStyles.boldLabel);
  276. EditorGUILayout.Space();
  277. if (showHelp) EditorGUILayout.HelpBox("The UV's of your mesh terrain may differ, so these options allow you to compensate for this.", MessageType.Info);
  278. pmg.flipHortizontally = EditorGUILayout.Toggle("Flip horizontally", pmg.flipHortizontally);
  279. pmg.flipVertically = EditorGUILayout.Toggle("Flip vertically", pmg.flipVertically);
  280. pmg.textureRotation = (PigmentMapGenerator.TextureRotation)EditorGUILayout.EnumPopup("Rotation", pmg.textureRotation);
  281. EditorGUILayout.Space();
  282. EditorGUILayout.EndVertical();
  283. }
  284. private void DoRenderer()
  285. {
  286. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  287. EditorGUILayout.LabelField("Renderer", EditorStyles.boldLabel);
  288. EditorGUILayout.Space();
  289. if (manualInput.boolValue)
  290. {
  291. EditorGUILayout.PropertyField(layerMask, new GUIContent("Layer mask"));
  292. EditorGUILayout.HelpBox("Manual input mode requires terrains to be on a dedicated layer", MessageType.None);
  293. }
  294. resIdx.intValue = EditorGUILayout.Popup("Resolution", resIdx.intValue, reslist, new GUILayoutOption[0]);
  295. pmg.useAlternativeRenderer = EditorGUILayout.ToggleLeft("Using third-party terrain shader", pmg.useAlternativeRenderer);
  296. if (showHelp) EditorGUILayout.HelpBox("Some third-party terrain shaders require you to use this, otherwise the result may be black.", MessageType.Info);
  297. if (pmg.useAlternativeRenderer) pmg.renderLightBrightness = EditorGUILayout.Slider("Brightness adjustment", pmg.renderLightBrightness, 0f, 1f);
  298. if (pmg.useAlternativeRenderer && showHelp) EditorGUILayout.HelpBox("To compensate for any shader variations on the terrain, you can use this to increase the brightness of the pigment map, in case it turns out too dark/bright.", MessageType.Info);
  299. EditorGUILayout.Space();
  300. EditorGUILayout.EndVertical();
  301. }
  302. private void DoPreview()
  303. {
  304. if (!useCustomPigmentMap.boolValue)
  305. {
  306. //Pigment map preview
  307. if (!pmg.pigmentMap) return;
  308. EditorGUILayout.BeginHorizontal();
  309. EditorGUILayout.LabelField(string.Format("Output pigment map ({0}x{0})", pmg.pigmentMap.height), EditorStyles.boldLabel);
  310. EditorGUILayout.EndHorizontal();
  311. Rect cRect = EditorGUILayout.GetControlRect();
  312. using (new EditorGUILayout.HorizontalScope())
  313. {
  314. EditorGUI.DrawPreviewTexture(new Rect(cRect.x, cRect.y, 150f, 150f), pmg.pigmentMap, null, ScaleMode.ScaleAndCrop);
  315. }
  316. GUILayout.Space(140f);
  317. //Single terrain
  318. if (!isMultiTerrain.boolValue)
  319. {
  320. if (pmg.workflow == TerrainUVUtil.Workflow.Terrain)
  321. {
  322. if (pmg.hasTerrainData)
  323. {
  324. EditorGUILayout.LabelField("The output texture file is stored next to the TerrainData asset", EditorStyles.helpBox);
  325. }
  326. else
  327. {
  328. EditorGUILayout.LabelField("The output texture file is stored next to the scene file", EditorStyles.helpBox);
  329. }
  330. }
  331. else if (pmg.workflow == TerrainUVUtil.Workflow.Mesh)
  332. {
  333. EditorGUILayout.LabelField("The output texture file is stored next to the material file", EditorStyles.helpBox);
  334. }
  335. }
  336. //Multi terrain
  337. else
  338. {
  339. if (pmg.workflow == TerrainUVUtil.Workflow.Terrain)
  340. {
  341. EditorGUILayout.LabelField("The output texture file is stored next to the scene file", EditorStyles.helpBox);
  342. }
  343. else if (pmg.workflow == TerrainUVUtil.Workflow.Mesh)
  344. {
  345. EditorGUILayout.LabelField("The output texture file is stored next to the material file", EditorStyles.helpBox);
  346. }
  347. }
  348. }
  349. }
  350. private void AssignActiveTerrains()
  351. {
  352. Terrain[] terrains = Terrain.activeTerrains;
  353. pmg.terrainObjects = new GameObject[terrains.Length];
  354. for (int i = 0; i < terrains.Length; i++)
  355. {
  356. pmg.terrainObjects[i] = terrains[i].gameObject;
  357. }
  358. }
  359. }
  360. }