VoxelScriptedImporter.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #if UNITY_2017_1_OR_NEWER
  2. using UnityEngine;
  3. using UnityEngine.Assertions;
  4. using UnityEditor;
  5. using UnityEditor.Experimental.AssetImporters;
  6. using System;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Collections.Generic;
  10. namespace VoxelImporter
  11. {
  12. [ScriptedImporter(1, new string[] { "vox", "qb" })]
  13. public class VoxelScriptedImporter : ScriptedImporter
  14. {
  15. public VoxelBase.FileType fileType;
  16. public bool legacyVoxImport;
  17. public VoxelBase.ImportMode importMode = VoxelBase.ImportMode.LowPoly;
  18. public Vector3 importScale = Vector3.one;
  19. public Vector3 importOffset;
  20. public bool combineFaces = true;
  21. public bool ignoreCavity = true;
  22. public bool outputStructure;
  23. public bool generateLightmapUVs;
  24. public float generateLightmapUVsAngleError = 8f;
  25. public float generateLightmapUVsAreaError = 15f;
  26. public float generateLightmapUVsHardAngle = 88f;
  27. public float generateLightmapUVsPackMargin = 4f;
  28. public bool generateTangents;
  29. public float meshFaceVertexOffset;
  30. public bool retainExisting = true;
  31. public bool loadFromVoxelFile = true;
  32. public bool generateMipMaps;
  33. public enum ColliderType
  34. {
  35. None,
  36. Box,
  37. Sphere,
  38. Capsule,
  39. Mesh,
  40. }
  41. public ColliderType colliderType;
  42. public override void OnImportAsset(AssetImportContext ctx)
  43. {
  44. {
  45. var ext = Path.GetExtension(ctx.assetPath).ToLower();
  46. if (ext == ".vox") fileType = VoxelBase.FileType.vox;
  47. else if (ext == ".qb") fileType = VoxelBase.FileType.qb;
  48. else return;
  49. }
  50. var gameObject = new GameObject(Path.GetFileNameWithoutExtension(ctx.assetPath));
  51. var voxelObject = gameObject.AddComponent<VoxelObject>();
  52. {
  53. voxelObject.legacyVoxImport = legacyVoxImport;
  54. voxelObject.importMode = importMode;
  55. voxelObject.importScale = importScale;
  56. voxelObject.importOffset = importOffset;
  57. voxelObject.combineFaces = combineFaces;
  58. voxelObject.ignoreCavity = ignoreCavity;
  59. voxelObject.voxelStructure = outputStructure ? ScriptableObject.CreateInstance<VoxelStructure>() : null;
  60. voxelObject.generateLightmapUVs = generateLightmapUVs;
  61. voxelObject.generateLightmapUVsAngleError = generateLightmapUVsAngleError;
  62. voxelObject.generateLightmapUVsAreaError = generateLightmapUVsAreaError;
  63. voxelObject.generateLightmapUVsHardAngle = generateLightmapUVsHardAngle;
  64. voxelObject.generateLightmapUVsPackMargin = generateLightmapUVsPackMargin;
  65. voxelObject.generateTangents = generateTangents;
  66. voxelObject.meshFaceVertexOffset = meshFaceVertexOffset;
  67. voxelObject.loadFromVoxelFile = loadFromVoxelFile;
  68. voxelObject.generateMipMaps = generateMipMaps;
  69. }
  70. var objectCore = new VoxelObjectCore(voxelObject);
  71. try
  72. {
  73. if (!objectCore.Create(ctx.assetPath, null))
  74. {
  75. Debug.LogErrorFormat("<color=green>[Voxel Importer]</color> ScriptedImporter error. file:{0}", ctx.assetPath);
  76. DestroyImmediate(gameObject);
  77. return;
  78. }
  79. }
  80. catch
  81. {
  82. Debug.LogErrorFormat("<color=green>[Voxel Importer]</color> ScriptedImporter error. file:{0}", ctx.assetPath);
  83. DestroyImmediate(gameObject);
  84. return;
  85. }
  86. #region Material
  87. if (retainExisting)
  88. {
  89. bool changed = false;
  90. var assets = AssetDatabase.LoadAllAssetsAtPath(ctx.assetPath);
  91. for (int i = 0; i < voxelObject.materials.Count; i++)
  92. {
  93. var material = assets.FirstOrDefault(c => c.name == string.Format("mat{0}", i)) as Material;
  94. if (material != null)
  95. {
  96. material.mainTexture = voxelObject.atlasTexture;
  97. voxelObject.materials[i] = material;
  98. changed = true;
  99. }
  100. }
  101. if (changed)
  102. {
  103. var renderer = gameObject.GetComponent<MeshRenderer>();
  104. renderer.sharedMaterials = voxelObject.materials.ToArray();
  105. }
  106. }
  107. #endregion
  108. #region Collider
  109. switch (colliderType)
  110. {
  111. case ColliderType.Box:
  112. gameObject.AddComponent<BoxCollider>();
  113. break;
  114. case ColliderType.Sphere:
  115. gameObject.AddComponent<SphereCollider>();
  116. break;
  117. case ColliderType.Capsule:
  118. gameObject.AddComponent<CapsuleCollider>();
  119. break;
  120. case ColliderType.Mesh:
  121. gameObject.AddComponent<MeshCollider>();
  122. break;
  123. }
  124. #endregion
  125. #if UNITY_2017_3_OR_NEWER
  126. ctx.AddObjectToAsset(gameObject.name, gameObject);
  127. ctx.AddObjectToAsset(voxelObject.mesh.name = "mesh", voxelObject.mesh);
  128. for (int i = 0; i < voxelObject.materials.Count; i++)
  129. {
  130. ctx.AddObjectToAsset(voxelObject.materials[i].name = string.Format("mat{0}", i), voxelObject.materials[i]);
  131. }
  132. ctx.AddObjectToAsset(voxelObject.atlasTexture.name = "tex", voxelObject.atlasTexture);
  133. if (voxelObject.voxelStructure != null)
  134. {
  135. ctx.AddObjectToAsset(voxelObject.voxelStructure.name = "structure", voxelObject.voxelStructure);
  136. }
  137. VoxelObject.DestroyImmediate(voxelObject);
  138. ctx.SetMainObject(gameObject);
  139. #else
  140. ctx.SetMainAsset(gameObject.name, gameObject);
  141. ctx.AddSubAsset(voxelObject.mesh.name = "mesh", voxelObject.mesh);
  142. for (int i = 0; i < voxelObject.materials.Count; i++)
  143. {
  144. ctx.AddSubAsset(voxelObject.materials[i].name = string.Format("mat{0}", i), voxelObject.materials[i]);
  145. }
  146. ctx.AddSubAsset(voxelObject.atlasTexture.name = "tex", voxelObject.atlasTexture);
  147. if (voxelObject.voxelStructure != null)
  148. {
  149. ctx.AddSubAsset(voxelObject.voxelStructure.name = "structure", voxelObject.voxelStructure);
  150. }
  151. VoxelObject.DestroyImmediate(voxelObject);
  152. #endif
  153. }
  154. }
  155. }
  156. #endif