源战役客户端
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.

213 lines
7.5 KiB

пре 1 месец
  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. using LuaFramework;
  13. /// <summary>
  14. /// Maps a list of source materials to a combined material. Included in MB2_TextureBakeResults
  15. /// </summary>
  16. /// <summary>
  17. /// Abstract root of the mesh combining classes
  18. /// </summary>
  19. public abstract class MB3_MeshBakerCommon : MB3_MeshBakerRoot {
  20. //todo should be list of <Renderer>
  21. public List<GameObject> objsToMesh;
  22. public abstract MB3_MeshCombiner meshCombiner{
  23. get;
  24. }
  25. public bool useObjsToMeshFromTexBaker = true;
  26. public bool clearBuffersAfterBake = true;
  27. //todo put this in the batch baker
  28. public string bakeAssetsInPlaceFolderPath;
  29. [HideInInspector] public GameObject resultPrefab;
  30. public override MB2_TextureBakeResults textureBakeResults{
  31. get {return meshCombiner.textureBakeResults; }
  32. set {meshCombiner.textureBakeResults = value; }
  33. }
  34. public override List<GameObject> GetObjectsToCombine(){
  35. if (useObjsToMeshFromTexBaker){
  36. MB3_TextureBaker tb = gameObject.GetComponent<MB3_TextureBaker>();
  37. if (tb == null) tb = gameObject.transform.parent.GetComponent<MB3_TextureBaker>();
  38. if (tb != null) {
  39. return tb.GetObjectsToCombine();
  40. } else {
  41. LogManager.LogWarning("Use Objects To Mesh From Texture Baker was checked but no texture baker");
  42. return new List<GameObject>();
  43. }
  44. } else {
  45. if (objsToMesh == null) objsToMesh = new List<GameObject>();
  46. return objsToMesh;
  47. }
  48. }
  49. public void EnableDisableSourceObjectRenderers(bool show){
  50. for (int i = 0; i < GetObjectsToCombine().Count; i++){
  51. GameObject go = GetObjectsToCombine()[i];
  52. if (go != null){
  53. Renderer mr = MB_Utility.GetRenderer(go);
  54. if (mr != null){
  55. mr.enabled = show;
  56. }
  57. }
  58. }
  59. }
  60. /// <summary>
  61. /// Clears the meshs and mesh related data but does not destroy it.
  62. /// </summary>
  63. public virtual void ClearMesh(){
  64. meshCombiner.ClearMesh();
  65. }
  66. /// <summary>
  67. /// Clears and desroys the mesh. Clears mesh related data.
  68. /// </summary>
  69. public virtual void DestroyMesh(){
  70. meshCombiner.DestroyMesh();
  71. }
  72. public virtual void DestroyMeshEditor(MB2_EditorMethodsInterface editorMethods){
  73. meshCombiner.DestroyMeshEditor(editorMethods);
  74. }
  75. public virtual int GetNumObjectsInCombined(){
  76. return meshCombiner.GetNumObjectsInCombined();
  77. }
  78. public virtual int GetNumVerticesFor(GameObject go){
  79. return meshCombiner.GetNumVerticesFor(go);
  80. }
  81. /// <summary>
  82. /// Gets the texture baker on this component or its parent if it exists
  83. /// </summary>
  84. /// <returns>The texture baker.</returns>
  85. public MB3_TextureBaker GetTextureBaker(){
  86. MB3_TextureBaker tb = GetComponent<MB3_TextureBaker>();
  87. if (tb != null) return tb;
  88. if (transform.parent != null) return transform.parent.GetComponent<MB3_TextureBaker>();
  89. return null;
  90. }
  91. /// <summary>
  92. /// Adds and deletes objects from the combined mesh. gos and deleteGOs can be null.
  93. /// You need to call Apply or ApplyAll to see the changes.
  94. /// objects in gos must not include objects already in the combined mesh.
  95. /// objects in gos and deleteGOs must be the game objects with a Renderer component
  96. /// This method is slow, so should be called as infrequently as possible.
  97. /// </summary>
  98. /// <returns>
  99. /// The first generated combined mesh
  100. /// </returns>
  101. /// <param name='gos'>
  102. /// gos. Array of objects to add to the combined mesh. Array can be null. Must not include objects
  103. /// already in the combined mesh. Array must contain game objects with a render component.
  104. /// </param>
  105. /// <param name='deleteGOs'>
  106. /// deleteGOs. Array of objects to delete from the combined mesh. Array can be null.
  107. /// </param>
  108. /// <param name='disableRendererInSource'>
  109. /// Disable renderer component on objects in gos after they have been added to the combined mesh.
  110. /// </param>
  111. /// <param name='fixOutOfBoundUVs'>
  112. /// 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.
  113. /// </param>
  114. /// </summary>
  115. public abstract bool AddDeleteGameObjects(GameObject[] gos, GameObject[] deleteGOs, bool disableRendererInSource = true);
  116. /// <summary>
  117. /// This is the best version to use for deleting game objects since the source GameObjects may have been destroyed
  118. /// Internaly Mesh Baker only stores the instanceID for Game Objects, so objects can be removed after they have been destroyed
  119. /// </summary>
  120. public abstract bool AddDeleteGameObjectsByID(GameObject[] gos, int[] deleteGOinstanceIDs, bool disableRendererInSource = true);
  121. /// <summary>
  122. /// Apply changes to the mesh. All channels set in this instance will be set in the combined mesh.
  123. /// </summary>
  124. public virtual void Apply(MB3_MeshCombiner.GenerateUV2Delegate uv2GenerationMethod=null){
  125. meshCombiner.name = name + "-mesh";
  126. meshCombiner.Apply(uv2GenerationMethod);
  127. }
  128. /// <summary>
  129. /// 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.
  130. /// </summary>
  131. public virtual void Apply(bool triangles,
  132. bool vertices,
  133. bool normals,
  134. bool tangents,
  135. bool uvs,
  136. bool uv2,
  137. bool uv3,
  138. bool uv4,
  139. bool colors,
  140. bool bones=false,
  141. MB3_MeshCombiner.GenerateUV2Delegate uv2GenerationMethod=null){
  142. meshCombiner.name = name + "-mesh";
  143. meshCombiner.Apply(triangles,vertices,normals,tangents,uvs,uv2,uv3,uv4,colors,bones,uv2GenerationMethod);
  144. }
  145. public virtual bool CombinedMeshContains(GameObject go){
  146. return meshCombiner.CombinedMeshContains(go);
  147. }
  148. public virtual void UpdateGameObjects(GameObject[] gos, bool recalcBounds = true, bool updateVertices = true, bool updateNormals = true, bool updateTangents = true,
  149. bool updateUV = false, bool updateUV1 = false, bool updateUV2 = false,
  150. bool updateColors = false, bool updateSkinningInfo = false){
  151. meshCombiner.name = name + "-mesh";
  152. meshCombiner.UpdateGameObjects(gos,recalcBounds, updateVertices, updateNormals, updateTangents, updateUV, updateUV1, updateUV2, updateColors, updateSkinningInfo);
  153. }
  154. public virtual void UpdateSkinnedMeshApproximateBounds(){
  155. if (_ValidateForUpdateSkinnedMeshBounds()){
  156. meshCombiner.UpdateSkinnedMeshApproximateBounds();
  157. }
  158. }
  159. public virtual void UpdateSkinnedMeshApproximateBoundsFromBones(){
  160. if (_ValidateForUpdateSkinnedMeshBounds()){
  161. meshCombiner.UpdateSkinnedMeshApproximateBoundsFromBones();
  162. }
  163. }
  164. public virtual void UpdateSkinnedMeshApproximateBoundsFromBounds(){
  165. if (_ValidateForUpdateSkinnedMeshBounds()){
  166. meshCombiner.UpdateSkinnedMeshApproximateBoundsFromBounds();
  167. }
  168. }
  169. protected virtual bool _ValidateForUpdateSkinnedMeshBounds(){
  170. if (meshCombiner.outputOption == MB2_OutputOptions.bakeMeshAssetsInPlace){
  171. LogManager.LogWarning("Can't UpdateSkinnedMeshApproximateBounds when output type is bakeMeshAssetsInPlace");
  172. return false;
  173. }
  174. if (meshCombiner.resultSceneObject == null){
  175. LogManager.LogWarning("Result Scene Object does not exist. No point in calling UpdateSkinnedMeshApproximateBounds.");
  176. return false;
  177. }
  178. SkinnedMeshRenderer smr = meshCombiner.resultSceneObject.GetComponentInChildren<SkinnedMeshRenderer>();
  179. if (smr == null){
  180. LogManager.LogWarning("No SkinnedMeshRenderer on result scene object.");
  181. return false;
  182. }
  183. return true;
  184. }
  185. }