I have a plane and collision shape (convex polygon)
as you can see, my plane and collision shape has roughness. And i need to get all coordinates and rotation of each polygon.
I just want to make tree generation.
I have a plane and collision shape (convex polygon)
as you can see, my plane and collision shape has roughness. And i need to get all coordinates and rotation of each polygon.
I just want to make tree generation.
I just want to make tree generation.
Are you sure you don't want to query where the shape crosses specific vertical lines where you intent to put trees? Presumably, you could use raycast for that.
Anyway, that is not what you asked. You want all the coordinates.
Presumably your MeshInstance has an ArrayMesh (What I describe here won't work for primitive meshes), we can get it in a variable like this:
var plane:MeshInstance = $Plane
var mesh:ArrayMesh = plane.mesh
And then you are going to create a MeshDataTool and populate it from the ArrayMesh:
var mdt := MeshDataTool.new()
mdt.create_from_surface(mesh, 0) # surface 0 of the mesh
Then you can iterate over the vertex:
for vertex_index in range(mdt.get_vertex_count()):
var vertex:Vector3 = mdt.get_vertex(vertex_index)
print(vertex)
Yet, presumably, you are interested on the faces, so let us iterate over the faces instead:
for face_index in range(mdt.get_face_count()):
var A := mdt.get_vertex(mdt.get_face_vertex(face_index, 0))
var B := mdt.get_vertex(mdt.get_face_vertex(face_index, 1))
var C := mdt.get_vertex(mdt.get_face_vertex(face_index, 2))
print(A, ", ", B, ", ", C)
Be aware that you get the coordinates in model space. They were taken from the ArrayMesh, and thus are unaware of the transform applied to the MeshInstance that is using the ArrayMesh.
Oh, but that is not what you asked either. You want "all coordinates and rotation". Well, we can compute a normal for each face, it is cross product and normalize:
for face_index in range(mdt.get_face_count()):
var A := mdt.get_vertex(mdt.get_face_vertex(face_index, 0))
var B := mdt.get_vertex(mdt.get_face_vertex(face_index, 1))
var C := mdt.get_vertex(mdt.get_face_vertex(face_index, 2))
var normal := (B - A).cross(C - A).normalized()
print (normal)
Except, that could be upside down. I don't know. Do you need (B - A).cross(C - A) or (C - A).cross(B - A)? If I don't care, but I know the normal should be going to positive y, we can do this as quick fix:
normal *= sign(normal.y)
That ensures that the y is always greater or equal to 0. However, beware that if the y is 0, this will make the whole vector 0.
And now that we have a normal, we can figure out the rotation. First we need to know around which axis we need to rotate the not rotated normal (which I'm taking to be Vector3.UP) to get the normal:
var axis = normal.cross(Vector3.UP).normalize()
And we also need the angle:
var angle = Vector3.UP.signed_angle_to(normal, axis)
And with that you can create a transform that represents the rotation:
var transform = Transform.IDENTITY.rotated(axis, angle)