MaterialData.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. #if UNITY_EDITOR
  7. namespace VoxelImporter
  8. {
  9. [Serializable]
  10. public class MaterialData : ISerializationCallbackReceiver
  11. {
  12. public void OnBeforeSerialize()
  13. {
  14. }
  15. public void OnAfterDeserialize()
  16. {
  17. IntVector3 max = IntVector3.zero;
  18. for (int i = 0; i < materialList.Count; i++)
  19. {
  20. max = IntVector3.Max(max, materialList[i]);
  21. }
  22. indexTable = new DataTable3<int>(max.x + 1, max.y + 1, max.z + 1);
  23. for (int i = 0; i < materialList.Count; i++)
  24. {
  25. indexTable.Set(materialList[i], i);
  26. }
  27. }
  28. public MaterialData Clone()
  29. {
  30. MaterialData clone = new MaterialData();
  31. clone.materialList = new List<IntVector3>(materialList);
  32. clone.name = name;
  33. clone.transparent = transparent;
  34. clone.OnAfterDeserialize();
  35. return clone;
  36. }
  37. public bool IsEqual(MaterialData src)
  38. {
  39. if(materialList != null && src.materialList != null)
  40. {
  41. if (materialList.Count != src.materialList.Count) return false;
  42. for (int i = 0; i < materialList.Count; i++)
  43. {
  44. if (materialList[i] != src.materialList[i]) return false;
  45. }
  46. }
  47. else if(materialList != null && src.materialList == null)
  48. {
  49. return false;
  50. }
  51. else if (materialList == null && src.materialList != null)
  52. {
  53. return false;
  54. }
  55. if (name != src.name) return false;
  56. if (transparent != src.transparent) return false;
  57. return true;
  58. }
  59. public void SetMaterial(IntVector3 pos)
  60. {
  61. if (!indexTable.Contains(pos))
  62. {
  63. indexTable.Set(pos, materialList.Count);
  64. materialList.Add(pos);
  65. }
  66. else
  67. {
  68. var index = indexTable.Get(pos);
  69. Assert.IsTrue(materialList[index] == pos);
  70. }
  71. }
  72. public void RemoveMaterial(IntVector3 pos)
  73. {
  74. if (indexTable.Contains(pos))
  75. {
  76. var index = indexTable.Get(pos);
  77. if (index < materialList.Count - 1)
  78. {
  79. materialList[index] = materialList[materialList.Count - 1];
  80. indexTable.Set(materialList[materialList.Count - 1], index);
  81. materialList.RemoveAt(materialList.Count - 1);
  82. }
  83. else
  84. {
  85. materialList.RemoveAt(index);
  86. }
  87. indexTable.Remove(pos);
  88. }
  89. }
  90. public bool GetMaterial(IntVector3 pos)
  91. {
  92. return indexTable.Contains(pos);
  93. }
  94. public void ClearMaterial()
  95. {
  96. indexTable.Clear();
  97. materialList.Clear();
  98. }
  99. public void AllAction(Action<IntVector3> action)
  100. {
  101. indexTable.AllAction((x, y, z, index) =>
  102. {
  103. action(new IntVector3(x, y, z));
  104. });
  105. }
  106. private DataTable3<int> indexTable = new DataTable3<int>();
  107. [SerializeField]
  108. private List<IntVector3> materialList = new List<IntVector3>();
  109. public string name;
  110. public bool transparent;
  111. #region FromVoxMaterial
  112. [Serializable]
  113. public struct Material
  114. {
  115. public enum StandardShaderRenderingMode
  116. {
  117. Opaque,
  118. Cutout,
  119. Fade,
  120. Transparent,
  121. }
  122. public StandardShaderRenderingMode renderingMode;
  123. public float metallic;
  124. public float smoothness;
  125. public float alpha;
  126. public Color emission;
  127. public float emissionPower;
  128. }
  129. public Material material;
  130. #endregion
  131. }
  132. }
  133. #endif