• Unity3D之Mesh(三)绘制四边形


    前言:

    由於之前的基本介紹,所以有關的知識點不做贅述,只上案例,知識作爲自己做試驗的記錄,便於日後查看。


    步驟:

    1、創建一個empty 的gameobject;

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

    具體脚本如下:

     1 using UnityEngine;
     2 using System.Collections;
     3 [RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
     4 public class quad : MonoBehaviour
     5 {
     6     /*
     7     creat a triangle by using Mesh 
     8      2016/11/21
     9                  ————Carl    
    10     */
    11     void Start()
    12     {
    13         creatQuad();
    14     }
    15 
    16     public float q_length = 3;
    17     public float q_width = 2;
    18 
    19     private void creatQuad()
    20     {
    21         /* 1. 顶点,三角形,法线,uv坐标, 绝对必要的部分只有顶点和三角形。  
    22               如果模型中不需要场景中的光照,那么就不需要法线。如果模型不需要贴材质,那么就不需要UV */
    23         Vector3[] vertices =
    24         {
    25            new Vector3(0,0,0),
    26            new Vector3(0,0,q_width),
    27            new Vector3(q_length,0,q_width),
    28            new Vector3(q_length,0,0),
    29         };
    30 
    31         Vector3[] normals =
    32         {
    33             Vector3.up,
    34             Vector3.up,
    35             Vector3.up,
    36             Vector3.up
    37         };
    38 
    39         Vector2[] uv =
    40         {
    41             Vector2.zero,
    42             -Vector2.left,
    43             Vector2.one,
    44             Vector2.right
    45         };
    46         /*2. 三角形,顶点索引:  
    47          三角形是由3个整数确定的,各个整数就是角的顶点的index。 各个三角形的顶点的顺序通常由下往上数, 可以是顺时针也可以是逆时针,这通常取决于我们从哪个方向看三角形。 通常,当mesh渲染时,"逆时针" 的面会被挡掉。 我们希望保证顺时针的面与法线的主向一致 */
    48         int[] indices = new int[6];
    49         indices[0] = 0;
    50         indices[1] = 1;
    51         indices[2] = 2;
    52 
    53         indices[3] = 0;
    54         indices[4] = 2;
    55         indices[5] = 3;
    56 
    57         Mesh mesh = new Mesh();
    58         mesh.vertices = vertices;
    59         mesh.normals = normals;
    60         mesh.uv = uv;
    61         mesh.triangles = indices;
    62 
    63         MeshFilter meshfilter = this.gameObject.GetComponent<MeshFilter>();
    64         meshfilter.mesh = mesh;
    65     }
    66 
    67 }

    程序結構説明:

    creatQuad()函數實現所有功能,由Start()函數調用。

    效果圖:

    一個細節知識備注:

     顶点,三角形,法线,uv坐标, 绝对必要的部分只有顶点和三角形。  
     如果模型中不需要场景中的光照,那么就不需要法线。如果模型不需要贴材质,那么就不需要UV 。

    
    

    【欢迎转载】

     转载请表明出处: 乐学习




  • 相关阅读:
    Excel函数和公式——Excel基础(8)
    Excel函数和公式——Excel基础(7)
    条件格式与公式——Excel基础(6)
    日期函数——Excel基础(5)
    Excel导入邮件合并——Excel基础(4)
    VScode与ssh
    python绘图相关知识点
    Python加密成.so或dll
    Pytorch从本地获取数据集
    Pytorch_3.3_ 线性回归的简洁实现
  • 原文地址:https://www.cnblogs.com/JLZT1223/p/6085739.html
Copyright © 2020-2023  润新知