surface_tool.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. public:
  38. struct Vertex {
  39. Vector3 vertex;
  40. Color color;
  41. Vector3 normal; // normal, binormal, tangent
  42. Vector3 binormal;
  43. Vector3 tangent;
  44. Vector2 uv;
  45. Vector2 uv2;
  46. Vector<int> bones;
  47. Vector<float> weights;
  48. Color custom[RS::ARRAY_CUSTOM_COUNT];
  49. uint32_t smooth_group = 0;
  50. bool operator==(const Vertex &p_vertex) const;
  51. Vertex() {}
  52. };
  53. enum CustomFormat {
  54. CUSTOM_RGBA8_UNORM = RS::ARRAY_CUSTOM_RGBA8_UNORM,
  55. CUSTOM_RGBA8_SNORM = RS::ARRAY_CUSTOM_RGBA8_SNORM,
  56. CUSTOM_RG_HALF = RS::ARRAY_CUSTOM_RG_HALF,
  57. CUSTOM_RGBA_HALF = RS::ARRAY_CUSTOM_RGBA_HALF,
  58. CUSTOM_R_FLOAT = RS::ARRAY_CUSTOM_R_FLOAT,
  59. CUSTOM_RG_FLOAT = RS::ARRAY_CUSTOM_RG_FLOAT,
  60. CUSTOM_RGB_FLOAT = RS::ARRAY_CUSTOM_RGB_FLOAT,
  61. CUSTOM_RGBA_FLOAT = RS::ARRAY_CUSTOM_RGBA_FLOAT,
  62. CUSTOM_MAX = RS::ARRAY_CUSTOM_MAX
  63. };
  64. enum SkinWeightCount {
  65. SKIN_4_WEIGHTS,
  66. SKIN_8_WEIGHTS
  67. };
  68. enum {
  69. /* 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. */
  70. SIMPLIFY_LOCK_BORDER = 1 << 0, // From meshopt_SimplifyLockBorder
  71. };
  72. typedef void (*OptimizeVertexCacheFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, size_t vertex_count);
  73. static OptimizeVertexCacheFunc optimize_vertex_cache_func;
  74. 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);
  75. static SimplifyFunc simplify_func;
  76. 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, size_t target_index_count, float target_error, unsigned int options, float *result_error, const float *attributes, const float *attribute_weights, size_t attribute_count);
  77. static SimplifyWithAttribFunc simplify_with_attrib_func;
  78. typedef float (*SimplifyScaleFunc)(const float *vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  79. static SimplifyScaleFunc simplify_scale_func;
  80. typedef size_t (*SimplifySloppyFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, const float *vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, float *out_result_error);
  81. static SimplifySloppyFunc simplify_sloppy_func;
  82. 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);
  83. static GenerateRemapFunc generate_remap_func;
  84. typedef void (*RemapVertexFunc)(void *destination, const void *vertices, size_t vertex_count, size_t vertex_size, const unsigned int *remap);
  85. static RemapVertexFunc remap_vertex_func;
  86. typedef void (*RemapIndexFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, const unsigned int *remap);
  87. static RemapIndexFunc remap_index_func;
  88. static void strip_mesh_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices);
  89. private:
  90. struct VertexHasher {
  91. static _FORCE_INLINE_ uint32_t hash(const Vertex &p_vtx);
  92. };
  93. struct SmoothGroupVertex {
  94. Vector3 vertex;
  95. uint32_t smooth_group = 0;
  96. bool operator==(const SmoothGroupVertex &p_vertex) const;
  97. SmoothGroupVertex(const Vertex &p_vertex) {
  98. vertex = p_vertex.vertex;
  99. smooth_group = p_vertex.smooth_group;
  100. };
  101. };
  102. struct SmoothGroupVertexHasher {
  103. static _FORCE_INLINE_ uint32_t hash(const SmoothGroupVertex &p_vtx);
  104. };
  105. struct TriangleHasher {
  106. static _FORCE_INLINE_ uint32_t hash(const int *p_triangle);
  107. static _FORCE_INLINE_ bool compare(const int *p_lhs, const int *p_rhs);
  108. };
  109. struct WeightSort {
  110. int index = 0;
  111. float weight = 0.0;
  112. bool operator<(const WeightSort &p_right) const {
  113. return weight < p_right.weight;
  114. }
  115. };
  116. bool begun = false;
  117. bool first = false;
  118. Mesh::PrimitiveType primitive = Mesh::PRIMITIVE_LINES;
  119. uint32_t format = 0;
  120. Ref<Material> material;
  121. //arrays
  122. LocalVector<Vertex> vertex_array;
  123. LocalVector<int> index_array;
  124. //memory
  125. Color last_color;
  126. Vector3 last_normal;
  127. Vector2 last_uv;
  128. Vector2 last_uv2;
  129. Vector<int> last_bones;
  130. Vector<float> last_weights;
  131. Plane last_tangent;
  132. uint32_t last_smooth_group = 0;
  133. SkinWeightCount skin_weights = SKIN_4_WEIGHTS;
  134. Color last_custom[RS::ARRAY_CUSTOM_COUNT];
  135. CustomFormat last_custom_format[RS::ARRAY_CUSTOM_COUNT];
  136. void _create_list_from_arrays(Array arr, LocalVector<Vertex> *r_vertex, LocalVector<int> *r_index, uint32_t &lformat);
  137. void _create_list(const Ref<Mesh> &p_existing, int p_surface, LocalVector<Vertex> *r_vertex, LocalVector<int> *r_index, uint32_t &lformat);
  138. //mikktspace callbacks
  139. static int mikktGetNumFaces(const SMikkTSpaceContext *pContext);
  140. static int mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace);
  141. static void mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert);
  142. static void mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert);
  143. static void mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert);
  144. static void mikktSetTSpaceDefault(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
  145. const tbool bIsOrientationPreserving, const int iFace, const int iVert);
  146. protected:
  147. static void _bind_methods();
  148. public:
  149. void set_skin_weight_count(SkinWeightCount p_weights);
  150. SkinWeightCount get_skin_weight_count() const;
  151. void set_custom_format(int p_channel_index, CustomFormat p_format);
  152. CustomFormat get_custom_format(int p_channel_index) const;
  153. Mesh::PrimitiveType get_primitive_type() const;
  154. void begin(Mesh::PrimitiveType p_primitive);
  155. void set_color(Color p_color);
  156. void set_normal(const Vector3 &p_normal);
  157. void set_tangent(const Plane &p_tangent);
  158. void set_uv(const Vector2 &p_uv);
  159. void set_uv2(const Vector2 &p_uv2);
  160. void set_custom(int p_channel_index, const Color &p_custom);
  161. void set_bones(const Vector<int> &p_bones);
  162. void set_weights(const Vector<float> &p_weights);
  163. void set_smooth_group(uint32_t p_group);
  164. void add_vertex(const Vector3 &p_vertex);
  165. 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>());
  166. void add_index(int p_index);
  167. void index();
  168. void deindex();
  169. void generate_normals(bool p_flip = false);
  170. void generate_tangents();
  171. void optimize_indices_for_cache();
  172. AABB get_aabb() const;
  173. Vector<int> generate_lod(float p_threshold, int p_target_index_count = 3);
  174. void set_material(const Ref<Material> &p_material);
  175. Ref<Material> get_material() const;
  176. void clear();
  177. LocalVector<Vertex> &get_vertex_array() { return vertex_array; }
  178. void create_from_triangle_arrays(const Array &p_arrays);
  179. static void create_vertex_array_from_triangle_arrays(const Array &p_arrays, LocalVector<Vertex> &ret, uint32_t *r_format = nullptr);
  180. Array commit_to_arrays();
  181. void create_from(const Ref<Mesh> &p_existing, int p_surface);
  182. void create_from_blend_shape(const Ref<Mesh> &p_existing, int p_surface, const String &p_blend_shape_name);
  183. void append_from(const Ref<Mesh> &p_existing, int p_surface, const Transform3D &p_xform);
  184. Ref<ArrayMesh> commit(const Ref<ArrayMesh> &p_existing = Ref<ArrayMesh>(), uint32_t p_compress_flags = 0);
  185. SurfaceTool();
  186. };
  187. VARIANT_ENUM_CAST(SurfaceTool::CustomFormat)
  188. VARIANT_ENUM_CAST(SurfaceTool::SkinWeightCount)
  189. #endif // SURFACE_TOOL_H