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