绘制正方体:
用mesh自带的函数绘制:
//创建一个mesh网格
Mesh mesh = new Mesh();
//添加网格过滤器
gameObject.AddComponent<MeshFilter>();
//给网格添加顶点
mesh.vertices = new Vector3[]
{
new Vector3(0,0,0),new Vector3(0,1,0),new Vector3(1,0,0),new Vector3(1,1,0),
new Vector3(0,0,1),new Vector3(0,1,1),new Vector3(1,0,1),new Vector3(1,1,1),
};
//为网格添加绘制顺序 3个顶点绘制1个三角形
mesh.triangles = new int[]
{
0,1,2, 3,2,1,//正面
2,3,6, 3,7,6,//右侧
6,7,4, 7,5,4,//后面
4,5,0, 5,1,0,//左侧
1,5,3, 5,7,3,//顶部
4,0,2, 2,6,4 //底部
};
//自动计算法线
mesh.RecalculateNormals();
//网格过滤器赋值
GetComponent<MeshFilter>().mesh = mesh;
上面就是简单的创建了Cube
下面的算是完整的Cube
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class cube : MonoBehaviour
{
public Texture texture;//纹理
// Start is called before the first frame update
void Start()
{
//添加网格碰撞器
gameObject.AddComponent<MeshCollider>();
//添加网格渲染器
gameObject.AddComponent<MeshRenderer>();
//添加网格过滤器
gameObject.AddComponent<MeshFilter>();
//创建一个mesh网格
Mesh mesh = new Mesh();
VertexHelper vh = new VertexHelper();
//前面
vh.AddVert(new Vector3(0, 0, 0), Color.black, Vector2.zero);
vh.AddVert(new Vector3(0, 1, 0), Color.black, Vector2.up);
vh.AddVert(new Vector3(1, 0, 0), Color.black, Vector2.right);
vh.AddVert(new Vector3(1, 1, 0), Color.black, Vector2.one);
//右侧面
vh.AddVert(new Vector3(1, 0, 0), Color.black, Vector2.zero);
vh.AddVert(new Vector3(1, 1, 0), Color.black, Vector2.up);
vh.AddVert(new Vector3(1, 0, 1), Color.black, Vector2.right);
vh.AddVert(new Vector3(1, 1, 1), Color.black, Vector2.one);
//后面
vh.AddVert(new Vector3(1, 0, 1), Color.black, Vector2.zero);
vh.AddVert(new Vector3(1, 1, 1), Color.black, Vector2.up);
vh.AddVert(new Vector3(0, 0, 1), Color.black, Vector2.right);
vh.AddVert(new Vector3(0, 1, 1), Color.black, Vector2.one);
//左侧面
vh.AddVert(new Vector3(0, 0, 1), Color.black, Vector2.zero);
vh.AddVert(new Vector3(0, 1, 1), Color.black, Vector2.up);
vh.AddVert(new Vector3(0, 0, 0), Color.black, Vector2.right);
vh.AddVert(new Vector3(0, 1, 0), Color.black, Vector2.one);
//上面
vh.AddVert(new Vector3(0, 1, 0), Color.black, Vector2.zero);
vh.AddVert(new Vector3(0, 1, 1), Color.black, Vector2.up);
vh.AddVert(new Vector3(1, 1, 0), Color.black, Vector2.right);
vh.AddVert(new Vector3(1, 1, 1), Color.black, Vector2.one);
//下面
vh.AddVert(new Vector3(0, 0, 1), Color.black, Vector2.zero);
vh.AddVert(new Vector3(0, 0, 0), Color.black, Vector2.up);
vh.AddVert(new Vector3(1, 0, 1), Color.black, Vector2.right);
vh.AddVert(new Vector3(1, 0, 0), Color.black, Vector2.one);
//从哪里开始渲染
//vh.AddTriangle(0, 1, 3);
//vh.AddTriangle(0, 3, 2);
//添加顶点
for (int i = 0; i < 6; i++)
{
vh.AddTriangle(0 + i * 4, 1 + i * 4, 3 + i * 4);
vh.AddTriangle(0 + i * 4, 3 + i * 4, 2 + i * 4);
}
//填充网格
vh.FillMesh(mesh);
//绘制法线
mesh.RecalculateBounds();
//赋值给网格过滤器
GetComponent<MeshFilter>().mesh = mesh;
//创建一个材质球
Material material = new Material(Shader.Find("Standard"));
//赋纹理
material.mainTexture = texture;
GetComponent<MeshCollider>().sharedMesh = mesh;
//将材质赋给渲染器
GetComponent<MeshRenderer>().material = material;
}
// Update is called once per frame
void Update()
{
}
}