# UV mapping icosphere

Discussion in 'Scripting' started by DevYHeavy, Apr 28, 2016.

1. ### DevYHeavy

Joined:
Apr 28, 2016
Posts:
19
I'm creating an Icosphere with a script, it generates the sphere perfectly but i'm having trouble UV mapping it.
Using this code for the mapping
Code (CSharp):
1. Vector2[] uvs = new Vector2[mesh.vertices.Length];
2.
3. for (int i = 0; i < mesh.vertices.Length; i++) {
4.
5. uvs [i] = new Vector2 ((Mathf.Atan2 (mesh.vertices [i].z, mesh.vertices [i].x) / Mathf.PI / 2), (Mathf.Acos (mesh.vertices [i].y / radius) / Mathf.PI));
6.
7. }
I get an Icosphere that looks like this Joined:
Aug 14, 2012
Posts:
2,130
3. ### andSol

Joined:
May 8, 2016
Posts:
22
@hpjohn I am facing that exact same problem and to avoid duplicating the question I will first try asking you here directly (also bumping this question in the process). Your conceptual explanation is good, however I am finding it difficult to implement a proper solution to that.

My current code is slightly different from the OP:

Code (CSharp):
1.      Vector3[] nVertices = mesh.vertices;
2.         Vector2[] UVs = new Vector2[nVertices.Length];
3.
4.         for (var i = 0; i < nVertices.Length; i++)
5.         {
6.             var unitVector = nVertices[i].normalized;
7.             Vector2 ISOuv = new Vector2(0, 0);
8.             ISOuv.x = (Mathf.Atan2(unitVector.x, unitVector.z) + Mathf.PI) / Mathf.PI *0.5f;
9.             ISOuv.y = (Mathf.Acos(unitVector.y) + Mathf.PI) / Mathf.PI - 1;
10.             UVs[i] = new Vector2(ISOuv.x, ISOuv.y);
11.         }
12.         mesh.uv = UVs;

4. ### xKosta

Joined:
Oct 15, 2014
Posts:
22
@andSol, did you find any solution for that?

5. ### EastOfEden

Joined:
Nov 23, 2013
Posts:
1
A cheat/simple solution which works for boulders , asteroids and similar objects is to make sure that the U (or V - whichever you have the problem with) coordinate starts at 0.0 , goes to 1.0 and then back to 0.0.

The opposite side of the rock will have the same texture but ... no seam.
Still you will have polar problems and such.
Super cheaty version is to simply

... Mathf.Atan( x , Mathf.Abs(z) ) ... unityunity