using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TownsMesh
{
// Use this for initialization
public int row = 80;
public int column = 80;
public int n_row = 20;
public int n_column = 20;
int num = 16;
public Mesh[] tMesh;// = new Mesh[];
private Vector3[] s_Vertives;
private Vector2[] uv;
//private Vector2[] newUV;
Dictionary<int, Vector2[]> newUV = new Dictionary<int, Vector2[]>();
GameObject[] tPolygon;
//private
private int[] hasmesh;
private Material material;
public void DoCreatPloygonMesh(Vector3[] s_Vertives, Vector2[] uv, Vector3[] normals, Texture img, int z_row = 80, int z_column = 80, int N_row = 20, int N_column = 20)
{
row = z_row;
column = z_column;
n_row = N_row;
n_column = N_column;
num = (row / n_row) * (column / n_column);
tPolygon = new GameObject[num];
tMesh = new Mesh[num];
for (int i = 0; i < num; i++)
{
tPolygon[i] = new GameObject("tPolygon" + i);
tMesh[i] = new Mesh();
}
Dictionary<int, Vector3[]> tVertices = new Dictionary<int, Vector3[]>();
Dictionary<int, List<int>> tTriangles = new Dictionary<int, List<int>>();
for (int i = 0; i < num; i++)
{
tVertices[i] = new Vector3[n_row * n_column * 4];
newUV[i] = new Vector2[n_row * n_column * 4];
tTriangles[i] = new List<int>();
}
//Dictionary<int, List<int>> tTriangles = new Dictionary<int, List<int>>();
for (int i = 0; i < num; i++)
{
tPolygon[i].AddComponent<MeshFilter>();
tPolygon[i].AddComponent<MeshRenderer>();
//tPolygon[i].GetComponent<Renderer>().material.shader = Shader.Find("Sprites/Default");
if(material==null)
{
material = new Material(Shader.Find("Sprites/Default"));
material.mainTexture = img;
}
tPolygon[i].GetComponent<Renderer>().material = material;
//tPolygon[i].GetComponent<Renderer>().material.mainTexture = img;
}
for (int i = 0; i < s_Vertives.Length - 2; i++)
{
int n = i / 4;
int c_row = n % row;
int c_column = n / column;
int r_row = c_row / n_row;
int r_column = c_column / n_column;
int w_id = r_column * (row / n_row) + r_row;
if (i % 4 == 0)
{
int k_row = c_row % n_row;
int k_column = c_column % n_column;
int k = k_column * n_column + k_row;
tTriangles[w_id].Add(0 + k * 4);
tTriangles[w_id].Add(1 + k * 4);
tTriangles[w_id].Add(2 + k * 4);
tTriangles[w_id].Add(2 + k * 4);
tTriangles[w_id].Add(3 + k * 4);
tTriangles[w_id].Add(0 + k * 4);
tVertices[w_id][k * 4] = s_Vertives[i];
tVertices[w_id][k * 4 + 1] = s_Vertives[i + 1];
tVertices[w_id][k * 4 + 2] = s_Vertives[i + 2];
tVertices[w_id][k * 4 + 3] = s_Vertives[i + 3];
newUV[w_id][k * 4] = uv[i];
newUV[w_id][k * 4 + 1] = uv[i + 1];
newUV[w_id][k * 4 + 2] = uv[i + 2];
newUV[w_id][k * 4 + 3] = uv[i + 3];
}
}
for (int i = 0; i < num; i++)
{
tMesh[i].vertices = tVertices[i];
//赋值三角形点排序
tMesh[i].triangles = tTriangles[i].ToArray();
//tMesh.normals = normals;
//tMesh.normals = normals;
tMesh[i].uv = newUV[i];
//重新设置UV,法线
tMesh[i].RecalculateBounds();
tMesh[i].RecalculateNormals();
//将绘制好的Mesh赋值
tPolygon[i].GetComponent<MeshFilter>().mesh = tMesh[i];
}
}
public void SetMap(int id, Sprite sprite, bool fx)
{
float width = sprite.texture.width;
float height = sprite.texture.height;
int c_row = id % row;
int c_column = id / column;
int r_row = c_row / n_row;
int r_column = c_column / n_column;
int w_id = r_column * (row / n_row) + r_row;
int k_row = c_row % n_row;
int k_column = c_column % n_column;
int k = k_column * n_column + k_row;
//newUV[id*4] = new Vector2(sprite.rect.x / width, (sprite.rect.y + sprite.rect.height) / height)
if (fx)
{
newUV[w_id][k * 4] = new Vector2(sprite.rect.x / width, (sprite.rect.y + sprite.rect.height) / height);
newUV[w_id][k * 4 + 1] = new Vector2((sprite.rect.x + sprite.rect.width) / width, (sprite.rect.y + sprite.rect.height) / height);
newUV[w_id][k * 4 + 2] = new Vector2((sprite.rect.x + sprite.rect.width) / width, sprite.rect.y / height);
newUV[w_id][k * 4 + 3] = new Vector2(sprite.rect.x / width, sprite.rect.y / height);
}
else
{
newUV[w_id][k * 4 + 1] = new Vector2(sprite.rect.x / width, (sprite.rect.y + sprite.rect.height) / height);
newUV[w_id][k * 4 + 0] = new Vector2((sprite.rect.x + sprite.rect.width) / width, (sprite.rect.y + sprite.rect.height) / height);
newUV[w_id][k * 4 + 3] = new Vector2((sprite.rect.x + sprite.rect.width) / width, sprite.rect.y / height);
newUV[w_id][k * 4 + 2] = new Vector2(sprite.rect.x / width, sprite.rect.y / height);
}
//tMesh.RecalculateBounds();
//tMesh.RecalculateNormals();
tMesh[w_id].uv = newUV[w_id];
}
public void SetMap2(int id)
{
int c_row = id % row;
int c_column = id / column;
int r_row = c_row / n_row;
int r_column = c_column / n_column;
int w_id = r_column * (row / n_row) + r_row;
int k_row = c_row % n_row;
int k_column = c_column % n_column;
int k = k_column * n_column + k_row;
newUV[w_id][k * 4] = new Vector2(0, 0);
newUV[w_id][k * 4 + 1] = new Vector2(0, 0);
newUV[w_id][k * 4 + 2] = new Vector2(0, 0);
newUV[w_id][k * 4 + 3] = new Vector2(0, 0);
//tMesh.RecalculateBounds();
//tMesh.RecalculateNormals();
tMesh[w_id].uv = newUV[w_id];
}
public void SetParent(Transform parent)
{
for (int i = 0; i < num; i++)
{
tPolygon[i].transform.SetParent(parent);
tPolygon[i].layer = parent.gameObject.layer;
tPolygon[i].transform.localPosition = Vector3.zero;
}
}
}
private void Creatmash()
{
float p_y = 0.0001f;
float p_x = 0.0002f;
if (tCreatMesh == null)
{
tCreatMesh = new TownsMesh();
Vector3[] s_Vertives = new Vector3[(int)mapSize.x * (int)mapSize.y * 4];
Vector2[] uv = new Vector2[(int)mapSize.x * (int)mapSize.y * 4];
Vector3[] normals = new Vector3[(int)mapSize.x * (int)mapSize.y * 4];
for (int row = 0; row < mapSize.x; row++)
{
for (int column = 0; column < mapSize.y; column++)
{
var px2 = row * tileSizeX * 0.01f - (column) * tileSizeX * 0.01f;
var py2 = column * (0.01f * tileSizeY) + row * (0.01f * tileSizeY);
float depth = column + row + row * p_y + p_x * column;
Vector3 pos = new Vector3(px2, py2, depth);
int id = (int)((column * mapSize.x) + row);
s_Vertives[id * 4] = new Vector3(-1.0f, 1, 0) + pos;
s_Vertives[id * 4 + 1] = new Vector3(1, 1, 0) + pos;
s_Vertives[id * 4 + 2] = new Vector3(1, 0, 0) + pos;
s_Vertives[id * 4 + 3] = new Vector3(-1.0f, 0, 0) + pos;
uv[id * 4] = new Vector2(0, 0);
uv[id * 4 + 1] = new Vector2(0, 0);
uv[id * 4 + 2] = new Vector2(0, 0);
uv[id * 4 + 3] = new Vector2(0, 0);
}
}
tCreatMesh.DoCreatPloygonMesh(s_Vertives, uv, normals, autotile.bitmaskSprites[0].texture, (int)mapSize.x, (int)mapSize.y,9,9);
tCreatMesh.SetParent(transform);
}
}