I created a mesh of square ring, which has 16 vertices, then there were 8 square,
each square has two triangles.
Please check the picture attached.
I got right mesh, it’s a ring, but wrong image texture.
The image texture repeated a whole image at the end (top of the square ring).
static float w = 0.75f;
static float h = 1f;
static float t = 0.3f;
static float arc = 0.1f;
GameObject main;
GameObject front;
GameObject bottom;
GameObject side;
public static Mesh createMesh(GameObject obj) {
Mesh mesh = null;
MeshFilter meshFilter = obj.AddComponent<MeshFilter>();
mesh = new Mesh();
meshFilter.mesh = mesh;
MeshRenderer renderer = obj.AddComponent<MeshRenderer>();
Material mat = new Material(Shader.Find("Legacy Shaders/Transparent/Diffuse"));
mat.color = Color.white;
renderer.material = mat;
return mesh;
}
private void Start() {
Mesh meshSide = createMesh(side);
NativeList<JobHandle> jobHandleList = new NativeList<JobHandle>(Allocator.Temp);
int id = JobSideMeshStruct.getId();
MeshData data = new MeshData();
meshDataMap.Add(id, data);
JobSideMeshStruct jobCreateSide = new JobSideMeshStruct(id);
jobHandleList.Add(jobCreateSide.Schedule());
JobHandle.CompleteAll(jobHandleList);
jobHandleList.Dispose();
meshSide.vertices = data.vertices;
meshSide.uv = data.uv;
meshSide.triangles = data.triangles;
StartCoroutine(loadSidePicture());
}
private IEnumerator loadSidePicture() {
side.GetComponent<MeshRenderer>().material.mainTexture = Resources.Load("type/side") as Texture;
yield return null;
}
static Dictionary<int, MeshData> meshDataMap = new Dictionary<int, MeshData>();
//[BurstCompile]
public struct JobSideMeshStruct : IJob
{
const int vecterCount = 16;
static private int ID = 0;
static private object l = new object();
public static int getId() {
int i = 0;
lock(l) {
ID++;
i = ID;
}
return i;
}
private int id;
public JobSideMeshStruct(int i) {
id = i;
}
public void Execute()
{
/*byte[] arr = new byte[inStr.Length];
inStr.CopyTo(arr);
string s = new string(Encoding.ASCII.GetChars(arr));*/
MeshData data = meshDataMap[id];
Vector3[] vertices = new Vector3[vecterCount+2];
Vector2[] uv = new Vector2[vecterCount+2];
int i = 0;
vertices[0] = new Vector3(arc, h, 0);
vertices[2] = new Vector3(0, h - arc, 0);
vertices[4] = new Vector3(0, arc, 0);
vertices[6] = new Vector3(arc, 0, 0);
vertices[8] = new Vector3(w - arc, 0, 0);
vertices[10] = new Vector3(w, arc, 0);
vertices[12] = new Vector3(w, h - arc, 0);
vertices[14] = new Vector3(w - arc, h, 0);
for (i = 1; i < vecterCount; i += 2) {
vertices[i] = vertices[i - 1];
vertices[i].z = t;
}
float bevelSize = (float)Math.Sqrt(2 * arc * arc);
float _w = w - 2 * arc;
float _h = h - 2 * arc;
float y = 0;
float len = (float) ((_w + _h) * 2 + bevelSize * 4);
Debug.Log("side total len="+len+ ",bevelSize="+ bevelSize+ ",_w="+ _w+ ", _h="+ _h);
String log = "";
int step = 0;
float last = 0;
float cur = 0;
for (i = 0; i < 16; i++) {
uv[i++] = new Vector2(1, y/len);
uv[i] = new Vector2(0, y/len);
cur = (y * 200);
log += "step=" + step + ", y=" + (y / len)+", img y="+ cur + ",v="+(cur-last)+"\n";
last = cur;
if (step % 2 == 0)
y += bevelSize;
else
{
if (step % 4 == 1)
y += _h;
else
y += _w;
}
step++;
}
Debug.Log(log);
/* this got no use
vertices[16] = vertices[0];
vertices[17] = vertices[1];
uv[16] = uv[0];
uv[17] = uv[1];
*/
data.vertices = vertices;
data.uv = uv;
int[] triangles = new int[48];
i = 0;
int idx = 0;
for (i=0; i< vecterCount; i+=2) {
triangles[idx++] = i;
triangles[idx++] = i + 2;
triangles[idx++] = i + 1;
triangles[idx++] = i + 1;
triangles[idx++] = i + 2;
triangles[idx++] = i + 3;
//Debug.Log("idx="+idx+", i="+(i+3));
}
for (i=0; i< triangles.Length-2; i++) {
if (triangles[i] >= vecterCount)
{
Debug.LogError("triangles[" + i + "]=" + triangles[i]);
triangles[i] = triangles[i] % vecterCount;
}
}
data.triangles = triangles;
}
}
How to make the texture closed right?
May thanks for any help!