MeshCreatorInspector.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /****************************************************************************
  2. Copyright (c) 2013, Jonathan Cecil and UCLA Game Lab
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice, this
  7. list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  12. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  13. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  15. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  17. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  18. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  20. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. *****************************************************************************/
  22. using UnityEditor;
  23. using UnityEngine;
  24. using System.Collections;
  25. using System.Collections.Generic;
  26. using System;
  27. /***
  28. * MeshCreatorInspector
  29. * modifies the inspector to show controls for the Mesh Creator Data component.
  30. * This script needs to be in the Editor folder of your project along
  31. * with the SimpleSurfaceEdge.cs, MeshCreator.cs, and the Triangulator.cs script.
  32. *
  33. ***/
  34. [CustomEditor(typeof(MeshCreatorData))]
  35. public class MeshCreatorInspector : Editor {
  36. private MeshCreatorData mcd;
  37. private MeshCreatorUndoManager mcud;
  38. private const float versionNumber = 0.7f;
  39. private bool showColliderInfo = false;
  40. private bool showMeshInfo = false;
  41. private bool showMaterialInfo = false;
  42. private bool showExperimentalInfo = false;
  43. private bool showToolInfo = false;
  44. // enums for mesh and collider type
  45. private ObjectColliderType colliderType;
  46. private ObjectMeshType meshType;
  47. /***
  48. * OnEnable
  49. * set the MeshCreator when component is added to the object
  50. ***/
  51. private void OnEnable()
  52. {
  53. mcd = target as MeshCreatorData;
  54. if (mcd == null) {
  55. Debug.LogError("MeshCreatorInspector::OnEnable(): couldn't find a MeshCreatorData.cs component. Is the file in your project?");
  56. }
  57. mcud = new MeshCreatorUndoManager(mcd, "Mesh Creator");
  58. }
  59. /***
  60. * OnInspectorGUI
  61. * this does the main display of information in the inspector.
  62. ***/
  63. public override void OnInspectorGUI() {
  64. mcud.CheckUndo();
  65. EditorGUIUtility.LookLikeInspector();
  66. // TODO: inspector layout should be redesigned so that it's easier to
  67. // see the texture and material information
  68. if (mcd != null) {
  69. // determine if we're looking at a scene object or a prefab object
  70. bool isPrefab = PrefabUtility.GetPrefabType(mcd.gameObject) == PrefabType.Prefab;
  71. // below is GUI code for normal scene view
  72. if (!isPrefab)
  73. {
  74. EditorGUILayout.LabelField("UCLA Game Lab Mesh Creator");
  75. EditorGUILayout.Space();
  76. EditorGUILayout.LabelField("Mesh Creation Outline", "");
  77. mcd.outlineTexture =
  78. EditorGUILayout.ObjectField("Mesh Outline Texture", mcd.outlineTexture, typeof(Texture2D), true) as Texture2D;
  79. mcd.pixelTransparencyThreshold = EditorGUILayout.Slider(" Pixel Threshold", mcd.pixelTransparencyThreshold, 1.0f, 255.0f);
  80. EditorGUILayout.Space();
  81. // what type of object being created, 2d or 3d?
  82. if (mcd.uvWrapMesh == true)
  83. {
  84. meshType = ObjectMeshType.Full3D;
  85. }
  86. else
  87. {
  88. meshType = ObjectMeshType.Flat2D;
  89. }
  90. meshType = (ObjectMeshType)EditorGUILayout.EnumPopup("Mesh Type", meshType);
  91. if (meshType == ObjectMeshType.Full3D)
  92. {
  93. mcd.uvWrapMesh = true;
  94. }
  95. else
  96. {
  97. mcd.uvWrapMesh = false;
  98. }
  99. //with colliders?
  100. if (mcd.generateCollider == false)
  101. {
  102. colliderType = ObjectColliderType.None;
  103. }
  104. else if (mcd.usePrimitiveCollider == false && mcd.useAABBCollider == false)
  105. {
  106. colliderType = ObjectColliderType.Mesh;
  107. }
  108. else if (mcd.usePrimitiveCollider == false && mcd.useAABBCollider == true)
  109. {
  110. colliderType = ObjectColliderType.BoundingBox;
  111. }
  112. else
  113. {
  114. colliderType = ObjectColliderType.Boxes;
  115. }
  116. colliderType = (ObjectColliderType)EditorGUILayout.EnumPopup("Collider Type", colliderType);
  117. if (colliderType == ObjectColliderType.None)
  118. {
  119. mcd.generateCollider = false;
  120. }
  121. else if (colliderType == ObjectColliderType.Mesh)
  122. {
  123. mcd.generateCollider = true;
  124. mcd.usePrimitiveCollider = false;
  125. mcd.useAABBCollider = false;
  126. }
  127. else if (colliderType == ObjectColliderType.BoundingBox)
  128. {
  129. mcd.generateCollider = true;
  130. mcd.usePrimitiveCollider = false;
  131. mcd.useAABBCollider = true;
  132. }
  133. else // ObjectColliderType.Boxes
  134. {
  135. mcd.generateCollider = true;
  136. mcd.usePrimitiveCollider = true;
  137. mcd.useAABBCollider = false;
  138. }
  139. EditorGUILayout.Space();
  140. if (mcd.uvWrapMesh) EditorGUILayout.TextArea("A 3d mesh will be created.");
  141. else
  142. {
  143. if (mcd.createEdges && mcd.createBacksidePlane) EditorGUILayout.TextArea("Flat front and back planes will be created, with a mesh side edge.");
  144. else if (mcd.createEdges) EditorGUILayout.TextArea("A flat front plane will be created, with a mesh side edge.");
  145. else if (mcd.createBacksidePlane) EditorGUILayout.TextArea("Flat front and back planes will be created.");
  146. else EditorGUILayout.TextArea("A flat front plane will be created.");
  147. }
  148. EditorGUILayout.Space();
  149. showMeshInfo = EditorGUILayout.Foldout(showMeshInfo, "Mesh Creation");
  150. if (showMeshInfo)
  151. {
  152. EditorGUILayout.LabelField(" Mesh id number", mcd.idNumber);
  153. if (!mcd.uvWrapMesh)
  154. {
  155. mcd.createEdges = EditorGUILayout.Toggle(" Create full mesh for edge?", mcd.createEdges);
  156. mcd.createBacksidePlane = EditorGUILayout.Toggle(" Create backside plane?", mcd.createBacksidePlane);
  157. }
  158. }
  159. EditorGUILayout.Space();
  160. showMaterialInfo = EditorGUILayout.Foldout(showMaterialInfo, "Mesh Materials");
  161. if (showMaterialInfo)
  162. {
  163. mcd.useAutoGeneratedMaterial = EditorGUILayout.Toggle(" Auto Generate Material?", mcd.useAutoGeneratedMaterial);
  164. if (!mcd.useAutoGeneratedMaterial) mcd.frontMaterial =
  165. EditorGUILayout.ObjectField(" Use Other Material", mcd.frontMaterial, typeof(Material), true) as Material;
  166. }
  167. EditorGUILayout.Space();
  168. showColliderInfo = EditorGUILayout.Foldout(showColliderInfo, "Collider Creation");
  169. if (showColliderInfo)
  170. {
  171. if (mcd.generateCollider && mcd.usePrimitiveCollider) mcd.maxNumberBoxes = EditorGUILayout.IntField(" Max Number Boxes", mcd.maxNumberBoxes);
  172. if (mcd.generateCollider)
  173. {
  174. mcd.usePhysicMaterial = EditorGUILayout.Toggle(" Use Physics Material?", mcd.usePhysicMaterial);
  175. if (mcd.usePhysicMaterial) mcd.physicMaterial =
  176. EditorGUILayout.ObjectField(" Physical Material", mcd.physicMaterial, typeof(PhysicMaterial), true) as PhysicMaterial;
  177. mcd.setTriggers = EditorGUILayout.Toggle(" Set Collider Triggers?", mcd.setTriggers);
  178. }
  179. }
  180. EditorGUILayout.Space();
  181. showExperimentalInfo = EditorGUILayout.Foldout(showExperimentalInfo, "Advanced");
  182. if (showExperimentalInfo)
  183. {
  184. EditorGUILayout.LabelField(" Mesh Scale", "");
  185. mcd.meshWidth = EditorGUILayout.FloatField(" Width", mcd.meshWidth);
  186. mcd.meshHeight = EditorGUILayout.FloatField(" Height", mcd.meshHeight);
  187. mcd.meshDepth = EditorGUILayout.FloatField(" Depth", mcd.meshDepth);
  188. EditorGUILayout.Space();
  189. EditorGUILayout.LabelField(" Edge Smoothing", "");
  190. mcd.mergeClosePoints = EditorGUILayout.Toggle(" Merge Close Vertices", mcd.mergeClosePoints);
  191. //mcd.mergePercent = EditorGUILayout.FloatField( "Merge Percent Points", mcd.mergePercent);
  192. if (mcd.mergeClosePoints) mcd.mergeDistance = EditorGUILayout.FloatField(" Merge Distance (px)", mcd.mergeDistance);
  193. EditorGUILayout.Space();
  194. EditorGUILayout.LabelField(" Pivot Position", "");
  195. mcd.pivotHeightOffset = EditorGUILayout.FloatField(" Pivot Height Offset", mcd.pivotHeightOffset);
  196. mcd.pivotWidthOffset = EditorGUILayout.FloatField(" Pivot Width Offset", mcd.pivotWidthOffset);
  197. mcd.pivotDepthOffset = EditorGUILayout.FloatField(" Pivot Depth Offset", mcd.pivotDepthOffset);
  198. }
  199. EditorGUILayout.Space();
  200. if (GUILayout.Button("Update Mesh", GUILayout.MaxWidth(100)))
  201. {
  202. // set entire scene for undo, object only won't work cause we're adding and removing components
  203. Undo.RegisterSceneUndo("Update Mesh Creator Object");
  204. // do some simple parameter checking here so we don't get into trouble
  205. if (mcd.maxNumberBoxes < 1)
  206. {
  207. Debug.LogWarning("Mesh Creator: minimum number of boxes should be one or more. Setting to 1 and continuing.");
  208. }
  209. else
  210. {
  211. MeshCreator.UpdateMesh(mcd.gameObject);
  212. }
  213. }
  214. showToolInfo = EditorGUILayout.Foldout(showToolInfo, "Mesh Creator Info");
  215. if (showToolInfo)
  216. {
  217. EditorGUILayout.LabelField(" Mesh Creator Data", "version " + MeshCreatorData.versionNumber.ToString());
  218. EditorGUILayout.LabelField(" Mesh Creator Editor", "version " + versionNumber.ToString());
  219. EditorGUILayout.LabelField(" Mesh Creator", "version " + MeshCreator.versionNumber.ToString());
  220. }
  221. } // end normal scene GUI code
  222. else // begin prefab inspector GUI code
  223. {
  224. EditorGUILayout.LabelField("UCLA Game Lab Mesh Creator");
  225. EditorGUILayout.Space();
  226. EditorGUILayout.LabelField("Mesh Creator must be used in a scene.");
  227. EditorGUILayout.LabelField("To manipulate Mesh Creator Data and update this prefab,");
  228. EditorGUILayout.LabelField("pull it into a scene, update Mesh Creator, and apply your");
  229. EditorGUILayout.LabelField("changes to the prefab.");
  230. EditorGUILayout.Space();
  231. EditorGUILayout.LabelField("Mesh Creation Outline", "");
  232. EditorGUILayout.ObjectField("Mesh Outline Texture", mcd.outlineTexture, typeof(Texture2D), true);
  233. EditorGUILayout.LabelField(" Pixel Threshold", mcd.pixelTransparencyThreshold.ToString() );
  234. EditorGUILayout.Space();
  235. // what type of object being created, 2d or 3d?
  236. if (mcd.uvWrapMesh == true)
  237. {
  238. meshType = ObjectMeshType.Full3D;
  239. }
  240. else
  241. {
  242. meshType = ObjectMeshType.Flat2D;
  243. }
  244. EditorGUILayout.LabelField("Mesh Type", meshType.ToString());
  245. //with colliders?
  246. if (mcd.generateCollider == false)
  247. {
  248. colliderType = ObjectColliderType.None;
  249. }
  250. else if (mcd.usePrimitiveCollider == false && mcd.useAABBCollider == false)
  251. {
  252. colliderType = ObjectColliderType.Mesh;
  253. }
  254. else if (mcd.usePrimitiveCollider == false && mcd.useAABBCollider == true)
  255. {
  256. colliderType = ObjectColliderType.BoundingBox;
  257. }
  258. else
  259. {
  260. colliderType = ObjectColliderType.Boxes;
  261. }
  262. EditorGUILayout.LabelField("Collider Type", colliderType.ToString());
  263. EditorGUILayout.Space();
  264. if (mcd.uvWrapMesh) EditorGUILayout.TextArea("A 3d mesh will be created.");
  265. else
  266. {
  267. if (mcd.createEdges && mcd.createBacksidePlane) EditorGUILayout.TextArea("Flat front and back planes will be created, with a mesh side edge.");
  268. else if (mcd.createEdges) EditorGUILayout.TextArea("A flat front plane will be created, with a mesh side edge.");
  269. else if (mcd.createBacksidePlane) EditorGUILayout.TextArea("Flat front and back planes will be created.");
  270. else EditorGUILayout.TextArea("A flat front plane will be created.");
  271. }
  272. EditorGUILayout.Space();
  273. showMeshInfo = EditorGUILayout.Foldout(showMeshInfo, "Mesh Creation");
  274. if (showMeshInfo)
  275. {
  276. EditorGUILayout.LabelField(" Mesh id number", mcd.idNumber);
  277. if (!mcd.uvWrapMesh)
  278. {
  279. EditorGUILayout.Toggle(" Create full mesh for edge?", mcd.createEdges);
  280. EditorGUILayout.Toggle(" Create backside plane?", mcd.createBacksidePlane);
  281. }
  282. }
  283. EditorGUILayout.Space();
  284. showMaterialInfo = EditorGUILayout.Foldout(showMaterialInfo, "Mesh Materials");
  285. if (showMaterialInfo)
  286. {
  287. EditorGUILayout.Toggle(" Auto Generate Material?", mcd.useAutoGeneratedMaterial);
  288. if (!mcd.useAutoGeneratedMaterial)
  289. EditorGUILayout.ObjectField(" Use Other Material", mcd.frontMaterial, typeof(Material), true);
  290. }
  291. EditorGUILayout.Space();
  292. showColliderInfo = EditorGUILayout.Foldout(showColliderInfo, "Collider Creation");
  293. if (showColliderInfo)
  294. {
  295. if (mcd.generateCollider && mcd.usePrimitiveCollider) EditorGUILayout.LabelField(" Max Number Boxes", mcd.maxNumberBoxes.ToString());
  296. if (mcd.generateCollider)
  297. {
  298. EditorGUILayout.Toggle(" Use Physics Material?", mcd.usePhysicMaterial);
  299. if (mcd.usePhysicMaterial)
  300. EditorGUILayout.ObjectField(" Physical Material", mcd.physicMaterial, typeof(PhysicMaterial), true);
  301. EditorGUILayout.Toggle(" Set Collider Triggers?", mcd.setTriggers);
  302. }
  303. }
  304. EditorGUILayout.Space();
  305. showExperimentalInfo = EditorGUILayout.Foldout(showExperimentalInfo, "Advanced");
  306. if (showExperimentalInfo)
  307. {
  308. EditorGUILayout.LabelField(" Mesh Scale", "");
  309. EditorGUILayout.LabelField(" Width", mcd.meshWidth.ToString());
  310. EditorGUILayout.LabelField(" Height", mcd.meshHeight.ToString());
  311. EditorGUILayout.LabelField(" Depth", mcd.meshDepth.ToString());
  312. EditorGUILayout.Space();
  313. EditorGUILayout.LabelField(" Edge Smoothing", "");
  314. EditorGUILayout.Toggle(" Merge Close Vertices", mcd.mergeClosePoints);
  315. if (mcd.mergeClosePoints) EditorGUILayout.LabelField(" Merge Distance (px)", mcd.mergeDistance.ToString());
  316. EditorGUILayout.Space();
  317. EditorGUILayout.LabelField(" Pivot Position", "");
  318. EditorGUILayout.LabelField(" Pivot Height Offset", mcd.pivotHeightOffset.ToString());
  319. EditorGUILayout.LabelField(" Pivot Width Offset", mcd.pivotWidthOffset.ToString());
  320. EditorGUILayout.LabelField(" Pivot Depth Offset", mcd.pivotDepthOffset.ToString());
  321. }
  322. showToolInfo = EditorGUILayout.Foldout(showToolInfo, "Mesh Creator Info");
  323. if (showToolInfo)
  324. {
  325. EditorGUILayout.LabelField(" Mesh Creator Data", "version " + MeshCreatorData.versionNumber.ToString());
  326. EditorGUILayout.LabelField(" Mesh Creator Editor", "version " + versionNumber.ToString());
  327. EditorGUILayout.LabelField(" Mesh Creator", "version " + MeshCreator.versionNumber.ToString());
  328. }
  329. } // end prefab inspector GUI code
  330. }
  331. else {
  332. Debug.LogError("MeshCreatorInspector::OnInspectorGUI(): couldn't find a MeshCreatorData component. Something has gone horribly wrong, try reloading your scene.");
  333. }
  334. mcud.CheckDirty();
  335. }
  336. }