VoxelChunksObject.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. namespace VoxelImporter
  7. {
  8. [AddComponentMenu("Voxel Importer/Voxel Chunks Object")]
  9. public class VoxelChunksObject : VoxelBase
  10. {
  11. public enum MaterialMode
  12. {
  13. Combine,
  14. Individual,
  15. }
  16. #if !UNITY_EDITOR
  17. void Awake()
  18. {
  19. Destroy(this);
  20. }
  21. #else
  22. public override bool EditorInitialize()
  23. {
  24. var result = base.EditorInitialize();
  25. //ver1.021 -> ver1.0.3
  26. if (material != null)
  27. {
  28. materials = new List<Material>();
  29. materials.Add(material);
  30. materialData = new List<MaterialData>();
  31. materialData.Add(new MaterialData());
  32. materialIndexes = new List<int>();
  33. materialIndexes.Add(0);
  34. material = null;
  35. result = true;
  36. }
  37. //ver1.0.4 new
  38. UpdateChunks();
  39. return result;
  40. }
  41. public VoxelChunksObjectChunk[] chunks; //ver1.0.4 new
  42. [SerializeField]
  43. protected Material material; //ver1.021 old
  44. public List<Material> materials; //ver1.0.3 new
  45. public Texture2D atlasTexture;
  46. public enum SplitMode
  47. {
  48. ChunkSize,
  49. QubicleMatrix = 100,
  50. WorldEditor = 100,
  51. }
  52. public SplitMode splitMode = SplitMode.QubicleMatrix;
  53. public IntVector3 chunkSize = new IntVector3(16, 16, 16);
  54. public bool createContactChunkFaces;
  55. public MaterialMode materialMode;
  56. public void UpdateChunks()
  57. {
  58. List<VoxelChunksObjectChunk> list = new List<VoxelChunksObjectChunk>();
  59. var all = Resources.FindObjectsOfTypeAll<VoxelChunksObjectChunk>();
  60. var transformCache = transform;
  61. for (int i = 0; i < all.Length; i++)
  62. {
  63. if (transformCache == all[i].transform.parent)
  64. list.Add(all[i]);
  65. }
  66. chunks = list.ToArray();
  67. if (updatedChunks != null)
  68. updatedChunks.Invoke();
  69. }
  70. public delegate void UpdatedChunks();
  71. public UpdatedChunks updatedChunks;
  72. #region Editor
  73. public Vector3 edit_importScale;
  74. public Vector3 edit_importOffset;
  75. public IntVector3 edit_chunkSize;
  76. public bool IsEditorChanged()
  77. {
  78. return edit_importScale != importScale ||
  79. edit_importOffset != importOffset ||
  80. edit_chunkSize != chunkSize;
  81. }
  82. public void RevertEditorParam()
  83. {
  84. edit_importScale = importScale;
  85. edit_importOffset = importOffset;
  86. edit_chunkSize = chunkSize;
  87. }
  88. public void ApplyEditorParam()
  89. {
  90. importScale = edit_importScale;
  91. importOffset = edit_importOffset;
  92. chunkSize = edit_chunkSize;
  93. }
  94. public override void SaveEditTmpData()
  95. {
  96. base.SaveEditTmpData();
  97. RevertEditorParam();
  98. }
  99. #endregion
  100. #region Asset
  101. public override bool IsUseAssetObject(UnityEngine.Object obj)
  102. {
  103. if (materialMode == VoxelChunksObject.MaterialMode.Combine)
  104. {
  105. if (materials != null)
  106. {
  107. for (int j = 0; j < materials.Count; j++)
  108. {
  109. if (materials[j] == obj) return true;
  110. }
  111. }
  112. if (atlasTexture == obj) return true;
  113. }
  114. if (chunks != null)
  115. {
  116. for (int i = 0; i < chunks.Length; i++)
  117. {
  118. var c = chunks[i];
  119. if (c == null) continue;
  120. if (c.mesh == obj) return true;
  121. if (materialMode == VoxelChunksObject.MaterialMode.Individual)
  122. {
  123. if (c.materials != null)
  124. {
  125. for (int j = 0; j < c.materials.Count; j++)
  126. {
  127. if (c.materials[j] == obj) return true;
  128. }
  129. }
  130. if (c.atlasTexture == obj) return true;
  131. }
  132. }
  133. }
  134. return false;
  135. }
  136. #endregion
  137. #region Undo
  138. public class RefreshCheckerChunk : RefreshChecker
  139. {
  140. public RefreshCheckerChunk(VoxelChunksObject voxelObject) : base(voxelObject)
  141. {
  142. controllerChunk = voxelObject;
  143. }
  144. public VoxelChunksObject controllerChunk;
  145. public VoxelChunksObject.SplitMode splitMode;
  146. public IntVector3 chunkSize;
  147. public bool createContactChunkFaces;
  148. public VoxelChunksObject.MaterialMode materialMode;
  149. public override void Save()
  150. {
  151. base.Save();
  152. splitMode = controllerChunk.splitMode;
  153. chunkSize = controllerChunk.chunkSize;
  154. createContactChunkFaces = controllerChunk.createContactChunkFaces;
  155. materialMode = controllerChunk.materialMode;
  156. }
  157. public override bool Check()
  158. {
  159. if (base.Check())
  160. return true;
  161. return splitMode != controllerChunk.splitMode ||
  162. chunkSize != controllerChunk.chunkSize ||
  163. createContactChunkFaces != controllerChunk.createContactChunkFaces ||
  164. materialMode != controllerChunk.materialMode;
  165. }
  166. }
  167. #endregion
  168. #endif
  169. }
  170. }