VoxelChunksObjectChunkExplosion.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace VoxelImporter
  6. {
  7. [ExecuteInEditMode, RequireComponent(typeof(VoxelChunksObjectChunk))]
  8. public class VoxelChunksObjectChunkExplosion : MonoBehaviour
  9. {
  10. protected VoxelChunksObjectExplosion explosionObject { get; private set; }
  11. protected Transform transformCache { get; private set; }
  12. protected Transform parentCache { get; private set; }
  13. protected Renderer rendererCache { get; private set; }
  14. public List<VoxelBaseExplosion.MeshData> meshes;
  15. public List<Material> materials;
  16. public Vector3 chunkBasicOffset;
  17. void Awake()
  18. {
  19. if (transform.parent == null) return;
  20. explosionObject = transform.parent.GetComponent<VoxelChunksObjectExplosion>();
  21. if (explosionObject == null) return;
  22. transformCache = transform;
  23. parentCache = explosionObject.transform;
  24. rendererCache = GetComponent<Renderer>();
  25. explosionObject.SetEnableExplosionObject(false);
  26. }
  27. public void DrawMesh()
  28. {
  29. if (explosionObject == null || meshes == null) return;
  30. if (explosionObject.materialMode == VoxelChunksObject.MaterialMode.Combine && explosionObject.materials != null)
  31. {
  32. var world = parentCache.localToWorldMatrix;
  33. for (int i = 0; i < meshes.Count; i++)
  34. {
  35. if (meshes[i].mesh == null) continue;
  36. var local = Matrix4x4.TRS(transformCache.localPosition, transformCache.localRotation, transformCache.localScale);
  37. var basic = Matrix4x4.TRS(chunkBasicOffset, Quaternion.identity, Vector3.one);
  38. var offset = local * basic.inverse;
  39. for (int j = 0; j < meshes[i].materialIndexes.Count; j++)
  40. {
  41. var matIndex = meshes[i].materialIndexes[j];
  42. if (matIndex < explosionObject.materials.Count)
  43. {
  44. if (j < meshes[i].mesh.subMeshCount)
  45. Graphics.DrawMesh(meshes[i].mesh, world * offset, explosionObject.materials[matIndex], 0, null, j, explosionObject.materialPropertyBlock);
  46. }
  47. }
  48. }
  49. }
  50. else if (explosionObject.materialMode == VoxelChunksObject.MaterialMode.Individual && materials != null)
  51. {
  52. var world = transformCache.localToWorldMatrix;
  53. for (int i = 0; i < meshes.Count; i++)
  54. {
  55. if (meshes[i].mesh == null) continue;
  56. var basic = Matrix4x4.TRS(chunkBasicOffset, Quaternion.identity, Vector3.one);
  57. for (int j = 0; j < meshes[i].materialIndexes.Count; j++)
  58. {
  59. var matIndex = meshes[i].materialIndexes[j];
  60. if (matIndex < materials.Count)
  61. {
  62. if (j < meshes[i].mesh.subMeshCount)
  63. Graphics.DrawMesh(meshes[i].mesh, world * basic.inverse, materials[matIndex], 0, null, j, explosionObject.materialPropertyBlock);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. public void SetEnableRenderer(bool enable)
  70. {
  71. if (rendererCache != null)
  72. {
  73. if (rendererCache != null && rendererCache.enabled != enable)
  74. rendererCache.enabled = enable;
  75. }
  76. }
  77. #region Editor
  78. public bool edit_objectFoldout = true;
  79. #endregion
  80. }
  81. }