surface_tool.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. /* Produce more regular triangle sizes and shapes during simplification, at some cost to geometric quality. */
  81. SIMPLIFY_REGULARIZE = 1 << 4, // From meshopt_SimplifyRegularize
  82. /* Allow collapses across attribute discontinuities, except for vertices that are tagged with 0x02 in vertex_lock. */
  83. SIMPLIFY_PERMISSIVE = 1 << 5, // From meshopt_SimplifyPermissive
  84. };
  85. typedef void (*OptimizeVertexCacheFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, size_t vertex_count);
  86. static OptimizeVertexCacheFunc optimize_vertex_cache_func;
  87. typedef size_t (*OptimizeVertexFetchRemapFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, size_t vertex_count);
  88. static OptimizeVertexFetchRemapFunc optimize_vertex_fetch_remap_func;
  89. 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);
  90. static SimplifyFunc simplify_func;
  91. 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);
  92. static SimplifyWithAttribFunc simplify_with_attrib_func;
  93. typedef float (*SimplifyScaleFunc)(const float *vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  94. static SimplifyScaleFunc simplify_scale_func;
  95. 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);
  96. static GenerateRemapFunc generate_remap_func;
  97. typedef void (*RemapVertexFunc)(void *destination, const void *vertices, size_t vertex_count, size_t vertex_size, const unsigned int *remap);
  98. static RemapVertexFunc remap_vertex_func;
  99. typedef void (*RemapIndexFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, const unsigned int *remap);
  100. static RemapIndexFunc remap_index_func;
  101. static void strip_mesh_arrays(PackedVector3Array &r_vertices, PackedInt32Array &r_indices);
  102. private:
  103. struct VertexHasher {
  104. static _FORCE_INLINE_ uint32_t hash(const Vertex &p_vtx);
  105. };
  106. struct SmoothGroupVertex {
  107. Vector3 vertex;
  108. uint32_t smooth_group = 0;
  109. bool operator==(const SmoothGroupVertex &p_vertex) const;
  110. SmoothGroupVertex(const Vertex &p_vertex) {
  111. vertex = p_vertex.vertex;
  112. smooth_group = p_vertex.smooth_group;
  113. }
  114. };
  115. struct SmoothGroupVertexHasher {
  116. static _FORCE_INLINE_ uint32_t hash(const SmoothGroupVertex &p_vtx);
  117. };
  118. struct TriangleHasher {
  119. static _FORCE_INLINE_ uint32_t hash(const int *p_triangle);
  120. static _FORCE_INLINE_ bool compare(const int *p_lhs, const int *p_rhs);
  121. };
  122. struct WeightSort {
  123. int index = 0;
  124. float weight = 0.0;
  125. bool operator<(const WeightSort &p_right) const {
  126. return weight < p_right.weight;
  127. }
  128. };
  129. bool begun = false;
  130. bool first = false;
  131. Mesh::PrimitiveType primitive = Mesh::PRIMITIVE_LINES;
  132. uint64_t format = 0;
  133. Ref<Material> material;
  134. //arrays
  135. LocalVector<Vertex> vertex_array;
  136. LocalVector<int> index_array;
  137. //memory
  138. Color last_color;
  139. Vector3 last_normal;
  140. Vector2 last_uv;
  141. Vector2 last_uv2;
  142. Vector<int> last_bones;
  143. Vector<float> last_weights;
  144. Plane last_tangent;
  145. uint32_t last_smooth_group = 0;
  146. SkinWeightCount skin_weights = SKIN_4_WEIGHTS;
  147. Color last_custom[RS::ARRAY_CUSTOM_COUNT];
  148. CustomFormat last_custom_format[RS::ARRAY_CUSTOM_COUNT];
  149. void _create_list_from_arrays(Array arr, LocalVector<Vertex> *r_vertex, LocalVector<int> *r_index, uint64_t &lformat);
  150. void _create_list(const Ref<Mesh> &p_existing, int p_surface, LocalVector<Vertex> *r_vertex, LocalVector<int> *r_index, uint64_t &lformat);
  151. //mikktspace callbacks
  152. static int mikktGetNumFaces(const SMikkTSpaceContext *pContext);
  153. static int mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace);
  154. static void mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert);
  155. static void mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert);
  156. static void mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert);
  157. static void mikktSetTSpaceDefault(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
  158. const tbool bIsOrientationPreserving, const int iFace, const int iVert);
  159. 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>());
  160. protected:
  161. static void _bind_methods();
  162. public:
  163. void set_skin_weight_count(SkinWeightCount p_weights);
  164. SkinWeightCount get_skin_weight_count() const;
  165. void set_custom_format(int p_channel_index, CustomFormat p_format);
  166. CustomFormat get_custom_format(int p_channel_index) const;
  167. Mesh::PrimitiveType get_primitive_type() const;
  168. void begin(Mesh::PrimitiveType p_primitive);
  169. void set_color(Color p_color);
  170. void set_normal(const Vector3 &p_normal);
  171. void set_tangent(const Plane &p_tangent);
  172. void set_uv(const Vector2 &p_uv);
  173. void set_uv2(const Vector2 &p_uv2);
  174. void set_custom(int p_channel_index, const Color &p_custom);
  175. void set_bones(const Vector<int> &p_bones);
  176. void set_weights(const Vector<float> &p_weights);
  177. void set_smooth_group(uint32_t p_group);
  178. void add_vertex(const Vector3 &p_vertex);
  179. 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>());
  180. void add_index(int p_index);
  181. void index();
  182. void deindex();
  183. void generate_normals(bool p_flip = false);
  184. void generate_tangents();
  185. void optimize_indices_for_cache();
  186. AABB get_aabb() const;
  187. Vector<int> generate_lod(float p_threshold, int p_target_index_count = 3);
  188. void set_material(const Ref<Material> &p_material);
  189. Ref<Material> get_material() const;
  190. void clear();
  191. LocalVector<Vertex> &get_vertex_array() {
  192. return vertex_array;
  193. }
  194. void create_from_triangle_arrays(const Array &p_arrays);
  195. void create_from_arrays(const Array &p_arrays, Mesh::PrimitiveType p_primitive_type = Mesh::PRIMITIVE_TRIANGLES);
  196. static void create_vertex_array_from_arrays(const Array &p_arrays, LocalVector<Vertex> &ret, uint64_t *r_format = nullptr);
  197. Array commit_to_arrays();
  198. void create_from(const Ref<Mesh> &p_existing, int p_surface);
  199. void create_from_blend_shape(const Ref<Mesh> &p_existing, int p_surface, const String &p_blend_shape_name);
  200. void append_from(const Ref<Mesh> &p_existing, int p_surface, const Transform3D &p_xform);
  201. Ref<ArrayMesh> commit(const Ref<ArrayMesh> &p_existing = Ref<ArrayMesh>(), uint64_t p_compress_flags = 0);
  202. SurfaceTool();
  203. };
  204. VARIANT_ENUM_CAST(SurfaceTool::CustomFormat)
  205. VARIANT_ENUM_CAST(SurfaceTool::SkinWeightCount)