MeshCreatorWizard.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 UnityEngine;
  23. using System.Collections;
  24. using UnityEditor;
  25. // enum for the dropdown object type selector
  26. public enum ObjectMeshType
  27. {
  28. Flat2D = 0,
  29. Full3D = 1
  30. }
  31. public enum ObjectColliderType
  32. {
  33. Boxes = 0,
  34. Mesh = 1,
  35. BoundingBox = 2,
  36. None = 3
  37. }
  38. // thanks to Chris Reilly for changing this to EditorWindow from Wizard
  39. public class MeshCreatorWizard : EditorWindow
  40. {
  41. private const float versionNumber = 0.7f;
  42. private Texture2D gameLabLogo = Resources.Load("games.ucla.logo.small") as Texture2D;
  43. public Texture2D textureToCreateMeshFrom;
  44. public bool withColliders;
  45. public string gameObjectName = "Mesh Creator Object";
  46. // enum for the meshtype(2d,3d) to be created
  47. public ObjectMeshType meshType = ObjectMeshType.Flat2D;
  48. // enum for they collider type to be created
  49. public ObjectColliderType colliderType = ObjectColliderType.Boxes;
  50. // window size
  51. static public Vector2 minWindowSize = new Vector2(600, 425);
  52. // Add menu named "Create Mesh Object" to the GameObject menu
  53. [MenuItem("GameObject/Create Mesh Object")]
  54. static void Init()
  55. {
  56. // Get existing open window or if none, make a new one:
  57. MeshCreatorWizard window = (MeshCreatorWizard)EditorWindow.GetWindow(typeof(MeshCreatorWizard), true, "Create Mesh Object v" + versionNumber);
  58. window.minSize = minWindowSize;
  59. }
  60. void OnGUI()
  61. {
  62. EditorGUIUtility.AddCursorRect(new Rect(10, 10, 400, 150), MouseCursor.Link);
  63. GUILayout.BeginHorizontal();
  64. GUILayout.FlexibleSpace();
  65. // display game lab logo & link
  66. if (GUILayout.Button(gameLabLogo))
  67. {
  68. Application.OpenURL("http://games.ucla.edu/");
  69. }
  70. GUILayout.FlexibleSpace();
  71. //basic instructions
  72. GUILayout.Label("Choose a texture with alpha channel to create a mesh from\nSquare images are recommended.\n\nThen select whether to create depth on the mesh and whether you\nwant colliders for your new mesh.\n\nEnter a game object name and you are good to go.\n\nAdvanced control is available once you create the object.", GUILayout.Width(400));
  73. GUILayout.FlexibleSpace();
  74. GUILayout.EndHorizontal();
  75. EditorGUILayout.Space();
  76. GUILayout.BeginHorizontal();
  77. GUILayout.FlexibleSpace();
  78. GUILayout.BeginVertical();
  79. //source texture
  80. EditorGUILayout.Space();
  81. EditorGUILayout.BeginHorizontal();
  82. GUILayout.Label("Texture to Create Mesh From", GUILayout.Width(175));
  83. GUILayoutOption[] textureDisplaySize = { GUILayout.Width(150), GUILayout.Height(150) };
  84. if (textureToCreateMeshFrom != null)
  85. {
  86. if (textureToCreateMeshFrom.height != textureToCreateMeshFrom.width)
  87. {
  88. if (textureToCreateMeshFrom.width > textureToCreateMeshFrom.height)
  89. {
  90. textureDisplaySize[0] = GUILayout.Width(150);
  91. textureDisplaySize[1] = GUILayout.Height(150 * textureToCreateMeshFrom.height / textureToCreateMeshFrom.width);
  92. }
  93. else
  94. {
  95. textureDisplaySize[0] = GUILayout.Width(150 * textureToCreateMeshFrom.width / textureToCreateMeshFrom.height);
  96. textureDisplaySize[1] = GUILayout.Height(150 );
  97. }
  98. }
  99. }
  100. textureToCreateMeshFrom = (Texture2D)EditorGUILayout.ObjectField(textureToCreateMeshFrom, typeof(Texture2D), false, textureDisplaySize);
  101. EditorGUILayout.EndHorizontal();
  102. EditorGUILayout.Space();
  103. // what type of object being created, 2d or 3d?
  104. GUILayout.BeginHorizontal();
  105. meshType = (ObjectMeshType)EditorGUILayout.EnumPopup("Mesh Type", meshType, GUILayout.Width(330));
  106. GUILayout.EndHorizontal();
  107. //with colliders?
  108. GUILayout.BeginHorizontal();
  109. colliderType = (ObjectColliderType)EditorGUILayout.EnumPopup("Collider Type", colliderType, GUILayout.Width(330));
  110. GUILayout.EndHorizontal();
  111. //object name
  112. GUILayout.BeginHorizontal();
  113. GUILayout.Label("Game Object Name", GUILayout.Width(175));
  114. gameObjectName = GUILayout.TextField(gameObjectName, 50, GUILayout.Width(175));
  115. GUILayout.EndHorizontal();
  116. EditorGUILayout.Space();
  117. EditorGUILayout.Space();
  118. //submit button
  119. GUILayout.BeginHorizontal();
  120. GUILayout.FlexibleSpace();
  121. if (GUILayout.Button("Create Mesh", GUILayout.Width(100))
  122. && textureToCreateMeshFrom != null)
  123. {
  124. // register the Undo
  125. Undo.RegisterSceneUndo("Create New Mesh Object");
  126. // create the new object and set the proper variables
  127. GameObject newObject = new GameObject(gameObjectName);
  128. MeshCreatorData mcd = newObject.AddComponent<MeshCreatorData>() as MeshCreatorData;
  129. // set up mesh creator data
  130. mcd.outlineTexture = textureToCreateMeshFrom;
  131. mcd.useAutoGeneratedMaterial = true;
  132. // for height and width, maintain the image's aspect ratio
  133. if (textureToCreateMeshFrom.height != textureToCreateMeshFrom.width)
  134. {
  135. float height = textureToCreateMeshFrom.height;
  136. float width = textureToCreateMeshFrom.width;
  137. Debug.LogWarning("MeshCreatorWizard:: image " + textureToCreateMeshFrom.name + " has non-square size " + width + "x" + height + ", adjusting scale to match.");
  138. if (height > width)
  139. {
  140. mcd.meshHeight = 1.0f;
  141. mcd.meshWidth = width / height;
  142. }
  143. else
  144. {
  145. mcd.meshHeight = height / width;
  146. mcd.meshWidth = 1.0f;
  147. }
  148. }
  149. else
  150. {
  151. mcd.meshHeight = 1.0f;
  152. mcd.meshWidth = 1.0f;
  153. }
  154. mcd.meshDepth = 1.0f;
  155. // set up the depth options
  156. if (meshType == ObjectMeshType.Full3D)
  157. {
  158. mcd.uvWrapMesh = true;
  159. mcd.createEdges = false;
  160. mcd.createBacksidePlane = false;
  161. }
  162. else
  163. {
  164. mcd.uvWrapMesh = false;
  165. mcd.createEdges = false;
  166. mcd.createBacksidePlane = false;
  167. }
  168. // set up the collider options
  169. if (colliderType == ObjectColliderType.Boxes)
  170. {
  171. mcd.generateCollider = true;
  172. mcd.usePrimitiveCollider = true;
  173. mcd.useAABBCollider = false;
  174. mcd.maxNumberBoxes = 20;
  175. mcd.usePhysicMaterial = false;
  176. //mcd.addRigidBody = false;
  177. }
  178. else if (colliderType == ObjectColliderType.Mesh)
  179. {
  180. mcd.generateCollider = true;
  181. mcd.usePrimitiveCollider = false;
  182. mcd.useAABBCollider = false;
  183. mcd.maxNumberBoxes = 20;
  184. mcd.usePhysicMaterial = false;
  185. //mcd.addRigidBody = false;
  186. }
  187. else if (colliderType == ObjectColliderType.BoundingBox)
  188. {
  189. mcd.generateCollider = true;
  190. mcd.usePrimitiveCollider = false;
  191. mcd.useAABBCollider = true;
  192. mcd.maxNumberBoxes = 20;
  193. mcd.usePhysicMaterial = false;
  194. //mcd.addRigidBody = false;
  195. }
  196. else // default to none
  197. {
  198. mcd.generateCollider = false;
  199. mcd.usePrimitiveCollider = false;
  200. mcd.maxNumberBoxes = 20;
  201. mcd.usePhysicMaterial = false;
  202. //mcd.addRigidBody = false;
  203. }
  204. // update the mesh
  205. MeshCreator.UpdateMesh(newObject);
  206. Close();
  207. }
  208. GUILayout.FlexibleSpace();
  209. GUILayout.EndHorizontal();
  210. GUILayout.EndVertical();
  211. GUILayout.EndHorizontal();
  212. }
  213. }