surface_tool.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*************************************************************************/
  2. /* surface_tool.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "scene/resources/mesh.h"
  33. #include "thirdparty/misc/mikktspace.h"
  34. class SurfaceTool : public Reference {
  35. GDCLASS(SurfaceTool, Reference);
  36. public:
  37. struct Vertex {
  38. Vector3 vertex;
  39. Color color;
  40. Vector3 normal; // normal, binormal, tangent
  41. Vector3 binormal;
  42. Vector3 tangent;
  43. Vector2 uv;
  44. Vector2 uv2;
  45. Vector<int> bones;
  46. Vector<float> weights;
  47. bool operator==(const Vertex &p_vertex) const;
  48. Vertex() {}
  49. };
  50. private:
  51. struct VertexHasher {
  52. static _FORCE_INLINE_ uint32_t hash(const Vertex &p_vtx);
  53. };
  54. bool begun;
  55. bool first;
  56. Mesh::PrimitiveType primitive;
  57. int format;
  58. Ref<Material> material;
  59. //arrays
  60. List<Vertex> vertex_array;
  61. List<int> index_array;
  62. Map<int, bool> smooth_groups;
  63. //memory
  64. Color last_color;
  65. Vector3 last_normal;
  66. Vector2 last_uv;
  67. Vector2 last_uv2;
  68. Vector<int> last_bones;
  69. Vector<float> last_weights;
  70. Plane last_tangent;
  71. void _create_list_from_arrays(Array arr, List<Vertex> *r_vertex, List<int> *r_index, int &lformat);
  72. void _create_list(const Ref<Mesh> &p_existing, int p_surface, List<Vertex> *r_vertex, List<int> *r_index, int &lformat);
  73. //mikktspace callbacks
  74. static int mikktGetNumFaces(const SMikkTSpaceContext *pContext);
  75. static int mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace);
  76. static void mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert);
  77. static void mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert);
  78. static void mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert);
  79. static void mikktSetTSpaceDefault(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
  80. const tbool bIsOrientationPreserving, const int iFace, const int iVert);
  81. protected:
  82. static void _bind_methods();
  83. public:
  84. void begin(Mesh::PrimitiveType p_primitive);
  85. void add_vertex(const Vector3 &p_vertex);
  86. void add_color(Color p_color);
  87. void add_normal(const Vector3 &p_normal);
  88. void add_tangent(const Plane &p_tangent);
  89. void add_uv(const Vector2 &p_uv);
  90. void add_uv2(const Vector2 &p_uv2);
  91. void add_bones(const Vector<int> &p_bones);
  92. void add_weights(const Vector<float> &p_weights);
  93. void add_smooth_group(bool p_smooth);
  94. void add_triangle_fan(const Vector<Vector3> &p_vertexes, 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>());
  95. void add_index(int p_index);
  96. void index();
  97. void deindex();
  98. void generate_normals(bool p_flip = false);
  99. void generate_tangents();
  100. void add_to_format(int p_flags) { format |= p_flags; }
  101. void set_material(const Ref<Material> &p_material);
  102. void clear();
  103. List<Vertex> &get_vertex_array() { return vertex_array; }
  104. void create_from_triangle_arrays(const Array &p_arrays);
  105. static Vector<Vertex> create_vertex_array_from_triangle_arrays(const Array &p_arrays);
  106. Array commit_to_arrays();
  107. void create_from(const Ref<Mesh> &p_existing, int p_surface);
  108. void append_from(const Ref<Mesh> &p_existing, int p_surface, const Transform &p_xform);
  109. Ref<ArrayMesh> commit(const Ref<ArrayMesh> &p_existing = Ref<ArrayMesh>(), uint32_t p_flags = Mesh::ARRAY_COMPRESS_DEFAULT);
  110. SurfaceTool();
  111. };
  112. #endif