@tool class_name PatchMesh extends PrimitiveMesh @export_category("Topology") @export_range(1, 32, 1) var num_cell : int = 1 : set(value) : num_cell = value request_update() @export_category("Transformation") @export var scale : Vector2 = Vector2(1., 1.) : set(value) : scale = value request_update() @export var rotation : Vector3 = Vector3(0., 0., 0.) : set(value) : rotation = value request_update() func _create_mesh_array() -> Array: var info : Array = [] info.resize(Mesh.ARRAY_MAX) # Patch goes from (-0.5, 0., -0.5) to (0.5, 0, 0.5) var progress : float = 1. / num_cell var vertices : PackedVector3Array = PackedVector3Array() vertices.resize((num_cell + 1) * (num_cell + 1)) var uvs : PackedVector2Array = PackedVector2Array() uvs.resize(vertices.size()) var transform : Transform3D = Transform3D.IDENTITY.rotated(Vector3(1, 0, 0), rotation.x).rotated(Vector3(0, 1, 0), rotation.y).rotated(Vector3(0, 1, 0), rotation.z).scaled(Vector3(scale.x, 1., scale.y)) var normal_transform : Transform3D = Transform3D.IDENTITY.rotated(Vector3(1, 0, 0), rotation.x).rotated(Vector3(0, 1, 0), rotation.y).rotated(Vector3(0, 1, 0), rotation.z) var normals : PackedVector3Array = PackedVector3Array() normals.resize(vertices.size()) normals.fill(Vector3(0., 1., 0.) * normal_transform) var size : Vector3 = Vector3(float(num_cell), 1., float(num_cell)) var i : int = 0 for z in range(num_cell + 1): for x in range(num_cell + 1): uvs[i] = Vector2(float(x) / float(num_cell), float(z) / float(num_cell)) vertices[i] = ((Vector3(float(x), 0., float(z)) / size) - Vector3(0.5, 0., 0.5)) * transform i = i + 1 var indices : PackedInt32Array = PackedInt32Array() indices.resize(num_cell * num_cell * 6) i = 0 for z in range(num_cell): var order : bool = z % 2 == 0 for x in range(num_cell): var idx : int = x * (num_cell + 1) + z if order: indices[i + 0] = idx indices[i + 1] = idx + num_cell + 2 indices[i + 2] = idx + num_cell + 1 indices[i + 3] = idx indices[i + 4] = idx + 1 indices[i + 5] = idx + num_cell + 2 else: indices[i + 0] = idx indices[i + 1] = idx + 1 indices[i + 2] = idx + num_cell + 1 indices[i + 3] = idx + 1 indices[i + 4] = idx + num_cell + 2 indices[i + 5] = idx + num_cell + 1 i += 6 order = !order info[Mesh.ARRAY_VERTEX] = vertices info[Mesh.ARRAY_NORMAL] = normals info[Mesh.ARRAY_TEX_UV] = uvs info[Mesh.ARRAY_INDEX] = indices return info
or share this direct link: