Mesh系列文章 - 自定义Mesh

就是在做项目的过程中,有用到三角形的,今天就写一下如何自定义三角形?

        先截个图,让大家有个感性认识!    

        //引用

        using UnityEngine;
        using System.Collections;
        public class Draw : MonoBehaviour
       {

              //三个物体的变换
              public Transform a1;
              public Transform a2;
              public Transform a3;
              void Awake()
             {

                  //添加Mesh Filter(模型网格)和Mesh Renderer(模型渲染)组件
                  gameObject.AddComponent<MeshFilter>();
                  gameObject.AddComponent<MeshRenderer>();

                  //对模型的材质赋值个颜色
                  gameObject.renderer.material.color=Color.green;

                  //获取模型网格
                  Mesh mesh = GetComponent<MeshFilter>().mesh;

                  //清楚模型的顶点和三角形索引
                  mesh.Clear();

                  //变换三个物体的坐标到代码所挂物体上的局部坐标!
                  Vector3 v1 = gameObject.transform.InverseTransformPoint(a1.position);
                  Vector3 v2 = gameObject.transform.InverseTransformPoint(a2.position);
                  Vector3 v3 = gameObject.transform.InverseTransformPoint(a3.position);

                  //然后赋值顶点
                  mesh.vertices = new Vector3[] { v1, v2, v3 };

                 //赋值三角形的索引

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

posted @ 2013-07-24 16:55  马语者  阅读(1403)  评论(0编辑  收藏  举报