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
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**************************************************************************/
/*  mesh.h                                                                */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#ifndef MESH_H
#define MESH_H

#include "core/local_vector.h"
#include "core/math/face3.h"
#include "core/math/triangle_mesh.h"
#include "core/resource.h"
#include "scene/resources/material.h"
#include "scene/resources/shape.h"
#include "servers/visual_server.h"

class Mesh : public Resource {
	GDCLASS(Mesh, Resource);

	mutable Ref<TriangleMesh> triangle_mesh; //cached
	mutable Vector<Vector3> debug_lines;
	Size2 lightmap_size_hint;

protected:
	static void _bind_methods();

public:
	enum {

		NO_INDEX_ARRAY = VisualServer::NO_INDEX_ARRAY,
		ARRAY_WEIGHTS_SIZE = VisualServer::ARRAY_WEIGHTS_SIZE
	};

	enum ArrayType {

		ARRAY_VERTEX = VisualServer::ARRAY_VERTEX,
		ARRAY_NORMAL = VisualServer::ARRAY_NORMAL,
		ARRAY_TANGENT = VisualServer::ARRAY_TANGENT,
		ARRAY_COLOR = VisualServer::ARRAY_COLOR,
		ARRAY_TEX_UV = VisualServer::ARRAY_TEX_UV,
		ARRAY_TEX_UV2 = VisualServer::ARRAY_TEX_UV2,
		ARRAY_BONES = VisualServer::ARRAY_BONES,
		ARRAY_WEIGHTS = VisualServer::ARRAY_WEIGHTS,
		ARRAY_INDEX = VisualServer::ARRAY_INDEX,
		ARRAY_MAX = VisualServer::ARRAY_MAX

	};

	enum ArrayFormat {
		/* ARRAY FORMAT FLAGS */
		ARRAY_FORMAT_VERTEX = 1 << ARRAY_VERTEX, // mandatory
		ARRAY_FORMAT_NORMAL = 1 << ARRAY_NORMAL,
		ARRAY_FORMAT_TANGENT = 1 << ARRAY_TANGENT,
		ARRAY_FORMAT_COLOR = 1 << ARRAY_COLOR,
		ARRAY_FORMAT_TEX_UV = 1 << ARRAY_TEX_UV,
		ARRAY_FORMAT_TEX_UV2 = 1 << ARRAY_TEX_UV2,
		ARRAY_FORMAT_BONES = 1 << ARRAY_BONES,
		ARRAY_FORMAT_WEIGHTS = 1 << ARRAY_WEIGHTS,
		ARRAY_FORMAT_INDEX = 1 << ARRAY_INDEX,

		ARRAY_COMPRESS_BASE = (ARRAY_INDEX + 1),
		ARRAY_COMPRESS_VERTEX = 1 << (ARRAY_VERTEX + ARRAY_COMPRESS_BASE), // mandatory
		ARRAY_COMPRESS_NORMAL = 1 << (ARRAY_NORMAL + ARRAY_COMPRESS_BASE),
		ARRAY_COMPRESS_TANGENT = 1 << (ARRAY_TANGENT + ARRAY_COMPRESS_BASE),
		ARRAY_COMPRESS_COLOR = 1 << (ARRAY_COLOR + ARRAY_COMPRESS_BASE),
		ARRAY_COMPRESS_TEX_UV = 1 << (ARRAY_TEX_UV + ARRAY_COMPRESS_BASE),
		ARRAY_COMPRESS_TEX_UV2 = 1 << (ARRAY_TEX_UV2 + ARRAY_COMPRESS_BASE),
		ARRAY_COMPRESS_BONES = 1 << (ARRAY_BONES + ARRAY_COMPRESS_BASE),
		ARRAY_COMPRESS_WEIGHTS = 1 << (ARRAY_WEIGHTS + ARRAY_COMPRESS_BASE),
		ARRAY_COMPRESS_INDEX = 1 << (ARRAY_INDEX + ARRAY_COMPRESS_BASE),

		ARRAY_FLAG_USE_2D_VERTICES = ARRAY_COMPRESS_INDEX << 1,
		ARRAY_FLAG_USE_16_BIT_BONES = ARRAY_COMPRESS_INDEX << 2,
		ARRAY_FLAG_USE_DYNAMIC_UPDATE = ARRAY_COMPRESS_INDEX << 3,
		ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION = ARRAY_COMPRESS_INDEX << 4,
		ARRAY_FLAG_USE_VERTEX_CACHE_OPTIMIZATION = ARRAY_COMPRESS_INDEX << 5,

		ARRAY_COMPRESS_DEFAULT = ARRAY_COMPRESS_NORMAL | ARRAY_COMPRESS_TANGENT | ARRAY_COMPRESS_COLOR | ARRAY_COMPRESS_TEX_UV | ARRAY_COMPRESS_TEX_UV2 | ARRAY_COMPRESS_WEIGHTS | ARRAY_FLAG_USE_OCTAHEDRAL_COMPRESSION

	};

	enum PrimitiveType {
		PRIMITIVE_POINTS = VisualServer::PRIMITIVE_POINTS,
		PRIMITIVE_LINES = VisualServer::PRIMITIVE_LINES,
		PRIMITIVE_LINE_STRIP = VisualServer::PRIMITIVE_LINE_STRIP,
		PRIMITIVE_LINE_LOOP = VisualServer::PRIMITIVE_LINE_LOOP,
		PRIMITIVE_TRIANGLES = VisualServer::PRIMITIVE_TRIANGLES,
		PRIMITIVE_TRIANGLE_STRIP = VisualServer::PRIMITIVE_TRIANGLE_STRIP,
		PRIMITIVE_TRIANGLE_FAN = VisualServer::PRIMITIVE_TRIANGLE_FAN,
	};

	enum BlendShapeMode {

		BLEND_SHAPE_MODE_NORMALIZED = VS::BLEND_SHAPE_MODE_NORMALIZED,
		BLEND_SHAPE_MODE_RELATIVE = VS::BLEND_SHAPE_MODE_RELATIVE,
	};

	enum StorageMode {
		STORAGE_MODE_GPU,
		STORAGE_MODE_CPU,
		STORAGE_MODE_CPU_AND_GPU,
	};

	struct CachedStats {
		bool dirty = true;
		uint32_t triangle_count = 0;
		uint32_t vertex_count = 0;
		uint32_t index_count = 0;
		uint32_t array_format = 0;
	};

	virtual int get_surface_count() const = 0;<--- Virtual function in base class
	virtual int surface_get_array_len(int p_idx) const = 0;<--- Virtual function in base class
	virtual int surface_get_array_index_len(int p_idx) const = 0;<--- Virtual function in base class
	virtual bool surface_is_softbody_friendly(int p_idx) const;
	virtual Array surface_get_arrays(int p_surface) const = 0;<--- Virtual function in base class
	virtual Array surface_get_blend_shape_arrays(int p_surface) const = 0;<--- Virtual function in base class
	virtual uint32_t surface_get_format(int p_idx) const = 0;<--- Virtual function in base class
	virtual PrimitiveType surface_get_primitive_type(int p_idx) const = 0;<--- Virtual function in base class
	virtual void surface_set_material(int p_idx, const Ref<Material> &p_material) = 0;<--- Virtual function in base class
	virtual Ref<Material> surface_get_material(int p_idx) const = 0;<--- Virtual function in base class
	virtual int get_blend_shape_count() const = 0;<--- Virtual function in base class
	int surface_get_triangle_count(int p_idx) const;
	int surface_get_index_count(int p_idx) const;
	virtual StringName get_blend_shape_name(int p_index) const = 0;<--- Virtual function in base class
	virtual void set_blend_shape_name(int p_index, const StringName &p_name) = 0;<--- Virtual function in base class

	int get_triangle_count() const;
	PoolVector<Face3> get_faces() const;
#ifdef TOOLS_ENABLED
	const CachedStats &get_cached_stats() const;
#endif
	Ref<TriangleMesh> generate_triangle_mesh() const;
	Ref<TriangleMesh> generate_triangle_mesh_from_aabb() const;
	void generate_debug_mesh_lines(Vector<Vector3> &r_lines);
	void generate_debug_mesh_indices(Vector<Vector3> &r_points);

	Ref<Shape> create_trimesh_shape() const;
	Ref<Shape> create_convex_shape(bool p_clean = true, bool p_simplify = false) const;

	Ref<Mesh> create_outline(float p_margin) const;

	virtual AABB get_aabb() const = 0;<--- Virtual function in base class
	virtual void set_storage_mode(StorageMode p_storage_mode);<--- Virtual function in base class

	void set_lightmap_size_hint(const Vector2 &p_size);
	Size2 get_lightmap_size_hint() const;
	void clear_cache() const;

	typedef Vector<PoolVector<Vector3>> (*ConvexDecompositionFunc)(const real_t *p_vertices, int p_vertex_count, const uint32_t *p_triangles, int p_triangle_count, int p_max_convex_hulls, Vector<PoolVector<uint32_t>> *r_convex_indices);

	static ConvexDecompositionFunc convex_decomposition_function;

	Vector<Ref<Shape>> convex_decompose(int p_max_convex_hulls = -1) const;

	Mesh();

private:
#ifdef TOOLS_ENABLED
	// Only for use in the editor.
	// No need to bloat exports.
	mutable CachedStats _cached_stats;
#endif
};

class ArrayMesh : public Mesh {
	GDCLASS(ArrayMesh, Mesh);
	RES_BASE_EXTENSION("mesh");

private:
	// Storing the mesh data on CPU
	struct CPUSurface {
		Array arrays;
		Array blend_shapes;
		PrimitiveType primitive_type;
		int num_verts = 0;
		int num_inds = 0;
	};

	struct Surface {
		String name;
		AABB aabb;
		Ref<Material> material;
		bool is_2d;
		// Watch for bugs here.
		// When calling add_surface() rather than add_surface_from_arrays(),
		// the creation flags will be unset, and left at default.
		// Conversion from CPU to GPU memory assumes that creation_flags are
		// correct, so is only TRULY safe when used with add_surface_from_arrays().
		uint32_t creation_flags = ARRAY_COMPRESS_DEFAULT;
		uint32_t creation_format = 0;
	};
	Vector<Surface> surfaces;
	LocalVector<CPUSurface *> _cpu_surfaces;
	RID mesh;
	AABB aabb;
	BlendShapeMode blend_shape_mode;
	Vector<StringName> blend_shapes;
	AABB custom_aabb;

	void _recompute_aabb();

	// Data can be held on GPU, CPU or both.
	// CPU is quicker for modifications, but can't be
	// used for rendering.
	bool _on_cpu = false;
	bool _on_gpu = true;
	StorageMode _storage_mode = STORAGE_MODE_GPU;

	void add_surface_from_arrays_cpu(PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes);
	void add_surface_from_arrays_cpu_with_probe(PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes, uint32_t p_flags, int p_surface_id);
	void clear_cpu_surfaces();
	bool on_cpu() const { return _on_cpu && ((int)_cpu_surfaces.size() == surfaces.size()); }

protected:
	virtual bool _is_generated() const { return false; }

	bool _set(const StringName &p_name, const Variant &p_value);
	bool _get(const StringName &p_name, Variant &r_ret) const;
	void _get_property_list(List<PropertyInfo> *p_list) const;

	static void _bind_methods();

public:
	void add_surface_from_arrays(PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes = Array(), uint32_t p_flags = ARRAY_COMPRESS_DEFAULT);
	void add_surface(uint32_t p_format, PrimitiveType p_primitive, const PoolVector<uint8_t> &p_array, int p_vertex_count, const PoolVector<uint8_t> &p_index_array, int p_index_count, const AABB &p_aabb, const Vector<PoolVector<uint8_t>> &p_blend_shapes = Vector<PoolVector<uint8_t>>(), const Vector<AABB> &p_bone_aabbs = Vector<AABB>());

	Array surface_get_arrays(int p_surface) const;<--- Function in derived class
	Array surface_get_blend_shape_arrays(int p_surface) const;<--- Function in derived class

	void add_blend_shape(const StringName &p_name);
	int get_blend_shape_count() const;<--- Function in derived class
	StringName get_blend_shape_name(int p_index) const;<--- Function in derived class
	void set_blend_shape_name(int p_index, const StringName &p_name);<--- Function in derived class
	void clear_blend_shapes();

	void set_blend_shape_mode(BlendShapeMode p_mode);
	BlendShapeMode get_blend_shape_mode() const;

	void surface_update_region(int p_surface, int p_offset, const PoolVector<uint8_t> &p_data);

	int get_surface_count() const;<--- Function in derived class
	void surface_remove(int p_idx);
	void clear_surfaces();

	void surface_set_custom_aabb(int p_idx, const AABB &p_aabb); //only recognized by driver

	int surface_get_array_len(int p_idx) const;<--- Function in derived class
	int surface_get_array_index_len(int p_idx) const;<--- Function in derived class
	uint32_t surface_get_format(int p_idx) const;<--- Function in derived class
	PrimitiveType surface_get_primitive_type(int p_idx) const;<--- Function in derived class

	virtual void surface_set_material(int p_idx, const Ref<Material> &p_material);<--- Function in derived class
	virtual Ref<Material> surface_get_material(int p_idx) const;<--- Function in derived class

	int surface_find_by_name(const String &p_name) const;
	void surface_set_name(int p_idx, const String &p_name);
	String surface_get_name(int p_idx) const;

	void add_surface_from_mesh_data(const Geometry::MeshData &p_mesh_data);

	void set_custom_aabb(const AABB &p_custom);
	AABB get_custom_aabb() const;

	AABB get_aabb() const;<--- Function in derived class
	virtual RID get_rid() const;

	void regen_normalmaps();

	Error lightmap_unwrap(const Transform &p_base_transform = Transform(), float p_texel_size = 0.05);
	Error lightmap_unwrap_cached(int *&r_cache_data, unsigned int &r_cache_size, bool &r_used_cache, const Transform &p_base_transform = Transform(), float p_texel_size = 0.05);

	virtual void reload_from_file();

	virtual void set_storage_mode(StorageMode p_storage_mode);<--- Function in derived class

	ArrayMesh();

	~ArrayMesh();
};

VARIANT_ENUM_CAST(Mesh::ArrayType);
VARIANT_ENUM_CAST(Mesh::ArrayFormat);
VARIANT_ENUM_CAST(Mesh::PrimitiveType);
VARIANT_ENUM_CAST(Mesh::BlendShapeMode);

#endif // MESH_H