- Home /
code generated cube uv map for each sub mesh
so i have built a cube using code (rather then spawning in from 3d modelling like maya or using the primitives unity has)
i understand how to actually set the uvs
1 2,6 5
0 3,7 4 < these correspond to the vertices as if you saw them on a uv map i.e 0 = new vector(0,0) << first one in array the numbers show where they are in the uv map.
BUvs = new Vector2[8] {
new Vector2(0,0),new Vector2(0,1),
new Vector2(0.5f,1),new Vector2(0.5f,0),
new Vector2(1,1),new Vector2(1,0),
new Vector2(0.5f,0),new Vector2(0.5f,1)
};
anyway the textures... look.... ok i guess but realistically
is there a way to assign submeshes to different uv sets?
i.e each face on the cube is a sub mesh with its own texture but if i could assign front and back face to a uv and right and left to uv2 so on then it would make it look a lot better
Answer by aldonaletto · Oct 31, 2013 at 11:31 PM
As weird as it may sound, you actually need 24 vertices to make a cube. That's because uvs and normals are vertex attributes: the normals are different for each face, and the uvs are also different in many vertices. The normals are the worst case: each face must have its own 4 vertices, thus you need 6 x 4 vertices in order to accomodate all the different normals.
not at all you need 8 , now to do what i want to do i might need that many im not sure yet thats why im asking
you can use 8 because of the way the normals work in unity and the way trigs are done,
when you generate a mesh in unity with code you define where the normals point by the way you set the trig i.e 0,1,2 will have different normals then 2,1,0 (the opposite normals)so on so forth
which is all you need with for a cube now with uvs i was hoping there was a way around it because you can have more then one set of uvs for a mesh, i was hoping you might be able to have uvs for submeshes
You're confusing winding order with normals: the winding order defines which's the front face, while the normals are vertex attributes used for lighting purposes: the shader must know each vertex's normal in order to calculate how the light reflects on the surface. If you don't assign normals, all shaders except the unlit ones will complain that "Shader wants normals, but mesh doesn't have them".
Submeshes don't have different uv arrays: there's only one common uv array for all submeshes in the mesh, as well as only one vertices array, one normals array etc. Each submesh has its own triangles array, which picks vertices (and uvs, and normals etc.) from the common array. You could have a submesh for each face, but this would still result in 24 vertices (and 24 uvs, 24 normals etc.), and in a higher cost since each submesh has its own material. Actually, the only reason for using submeshes is the possibility of "painting" each submesh with a different material - but a single mesh with 24 vertices and a single material is way more efficient.
sorry i had to type fast and im not good at explaining,
so your saying the only way i can get equal uvs on each side of the cube is by making each face have seperate vertices....
You must use separate vertices whenever the other attributes are different. If ou had only to worry about uvs, you could even share some vertices where the uvs are equal (what dependes on how the texture is mapped), but the normals always require 24 vertices in a cube because they differ at the corners - exactly where the vertex position is the same for 3 faces. In the end, making a cube with 24 vertices is the best alternative: you're free to assign the right uv to each corner, and also to correctly define the normals (don't underestimate normals: a mesh with wrong normals looks terribly ugly when illu$$anonymous$$ated).
Your answer
Follow this Question
Related Questions
Automatic UV Mapping for a Cube in C# 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
[C#] how to add a primitive in this script??? 3 Answers
Find extreme points of rotated gameObjects? (screenshot example) 2 Answers