- Home /
How do I calculate the UV's for a procedurally generated shape?
I'm creating a mesh via script, but apparently I need UV's.
So two questions I guess: 1 - do I actually need UV's if I'm just using a material and not a texture?
and 2 - how do I procedurally generate the UV's for an irregular shape? I have a feeling this is a very complex problem but I just don't know...
Question one makes more sense rewritten: "using a material without a texture." Either the material has no texture slot, or you leave it blank (in that case you may need to make UV's, but they can all be 0's.)
Either way, the only purpose of UVs is to say where the different colors on a texture should go. So, no different texture colors, means no need for a UV.
So can I just have an empty UV array or should it be the same length and the triangle list but filled with zero's?
If it does get mad about not having UVs, then use all 0's the same length as the vertex array.
An empty texture slot is really filled in by Unity with a tiny white texture. That's why diffuse shader, no texture, color=red works.
Answer by Jeff-Kesselman · Jun 20, 2014 at 08:19 PM
1 - no you shouldn't, just be sure to use a shader that doesn't expect a texture and you should be fine
2 - yes arbitrarily mapping UVs to a polygonal shape is a somewhat complex, but well known problem . You need to "unwrap' the shape and make a (mostly) topologically equivalent plane of polygons from it.
See http://forum.devmaster.net/t/uvw-unwrap-algorithms/21409
Answer by arnebp · Oct 04, 2016 at 05:11 AM
The UnityEditor namespace holds functionality to easily unwrap a mesh and generate UVs via scripting.
Have a look at UnityEditor.Unwrapping.GeneratePerTriangleUV(...) and UnityEditor.Unwrapping.GenerateSecondaryUVSet(...)
Remember, that these can only be used in the Unity editor and not runtime in your app/game.
Your answer
Follow this Question
Related Questions
Editor lags when coming out of play mode...? 0 Answers
What is wrong with this mesh editing code? 0 Answers
Create planar UVs on procedurally generated mesh 1 Answer
Placing objects in world at vertices position? 0 Answers
Can and should a procedural mesh be shared between multiple GameObjects 1 Answer