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 | |
31 | #ifndef SURFACE_TOOL_H |
32 | #define SURFACE_TOOL_H |
33 | |
34 | #include "core/templates/local_vector.h" |
35 | #include "scene/resources/mesh.h" |
36 | #include "thirdparty/misc/mikktspace.h" |
37 | |
38 | class SurfaceTool : public RefCounted { |
39 | GDCLASS(SurfaceTool, RefCounted); |
40 | |
41 | static const uint32_t custom_mask[RS::ARRAY_CUSTOM_COUNT]; |
42 | static const uint32_t custom_shift[RS::ARRAY_CUSTOM_COUNT]; |
43 | |
44 | public: |
45 | struct Vertex { |
46 | Vector3 vertex; |
47 | Color color; |
48 | Vector3 normal; // normal, binormal, tangent |
49 | Vector3 binormal; |
50 | Vector3 tangent; |
51 | Vector2 uv; |
52 | Vector2 uv2; |
53 | Vector<int> bones; |
54 | Vector<float> weights; |
55 | Color custom[RS::ARRAY_CUSTOM_COUNT]; |
56 | uint32_t smooth_group = 0; |
57 | |
58 | bool operator==(const Vertex &p_vertex) const; |
59 | |
60 | Vertex() {} |
61 | }; |
62 | |
63 | enum CustomFormat { |
64 | CUSTOM_RGBA8_UNORM = RS::ARRAY_CUSTOM_RGBA8_UNORM, |
65 | CUSTOM_RGBA8_SNORM = RS::ARRAY_CUSTOM_RGBA8_SNORM, |
66 | CUSTOM_RG_HALF = RS::ARRAY_CUSTOM_RG_HALF, |
67 | CUSTOM_RGBA_HALF = RS::ARRAY_CUSTOM_RGBA_HALF, |
68 | CUSTOM_R_FLOAT = RS::ARRAY_CUSTOM_R_FLOAT, |
69 | CUSTOM_RG_FLOAT = RS::ARRAY_CUSTOM_RG_FLOAT, |
70 | CUSTOM_RGB_FLOAT = RS::ARRAY_CUSTOM_RGB_FLOAT, |
71 | CUSTOM_RGBA_FLOAT = RS::ARRAY_CUSTOM_RGBA_FLOAT, |
72 | CUSTOM_MAX = RS::ARRAY_CUSTOM_MAX |
73 | }; |
74 | |
75 | enum SkinWeightCount { |
76 | SKIN_4_WEIGHTS, |
77 | SKIN_8_WEIGHTS |
78 | }; |
79 | |
80 | enum { |
81 | /* 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. */ |
82 | SIMPLIFY_LOCK_BORDER = 1 << 0, // From meshopt_SimplifyLockBorder |
83 | }; |
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 (*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); |
88 | static SimplifyFunc simplify_func; |
89 | 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); |
90 | static SimplifyWithAttribFunc simplify_with_attrib_func; |
91 | typedef float (*SimplifyScaleFunc)(const float *vertex_positions, size_t vertex_count, size_t vertex_positions_stride); |
92 | static SimplifyScaleFunc simplify_scale_func; |
93 | 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); |
94 | static SimplifySloppyFunc simplify_sloppy_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 | |
103 | private: |
104 | struct VertexHasher { |
105 | static _FORCE_INLINE_ uint32_t hash(const Vertex &p_vtx); |
106 | }; |
107 | |
108 | struct SmoothGroupVertex { |
109 | Vector3 vertex; |
110 | uint32_t smooth_group = 0; |
111 | bool operator==(const SmoothGroupVertex &p_vertex) const; |
112 | |
113 | SmoothGroupVertex(const Vertex &p_vertex) { |
114 | vertex = p_vertex.vertex; |
115 | smooth_group = p_vertex.smooth_group; |
116 | }; |
117 | }; |
118 | |
119 | struct SmoothGroupVertexHasher { |
120 | static _FORCE_INLINE_ uint32_t hash(const SmoothGroupVertex &p_vtx); |
121 | }; |
122 | |
123 | struct TriangleHasher { |
124 | static _FORCE_INLINE_ uint32_t hash(const int *p_triangle); |
125 | static _FORCE_INLINE_ bool compare(const int *p_lhs, const int *p_rhs); |
126 | }; |
127 | |
128 | struct WeightSort { |
129 | int index = 0; |
130 | float weight = 0.0; |
131 | bool operator<(const WeightSort &p_right) const { |
132 | return weight < p_right.weight; |
133 | } |
134 | }; |
135 | |
136 | bool begun = false; |
137 | bool first = false; |
138 | Mesh::PrimitiveType primitive = Mesh::PRIMITIVE_LINES; |
139 | uint32_t format = 0; |
140 | Ref<Material> material; |
141 | //arrays |
142 | LocalVector<Vertex> vertex_array; |
143 | LocalVector<int> index_array; |
144 | |
145 | //memory |
146 | Color last_color; |
147 | Vector3 last_normal; |
148 | Vector2 last_uv; |
149 | Vector2 last_uv2; |
150 | Vector<int> last_bones; |
151 | Vector<float> last_weights; |
152 | Plane last_tangent; |
153 | uint32_t last_smooth_group = 0; |
154 | |
155 | SkinWeightCount skin_weights = SKIN_4_WEIGHTS; |
156 | |
157 | Color last_custom[RS::ARRAY_CUSTOM_COUNT]; |
158 | |
159 | CustomFormat last_custom_format[RS::ARRAY_CUSTOM_COUNT]; |
160 | |
161 | void _create_list_from_arrays(Array arr, LocalVector<Vertex> *r_vertex, LocalVector<int> *r_index, uint32_t &lformat); |
162 | void _create_list(const Ref<Mesh> &p_existing, int p_surface, LocalVector<Vertex> *r_vertex, LocalVector<int> *r_index, uint32_t &lformat); |
163 | |
164 | //mikktspace callbacks |
165 | static int mikktGetNumFaces(const SMikkTSpaceContext *pContext); |
166 | static int mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace); |
167 | static void mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert); |
168 | static void mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert); |
169 | static void mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert); |
170 | static void mikktSetTSpaceDefault(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT, |
171 | const tbool bIsOrientationPreserving, const int iFace, const int iVert); |
172 | |
173 | 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>()); |
174 | |
175 | protected: |
176 | static void _bind_methods(); |
177 | |
178 | public: |
179 | void set_skin_weight_count(SkinWeightCount p_weights); |
180 | SkinWeightCount get_skin_weight_count() const; |
181 | |
182 | void set_custom_format(int p_channel_index, CustomFormat p_format); |
183 | CustomFormat get_custom_format(int p_channel_index) const; |
184 | |
185 | Mesh::PrimitiveType get_primitive_type() const; |
186 | |
187 | void begin(Mesh::PrimitiveType p_primitive); |
188 | |
189 | void set_color(Color p_color); |
190 | void set_normal(const Vector3 &p_normal); |
191 | void set_tangent(const Plane &p_tangent); |
192 | void set_uv(const Vector2 &p_uv); |
193 | void set_uv2(const Vector2 &p_uv2); |
194 | void set_custom(int p_channel_index, const Color &p_custom); |
195 | void set_bones(const Vector<int> &p_bones); |
196 | void set_weights(const Vector<float> &p_weights); |
197 | void set_smooth_group(uint32_t p_group); |
198 | |
199 | void add_vertex(const Vector3 &p_vertex); |
200 | |
201 | 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>()); |
202 | |
203 | void add_index(int p_index); |
204 | |
205 | void index(); |
206 | void deindex(); |
207 | void generate_normals(bool p_flip = false); |
208 | void generate_tangents(); |
209 | |
210 | void optimize_indices_for_cache(); |
211 | AABB get_aabb() const; |
212 | Vector<int> generate_lod(float p_threshold, int p_target_index_count = 3); |
213 | |
214 | void set_material(const Ref<Material> &p_material); |
215 | Ref<Material> get_material() const; |
216 | |
217 | void clear(); |
218 | |
219 | LocalVector<Vertex> &get_vertex_array() { return vertex_array; } |
220 | |
221 | void create_from_triangle_arrays(const Array &p_arrays); |
222 | static void create_vertex_array_from_triangle_arrays(const Array &p_arrays, LocalVector<Vertex> &ret, uint32_t *r_format = nullptr); |
223 | Array commit_to_arrays(); |
224 | void create_from(const Ref<Mesh> &p_existing, int p_surface); |
225 | void create_from_blend_shape(const Ref<Mesh> &p_existing, int p_surface, const String &p_blend_shape_name); |
226 | void append_from(const Ref<Mesh> &p_existing, int p_surface, const Transform3D &p_xform); |
227 | Ref<ArrayMesh> commit(const Ref<ArrayMesh> &p_existing = Ref<ArrayMesh>(), uint32_t p_compress_flags = 0); |
228 | |
229 | SurfaceTool(); |
230 | }; |
231 | |
232 | VARIANT_ENUM_CAST(SurfaceTool::CustomFormat) |
233 | VARIANT_ENUM_CAST(SurfaceTool::SkinWeightCount) |
234 | |
235 | #endif // SURFACE_TOOL_H |
236 | |