zoukankan      html  css  js  c++  java
  • Unity3D之Mesh(四)绘制多边形

    来自https://www.cnblogs.com/JLZT1223/p/6086191.html

    1、

    总的来说绘制平面的思想十分简单,就是将需要的平面拆分成几个三角形然后进行绘制就可以啦,主要的思路就在于三角形的拆分。如果说一个平面有7个顶点,我们把它们分别编号0到6,拆分情况如图所示:

    即:如果用n来表示顶点的个数,那么在同一个平面内,可以分割的三角形个数是:n-2;

    2、

    在这里,我们选择应用Mesh Filter组件来进行绘制,Mesh Filter组件里的Mesh属性就是我们这次主要操作的对象,在这里,我们用到:

      mesh.vertices数组 和 mesh.triangles数组,第一个是vector3的数组,第二个是int的数组。

      其中mesh.vertices存储的就是平面的顶点信息,对应上图就是0到6号这六个点的坐标。

      mesh.triangles存储的是平面绘制时,绘制三角形的顶点顺序,对应上图应该是:

      061 651 521 542 432(顺时针)

      每三个一组代表一个三角形,但是大家在这里要注意一下,就是最终绘制出的小三角形是单向图,就是一面可以看到,另一面是看不到的,所以,为了保证所有的小三角形朝向一至,要对mesh.triangles数组在进行调整,调整结果如下:

      016 156 125 245 234(逆时针)

      就是保证小三角形顶点都是按顺时针或者逆时针读取~大家想想就明白了~

      故:基本算法思想就是:

      入口参数:vector3[] vertices,储存平面定点信息,顶点需按顺序储存

      算法思想:从数组首尾向中间遍历,生成triangles顶点ID数组(下列代码中注释的部分)


    步驟:

    1、創建一個empty 的gameobject;

    2、添加一個脚本給這個game object;

    算法实现代码如下:

    复制代码
    using UnityEngine;
    using System.Collections;
    [RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
    public class quad : MonoBehaviour
    {
        /*
        creat a triangle by using Mesh 
         2016/11/21
                     ————Carl    
        */
        void Start()
        {
            creatPolygon();
        }
    
       
        private void creatPolygon()
        {
            /* 1. 顶点,三角形,法线,uv坐标, 绝对必要的部分只有顶点和三角形。  
                  如果模型中不需要场景中的光照,那么就不需要法线。如果模型不需要贴材质,那么就不需要UV */
            Vector3[] vertices =
            {
             new Vector3 (2f,0,0),
             new Vector3(4f, 0, 0),
             new Vector3(6f, 0, 0),
             new Vector3(10f, 0, 0),
             new Vector3(10f, 20f, 0),
             new Vector3(6f,10f, 0),
             new Vector3(4f, 4f, 0)
            
            };
    
            Vector3[] normals =
            {
                Vector3.up,
                Vector3.up,
                Vector3.up,
                Vector3.up,
                Vector3.up,
                Vector3.up,
                Vector3.up
               
            };
    
            Vector2[] uv =
            {
                Vector2.zero,
                -Vector2.left,
                Vector2.one,
                Vector2.right,
                Vector2.zero,
                -Vector2.left,
                Vector2.one
              
            };
            /*2. 三角形,顶点索引:  
             三角形是由3个整数确定的,各个整数就是角的顶点的index。 各个三角形的顶点的顺序通常由下往上数, 可以是顺时针也可以是逆时针,这通常取决于我们从哪个方向看三角形。 通常,当mesh渲染时,"逆时针" 的面会被挡掉。 我们希望保证顺时针的面与法线的主向一致 */
            int[] indices = new int[15];
            indices[0] = 0;
            indices[1] = 6;
            indices[2] = 1;
    
            indices[3] = 6;
            indices[4] = 2;
            indices[5] = 1;
    
            indices[6] =6;
            indices[7] = 5;
            indices[8] = 2;
    
            indices[9] = 5;
            indices[10] = 4;
            indices[11] = 2;
    
            indices[12] = 4;
            indices[13] = 3;
            indices[14] = 2;
            //int numberOfTriangles = vertices.Length - 2;//三角形的数量等于顶点数减2
            //int[] indices = new int[numberOfTriangles * 3];//triangles数组大小等于三角形数量乘3 此时是15
            //int f = 0, b = vertices.Length - 1;//f记录前半部分遍历位置,b记录后半部分遍历位置 即0-7
            //for (int i = 1; i <= numberOfTriangles; i++)//每次给 triangles数组中的三个元素赋值,共赋值
            //{ //numberOfTriangles次
            //    if (i % 2 == 1)
            //    {
            //        indices[3 * i - 3] = f++;
            //        indices[3 * i - 2] = f;
            //        indices[3 * i - 1] = b;//正向赋值,对于i=1赋值为:0,1,2
            //    }
            //    else
            //    {
            //        indices[3 * i - 1] = b--;
            //        indices[3 * i - 2] = b;
            //        indices[3 * i - 3] = f;//逆向赋值,对于i=2赋值为:1,5,6
            //    }
    
            Mesh mesh = new Mesh();
                mesh.vertices = vertices;
                mesh.normals = normals;
                mesh.uv = uv;
                mesh.triangles = indices;
    
                MeshFilter meshfilter = this.gameObject.GetComponent<MeshFilter>();
                meshfilter.mesh = mesh;
            }
    
        }
    复制代码

    效果图:

  • 相关阅读:
    Tomcat日志、项目中的log4j日志、e.printStackTrace()——我的日志最后到底跑哪去了?
    MySQL中有关TIMESTAMP和DATETIME的总结
    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
    @RequestBody和@RequestParam区别
    Synchronized的jvm实现
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
  • 原文地址:https://www.cnblogs.com/slqt/p/10342458.html
Copyright © 2011-2022 走看看