1 /**
2 Helper tool to access and edit $(D Mesh) data.
3 
4 Copyright:
5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur.  
6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md)  
7 Copyright (c) 2017-2018 Godot-D contributors  
8 
9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License)
10 
11 
12 */
13 module godot.meshdatatool;
14 import std.meta : AliasSeq, staticIndexOf;
15 import std.traits : Unqual;
16 import godot.d.traits;
17 import godot.core;
18 import godot.c;
19 import godot.d.bind;
20 import godot.d.reference;
21 import godot.globalenums;
22 import godot.object;
23 import godot.classdb;
24 import godot.reference;
25 import godot.arraymesh;
26 import godot.material;
27 /**
28 Helper tool to access and edit $(D Mesh) data.
29 
30 MeshDataTool provides access to individual vertices in a $(D Mesh). It allows users to read and edit vertex data of meshes. It also creates an array of faces and edges.
31 To use MeshDataTool, load a mesh with $(D createFromSurface). When you are finished editing the data commit the data to a mesh with $(D commitToSurface).
32 Below is an example of how MeshDataTool may be used.
33 
34 
35 var mesh = ArrayMesh.new()
36 mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, CubeMesh.new().get_mesh_arrays())
37 var mdt = MeshDataTool.new()
38 mdt.create_from_surface(mesh, 0)
39 for i in range(mdt.get_vertex_count()):
40     var vertex = mdt.get_vertex(i)
41     # In this example we extend the mesh by one unit, which results in separated faces as it is flat shaded.
42     vertex += mdt.get_vertex_normal(i)
43     # Save your change.
44     mdt.set_vertex(i, vertex)
45 mesh.surface_remove(0)
46 mdt.commit_to_surface(mesh)
47 var mi = MeshInstance.new()
48 mi.mesh = mesh
49 add_child(mi)
50 
51 
52 See also $(D ArrayMesh), $(D ImmediateGeometry) and $(D SurfaceTool) for procedural geometry generation.
53 $(B Note:) Godot uses clockwise $(D url=https://learnopengl.com/Advanced-OpenGL/Face-culling)winding order$(D /url) for front faces of triangle primitive modes.
54 */
55 @GodotBaseClass struct MeshDataTool
56 {
57 	package(godot) enum string _GODOT_internal_name = "MeshDataTool";
58 public:
59 @nogc nothrow:
60 	union { /** */ godot_object _godot_object; /** */ Reference _GODOT_base; }
61 	alias _GODOT_base this;
62 	alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses);
63 	package(godot) __gshared bool _classBindingInitialized = false;
64 	package(godot) static struct GDNativeClassBinding
65 	{
66 		__gshared:
67 		@GodotName("clear") GodotMethod!(void) clear;
68 		@GodotName("commit_to_surface") GodotMethod!(GodotError, ArrayMesh) commitToSurface;
69 		@GodotName("create_from_surface") GodotMethod!(GodotError, ArrayMesh, long) createFromSurface;
70 		@GodotName("get_edge_count") GodotMethod!(long) getEdgeCount;
71 		@GodotName("get_edge_faces") GodotMethod!(PoolIntArray, long) getEdgeFaces;
72 		@GodotName("get_edge_meta") GodotMethod!(Variant, long) getEdgeMeta;
73 		@GodotName("get_edge_vertex") GodotMethod!(long, long, long) getEdgeVertex;
74 		@GodotName("get_face_count") GodotMethod!(long) getFaceCount;
75 		@GodotName("get_face_edge") GodotMethod!(long, long, long) getFaceEdge;
76 		@GodotName("get_face_meta") GodotMethod!(Variant, long) getFaceMeta;
77 		@GodotName("get_face_normal") GodotMethod!(Vector3, long) getFaceNormal;
78 		@GodotName("get_face_vertex") GodotMethod!(long, long, long) getFaceVertex;
79 		@GodotName("get_format") GodotMethod!(long) getFormat;
80 		@GodotName("get_material") GodotMethod!(Material) getMaterial;
81 		@GodotName("get_vertex") GodotMethod!(Vector3, long) getVertex;
82 		@GodotName("get_vertex_bones") GodotMethod!(PoolIntArray, long) getVertexBones;
83 		@GodotName("get_vertex_color") GodotMethod!(Color, long) getVertexColor;
84 		@GodotName("get_vertex_count") GodotMethod!(long) getVertexCount;
85 		@GodotName("get_vertex_edges") GodotMethod!(PoolIntArray, long) getVertexEdges;
86 		@GodotName("get_vertex_faces") GodotMethod!(PoolIntArray, long) getVertexFaces;
87 		@GodotName("get_vertex_meta") GodotMethod!(Variant, long) getVertexMeta;
88 		@GodotName("get_vertex_normal") GodotMethod!(Vector3, long) getVertexNormal;
89 		@GodotName("get_vertex_tangent") GodotMethod!(Plane, long) getVertexTangent;
90 		@GodotName("get_vertex_uv") GodotMethod!(Vector2, long) getVertexUv;
91 		@GodotName("get_vertex_uv2") GodotMethod!(Vector2, long) getVertexUv2;
92 		@GodotName("get_vertex_weights") GodotMethod!(PoolRealArray, long) getVertexWeights;
93 		@GodotName("set_edge_meta") GodotMethod!(void, long, Variant) setEdgeMeta;
94 		@GodotName("set_face_meta") GodotMethod!(void, long, Variant) setFaceMeta;
95 		@GodotName("set_material") GodotMethod!(void, Material) setMaterial;
96 		@GodotName("set_vertex") GodotMethod!(void, long, Vector3) setVertex;
97 		@GodotName("set_vertex_bones") GodotMethod!(void, long, PoolIntArray) setVertexBones;
98 		@GodotName("set_vertex_color") GodotMethod!(void, long, Color) setVertexColor;
99 		@GodotName("set_vertex_meta") GodotMethod!(void, long, Variant) setVertexMeta;
100 		@GodotName("set_vertex_normal") GodotMethod!(void, long, Vector3) setVertexNormal;
101 		@GodotName("set_vertex_tangent") GodotMethod!(void, long, Plane) setVertexTangent;
102 		@GodotName("set_vertex_uv") GodotMethod!(void, long, Vector2) setVertexUv;
103 		@GodotName("set_vertex_uv2") GodotMethod!(void, long, Vector2) setVertexUv2;
104 		@GodotName("set_vertex_weights") GodotMethod!(void, long, PoolRealArray) setVertexWeights;
105 	}
106 	/// 
107 	pragma(inline, true) bool opEquals(in MeshDataTool other) const
108 	{ return _godot_object.ptr is other._godot_object.ptr; }
109 	/// 
110 	pragma(inline, true) typeof(null) opAssign(typeof(null) n)
111 	{ _godot_object.ptr = n; return null; }
112 	/// 
113 	pragma(inline, true) bool opEquals(typeof(null) n) const
114 	{ return _godot_object.ptr is n; }
115 	/// 
116 	size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; }
117 	mixin baseCasts;
118 	/// Construct a new instance of MeshDataTool.
119 	/// Note: use `memnew!MeshDataTool` instead.
120 	static MeshDataTool _new()
121 	{
122 		static godot_class_constructor constructor;
123 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("MeshDataTool");
124 		if(constructor is null) return typeof(this).init;
125 		return cast(MeshDataTool)(constructor());
126 	}
127 	@disable new(size_t s);
128 	/**
129 	Clears all data currently in MeshDataTool.
130 	*/
131 	void clear()
132 	{
133 		checkClassBinding!(typeof(this))();
134 		ptrcall!(void)(GDNativeClassBinding.clear, _godot_object);
135 	}
136 	/**
137 	Adds a new surface to specified $(D Mesh) with edited data.
138 	*/
139 	GodotError commitToSurface(ArrayMesh mesh)
140 	{
141 		checkClassBinding!(typeof(this))();
142 		return ptrcall!(GodotError)(GDNativeClassBinding.commitToSurface, _godot_object, mesh);
143 	}
144 	/**
145 	Uses specified surface of given $(D Mesh) to populate data for MeshDataTool.
146 	Requires $(D Mesh) with primitive type $(D constant Mesh.PRIMITIVE_TRIANGLES).
147 	*/
148 	GodotError createFromSurface(ArrayMesh mesh, in long surface)
149 	{
150 		checkClassBinding!(typeof(this))();
151 		return ptrcall!(GodotError)(GDNativeClassBinding.createFromSurface, _godot_object, mesh, surface);
152 	}
153 	/**
154 	Returns the number of edges in this $(D Mesh).
155 	*/
156 	long getEdgeCount() const
157 	{
158 		checkClassBinding!(typeof(this))();
159 		return ptrcall!(long)(GDNativeClassBinding.getEdgeCount, _godot_object);
160 	}
161 	/**
162 	Returns array of faces that touch given edge.
163 	*/
164 	PoolIntArray getEdgeFaces(in long idx) const
165 	{
166 		checkClassBinding!(typeof(this))();
167 		return ptrcall!(PoolIntArray)(GDNativeClassBinding.getEdgeFaces, _godot_object, idx);
168 	}
169 	/**
170 	Returns meta information assigned to given edge.
171 	*/
172 	Variant getEdgeMeta(in long idx) const
173 	{
174 		checkClassBinding!(typeof(this))();
175 		return ptrcall!(Variant)(GDNativeClassBinding.getEdgeMeta, _godot_object, idx);
176 	}
177 	/**
178 	Returns index of specified vertex connected to given edge.
179 	Vertex argument can only be 0 or 1 because edges are comprised of two vertices.
180 	*/
181 	long getEdgeVertex(in long idx, in long vertex) const
182 	{
183 		checkClassBinding!(typeof(this))();
184 		return ptrcall!(long)(GDNativeClassBinding.getEdgeVertex, _godot_object, idx, vertex);
185 	}
186 	/**
187 	Returns the number of faces in this $(D Mesh).
188 	*/
189 	long getFaceCount() const
190 	{
191 		checkClassBinding!(typeof(this))();
192 		return ptrcall!(long)(GDNativeClassBinding.getFaceCount, _godot_object);
193 	}
194 	/**
195 	Returns specified edge associated with given face.
196 	Edge argument must 2 or less because a face only has three edges.
197 	*/
198 	long getFaceEdge(in long idx, in long edge) const
199 	{
200 		checkClassBinding!(typeof(this))();
201 		return ptrcall!(long)(GDNativeClassBinding.getFaceEdge, _godot_object, idx, edge);
202 	}
203 	/**
204 	Returns the metadata associated with the given face.
205 	*/
206 	Variant getFaceMeta(in long idx) const
207 	{
208 		checkClassBinding!(typeof(this))();
209 		return ptrcall!(Variant)(GDNativeClassBinding.getFaceMeta, _godot_object, idx);
210 	}
211 	/**
212 	Calculates and returns the face normal of the given face.
213 	*/
214 	Vector3 getFaceNormal(in long idx) const
215 	{
216 		checkClassBinding!(typeof(this))();
217 		return ptrcall!(Vector3)(GDNativeClassBinding.getFaceNormal, _godot_object, idx);
218 	}
219 	/**
220 	Returns the specified vertex of the given face.
221 	Vertex argument must be 2 or less because faces contain three vertices.
222 	*/
223 	long getFaceVertex(in long idx, in long vertex) const
224 	{
225 		checkClassBinding!(typeof(this))();
226 		return ptrcall!(long)(GDNativeClassBinding.getFaceVertex, _godot_object, idx, vertex);
227 	}
228 	/**
229 	Returns the $(D Mesh)'s format. Format is an integer made up of $(D Mesh) format flags combined together. For example, a mesh containing both vertices and normals would return a format of `3` because $(D constant ArrayMesh.ARRAY_FORMAT_VERTEX) is `1` and $(D constant ArrayMesh.ARRAY_FORMAT_NORMAL) is `2`.
230 	See $(D ArrayMesh.arrayformat) for a list of format flags.
231 	*/
232 	long getFormat() const
233 	{
234 		checkClassBinding!(typeof(this))();
235 		return ptrcall!(long)(GDNativeClassBinding.getFormat, _godot_object);
236 	}
237 	/**
238 	Returns the material assigned to the $(D Mesh).
239 	*/
240 	Ref!Material getMaterial() const
241 	{
242 		checkClassBinding!(typeof(this))();
243 		return ptrcall!(Material)(GDNativeClassBinding.getMaterial, _godot_object);
244 	}
245 	/**
246 	Returns the vertex at given index.
247 	*/
248 	Vector3 getVertex(in long idx) const
249 	{
250 		checkClassBinding!(typeof(this))();
251 		return ptrcall!(Vector3)(GDNativeClassBinding.getVertex, _godot_object, idx);
252 	}
253 	/**
254 	Returns the bones of the given vertex.
255 	*/
256 	PoolIntArray getVertexBones(in long idx) const
257 	{
258 		checkClassBinding!(typeof(this))();
259 		return ptrcall!(PoolIntArray)(GDNativeClassBinding.getVertexBones, _godot_object, idx);
260 	}
261 	/**
262 	Returns the color of the given vertex.
263 	*/
264 	Color getVertexColor(in long idx) const
265 	{
266 		checkClassBinding!(typeof(this))();
267 		return ptrcall!(Color)(GDNativeClassBinding.getVertexColor, _godot_object, idx);
268 	}
269 	/**
270 	Returns the total number of vertices in $(D Mesh).
271 	*/
272 	long getVertexCount() const
273 	{
274 		checkClassBinding!(typeof(this))();
275 		return ptrcall!(long)(GDNativeClassBinding.getVertexCount, _godot_object);
276 	}
277 	/**
278 	Returns an array of edges that share the given vertex.
279 	*/
280 	PoolIntArray getVertexEdges(in long idx) const
281 	{
282 		checkClassBinding!(typeof(this))();
283 		return ptrcall!(PoolIntArray)(GDNativeClassBinding.getVertexEdges, _godot_object, idx);
284 	}
285 	/**
286 	Returns an array of faces that share the given vertex.
287 	*/
288 	PoolIntArray getVertexFaces(in long idx) const
289 	{
290 		checkClassBinding!(typeof(this))();
291 		return ptrcall!(PoolIntArray)(GDNativeClassBinding.getVertexFaces, _godot_object, idx);
292 	}
293 	/**
294 	Returns the metadata associated with the given vertex.
295 	*/
296 	Variant getVertexMeta(in long idx) const
297 	{
298 		checkClassBinding!(typeof(this))();
299 		return ptrcall!(Variant)(GDNativeClassBinding.getVertexMeta, _godot_object, idx);
300 	}
301 	/**
302 	Returns the normal of the given vertex.
303 	*/
304 	Vector3 getVertexNormal(in long idx) const
305 	{
306 		checkClassBinding!(typeof(this))();
307 		return ptrcall!(Vector3)(GDNativeClassBinding.getVertexNormal, _godot_object, idx);
308 	}
309 	/**
310 	Returns the tangent of the given vertex.
311 	*/
312 	Plane getVertexTangent(in long idx) const
313 	{
314 		checkClassBinding!(typeof(this))();
315 		return ptrcall!(Plane)(GDNativeClassBinding.getVertexTangent, _godot_object, idx);
316 	}
317 	/**
318 	Returns the UV of the given vertex.
319 	*/
320 	Vector2 getVertexUv(in long idx) const
321 	{
322 		checkClassBinding!(typeof(this))();
323 		return ptrcall!(Vector2)(GDNativeClassBinding.getVertexUv, _godot_object, idx);
324 	}
325 	/**
326 	Returns the UV2 of the given vertex.
327 	*/
328 	Vector2 getVertexUv2(in long idx) const
329 	{
330 		checkClassBinding!(typeof(this))();
331 		return ptrcall!(Vector2)(GDNativeClassBinding.getVertexUv2, _godot_object, idx);
332 	}
333 	/**
334 	Returns bone weights of the given vertex.
335 	*/
336 	PoolRealArray getVertexWeights(in long idx) const
337 	{
338 		checkClassBinding!(typeof(this))();
339 		return ptrcall!(PoolRealArray)(GDNativeClassBinding.getVertexWeights, _godot_object, idx);
340 	}
341 	/**
342 	Sets the metadata of the given edge.
343 	*/
344 	void setEdgeMeta(VariantArg1)(in long idx, in VariantArg1 meta)
345 	{
346 		checkClassBinding!(typeof(this))();
347 		ptrcall!(void)(GDNativeClassBinding.setEdgeMeta, _godot_object, idx, meta);
348 	}
349 	/**
350 	Sets the metadata of the given face.
351 	*/
352 	void setFaceMeta(VariantArg1)(in long idx, in VariantArg1 meta)
353 	{
354 		checkClassBinding!(typeof(this))();
355 		ptrcall!(void)(GDNativeClassBinding.setFaceMeta, _godot_object, idx, meta);
356 	}
357 	/**
358 	Sets the material to be used by newly-constructed $(D Mesh).
359 	*/
360 	void setMaterial(Material material)
361 	{
362 		checkClassBinding!(typeof(this))();
363 		ptrcall!(void)(GDNativeClassBinding.setMaterial, _godot_object, material);
364 	}
365 	/**
366 	Sets the position of the given vertex.
367 	*/
368 	void setVertex(in long idx, in Vector3 vertex)
369 	{
370 		checkClassBinding!(typeof(this))();
371 		ptrcall!(void)(GDNativeClassBinding.setVertex, _godot_object, idx, vertex);
372 	}
373 	/**
374 	Sets the bones of the given vertex.
375 	*/
376 	void setVertexBones(in long idx, in PoolIntArray bones)
377 	{
378 		checkClassBinding!(typeof(this))();
379 		ptrcall!(void)(GDNativeClassBinding.setVertexBones, _godot_object, idx, bones);
380 	}
381 	/**
382 	Sets the color of the given vertex.
383 	*/
384 	void setVertexColor(in long idx, in Color color)
385 	{
386 		checkClassBinding!(typeof(this))();
387 		ptrcall!(void)(GDNativeClassBinding.setVertexColor, _godot_object, idx, color);
388 	}
389 	/**
390 	Sets the metadata associated with the given vertex.
391 	*/
392 	void setVertexMeta(VariantArg1)(in long idx, in VariantArg1 meta)
393 	{
394 		checkClassBinding!(typeof(this))();
395 		ptrcall!(void)(GDNativeClassBinding.setVertexMeta, _godot_object, idx, meta);
396 	}
397 	/**
398 	Sets the normal of the given vertex.
399 	*/
400 	void setVertexNormal(in long idx, in Vector3 normal)
401 	{
402 		checkClassBinding!(typeof(this))();
403 		ptrcall!(void)(GDNativeClassBinding.setVertexNormal, _godot_object, idx, normal);
404 	}
405 	/**
406 	Sets the tangent of the given vertex.
407 	*/
408 	void setVertexTangent(in long idx, in Plane tangent)
409 	{
410 		checkClassBinding!(typeof(this))();
411 		ptrcall!(void)(GDNativeClassBinding.setVertexTangent, _godot_object, idx, tangent);
412 	}
413 	/**
414 	Sets the UV of the given vertex.
415 	*/
416 	void setVertexUv(in long idx, in Vector2 uv)
417 	{
418 		checkClassBinding!(typeof(this))();
419 		ptrcall!(void)(GDNativeClassBinding.setVertexUv, _godot_object, idx, uv);
420 	}
421 	/**
422 	Sets the UV2 of the given vertex.
423 	*/
424 	void setVertexUv2(in long idx, in Vector2 uv2)
425 	{
426 		checkClassBinding!(typeof(this))();
427 		ptrcall!(void)(GDNativeClassBinding.setVertexUv2, _godot_object, idx, uv2);
428 	}
429 	/**
430 	Sets the bone weights of the given vertex.
431 	*/
432 	void setVertexWeights(in long idx, in PoolRealArray weights)
433 	{
434 		checkClassBinding!(typeof(this))();
435 		ptrcall!(void)(GDNativeClassBinding.setVertexWeights, _godot_object, idx, weights);
436 	}
437 }