I would appreciate any help that anyone could provide here.
I’ve included a very simplified version of the problem I am having.
I am creating meshes procedurally and the mesh itself forms correctly but I have a problem getting the UVs and textures to behave around the seam.
private var mesh : Mesh;
private var sphere : vsphere;
transform.rotation = Quaternion.identity;
class neighbor
{
var xv : int = 0;
var yv : int = 0;
function neighbor(i : int, j : int)
{
xv = i;
yv = j;
}
};
class sphereVertex
{
var hgt : float = 0.0;
var xv : int = 0;
var yv : int = 0;
var pos : int = 0;
var divisions : int = 40;
var hdivisions : int = 0;
var rot : Quaternion;
var neighbors : neighbor[] = new neighbor[9];
function sphereVertex(x : int, y : int, s : int, s2 : int,xdeg : float,ydeg : float)
{
xv = x;
yv = y;
divisions = s;
hdivisions = s2;
pos = (y * divisions) + x;
rot = Quaternion.Euler (ydeg,xdeg,0);
setNeighbors();
}
function setNeighbors()
{
var yval : int;
var yhval : int;
var ysval : int;
var xval : int;
var xhval : int;
var xsval : int;
xval = testValue(yv - 1,hdivisions,xv - 1,divisions);
xhval = testValue(yv, hdivisions,xv, divisions);
xsval = testValue(yv + 1,hdivisions,xv + 1,divisions);
yval = yv - 1;
yhval = yv;
ysval = yv+1;
neighbors[0] = new neighbor(xval,yval);
neighbors[1] = new neighbor(xval,yhval);
neighbors[2] = new neighbor(xval,ysval);
neighbors[3] = new neighbor(xhval,yval);
neighbors[4] = new neighbor(xhval,yhval);
neighbors[5] = new neighbor(xhval,ysval);
neighbors[6] = new neighbor(xsval,yval);
neighbors[7] = new neighbor(xsval,yhval);
neighbors[8] = new neighbor(xsval,ysval);
}
function testValue(val1 : int, div1 : int,val2 : int, div2 : int) : int
{
if(val2 < 0)
return(div2 - 1);
if(val2 >= div2)
return(0);
return(val2);
}
};
class vsphere
{
private var divisions : int;
public var tmatrix : Array;
var verts : Vector3[];
var uvs : Vector2[];
var uvScale : Vector2;
var triangles : Array;
var divs : int;
var div2 : int;
function vsphere(div : int, pole : boolean,fromFile : boolean)
{
var x : int;
var y : int;
var xdeg : float;
var ydeg : float;
var vtx : sphereVertex;
divs = div;
div2 = divs/2;
verts = new Vector3[divs * div2];
uvs = new Vector2[divs * div2];
uvScale = new Vector2(.9 / divs, .9 / div2);
divisions = divs;
tmatrix = new Array(divs);
pinchedSphere();
}
function pinchedSphere()
{
var x : int;
var y : int;
var deg = 360.0/divs;
// this is basically building a 20 x 40 grid and deforming to the shape of a sphere
for (x=0;x<40;x++)
{
tmatrix[x] = new Array(1);
xdeg = x * deg;
for (y=0; y < 20; y++)
{
ydeg = y * deg;
vtx = new sphereVertex(x,y,divs,div2,xdeg,ydeg);
vtx.hgt = 1.0;
tmatrix[x][y] = vtx;
verts[vtx.pos] = vtx.rot * Vector3(0,vtx.hgt,0);
uvs[vtx.pos] = Vector2.Scale(Vector2 (vtx.xv, vtx.yv), uvScale); //<---- this leaves a line of distortion through sphere along seam
}
}
//for (var i=0;i<verts.Length;i++)
// uvs[i] = Vector2 (verts[i].x, verts[i].z); //<--- this leaves larger areas of distortion around equator
}
function triangulate()
{
var x : int;
var y : int;
triangles = new Array();
var vtx : sphereVertex;
var nbrs : neighbor[];
var tri1 : int;
var tri2 : int;
var tri3 : int;
var tri4 : int;
for(x=0;x<divs;x++)
{
for (y=2; y < div2; y++)
{
vtx = tmatrix[x][y];
nbrs = vtx.neighbors;
tri1 = tmatrix[nbrs[0].xv][nbrs[0].yv].pos;
tri2 = tmatrix[nbrs[1].xv][nbrs[1].yv].pos;
tri3 = tmatrix[nbrs[4].xv][nbrs[4].yv].pos;
tri4 = tmatrix[nbrs[3].xv][nbrs[3].yv].pos;
triangles.Push(tri1,tri2,tri3);
triangles.Push(tri3,tri4,tri1);
}
}
}
};
function Start()
{
gameObject.AddComponent(MeshFilter);
mesh = GetComponent(MeshFilter).mesh;
sphere = new vsphere(40,false,false);
mesh.vertices = sphere.verts;
mesh.uv = sphere.uvs;
sphere.triangulate();
mesh.triangles = sphere.triangles;
mesh.RecalculateNormals();
mesh.Optimize();
GetComponent(MeshCollider).sharedMesh = mesh;
}




