surface_tool.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*************************************************************************/
  2. /* surface_tool.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. typedef void (*OptimizeVertexCacheFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, size_t vertex_count);
  69. static OptimizeVertexCacheFunc optimize_vertex_cache_func;
  70. 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, float *r_error);
  71. static SimplifyFunc simplify_func;
  72. 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, float *result_error, const float *attributes, const float *attribute_weights, size_t attribute_count);
  73. static SimplifyWithAttribFunc simplify_with_attrib_func;
  74. typedef float (*SimplifyScaleFunc)(const float *vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  75. static SimplifyScaleFunc simplify_scale_func;
  76. 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);
  77. static SimplifySloppyFunc simplify_sloppy_func;
  78. 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);
  79. static GenerateRemapFunc generate_remap_func;
  80. typedef void (*RemapVertexFunc)(void *destination, const void *vertices, size_t vertex_count, size_t vertex_size, const unsigned int *remap);
  81. static RemapVertexFunc remap_vertex_func;
  82. typedef void (*RemapIndexFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, const unsigned int *remap);
  83. static RemapIndexFunc remap_index_func;
  84. static void strip_mesh_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices);
  85. private:
  86. struct VertexHasher {
  87. static _FORCE_INLINE_ uint32_t hash(const Vertex &p_vtx);
  88. };
  89. struct TriangleHasher {
  90. static _FORCE_INLINE_ uint32_t hash(const int *p_triangle);
  91. static _FORCE_INLINE_ bool compare(const int *p_lhs, const int *p_rhs);
  92. };
  93. struct WeightSort {
  94. int index = 0;
  95. float weight = 0.0;
  96. bool operator<(const WeightSort &p_right) const {
  97. return weight < p_right.weight;
  98. }
  99. };
  100. bool begun = false;
  101. bool first = false;
  102. Mesh::PrimitiveType primitive = Mesh::PRIMITIVE_LINES;
  103. uint32_t format = 0;
  104. Ref<Material> material;
  105. //arrays
  106. LocalVector<Vertex> vertex_array;
  107. LocalVector<int> index_array;
  108. //memory
  109. Color last_color;
  110. Vector3 last_normal;
  111. Vector2 last_uv;
  112. Vector2 last_uv2;
  113. Vector<int> last_bones;
  114. Vector<float> last_weights;
  115. Plane last_tangent;
  116. uint32_t last_smooth_group = 0;
  117. SkinWeightCount skin_weights = SKIN_4_WEIGHTS;
  118. Color last_custom[RS::ARRAY_CUSTOM_COUNT];
  119. CustomFormat last_custom_format[RS::ARRAY_CUSTOM_COUNT];
  120. void _create_list_from_arrays(Array arr, LocalVector<Vertex> *r_vertex, LocalVector<int> *r_index, uint32_t &lformat);
  121. void _create_list(const Ref<Mesh> &p_existing, int p_surface, LocalVector<Vertex> *r_vertex, LocalVector<int> *r_index, uint32_t &lformat);
  122. //mikktspace callbacks
  123. static int mikktGetNumFaces(const SMikkTSpaceContext *pContext);
  124. static int mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace);
  125. static void mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert);
  126. static void mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert);
  127. static void mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert);
  128. static void mikktSetTSpaceDefault(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
  129. const tbool bIsOrientationPreserving, const int iFace, const int iVert);
  130. protected:
  131. static void _bind_methods();
  132. public:
  133. void set_skin_weight_count(SkinWeightCount p_weights);
  134. SkinWeightCount get_skin_weight_count() const;
  135. void set_custom_format(int p_channel_index, CustomFormat p_format);
  136. CustomFormat get_custom_format(int p_channel_index) const;
  137. Mesh::PrimitiveType get_primitive_type() const;
  138. void begin(Mesh::PrimitiveType p_primitive);
  139. void set_color(Color p_color);
  140. void set_normal(const Vector3 &p_normal);
  141. void set_tangent(const Plane &p_tangent);
  142. void set_uv(const Vector2 &p_uv);
  143. void set_uv2(const Vector2 &p_uv2);
  144. void set_custom(int p_channel_index, const Color &p_custom);
  145. void set_bones(const Vector<int> &p_bones);
  146. void set_weights(const Vector<float> &p_weights);
  147. void set_smooth_group(uint32_t p_group);
  148. void add_vertex(const Vector3 &p_vertex);
  149. 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>());
  150. void add_index(int p_index);
  151. void index();
  152. void deindex();
  153. void generate_normals(bool p_flip = false);
  154. void generate_tangents();
  155. void optimize_indices_for_cache();
  156. AABB get_aabb() const;
  157. Vector<int> generate_lod(float p_threshold, int p_target_index_count = 3);
  158. void set_material(const Ref<Material> &p_material);
  159. Ref<Material> get_material() const;
  160. void clear();
  161. LocalVector<Vertex> &get_vertex_array() { return vertex_array; }
  162. void create_from_triangle_arrays(const Array &p_arrays);
  163. static void create_vertex_array_from_triangle_arrays(const Array &p_arrays, LocalVector<Vertex> &ret, uint32_t *r_format = nullptr);
  164. Array commit_to_arrays();
  165. void create_from(const Ref<Mesh> &p_existing, int p_surface);
  166. void create_from_blend_shape(const Ref<Mesh> &p_existing, int p_surface, const String &p_blend_shape_name);
  167. void append_from(const Ref<Mesh> &p_existing, int p_surface, const Transform3D &p_xform);
  168. Ref<ArrayMesh> commit(const Ref<ArrayMesh> &p_existing = Ref<ArrayMesh>(), uint32_t p_compress_flags = 0);
  169. SurfaceTool();
  170. };
  171. VARIANT_ENUM_CAST(SurfaceTool::CustomFormat)
  172. VARIANT_ENUM_CAST(SurfaceTool::SkinWeightCount)
  173. #endif // SURFACE_TOOL_H