Procedural Planets and Asteroids?

Hello,
I am making a space-exploration game, and I cannot figure out how to generate unique meshes and colliders from code in a sphere/asteroid-like shape. I would prefer to use as few separate scripts as possible.

Note: I code in C#

Any help is appreciated,
-Carlpilot :slight_smile:

That’s a big subject :slight_smile: Try instantiating a sphere then applying a random variation to all the verts. There are a lot of discussions on procedural generation, usually using noise functions.

There is also a Unity example project for procedural generation

Adding to what tobyheadcast said:

I would just use this thingy from the asset store, it just looks sooo pretty!

If you really want to do it yourself, what I would do is use cellular automaton rules on a random set of points to create a pseudo-random shape.

Thank you, but there is one problem:
Each time a mesh is generated, there are these “cracks” between vertices.

Any idea on how to fix this?

-Carlpilot

Make your random variation smaller, start with something that makes just a slight unevenness to the surface.

Then look into noise functions to get more realistic results.

Even with a tiny variation, there are small cracks around the edges of what appears to be six planes. Is there a way to remove these? Also, is there a way to recalculate the collider based on the mesh?

I may be wrong, but when two triangles share a vertex, it’s not just one vertex. It’s two, but they both have the same coordinates. So if you move one without moving the other the same amount, you end up with gaps in the geometry. Just looping through all of the vertexes and randomly assigning new values isn’t sufficient. You have to assign the random values to all of the vertices that share a common coordinate.

or a quick search of the forums for “procedural generated planet” the #1 search result has some good results

Are you setting up the normals as well when creating the mesh? Make sure they point outwards. That might be why the mesh has “holes” in it.

That or you are actually generating the mesh with the vertices apart, in which case you need to fix your script :slight_smile: