- Home /
Is it possible to save runtime generated mesh as a texture ?
Hi all,
I have bunch of quads organized in a grid 8x8. Each quad has one material assigned out of few available. I rotate each quad by random angle and combine their meshes in specific order. This results in rectangular mosaic with jagged edges, exactly what I want to have.
But this object with mesh combined in such way is just one of multiple similar objects I want to have in my scene. And by multiple I mean up to few thousands of them. Most of them cannot be manipulated by player until some specific conditions are met and they are just stacked around. So I guess it doesn't make much sense to generate let's say 5k of those objects just to have 4995 of them sit there and eat resources.
If I could somehow save each generated mesh as a texture I could just create 5k of simple quads, merge all those not accessible for player into one big mesh (or a few if I happen to hit 65k vertices limit) and rest would be kept as my jagged mosaic objects.
Next I would generate 5k - [number of interactive objects] of "mosaics", merge them all into an atlas and use that to texture my big mesh. With pooling I have already implemented that would be rather fast and not memory intensive operation. All objects that player can interact with would stay in scene and have their meshes, rest would be just look like them. And if some object should be interactive I would just regenerate my big mesh without that one piece.
I don't know if I'm explaining this properly so the bottom line is: is there any way to save mesh (or maybe I should say it's colors?) to texture? And I mean with code not with some editor tool.
Answer by robertbu · Oct 14, 2014 at 08:35 PM
You can get the pixels from the screen using either Application.CaptureScreensot() or Texture2D.ReadPixels(). If you have Pro, you can use a RenderTexture to capture what a camera sees.
But I have to question your approach. Given a texture atlas of your possible textures, and a mesh, you can present up to 16,000 quads efficiently. The only thing you save by capturing the pixels is a relatively low number of vertices/triangles.
Hmm, but those methods only work if object is actually displayed on the screen, right? In that case it's not what I'm looking for. I was hoping for sth like getting array of pixel values from mesh so I could literally save it to bmp file for example.
Anyway thanks for answer, those things might come in handy at some point.
Actually RendererTexture does not require the object to be seen. For the others, yes they need to be seen for 1 frame.
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
How to load and render a sprite in script? 1 Answer
How to apply a texture to a submesh 1 Answer
Apply material to imported mesh 3 Answers
Swapping out materials on a skinned mesh renderer? 3 Answers