I’m not sure if this is the correct place to ask this or not. If its not please forgive me…
I’ve been working on a script to create a sphere, that I can then deform. So far I can create the mesh of the sphere. The code is attached.
Much of this code I’ve gotten, read about or seen on the web in one form or another, but its working in Unity now My issue is that the UVs are all messed up and I’m not sure how to correct it. This is my first real stab at creating 3D meshes and I’m just stuck.
Any ideas how to fix the UVs so a texture (of say a planet like earth) would look correct on the mesh?
Remember that UVs need to be assigned in the 0-1 space. 0,0 is bottom-left and 1,1 is top-right. Your script is very complicated, so try to simplify what you are doing. Maybe begin by drawing only a simple plane with the following points and UVs:
0,0
1,0
0,1
1,1
Then expand it to be a 2x2 plane or 10x10 plane and see how that affects the UV mapping. The only way to learn this stuff is by doing!
Your script is working just fine. There is one simple caveat:
Vector3 vertex = new Vector3(p.x/length, p.y/length, p.z/length);
Vector2 uv = new Vector3(p.x/length, p.z/length);
here you basically map both top and bottom hemispheres into same uvs. For quick resolve just check p.y sign and move uv-s accordingly (just don’t forget to scale them ;-))
P.S. if you are really into it: that mapping is not that good if you’ll have objects viewed from more a single point - uvs gets really skewed near poles.