How to generate a Collider Mesh from an image ?

Hi uniters !

I’m here to ask for help because I’m struggling on something that may be easy for many of you.

I want to give the opportunity to import our own map in the game as a PNG image.

Then I would like to convert it to a playable terrain by just applying it to a cube (this part is ok).
And i need to generate a dynamic mesh collider that correspond to the visible part of the image, to allow a player going trough theses spaces.

Do you have any clues on how I can achieve this, while still using the internal physics system ?

Thanks a lot for your help !

No ideas ? :slight_smile:

Well, a PNG only has colour information, so on its own isn’t going to do the trick. You’re going to either have to make an assumption about what colours represent collision area, or you’re going to have to supply additional data somehow, or you’re going to have to rely on edge detection filters or similar. Without knowing the type of image you wish to use we really can’t help there.

Anyway, from there you’ll need to do some image processing to identify in image space the collidable areas. Then convert those to 3D and use them to construct colliders. Again, without knowing what kind of map we’re talking about, it’s hard to give advice. You could trace edges and extrude to create mesh colliders, or you could fill areas with primitives. Either way there are algorithms that will help.

Ok, thanks for your answer.
I’ll link you a few exemple of how a map looks :

1366316--68364--$24143.jpg

I’m not really sure where to start.
All I have now, is a plane with a mesh collider.
I added a material with an unlit transparent shader and the map is the texture.

If I analyze my map, pixel by pixel and store in bi-dimensional array, if a pixel is collidable or not, from this grid, will it be possible to generate a mesh collider ?

Does today’s daily deal do what you want?

This is what I need, certainly, but I would like to achieve the same kind of result with help and searching, instead of paying for this (but it looks great, I don’t deny it !).

Then you should expect to spend a lot of time learning the algorithm behind such a technique, and then a lot of time actually implementing it. I haven’t used that asset yet, but the video is practically magic.

Yeah $8 is well worth it.

You might be able to achieve what you want by making parts and just parent them together.

As i’m making a 2d game, what I could do is scan for each pixel of my map and store playable pix in an array.
Then as 1 unit = 1 pixel, I could create a cube on each playable pixel for the collision.

Then after that I guess it will be okay, but very heavy to load. It will need optimization.

Except this, I don’t know how I could do ^^'.

Maybe just wait for 4.3

You are on the right track! To get pixeldata read this: Unity - Scripting API: Texture2D.GetPixel …that is the simple part, next part is more challenging: Unity - Manual: Using meshes with C# scripts

Check out Procedural Examples from demo projects: http://unity3d.com/gallery/demos/demo-projects …there is some mesh generation.

Thanks alot !
I’ll try to do it with the informations given.
If it’ll get easier with 4.3, then I’ll modify the concerned things.

Thanks for your help everyone, can’t fail now :wink: !

Watch the keynote vids, it get’s done automatically

You’re heading in the right direction here. Since it’s cubes, you could probably optimise ~80% of them out by finding fully covered areas and replacing them with a single larger cube. There are algorithms for this which you should be able to find with a bit of a search, too.

sorry to revive an old post, but anyone succeeded to achieve that kind of thing? I’m into the same kind of problem and I don’t know where to start.