- Home /
Can you store arbitrary data in a Mesh?
I’m familiar enough with meshes that I know in addition to vertex data, they can store uv, normal, and even color data for each vertex in the mesh, so are there other kinds of data you can put into a mesh? For context I’ve been making procedurally generated meshes that serve as terrain for a map and am at the point where I want to start storing region data, like were trees should spawn, what color should the ground be, ect. So, I was wondering, one, could I store this information in the meshes themselves and two, if I can do this, is It a good idea?
Answer by madks13 · Aug 20, 2018 at 12:03 AM
No
No
Each class/struct in Unity serves only one purpose. As such, meshes only have data needed for proper displaying of said meshes.
It also is a bad idea because clumping all data together would make a big monolithic class/struct which would be hard to debug, maintain and evolve. In programming, you always want to separate a program in as small as possible, elementary parts that are easy to debug, maintain and evolve. Also, even if you could attach such complementary data, how would you use it? A mesh that represents terrain and placables at same time? That would make each mesh extremly complex and you yourself wouldn't be able to debug them.
I suggest you separate your generation in layers of detail. For example :
General topography : terrain heigh levels
Detailed topography : adding more details to the terrain heights (is it jagged? lean? how each chunk connects?...)
General details : which biome to use where
Detailed details : custom parameters for each biome based on previous generations
General placeables : where to put trees? houses? loot? enemies?
Detailed placeables : what type of tree? enemy? how big/small each unit?...
With this layered system you would be able to have as much or as few details as you want on any individual part/chunk/mesh you want.
After explaining it like that my original idea does seem rather foolish, so thanks for giving a detailed answer with a better approach to my problem
$$anonymous$$eep in $$anonymous$$d the last part is a simple example to illustrate my explanation. You don't have to have as many, or as few, layers of it. The layered system can have as many or as few as needed. The one thing to keep in $$anonymous$$d is that you will need to properly seprarate the generation to be able to parallelize it, since it generaly adds generation time.
That said, it only applies if you are continually generating. If you generate everything at the begining, i don't think users will $$anonymous$$d waiting a few seconds more for it to finish.
Answer by trapazza · Dec 08, 2020 at 11:28 PM
@FxWolfxe Actually yes (not for some of the purposes you mentioned though) but it's indeed a common technique used as a way to feed per-vertex data to a shader.
As an example, the Color property is seldom used in shading and it gives you 4 floats for free to store whatever you want. In my case, I use the red and green channels to store the uv coords to a secondary texture, and the blue and alpha channels to store the normalized terrain height and the blend factor between the two main textures.
Your answer
Follow this Question
Related Questions
Materials on an imported mesh 1 Answer
Missing/Replaced Unity default mesh Assets? 1 Answer
Mesh between lines - Mesh.SetIndices 1 Answer
Different prefabs using same mesh. Any performance gain? 1 Answer
Mesh coloring isnt working 1 Answer