1 /** 2 $(D Mesh) type that provides utility for constructing a surface from arrays. 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.arraymesh; 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.mesh; 25 /** 26 $(D Mesh) type that provides utility for constructing a surface from arrays. 27 28 The $(D ArrayMesh) is used to construct a $(D Mesh) by specifying the attributes as arrays. 29 The most basic example is the creation of a single triangle: 30 31 32 var vertices = PoolVector3Array() 33 vertices.push_back(Vector3(0, 1, 0)) 34 vertices.push_back(Vector3(1, 0, 0)) 35 vertices.push_back(Vector3(0, 0, 1)) 36 # Initialize the ArrayMesh. 37 var arr_mesh = ArrayMesh.new() 38 var arrays = [] 39 arrays.resize(ArrayMesh.ARRAY_MAX) 40 arrays$(D ArrayMesh.ARRAY_VERTEX) = vertices 41 # Create the Mesh. 42 arr_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays) 43 var m = MeshInstance.new() 44 m.mesh = arr_mesh 45 46 47 The $(D MeshInstance) is ready to be added to the $(D SceneTree) to be shown. 48 See also $(D ImmediateGeometry), $(D MeshDataTool) and $(D SurfaceTool) for procedural geometry generation. 49 $(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. 50 */ 51 @GodotBaseClass struct ArrayMesh 52 { 53 package(godot) enum string _GODOT_internal_name = "ArrayMesh"; 54 public: 55 @nogc nothrow: 56 union { /** */ godot_object _godot_object; /** */ Mesh _GODOT_base; } 57 alias _GODOT_base this; 58 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 59 package(godot) __gshared bool _classBindingInitialized = false; 60 package(godot) static struct GDNativeClassBinding 61 { 62 __gshared: 63 @GodotName("add_blend_shape") GodotMethod!(void, String) addBlendShape; 64 @GodotName("add_surface_from_arrays") GodotMethod!(void, long, Array, Array, long) addSurfaceFromArrays; 65 @GodotName("clear_blend_shapes") GodotMethod!(void) clearBlendShapes; 66 @GodotName("get_blend_shape_count") GodotMethod!(long) getBlendShapeCount; 67 @GodotName("get_blend_shape_mode") GodotMethod!(Mesh.BlendShapeMode) getBlendShapeMode; 68 @GodotName("get_blend_shape_name") GodotMethod!(String, long) getBlendShapeName; 69 @GodotName("get_custom_aabb") GodotMethod!(AABB) getCustomAabb; 70 @GodotName("lightmap_unwrap") GodotMethod!(GodotError, Transform, double) lightmapUnwrap; 71 @GodotName("regen_normalmaps") GodotMethod!(void) regenNormalmaps; 72 @GodotName("set_blend_shape_mode") GodotMethod!(void, long) setBlendShapeMode; 73 @GodotName("set_custom_aabb") GodotMethod!(void, AABB) setCustomAabb; 74 @GodotName("surface_find_by_name") GodotMethod!(long, String) surfaceFindByName; 75 @GodotName("surface_get_array_index_len") GodotMethod!(long, long) surfaceGetArrayIndexLen; 76 @GodotName("surface_get_array_len") GodotMethod!(long, long) surfaceGetArrayLen; 77 @GodotName("surface_get_format") GodotMethod!(long, long) surfaceGetFormat; 78 @GodotName("surface_get_name") GodotMethod!(String, long) surfaceGetName; 79 @GodotName("surface_get_primitive_type") GodotMethod!(Mesh.PrimitiveType, long) surfaceGetPrimitiveType; 80 @GodotName("surface_remove") GodotMethod!(void, long) surfaceRemove; 81 @GodotName("surface_set_name") GodotMethod!(void, long, String) surfaceSetName; 82 @GodotName("surface_update_region") GodotMethod!(void, long, long, PoolByteArray) surfaceUpdateRegion; 83 } 84 /// 85 pragma(inline, true) bool opEquals(in ArrayMesh other) const 86 { return _godot_object.ptr is other._godot_object.ptr; } 87 /// 88 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 89 { _godot_object.ptr = n; return null; } 90 /// 91 pragma(inline, true) bool opEquals(typeof(null) n) const 92 { return _godot_object.ptr is n; } 93 /// 94 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 95 mixin baseCasts; 96 /// Construct a new instance of ArrayMesh. 97 /// Note: use `memnew!ArrayMesh` instead. 98 static ArrayMesh _new() 99 { 100 static godot_class_constructor constructor; 101 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("ArrayMesh"); 102 if(constructor is null) return typeof(this).init; 103 return cast(ArrayMesh)(constructor()); 104 } 105 @disable new(size_t s); 106 /// 107 enum ArrayFormat : int 108 { 109 /** 110 Array format will include vertices (mandatory). 111 */ 112 arrayFormatVertex = 1, 113 /** 114 Array format will include normals. 115 */ 116 arrayFormatNormal = 2, 117 /** 118 Array format will include tangents. 119 */ 120 arrayFormatTangent = 4, 121 /** 122 Array format will include a color array. 123 */ 124 arrayFormatColor = 8, 125 /** 126 Array format will include UVs. 127 */ 128 arrayFormatTexUv = 16, 129 /** 130 Array format will include another set of UVs. 131 */ 132 arrayFormatTexUv2 = 32, 133 /** 134 Array format will include bone indices. 135 */ 136 arrayFormatBones = 64, 137 /** 138 Array format will include bone weights. 139 */ 140 arrayFormatWeights = 128, 141 /** 142 Index array will be used. 143 */ 144 arrayFormatIndex = 256, 145 } 146 /// 147 enum ArrayType : int 148 { 149 /** 150 $(D PoolVector3Array), $(D PoolVector2Array), or $(D Array) of vertex positions. 151 */ 152 arrayVertex = 0, 153 /** 154 $(D PoolVector3Array) of vertex normals. 155 */ 156 arrayNormal = 1, 157 /** 158 $(D PoolRealArray) of vertex tangents. Each element in groups of 4 floats, first 3 floats determine the tangent, and the last the binormal direction as -1 or 1. 159 */ 160 arrayTangent = 2, 161 /** 162 $(D PoolColorArray) of vertex colors. 163 */ 164 arrayColor = 3, 165 /** 166 $(D PoolVector2Array) for UV coordinates. 167 */ 168 arrayTexUv = 4, 169 /** 170 $(D PoolVector2Array) for second UV coordinates. 171 */ 172 arrayTexUv2 = 5, 173 /** 174 $(D PoolRealArray) or $(D PoolIntArray) of bone indices. Each element in groups of 4 floats. 175 */ 176 arrayBones = 6, 177 /** 178 $(D PoolRealArray) of bone weights. Each element in groups of 4 floats. 179 */ 180 arrayWeights = 7, 181 /** 182 $(D PoolIntArray) of integers used as indices referencing vertices, colors, normals, tangents, and textures. All of those arrays must have the same number of elements as the vertex array. No index can be beyond the vertex array size. When this index array is present, it puts the function into "index mode," where the index selects the *i*'th vertex, normal, tangent, color, UV, etc. This means if you want to have different normals or colors along an edge, you have to duplicate the vertices. 183 For triangles, the index array is interpreted as triples, referring to the vertices of each triangle. For lines, the index array is in pairs indicating the start and end of each line. 184 */ 185 arrayIndex = 8, 186 /** 187 Represents the size of the $(D arraytype) enum. 188 */ 189 arrayMax = 9, 190 } 191 /// 192 enum Constants : int 193 { 194 /** 195 Default value used for index_array_len when no indices are present. 196 */ 197 noIndexArray = -1, 198 arrayVertex = 0, 199 arrayNormal = 1, 200 arrayFormatVertex = 1, 201 arrayFormatNormal = 2, 202 arrayTangent = 2, 203 arrayColor = 3, 204 /** 205 Amount of weights/bone indices per vertex (always 4). 206 */ 207 arrayWeightsSize = 4, 208 arrayFormatTangent = 4, 209 arrayTexUv = 4, 210 arrayTexUv2 = 5, 211 arrayBones = 6, 212 arrayWeights = 7, 213 arrayIndex = 8, 214 arrayFormatColor = 8, 215 arrayMax = 9, 216 arrayFormatTexUv = 16, 217 arrayFormatTexUv2 = 32, 218 arrayFormatBones = 64, 219 arrayFormatWeights = 128, 220 arrayFormatIndex = 256, 221 } 222 /** 223 Adds name for a blend shape that will be added with $(D addSurfaceFromArrays). Must be called before surface is added. 224 */ 225 void addBlendShape(in String name) 226 { 227 checkClassBinding!(typeof(this))(); 228 ptrcall!(void)(GDNativeClassBinding.addBlendShape, _godot_object, name); 229 } 230 /** 231 Creates a new surface. 232 Surfaces are created to be rendered using a `primitive`, which may be any of the types defined in $(D Mesh.primitivetype). (As a note, when using indices, it is recommended to only use points, lines, or triangles.) $(D Mesh.getSurfaceCount) will become the `surf_idx` for this new surface. 233 The `arrays` argument is an array of arrays. See $(D arraytype) for the values used in this array. For example, `arrays$(D 0)` is the array of vertices. That first vertex sub-array is always required; the others are optional. Adding an index array puts this function into "index mode" where the vertex and other arrays become the sources of data and the index array defines the vertex order. All sub-arrays must have the same length as the vertex array or be empty, except for $(D constant ARRAY_INDEX) if it is used. 234 */ 235 void addSurfaceFromArrays(in long primitive, in Array arrays, in Array blend_shapes = Array.make(), in long compress_flags = 97280) 236 { 237 checkClassBinding!(typeof(this))(); 238 ptrcall!(void)(GDNativeClassBinding.addSurfaceFromArrays, _godot_object, primitive, arrays, blend_shapes, compress_flags); 239 } 240 /** 241 Removes all blend shapes from this $(D ArrayMesh). 242 */ 243 void clearBlendShapes() 244 { 245 checkClassBinding!(typeof(this))(); 246 ptrcall!(void)(GDNativeClassBinding.clearBlendShapes, _godot_object); 247 } 248 /** 249 Returns the number of blend shapes that the $(D ArrayMesh) holds. 250 */ 251 long getBlendShapeCount() const 252 { 253 checkClassBinding!(typeof(this))(); 254 return ptrcall!(long)(GDNativeClassBinding.getBlendShapeCount, _godot_object); 255 } 256 /** 257 258 */ 259 Mesh.BlendShapeMode getBlendShapeMode() const 260 { 261 checkClassBinding!(typeof(this))(); 262 return ptrcall!(Mesh.BlendShapeMode)(GDNativeClassBinding.getBlendShapeMode, _godot_object); 263 } 264 /** 265 Returns the name of the blend shape at this index. 266 */ 267 String getBlendShapeName(in long index) const 268 { 269 checkClassBinding!(typeof(this))(); 270 return ptrcall!(String)(GDNativeClassBinding.getBlendShapeName, _godot_object, index); 271 } 272 /** 273 274 */ 275 AABB getCustomAabb() const 276 { 277 checkClassBinding!(typeof(this))(); 278 return ptrcall!(AABB)(GDNativeClassBinding.getCustomAabb, _godot_object); 279 } 280 /** 281 Will perform a UV unwrap on the $(D ArrayMesh) to prepare the mesh for lightmapping. 282 */ 283 GodotError lightmapUnwrap(in Transform transform, in double texel_size) 284 { 285 checkClassBinding!(typeof(this))(); 286 return ptrcall!(GodotError)(GDNativeClassBinding.lightmapUnwrap, _godot_object, transform, texel_size); 287 } 288 /** 289 Will regenerate normal maps for the $(D ArrayMesh). 290 */ 291 void regenNormalmaps() 292 { 293 checkClassBinding!(typeof(this))(); 294 ptrcall!(void)(GDNativeClassBinding.regenNormalmaps, _godot_object); 295 } 296 /** 297 298 */ 299 void setBlendShapeMode(in long mode) 300 { 301 checkClassBinding!(typeof(this))(); 302 ptrcall!(void)(GDNativeClassBinding.setBlendShapeMode, _godot_object, mode); 303 } 304 /** 305 306 */ 307 void setCustomAabb(in AABB aabb) 308 { 309 checkClassBinding!(typeof(this))(); 310 ptrcall!(void)(GDNativeClassBinding.setCustomAabb, _godot_object, aabb); 311 } 312 /** 313 Returns the index of the first surface with this name held within this $(D ArrayMesh). If none are found, -1 is returned. 314 */ 315 long surfaceFindByName(in String name) const 316 { 317 checkClassBinding!(typeof(this))(); 318 return ptrcall!(long)(GDNativeClassBinding.surfaceFindByName, _godot_object, name); 319 } 320 /** 321 Returns the length in indices of the index array in the requested surface (see $(D addSurfaceFromArrays)). 322 */ 323 long surfaceGetArrayIndexLen(in long surf_idx) const 324 { 325 checkClassBinding!(typeof(this))(); 326 return ptrcall!(long)(GDNativeClassBinding.surfaceGetArrayIndexLen, _godot_object, surf_idx); 327 } 328 /** 329 Returns the length in vertices of the vertex array in the requested surface (see $(D addSurfaceFromArrays)). 330 */ 331 long surfaceGetArrayLen(in long surf_idx) const 332 { 333 checkClassBinding!(typeof(this))(); 334 return ptrcall!(long)(GDNativeClassBinding.surfaceGetArrayLen, _godot_object, surf_idx); 335 } 336 /** 337 Returns the format mask of the requested surface (see $(D addSurfaceFromArrays)). 338 */ 339 long surfaceGetFormat(in long surf_idx) const 340 { 341 checkClassBinding!(typeof(this))(); 342 return ptrcall!(long)(GDNativeClassBinding.surfaceGetFormat, _godot_object, surf_idx); 343 } 344 /** 345 Gets the name assigned to this surface. 346 */ 347 String surfaceGetName(in long surf_idx) const 348 { 349 checkClassBinding!(typeof(this))(); 350 return ptrcall!(String)(GDNativeClassBinding.surfaceGetName, _godot_object, surf_idx); 351 } 352 /** 353 Returns the primitive type of the requested surface (see $(D addSurfaceFromArrays)). 354 */ 355 Mesh.PrimitiveType surfaceGetPrimitiveType(in long surf_idx) const 356 { 357 checkClassBinding!(typeof(this))(); 358 return ptrcall!(Mesh.PrimitiveType)(GDNativeClassBinding.surfaceGetPrimitiveType, _godot_object, surf_idx); 359 } 360 /** 361 Removes a surface at position `surf_idx`, shifting greater surfaces one `surf_idx` slot down. 362 */ 363 void surfaceRemove(in long surf_idx) 364 { 365 checkClassBinding!(typeof(this))(); 366 ptrcall!(void)(GDNativeClassBinding.surfaceRemove, _godot_object, surf_idx); 367 } 368 /** 369 Sets a name for a given surface. 370 */ 371 void surfaceSetName(in long surf_idx, in String name) 372 { 373 checkClassBinding!(typeof(this))(); 374 ptrcall!(void)(GDNativeClassBinding.surfaceSetName, _godot_object, surf_idx, name); 375 } 376 /** 377 Updates a specified region of mesh arrays on the GPU. 378 $(B Warning:) Only use if you know what you are doing. You can easily cause crashes by calling this function with improper arguments. 379 */ 380 void surfaceUpdateRegion(in long surf_idx, in long offset, in PoolByteArray data) 381 { 382 checkClassBinding!(typeof(this))(); 383 ptrcall!(void)(GDNativeClassBinding.surfaceUpdateRegion, _godot_object, surf_idx, offset, data); 384 } 385 /** 386 Sets the blend shape mode to one of $(D Mesh.blendshapemode). 387 */ 388 @property Mesh.BlendShapeMode blendShapeMode() 389 { 390 return getBlendShapeMode(); 391 } 392 /// ditto 393 @property void blendShapeMode(long v) 394 { 395 setBlendShapeMode(v); 396 } 397 /** 398 Overrides the $(D AABB) with one defined by user for use with frustum culling. Especially useful to avoid unexpected culling when using a shader to offset vertices. 399 */ 400 @property AABB customAabb() 401 { 402 return getCustomAabb(); 403 } 404 /// ditto 405 @property void customAabb(AABB v) 406 { 407 setCustomAabb(v); 408 } 409 }