VoxelSkinnedAnimationObjectExplosion.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace VoxelImporter
  6. {
  7. [AddComponentMenu("Voxel Importer/Extra/Explosion/Voxel Skinned Animation Object Explosion")]
  8. [ExecuteInEditMode, RequireComponent(typeof(VoxelSkinnedAnimationObject))]
  9. public class VoxelSkinnedAnimationObjectExplosion : VoxelBaseExplosion
  10. {
  11. protected VoxelSkinnedAnimationObject voxelObject { get; private set; }
  12. protected SkinnedMeshRenderer skinnedMeshRendererCache { get; private set; }
  13. [Serializable]
  14. public class SkinnedAnimationMeshData : MeshData
  15. {
  16. public Mesh bakeMesh;
  17. }
  18. public List<SkinnedAnimationMeshData> meshes;
  19. public List<Material> materials;
  20. protected override void Awake()
  21. {
  22. base.Awake();
  23. voxelObject = GetComponent<VoxelSkinnedAnimationObject>();
  24. skinnedMeshRendererCache = GetComponent<SkinnedMeshRenderer>();
  25. }
  26. protected override void OnDestroy()
  27. {
  28. base.OnDestroy();
  29. }
  30. public void BakeExplosionPlay(float lifeTime, Action doneAction = null)
  31. {
  32. BakeMesh();
  33. base.ExplosionPlay(lifeTime, doneAction);
  34. }
  35. public override void DrawMesh()
  36. {
  37. if (materials != null && meshes != null)
  38. {
  39. var world = transformCache.localToWorldMatrix;
  40. for (int i = 0; i < meshes.Count; i++)
  41. {
  42. for (int j = 0; j < meshes[i].materialIndexes.Count; j++)
  43. {
  44. if (meshes[i].bakeMesh != null)
  45. {
  46. if (j < meshes[i].bakeMesh.subMeshCount)
  47. Graphics.DrawMesh(meshes[i].bakeMesh, world, materials[meshes[i].materialIndexes[j]], 0, null, j, materialPropertyBlock);
  48. }
  49. else
  50. {
  51. if (j < meshes[i].mesh.subMeshCount)
  52. Graphics.DrawMesh(meshes[i].mesh, world, materials[meshes[i].materialIndexes[j]], 0, null, j, materialPropertyBlock);
  53. }
  54. }
  55. }
  56. }
  57. }
  58. public void BakeMesh()
  59. {
  60. if (meshes == null || skinnedMeshRendererCache == null) return;
  61. var saveMesh = skinnedMeshRendererCache.sharedMesh;
  62. var localPosition = transformCache.localPosition;
  63. var localRotation = transformCache.localRotation;
  64. var localScale = transformCache.localScale;
  65. transformCache.localScale = Vector3.one;
  66. for (int i = 0; i < meshes.Count; i++)
  67. {
  68. if (meshes[i].mesh != null)
  69. {
  70. skinnedMeshRendererCache.sharedMesh = meshes[i].mesh;
  71. if (meshes[i].bakeMesh == null)
  72. {
  73. meshes[i].bakeMesh = new Mesh();
  74. }
  75. else
  76. {
  77. #if UNITY_EDITOR
  78. if (!UnityEditor.EditorApplication.isPlaying)
  79. {
  80. meshes[i].bakeMesh.Clear(false);
  81. meshes[i].bakeMesh.ClearBlendShapes();
  82. }
  83. else
  84. {
  85. meshes[i].bakeMesh = new Mesh();
  86. }
  87. #else
  88. meshes[i].bakeMesh = new Mesh();
  89. #endif
  90. }
  91. skinnedMeshRendererCache.BakeMesh(meshes[i].bakeMesh);
  92. {
  93. var bounds = meshes[i].bakeMesh.bounds;
  94. bounds.size = meshes[i].mesh.bounds.size;
  95. meshes[i].bakeMesh.bounds = bounds;
  96. }
  97. }
  98. else
  99. {
  100. meshes[i].bakeMesh = null;
  101. }
  102. }
  103. skinnedMeshRendererCache.sharedMesh = saveMesh;
  104. transformCache.localPosition = localPosition;
  105. transformCache.localRotation = localRotation;
  106. transformCache.localScale = localScale;
  107. }
  108. #if UNITY_EDITOR
  109. public bool edit_bakeFoldout = true;
  110. #region Asset
  111. public override bool IsUseAssetObject(UnityEngine.Object obj)
  112. {
  113. if (meshes != null)
  114. {
  115. for (int i = 0; i < meshes.Count; i++)
  116. {
  117. if (meshes[i] == null) continue;
  118. if (meshes[i].mesh == obj) return true;
  119. if (meshes[i].bakeMesh == obj) return true;
  120. }
  121. }
  122. if (materials != null)
  123. {
  124. for (int i = 0; i < materials.Count; i++)
  125. {
  126. if (materials[i] == obj) return true;
  127. }
  128. }
  129. return false;
  130. }
  131. #endregion
  132. #endif
  133. }
  134. }