MB3_MeshBakerCommon.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. //----------------------------------------------
  2. // MeshBaker
  3. // Copyright © 2011-2012 Ian Deane
  4. //----------------------------------------------
  5. using UnityEngine;
  6. using System.Collections;
  7. using System.Collections.Specialized;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Text;
  11. using DigitalOpus.MB.Core;
  12. /// <summary>
  13. /// Maps a list of source materials to a combined material. Included in MB2_TextureBakeResults
  14. /// </summary>
  15. /// <summary>
  16. /// Abstract root of the mesh combining classes
  17. /// </summary>
  18. public abstract class MB3_MeshBakerCommon : MB3_MeshBakerRoot {
  19. //todo should be list of <Renderer>
  20. public List<GameObject> objsToMesh;
  21. public abstract MB3_MeshCombiner meshCombiner{
  22. get;
  23. }
  24. public bool useObjsToMeshFromTexBaker = true;
  25. public bool clearBuffersAfterBake = true;
  26. //t0do put this in the batch baker
  27. public string bakeAssetsInPlaceFolderPath;
  28. [HideInInspector] public GameObject resultPrefab;
  29. #if UNITY_EDITOR
  30. [ContextMenu("Create Mesh Baker Settings Asset")]
  31. public void CreateMeshBakerSettingsAsset()
  32. {
  33. string newFilePath = UnityEditor.EditorUtility.SaveFilePanelInProject("New Mesh Baker Settings", "MeshBakerSettings", "asset", "Create a new Mesh Baker Settings Asset");
  34. if (newFilePath != null)
  35. {
  36. MB3_MeshCombinerSettings asset = ScriptableObject.CreateInstance<MB3_MeshCombinerSettings>();
  37. UnityEditor.AssetDatabase.CreateAsset(asset, newFilePath);
  38. }
  39. }
  40. [ContextMenu("Copy settings from Shared Settings")]
  41. public void CopyMySettingsToAssignedSettingsAsset()
  42. {
  43. if (meshCombiner.settingsHolder == null)
  44. {
  45. Debug.LogError("No Shared Settings Asset Assigned.");
  46. return;
  47. }
  48. UnityEditor.Undo.RecordObject(this, "Undo copy settings");
  49. _CopySettings(meshCombiner.settingsHolder.GetMeshBakerSettings(), meshCombiner);
  50. Debug.Log("Copied settings from assigned Shared Settings to this Mesh Baker.");
  51. UnityEditor.EditorUtility.SetDirty(this);
  52. }
  53. [ContextMenu("Copy settings to Shared Settings")]
  54. public void CopyAssignedSettingsAssetToMySettings()
  55. {
  56. if (meshCombiner.settingsHolder == null)
  57. {
  58. Debug.LogError("No Shared Settings Asset Assigned.");
  59. return;
  60. }
  61. if (meshCombiner.settingsHolder is UnityEngine.Object) UnityEditor.Undo.RecordObject((UnityEngine.Object) meshCombiner.settingsHolder, "Undo copy settings");
  62. _CopySettings(meshCombiner, meshCombiner.settingsHolder.GetMeshBakerSettings());
  63. Debug.Log("Copied settings from this Mesh Baker to the assigned Shared Settings asset.");
  64. if (meshCombiner.settingsHolder is UnityEngine.Object) UnityEditor.EditorUtility.SetDirty((UnityEngine.Object)meshCombiner.settingsHolder);
  65. }
  66. void _CopySettings(MB_IMeshBakerSettings src, MB_IMeshBakerSettings targ)
  67. {
  68. targ.clearBuffersAfterBake = src.clearBuffersAfterBake;
  69. targ.doBlendShapes = src.doBlendShapes;
  70. targ.doCol = src.doCol;
  71. targ.doNorm = src.doNorm;
  72. targ.doTan = src.doTan;
  73. targ.doUV = src.doUV;
  74. targ.doUV3 = src.doUV3;
  75. targ.doUV4 = src.doUV4;
  76. targ.doUV5 = src.doUV5;
  77. targ.doUV6 = src.doUV6;
  78. targ.doUV7 = src.doUV7;
  79. targ.doUV8 = src.doUV8;
  80. targ.optimizeAfterBake = src.optimizeAfterBake;
  81. targ.pivotLocationType = src.pivotLocationType;
  82. targ.lightmapOption = src.lightmapOption;
  83. targ.renderType = src.renderType;
  84. targ.uv2UnwrappingParamsHardAngle = src.uv2UnwrappingParamsHardAngle;
  85. targ.uv2UnwrappingParamsPackMargin = src.uv2UnwrappingParamsPackMargin;
  86. }
  87. #endif
  88. public override MB2_TextureBakeResults textureBakeResults{
  89. get {return meshCombiner.textureBakeResults; }
  90. set {meshCombiner.textureBakeResults = value; }
  91. }
  92. public override List<GameObject> GetObjectsToCombine(){
  93. if (useObjsToMeshFromTexBaker){
  94. MB3_TextureBaker tb = gameObject.GetComponent<MB3_TextureBaker>();
  95. if (tb == null) tb = gameObject.transform.parent.GetComponent<MB3_TextureBaker>();
  96. if (tb != null) {
  97. return tb.GetObjectsToCombine();
  98. } else {
  99. Debug.LogWarning("Use Objects To Mesh From Texture Baker was checked but no texture baker");
  100. return new List<GameObject>();
  101. }
  102. } else {
  103. if (objsToMesh == null) objsToMesh = new List<GameObject>();
  104. return objsToMesh;
  105. }
  106. }
  107. public void EnableDisableSourceObjectRenderers(bool show){
  108. for (int i = 0; i < GetObjectsToCombine().Count; i++){
  109. GameObject go = GetObjectsToCombine()[i];
  110. if (go != null){
  111. Renderer mr = MB_Utility.GetRenderer(go);
  112. if (mr != null){
  113. mr.enabled = show;
  114. }
  115. LODGroup lodG = mr.GetComponentInParent<LODGroup>();
  116. if (lodG != null)
  117. {
  118. bool isOnlyInGroup = true;
  119. LOD[] lods = lodG.GetLODs();
  120. for (int j = 0; j < lods.Length; j++)
  121. {
  122. for (int k = 0; k < lods[j].renderers.Length; k++)
  123. {
  124. if (lods[j].renderers[k] != mr)
  125. {
  126. isOnlyInGroup = false;
  127. break;
  128. }
  129. }
  130. }
  131. if (isOnlyInGroup)
  132. {
  133. lodG.enabled = show;
  134. }
  135. }
  136. }
  137. }
  138. }
  139. /// <summary>
  140. /// Clears the meshs and mesh related data but does not destroy it.
  141. /// </summary>
  142. public virtual void ClearMesh(){
  143. meshCombiner.ClearMesh();
  144. }
  145. /// <summary>
  146. /// Clears and desroys the mesh. Clears mesh related data.
  147. /// </summary>
  148. public virtual void DestroyMesh(){
  149. meshCombiner.DestroyMesh();
  150. }
  151. public virtual void DestroyMeshEditor(MB2_EditorMethodsInterface editorMethods){
  152. meshCombiner.DestroyMeshEditor(editorMethods);
  153. }
  154. public virtual int GetNumObjectsInCombined(){
  155. return meshCombiner.GetNumObjectsInCombined();
  156. }
  157. public virtual int GetNumVerticesFor(GameObject go){
  158. return meshCombiner.GetNumVerticesFor(go);
  159. }
  160. /// <summary>
  161. /// Gets the texture baker on this component or its parent if it exists
  162. /// </summary>
  163. /// <returns>The texture baker.</returns>
  164. public MB3_TextureBaker GetTextureBaker(){
  165. MB3_TextureBaker tb = GetComponent<MB3_TextureBaker>();
  166. if (tb != null) return tb;
  167. if (transform.parent != null) return transform.parent.GetComponent<MB3_TextureBaker>();
  168. return null;
  169. }
  170. /// <summary>
  171. /// Adds and deletes objects from the combined mesh. gos and deleteGOs can be null.
  172. /// You need to call Apply or ApplyAll to see the changes.
  173. /// objects in gos must not include objects already in the combined mesh.
  174. /// objects in gos and deleteGOs must be the game objects with a Renderer component
  175. /// This method is slow, so should be called as infrequently as possible.
  176. /// </summary>
  177. /// <returns>
  178. /// The first generated combined mesh
  179. /// </returns>
  180. /// <param name='gos'>
  181. /// gos. Array of objects to add to the combined mesh. Array can be null. Must not include objects
  182. /// already in the combined mesh. Array must contain game objects with a render component.
  183. /// </param>
  184. /// <param name='deleteGOs'>
  185. /// deleteGOs. Array of objects to delete from the combined mesh. Array can be null.
  186. /// </param>
  187. /// <param name='disableRendererInSource'>
  188. /// Disable renderer component on objects in gos after they have been added to the combined mesh.
  189. /// </param>
  190. /// <param name='fixOutOfBoundUVs'>
  191. /// Whether to fix out of bounds UVs in meshes as they are being added. This paramater should be set to the same as the combined material.
  192. /// </param>
  193. /// </summary>
  194. public abstract bool AddDeleteGameObjects(GameObject[] gos, GameObject[] deleteGOs, bool disableRendererInSource = true);
  195. /// <summary>
  196. /// This is the best version to use for deleting game objects since the source GameObjects may have been destroyed
  197. /// Internaly Mesh Baker only stores the instanceID for Game Objects, so objects can be removed after they have been destroyed
  198. /// </summary>
  199. public abstract bool AddDeleteGameObjectsByID(GameObject[] gos, int[] deleteGOinstanceIDs, bool disableRendererInSource = true);
  200. /// <summary>
  201. /// Apply changes to the mesh. All channels set in this instance will be set in the combined mesh.
  202. /// </summary>
  203. public virtual void Apply(MB3_MeshCombiner.GenerateUV2Delegate uv2GenerationMethod=null){
  204. meshCombiner.name = name + "-mesh";
  205. meshCombiner.Apply(uv2GenerationMethod);
  206. }
  207. /// <summary>
  208. /// Applys the changes to flagged properties of the mesh. This method is slow, and should only be called once per frame. The speed is directly proportional to the number of flags that are true. Only apply necessary properties.
  209. /// </summary>
  210. public virtual void Apply(bool triangles,
  211. bool vertices,
  212. bool normals,
  213. bool tangents,
  214. bool uvs,
  215. bool uv2,
  216. bool uv3,
  217. bool uv4,
  218. bool colors,
  219. bool bones=false,
  220. bool blendShapesFlag=false,
  221. MB3_MeshCombiner.GenerateUV2Delegate uv2GenerationMethod=null){
  222. meshCombiner.name = name + "-mesh";
  223. meshCombiner.Apply(triangles,vertices,normals,tangents,uvs,uv2,uv3,uv4,colors,bones, blendShapesFlag,uv2GenerationMethod);
  224. }
  225. public virtual bool CombinedMeshContains(GameObject go){
  226. return meshCombiner.CombinedMeshContains(go);
  227. }
  228. /// <summary>
  229. /// Updates the data in the combined mesh for meshes that are already in the combined mesh.
  230. /// This is faster than adding and removing a mesh and has a much lower memory footprint.
  231. /// This method can only be used if the meshes being updated have the same layout(number of
  232. /// vertices, triangles, submeshes).
  233. /// This is faster than removing and re-adding
  234. /// For efficiency update as few channels as possible.
  235. /// Apply must be called to apply the changes to the combined mesh
  236. /// </summary>
  237. public virtual void UpdateGameObjects(GameObject[] gos)
  238. {
  239. meshCombiner.name = name + "-mesh";
  240. meshCombiner.UpdateGameObjects(gos, true, true, true, true, true,
  241. false,false,false,false,false,false,false,false,false);
  242. }
  243. /// <summary>
  244. /// Updates the data in the combined mesh for meshes that are already in the combined mesh.
  245. /// This is faster than adding and removing a mesh and has a much lower memory footprint.
  246. /// This method can only be used if the meshes being updated have the same layout(number of
  247. /// vertices, triangles, submeshes).
  248. /// This is faster than removing and re-adding
  249. /// For efficiency update as few channels as possible.
  250. /// Apply must be called to apply the changes to the combined mesh
  251. /// </summary>
  252. public virtual void UpdateGameObjects(GameObject[] gos, bool updateBounds)
  253. {
  254. meshCombiner.name = name + "-mesh";
  255. meshCombiner.UpdateGameObjects(gos, true, true, true, true, true,
  256. false, false, false, false, false, false, false, false, false);
  257. }
  258. /// <summary>
  259. /// Updates the data in the combined mesh for meshes that are already in the combined mesh.
  260. /// This is faster than adding and removing a mesh and has a much lower memory footprint.
  261. /// This method can only be used if the meshes being updated have the same layout(number of
  262. /// vertices, triangles, submeshes).
  263. /// This is faster than removing and re-adding
  264. /// For efficiency update as few channels as possible.
  265. /// Apply must be called to apply the changes to the combined mesh
  266. /// </summary>
  267. public virtual void UpdateGameObjects(GameObject[] gos, bool recalcBounds, bool updateVertices, bool updateNormals, bool updateTangents,
  268. bool updateUV, bool updateUV1, bool updateUV2,
  269. bool updateColors, bool updateSkinningInfo){
  270. meshCombiner.name = name + "-mesh";
  271. meshCombiner.UpdateGameObjects(gos,recalcBounds, updateVertices, updateNormals, updateTangents, updateUV, updateUV2, false, false, updateColors, updateSkinningInfo);
  272. }
  273. /// <summary>
  274. /// Updates the data in the combined mesh for meshes that are already in the combined mesh.
  275. /// This is faster than adding and removing a mesh and has a much lower memory footprint.
  276. /// This method can only be used if the meshes being updated have the same layout(number of
  277. /// vertices, triangles, submeshes).
  278. /// This is faster than removing and re-adding
  279. /// For efficiency update as few channels as possible.
  280. /// Apply must be called to apply the changes to the combined mesh
  281. /// </summary>
  282. public virtual bool UpdateGameObjects(GameObject[] gos, bool recalcBounds,
  283. bool updateVertices, bool updateNormals, bool updateTangents,
  284. bool updateUV, bool updateUV2, bool updateUV3, bool updateUV4,
  285. bool updateUV5, bool updateUV6, bool updateUV7, bool updateUV8,
  286. bool updateColors, bool updateSkinningInfo)
  287. {
  288. meshCombiner.name = name + "-mesh";
  289. return meshCombiner.UpdateGameObjects(gos, recalcBounds, updateVertices, updateNormals, updateTangents, updateUV, updateUV2, updateUV3, updateUV4, updateUV5, updateUV6, updateUV7, updateUV8, updateColors, updateSkinningInfo);
  290. }
  291. public virtual void UpdateSkinnedMeshApproximateBounds(){
  292. if (_ValidateForUpdateSkinnedMeshBounds()){
  293. meshCombiner.UpdateSkinnedMeshApproximateBounds();
  294. }
  295. }
  296. public virtual void UpdateSkinnedMeshApproximateBoundsFromBones(){
  297. if (_ValidateForUpdateSkinnedMeshBounds()){
  298. meshCombiner.UpdateSkinnedMeshApproximateBoundsFromBones();
  299. }
  300. }
  301. public virtual void UpdateSkinnedMeshApproximateBoundsFromBounds(){
  302. if (_ValidateForUpdateSkinnedMeshBounds()){
  303. meshCombiner.UpdateSkinnedMeshApproximateBoundsFromBounds();
  304. }
  305. }
  306. protected virtual bool _ValidateForUpdateSkinnedMeshBounds(){
  307. if (meshCombiner.outputOption == MB2_OutputOptions.bakeMeshAssetsInPlace){
  308. Debug.LogWarning("Can't UpdateSkinnedMeshApproximateBounds when output type is bakeMeshAssetsInPlace");
  309. return false;
  310. }
  311. if (meshCombiner.resultSceneObject == null){
  312. Debug.LogWarning("Result Scene Object does not exist. No point in calling UpdateSkinnedMeshApproximateBounds.");
  313. return false;
  314. }
  315. SkinnedMeshRenderer smr = meshCombiner.resultSceneObject.GetComponentInChildren<SkinnedMeshRenderer>();
  316. if (smr == null){
  317. Debug.LogWarning("No SkinnedMeshRenderer on result scene object.");
  318. return false;
  319. }
  320. return true;
  321. }
  322. }