using UnityEngine;
using System.Collections; public class Quad : MonoBehaviour
{
// Use this for initialization
void Start () {
CreateQuad ();
} private float m_width = ;
private float m_length = ; public void CreateQuad()
{
/* 1. 顶点,三角形,法线,uv坐标, 绝对必要的部分只有顶点和三角形。
如果模型中不需要场景中的光照,那么就不需要法线。如果模型不需要贴材质,那么就不需要UV */
Vector3[] vertices = new Vector3[];
Vector3[] normals = new Vector3[];
Vector2[] uv = new Vector2[]; vertices [] = new Vector3 (, , );
uv [] = new Vector2 (, );
normals [] = Vector3.up; vertices [] = new Vector3 (, , m_length);
uv [] = new Vector2 (, );
normals [] = Vector3.up; vertices [] = new Vector3 (m_width, , m_length);
uv [] = new Vector2 (, );
normals [] = Vector3.up; vertices [] = new Vector3 (m_width, , );
uv [] = new Vector2 (, );
normals [] = Vector3.up; /*2. 三角形,顶点索引:
三角形是由3个整数确定的,各个整数就是角的顶点的index。 各个三角形的顶点的顺序通常由下往上数, 可以是顺时针也可以是逆时针,这通常取决于我们从哪个方向看三角形。 通常,当mesh渲染时,"逆时针" 的面会被挡掉。 我们希望保证顺时针的面与法线的主向一致 */
int[] indices = new int[];
indices [] = ;
indices [] = ;
indices [] = ; indices [] = ;
indices [] = ;
indices [] = ; Mesh mesh = new Mesh ();
mesh.vertices = vertices;
mesh.normals = normals;
mesh.uv = uv;
mesh.triangles = indices;
mesh.RecalculateBounds (); MeshFilter filter = this.gameObject.AddComponent<MeshFilter> ();
if(filter != null)
{
filter.sharedMesh = mesh;
} MeshRenderer meshRender = this.gameObject.AddComponent<MeshRenderer> ();
Shader shader = Shader.Find ("Diffuse");
meshRender.sharedMaterial = new Material (shader);
}
}