+1 vote

To long story short, I'm looking into different methods to spread grass over a large terrain in an automatic manner (not me placing them).

I was thinking that if I could get the terrain mesh data, I could somehow go trough all the faces and place new objects at those positions, but cant find a way to do this. Is it possible at all?

Any other suggestions on how to go about this problem? :)

in Engine by (289 points)

I want to do the exact same thing. Did you make your way through this?

1 Answer

+1 vote
Best answer

I think you can do that with MeshDataTool, a helper class that you build from a mesh to access its attributes (vertices, UVs etc) http://docs.godotengine.org/en/stable/classes/class_meshdatatool.html?highlight=MeshDataTool

I never used this and its not well documented, but from what I see, it doesn't look complicated to use.
This is a guess on how to use it:

func do_stuff_with_mesh(mesh):
    var tool = MeshDataTool.new()

    # Initialize the tool from the mesh and a surface index.
    # Meshes contain surfaces, which in turn contain vertices.
    # I guess you want the first surface, but keep that in mind
    # in case you have multi-material meshes
    tool.create_from_surface(mesh, 0)

    # Then iterate all vertices and whatever
    for i in range(0, tool.get_vertex_count()):
        var position = tool.get_vertex(i)
        # ...

    # Delete the tool once you're done with it, because it only inherits Object
    # (or use .clear() if you want to re-use it for another mesh)
    tool.free()

(not tested)

by (27,778 points)
selected by

How did I miss this? I was looking at the surface tool, but it was only for building new stuff, not reading what was already there.

Thanks a lot :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.