surface_tool.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #pragma once
  31. #include "core/templates/local_vector.h"
  32. #include "scene/resources/mesh.h"
  33. #include "thirdparty/misc/mikktspace.h"
  34. class SurfaceTool : public RefCounted {
  35. GDCLASS(SurfaceTool, RefCounted);
  36. static const uint32_t custom_mask[RS::ARRAY_CUSTOM_COUNT];
  37. static const uint32_t custom_shift[RS::ARRAY_CUSTOM_COUNT];
  38. public:
  39. struct Vertex {
  40. // Trivial data for which the hash is computed using hash_buffer.
  41. // ----------------------------------------------------------------
  42. uint32_t smooth_group = 0; // Must be first.
  43. Color color;
  44. Vector3 normal; // normal, binormal, tangent.
  45. Vector3 binormal;
  46. Vector3 tangent;
  47. Vector2 uv;
  48. Vector2 uv2;
  49. Color custom[RS::ARRAY_CUSTOM_COUNT];
  50. Vector3 vertex; // Must be last.
  51. // ----------------------------------------------------------------
  52. Vector<int> bones;
  53. Vector<float> weights;
  54. bool operator==(const Vertex &p_vertex) const;
  55. };
  56. enum CustomFormat {
  57. CUSTOM_RGBA8_UNORM = RS::ARRAY_CUSTOM_RGBA8_UNORM,
  58. CUSTOM_RGBA8_SNORM = RS::ARRAY_CUSTOM_RGBA8_SNORM,
  59. CUSTOM_RG_HALF = RS::ARRAY_CUSTOM_RG_HALF,
  60. CUSTOM_RGBA_HALF = RS::ARRAY_CUSTOM_RGBA_HALF,
  61. CUSTOM_R_FLOAT = RS::ARRAY_CUSTOM_R_FLOAT,
  62. CUSTOM_RG_FLOAT = RS::ARRAY_CUSTOM_RG_FLOAT,
  63. CUSTOM_RGB_FLOAT = RS::ARRAY_CUSTOM_RGB_FLOAT,
  64. CUSTOM_RGBA_FLOAT = RS::ARRAY_CUSTOM_RGBA_FLOAT,
  65. CUSTOM_MAX = RS::ARRAY_CUSTOM_MAX
  66. };
  67. enum SkinWeightCount {
  68. SKIN_4_WEIGHTS,
  69. SKIN_8_WEIGHTS
  70. };
  71. enum {
  72. /* 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. */
  73. SIMPLIFY_LOCK_BORDER = 1 << 0, // From meshopt_SimplifyLockBorder
  74. /* Improve simplification performance assuming input indices are a sparse subset of the mesh. Note that error becomes relative to subset extents. */
  75. SIMPLIFY_SPARSE = 1 << 1, // From meshopt_SimplifySparse
  76. /* Treat error limit and resulting error as absolute instead of relative to mesh extents. */
  77. SIMPLIFY_ERROR_ABSOLUTE = 1 << 2, // From meshopt_SimplifyErrorAbsolute
  78. /* Remove disconnected parts of the mesh during simplification incrementally, regardless of the topological restrictions inside components. */
  79. SIMPLIFY_PRUNE = 1 << 3, // From meshopt_SimplifyPrune
  80. };
  81. typedef void (*OptimizeVertexCacheFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, size_t vertex_count);
  82. static OptimizeVertexCacheFunc optimize_vertex_cache_func;
  83. typedef size_t (*OptimizeVertexFetchRemapFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, size_t vertex_count);
  84. static OptimizeVertexFetchRemapFunc optimize_vertex_fetch_remap_func;
  85. 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);
  86. static SimplifyFunc simplify_func;
  87. 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);
  88. static SimplifyWithAttribFunc simplify_with_attrib_func;
  89. typedef float (*SimplifyScaleFunc)(const float *vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  90. static SimplifyScaleFunc simplify_scale_func;
  91. 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);
  92. static GenerateRemapFunc generate_remap_func;
  93. typedef void (*RemapVertexFunc)(void *destination, const void *vertices, size_t vertex_count, size_t vertex_size, const unsigned int *remap);
  94. static RemapVertexFunc remap_vertex_func;
  95. typedef void (*RemapIndexFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, const unsigned int *remap);
  96. static RemapIndexFunc remap_index_func;
  97. static void strip_mesh_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices);
  98. private:
  99. struct VertexHasher {
  100. static _FORCE_INLINE_ uint32_t hash(const Vertex &p_vtx);
  101. };
  102. struct SmoothGroupVertex {
  103. Vector3 vertex;
  104. uint32_t smooth_group = 0;
  105. bool operator==(const SmoothGroupVertex &p_vertex) const;
  106. SmoothGroupVertex(const Vertex &p_vertex) {
  107. vertex = p_vertex.vertex;
  108. smooth_group = p_vertex.smooth_group;
  109. }
  110. };
  111. struct SmoothGroupVertexHasher {
  112. static _FORCE_INLINE_ uint32_t hash(const SmoothGroupVertex &p_vtx);
  113. };
  114. struct TriangleHasher {
  115. static _FORCE_INLINE_ uint32_t hash(const int *p_triangle);
  116. static _FORCE_INLINE_ bool compare(const int *p_lhs, const int *p_rhs);
  117. };
  118. struct WeightSort {
  119. int index = 0;
  120. float weight = 0.0;
  121. bool operator<(const WeightSort &p_right) const {
  122. return weight < p_right.weight;
  123. }
  124. };
  125. bool begun = false;
  126. bool first = false;
  127. Mesh::PrimitiveType primitive = Mesh::PRIMITIVE_LINES;
  128. uint64_t format = 0;
  129. Ref<Material> material;
  130. //arrays
  131. LocalVector<Vertex> vertex_array;
  132. LocalVector<int> index_array;
  133. //memory
  134. Color last_color;
  135. Vector3 last_normal;
  136. Vector2 last_uv;
  137. Vector2 last_uv2;
  138. Vector<int> last_bones;
  139. Vector<float> last_weights;
  140. Plane last_tangent;
  141. uint32_t last_smooth_group = 0;
  142. SkinWeightCount skin_weights = SKIN_4_WEIGHTS;
  143. Color last_custom[RS::ARRAY_CUSTOM_COUNT];
  144. CustomFormat last_custom_format[RS::ARRAY_CUSTOM_COUNT];
  145. void _create_list_from_arrays(Array arr, LocalVector<Vertex> *r_vertex, LocalVector<int> *r_index, uint64_t &lformat);
  146. void _create_list(const Ref<Mesh> &p_existing, int p_surface, LocalVector<Vertex> *r_vertex, LocalVector<int> *r_index, uint64_t &lformat);
  147. //mikktspace callbacks
  148. static int mikktGetNumFaces(const SMikkTSpaceContext *pContext);
  149. static int mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace);
  150. static void mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert);
  151. static void mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert);
  152. static void mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert);
  153. static void mikktSetTSpaceDefault(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
  154. const tbool bIsOrientationPreserving, const int iFace, const int iVert);
  155. 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>());
  156. protected:
  157. static void _bind_methods();
  158. public:
  159. void set_skin_weight_count(SkinWeightCount p_weights);
  160. SkinWeightCount get_skin_weight_count() const;
  161. void set_custom_format(int p_channel_index, CustomFormat p_format);
  162. CustomFormat get_custom_format(int p_channel_index) const;
  163. Mesh::PrimitiveType get_primitive_type() const;
  164. void begin(Mesh::PrimitiveType p_primitive);
  165. void set_color(Color p_color);
  166. void set_normal(const Vector3 &p_normal);
  167. void set_tangent(const Plane &p_tangent);
  168. void set_uv(const Vector2 &p_uv);
  169. void set_uv2(const Vector2 &p_uv2);
  170. void set_custom(int p_channel_index, const Color &p_custom);
  171. void set_bones(const Vector<int> &p_bones);
  172. void set_weights(const Vector<float> &p_weights);
  173. void set_smooth_group(uint32_t p_group);
  174. void add_vertex(const Vector3 &p_vertex);
  175. 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>());
  176. void add_index(int p_index);
  177. void index();
  178. void deindex();
  179. void generate_normals(bool p_flip = false);
  180. void generate_tangents();
  181. void optimize_indices_for_cache();
  182. AABB get_aabb() const;
  183. Vector<int> generate_lod(float p_threshold, int p_target_index_count = 3);
  184. void set_material(const Ref<Material> &p_material);
  185. Ref<Material> get_material() const;
  186. void clear();
  187. LocalVector<Vertex> &get_vertex_array() {
  188. return vertex_array;
  189. }
  190. void create_from_triangle_arrays(const Array &p_arrays);
  191. void create_from_arrays(const Array &p_arrays, Mesh::PrimitiveType p_primitive_type = Mesh::PRIMITIVE_TRIANGLES);
  192. static void create_vertex_array_from_arrays(const Array &p_arrays, LocalVector<Vertex> &ret, uint64_t *r_format = nullptr);
  193. Array commit_to_arrays();
  194. void create_from(const Ref<Mesh> &p_existing, int p_surface);
  195. void create_from_blend_shape(const Ref<Mesh> &p_existing, int p_surface, const String &p_blend_shape_name);
  196. void append_from(const Ref<Mesh> &p_existing, int p_surface, const Transform3D &p_xform);
  197. Ref<ArrayMesh> commit(const Ref<ArrayMesh> &p_existing = Ref<ArrayMesh>(), uint64_t p_compress_flags = 0);
  198. SurfaceTool();
  199. };
  200. VARIANT_ENUM_CAST(SurfaceTool::CustomFormat)
  201. VARIANT_ENUM_CAST(SurfaceTool::SkinWeightCount)