UV issues creating a mesh

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 :slight_smile: 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?

320214–11292–$planetbuilder_104.cs (6.56 KB)

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.

So Ive re-factored my code a bit but am still having issues with texture mapping my mesh. You can see some test here:

http://justinzaun.com/unity/Test.html
http://justinzaun.com/unity/Test%20-%20No%20SubDivide.html

The code is online as well:
http://justinzaun.com/unity/PlanetBuilder.cs
(It needs to go into an Editor folder and puts a new menu item under GameObject menu.)

Maybe I’m thinking about this the wrong way? Is there a better texture format I should be using rather than the standard rectangle?

Are there any examples on the net on how to texturemap a Octahedron?

It would be nice to be able to map a “standard” planet texture to my sphere, but if I can get anything to map properly I would be ecstatic.

My ultimate goal would be to automatically generate a texture, so as long as I know the format of the texture to be mapped I think it will work out.