surface_tool.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**************************************************************************/
  2. /* surface_tool.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef SURFACE_TOOL_H
  31. #define SURFACE_TOOL_H
  32. #include "core/templates/local_vector.h"
  33. #include "scene/resources/mesh.h"
  34. #include "thirdparty/misc/mikktspace.h"
  35. class SurfaceTool : public RefCounted {
  36. GDCLASS(SurfaceTool, RefCounted);
  37. static const uint32_t custom_mask[RS::ARRAY_CUSTOM_COUNT];
  38. static const uint32_t custom_shift[RS::ARRAY_CUSTOM_COUNT];
  39. public:
  40. struct Vertex {
  41. Vector3 vertex;
  42. Color color;
  43. Vector3 normal; // normal, binormal, tangent
  44. Vector3 binormal;
  45. Vector3 tangent;
  46. Vector2 uv;
  47. Vector2 uv2;
  48. Vector<int> bones;
  49. Vector<float> weights;
  50. Color custom[RS::ARRAY_CUSTOM_COUNT];
  51. uint32_t smooth_group = 0;
  52. bool operator==(const Vertex &p_vertex) const;
  53. Vertex() {}
  54. };
  55. enum CustomFormat {
  56. CUSTOM_RGBA8_UNORM = RS::ARRAY_CUSTOM_RGBA8_UNORM,
  57. CUSTOM_RGBA8_SNORM = RS::ARRAY_CUSTOM_RGBA8_SNORM,
  58. CUSTOM_RG_HALF = RS::ARRAY_CUSTOM_RG_HALF,
  59. CUSTOM_RGBA_HALF = RS::ARRAY_CUSTOM_RGBA_HALF,
  60. CUSTOM_R_FLOAT = RS::ARRAY_CUSTOM_R_FLOAT,
  61. CUSTOM_RG_FLOAT = RS::ARRAY_CUSTOM_RG_FLOAT,
  62. CUSTOM_RGB_FLOAT = RS::ARRAY_CUSTOM_RGB_FLOAT,
  63. CUSTOM_RGBA_FLOAT = RS::ARRAY_CUSTOM_RGBA_FLOAT,
  64. CUSTOM_MAX = RS::ARRAY_CUSTOM_MAX
  65. };
  66. enum SkinWeightCount {
  67. SKIN_4_WEIGHTS,
  68. SKIN_8_WEIGHTS
  69. };
  70. enum {
  71. /* Do not move vertices that are located on the topological border (vertices on triangle edges that don't have a paired triangle). Useful for simplifying portions of the larger mesh. */
  72. SIMPLIFY_LOCK_BORDER = 1 << 0, // From meshopt_SimplifyLockBorder
  73. /* Improve simplification performance assuming input indices are a sparse subset of the mesh. Note that error becomes relative to subset extents. */
  74. SIMPLIFY_SPARSE = 1 << 1, // From meshopt_SimplifySparse
  75. /* Treat error limit and resulting error as absolute instead of relative to mesh extents. */
  76. SIMPLIFY_ERROR_ABSOLUTE = 1 << 2, // From meshopt_SimplifyErrorAbsolute
  77. /* Remove disconnected parts of the mesh during simplification incrementally, regardless of the topological restrictions inside components. */
  78. SIMPLIFY_PRUNE = 1 << 3, // From meshopt_SimplifyPrune
  79. };
  80. typedef void (*OptimizeVertexCacheFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, size_t vertex_count);
  81. static OptimizeVertexCacheFunc optimize_vertex_cache_func;
  82. typedef size_t (*OptimizeVertexFetchRemapFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, size_t vertex_count);
  83. static OptimizeVertexFetchRemapFunc optimize_vertex_fetch_remap_func;
  84. typedef size_t (*SimplifyFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, const float *vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, unsigned int options, float *r_error);
  85. static SimplifyFunc simplify_func;
  86. typedef size_t (*SimplifyWithAttribFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, const float *vertex_data, size_t vertex_count, size_t vertex_stride, const float *attributes, size_t attribute_stride, const float *attribute_weights, size_t attribute_count, const unsigned char *vertex_lock, size_t target_index_count, float target_error, unsigned int options, float *result_error);
  87. static SimplifyWithAttribFunc simplify_with_attrib_func;
  88. typedef float (*SimplifyScaleFunc)(const float *vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  89. static SimplifyScaleFunc simplify_scale_func;
  90. typedef size_t (*GenerateRemapFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, const void *vertices, size_t vertex_count, size_t vertex_size);
  91. static GenerateRemapFunc generate_remap_func;
  92. typedef void (*RemapVertexFunc)(void *destination, const void *vertices, size_t vertex_count, size_t vertex_size, const unsigned int *remap);
  93. static RemapVertexFunc remap_vertex_func;
  94. typedef void (*RemapIndexFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, const unsigned int *remap);
  95. static RemapIndexFunc remap_index_func;
  96. static void strip_mesh_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices);
  97. private:
  98. struct VertexHasher {
  99. static _FORCE_INLINE_ uint32_t hash(const Vertex &p_vtx);
  100. };
  101. struct SmoothGroupVertex {
  102. Vector3 vertex;
  103. uint32_t smooth_group = 0;
  104. bool operator==(const SmoothGroupVertex &p_vertex) const;
  105. SmoothGroupVertex(const Vertex &p_vertex) {
  106. vertex = p_vertex.vertex;
  107. smooth_group = p_vertex.smooth_group;
  108. }
  109. };
  110. struct SmoothGroupVertexHasher {
  111. static _FORCE_INLINE_ uint32_t hash(const SmoothGroupVertex &p_vtx);
  112. };
  113. struct TriangleHasher {
  114. static _FORCE_INLINE_ uint32_t hash(const int *p_triangle);
  115. static _FORCE_INLINE_ bool compare(const int *p_lhs, const int *p_rhs);
  116. };
  117. struct WeightSort {
  118. int index = 0;
  119. float weight = 0.0;
  120. bool operator<(const WeightSort &p_right) const {
  121. return weight < p_right.weight;
  122. }
  123. };
  124. bool begun = false;
  125. bool first = false;
  126. Mesh::PrimitiveType primitive = Mesh::PRIMITIVE_LINES;
  127. uint64_t format = 0;
  128. Ref<Material> material;
  129. //arrays
  130. LocalVector<Vertex> vertex_array;
  131. LocalVector<int> index_array;
  132. //memory
  133. Color last_color;
  134. Vector3 last_normal;
  135. Vector2 last_uv;
  136. Vector2 last_uv2;
  137. Vector<int> last_bones;
  138. Vector<float> last_weights;
  139. Plane last_tangent;
  140. uint32_t last_smooth_group = 0;
  141. SkinWeightCount skin_weights = SKIN_4_WEIGHTS;
  142. Color last_custom[RS::ARRAY_CUSTOM_COUNT];
  143. CustomFormat last_custom_format[RS::ARRAY_CUSTOM_COUNT];
  144. void _create_list_from_arrays(Array arr, LocalVector<Vertex> *r_vertex, LocalVector<int> *r_index, uint64_t &lformat);
  145. void _create_list(const Ref<Mesh> &p_existing, int p_surface, LocalVector<Vertex> *r_vertex, LocalVector<int> *r_index, uint64_t &lformat);
  146. //mikktspace callbacks
  147. static int mikktGetNumFaces(const SMikkTSpaceContext *pContext);
  148. static int mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace);
  149. static void mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert);
  150. static void mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert);
  151. static void mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert);
  152. static void mikktSetTSpaceDefault(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
  153. const tbool bIsOrientationPreserving, const int iFace, const int iVert);
  154. void _add_triangle_fan(const Vector<Vector3> &p_vertices, const Vector<Vector2> &p_uvs = Vector<Vector2>(), const Vector<Color> &p_colors = Vector<Color>(), const Vector<Vector2> &p_uv2s = Vector<Vector2>(), const Vector<Vector3> &p_normals = Vector<Vector3>(), const TypedArray<Plane> &p_tangents = TypedArray<Plane>());
  155. protected:
  156. static void _bind_methods();
  157. public:
  158. void set_skin_weight_count(SkinWeightCount p_weights);
  159. SkinWeightCount get_skin_weight_count() const;
  160. void set_custom_format(int p_channel_index, CustomFormat p_format);
  161. CustomFormat get_custom_format(int p_channel_index) const;
  162. Mesh::PrimitiveType get_primitive_type() const;
  163. void begin(Mesh::PrimitiveType p_primitive);
  164. void set_color(Color p_color);
  165. void set_normal(const Vector3 &p_normal);
  166. void set_tangent(const Plane &p_tangent);
  167. void set_uv(const Vector2 &p_uv);
  168. void set_uv2(const Vector2 &p_uv2);
  169. void set_custom(int p_channel_index, const Color &p_custom);
  170. void set_bones(const Vector<int> &p_bones);
  171. void set_weights(const Vector<float> &p_weights);
  172. void set_smooth_group(uint32_t p_group);
  173. void add_vertex(const Vector3 &p_vertex);
  174. void add_triangle_fan(const Vector<Vector3> &p_vertices, const Vector<Vector2> &p_uvs = Vector<Vector2>(), const Vector<Color> &p_colors = Vector<Color>(), const Vector<Vector2> &p_uv2s = Vector<Vector2>(), const Vector<Vector3> &p_normals = Vector<Vector3>(), const Vector<Plane> &p_tangents = Vector<Plane>());
  175. void add_index(int p_index);
  176. void index();
  177. void deindex();
  178. void generate_normals(bool p_flip = false);
  179. void generate_tangents();
  180. void optimize_indices_for_cache();
  181. AABB get_aabb() const;
  182. Vector<int> generate_lod(float p_threshold, int p_target_index_count = 3);
  183. void set_material(const Ref<Material> &p_material);
  184. Ref<Material> get_material() const;
  185. void clear();
  186. LocalVector<Vertex> &get_vertex_array() {
  187. return vertex_array;
  188. }
  189. void create_from_triangle_arrays(const Array &p_arrays);
  190. void create_from_arrays(const Array &p_arrays, Mesh::PrimitiveType p_primitive_type = Mesh::PRIMITIVE_TRIANGLES);
  191. static void create_vertex_array_from_arrays(const Array &p_arrays, LocalVector<Vertex> &ret, uint64_t *r_format = nullptr);
  192. Array commit_to_arrays();
  193. void create_from(const Ref<Mesh> &p_existing, int p_surface);
  194. void create_from_blend_shape(const Ref<Mesh> &p_existing, int p_surface, const String &p_blend_shape_name);
  195. void append_from(const Ref<Mesh> &p_existing, int p_surface, const Transform3D &p_xform);
  196. Ref<ArrayMesh> commit(const Ref<ArrayMesh> &p_existing = Ref<ArrayMesh>(), uint64_t p_compress_flags = 0);
  197. SurfaceTool();
  198. };
  199. VARIANT_ENUM_CAST(SurfaceTool::CustomFormat)
  200. VARIANT_ENUM_CAST(SurfaceTool::SkinWeightCount)
  201. #endif // SURFACE_TOOL_H