# Unity网格编程，写出自己想要的形状——（单面墙多门窗）

V0(1, 1, 0),
V1(-1, 1, 0),
V2(1, -1, 0),
V3(-1, -1, 0)

mesh.triangles = new int[2 * 3]{0, 3, 1, 　　0, 2, 3};

（暂时略过）

### 这里是一面墙抠2个以上的洞的情况

using UnityEngine;
public class MyMesh : MonoBehaviour {
public Material ma;
public Vector2[] uvs;
void Start ()
{
测试第二次不重复顶点();
}
public float 墙体高度 = 4;
public float 墙体长度 = 10;
/// <summary>
/// 门窗的位置和类型x为左侧位置 y为右侧位置  z为底部位置 w为顶部位置
/// </summary>
public Vector4[] 门窗;
public int factor;//UV缩放调整
/// <summary>
/// 根据新增门窗计算三角面和顶点数量
/// </summary>
void GetLength(out int san, out int ding)
{
san = 4; ding = 6;
if (门窗.Length == 0) return;
for (int i = 0; i < 门窗.Length; i++)
{
san += 8;
ding += 18;
}
}
void 测试第二次不重复顶点()
{
int nowSan = 0, nowDing = 0;//当前计算到的三角面和顶点
int san, ding;//三角面顶点总数
GetLength(out san, out ding);//计算下三角形数和顶点数
Mesh mesh = new Mesh();
Vector3[] vertices  = new Vector3[san];
int[] triangle = new int[ding];
Vector3 pos = this.transform.position;
//计算顶点和顶点排序
if (门窗.Length == 0)//没传门窗信息 则生成一个墙体
{
vertices[nowSan++] = new Vector3(pos.x, pos.y + 墙体高度, pos.z);
vertices[nowSan++] = pos;
vertices[nowSan++] = new Vector3(pos.x + 墙体长度, pos.y, pos.z);
vertices[nowSan++] = new Vector3(pos.x + 墙体长度, pos.y + 墙体高度, pos.z);
triangle = new int[2 * 3] { 3, 1,0, 1, 3, 2 };
}
else
{
//门窗数量大于1 先生成左侧两个
vertices[nowSan] = new Vector3(pos.x, pos.y + 墙体高度, pos.z);//0
vertices[nowSan+1] = pos;//1
vertices[nowSan+2] = new Vector3(pos.x + 门窗[0].x, pos.y, pos.z);//2
vertices[nowSan+3] = new Vector3(pos.x + 门窗[0].x, pos.y + 墙体高度, pos.z);//3
triangle[nowDing++] = 3; triangle[nowDing++] = 2; triangle[nowDing++] = 1;
triangle[nowDing++] = 1; triangle[nowDing++] = 0; triangle[nowDing++] = 3;
nowSan = 3;
for (int i = 0; i < 门窗.Length; i++)//再生成剩下的   循环赋值三角形和顶点信息
{
vertices[nowSan + 1] = new Vector3(pos.x + 门窗[i].y, pos.y, pos.z);                             //4      12
vertices[nowSan + 2] = new Vector3(pos.x + 门窗[i].y, pos.y + 门窗[i].z, pos.z);                 //5      13
vertices[nowSan + 3] = new Vector3(pos.x + 门窗[i].x, pos.y + 门窗[i].z, pos.z);                 //6      14
vertices[nowSan + 4] = new Vector3(pos.x + 门窗[i].x, pos.y + 门窗[i].w, pos.z);                 //7      15
vertices[nowSan + 5] = new Vector3(pos.x + 门窗[i].y, pos.y + 门窗[i].w, pos.z);                  //8      16
vertices[nowSan + 6] = new Vector3(pos.x + 门窗[i].y, pos.y + 墙体高度, pos.z);                   //9     17
vertices[nowSan + 7] = new Vector3(pos.x + ((i == 门窗.Length - 1) ? 墙体长度: 门窗[i+1].x)  , pos.y+ 墙体高度, pos.z);   //10     18
vertices[nowSan + 8] = new Vector3(pos.x + ((i == 门窗.Length - 1) ? 墙体长度: 门窗[i+1].x)  , pos.y , pos.z);           //11     19
//顶点顺序排序
triangle[nowDing++] = nowSan + (i == 0 ? -1 : 0); triangle[nowDing++] = nowSan + 2; triangle[nowDing++] = nowSan + 1;
triangle[nowDing++] = nowSan + (i == 0 ? -1 : 0); triangle[nowDing++] = nowSan + 3; triangle[nowDing++] = nowSan + 2;
triangle[nowDing++] = nowSan + 4; triangle[nowDing++] = nowSan + 6; triangle[nowDing++] = nowSan + 5;
triangle[nowDing++] = nowSan + 4; triangle[nowDing++] = nowSan + (i == 0 ? 0 : -1); triangle[nowDing++] = nowSan + 6;
triangle[nowDing++] = nowSan + 1; triangle[nowDing++] = nowSan + 6; triangle[nowDing++] = nowSan + 7;
triangle[nowDing++] = nowSan + 1; triangle[nowDing++] = nowSan + 7; triangle[nowDing++] = nowSan + 8;
nowSan += 8;
}
}
//UV计算
int iuv = 0, idx = 0;
Vector3[] uvv = new Vector3[san];
uvs = new Vector2[san];
uvv[0] = Vector3.zero;
for (int u = 1; u < san; u++)
uvv[u] = vertices[idx] - vertices[idx + u];
idx += 8;
uvs[iuv++] = Vector2.zero;
for (int u = 1; u < san; u++)
uvs[iuv++] = new Vector2(uvv[u].x, -uvv[u].y) * factor;
mesh.vertices = vertices;
mesh.triangles = triangle;
mesh.uv = uvs;
mesh.MarkDynamic();
mesh.RecalculateBounds();
mesh.RecalculateNormals();
render.material = ma;
filter.mesh = mesh;
}
}


...无奈 竟然有人复制了我的博客....这里

08-18 474
07-12
03-14 4094
07-05 5783
09-24 7624
09-03 4382
08-30 3792
01-08 5192
08-27 3027
01-25
08-18