最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity中 mesh生成斜坡的示例代碼

 更新時(shí)間:2021年05月28日 14:44:23   作者:nanyang0310  
Mesh是指模型的網(wǎng)格,3D模型是由多邊形拼接而成,而多邊形實(shí)際上是由多個(gè)三角形拼接而成的,今天通過(guò)本文給大家介紹Unity中 mesh生成斜坡功能,感興趣的朋友一起看看吧

Mesh概念:

Mesh是Unity中的一個(gè)組件,稱為網(wǎng)格組件。通俗的講,Mesh是指模型的網(wǎng)格,3D模型是由多邊形拼接而成,而多邊形實(shí)際上是由多個(gè)三角形拼接而成的。所以一個(gè)3D模型的表面其實(shí)是由多個(gè)彼此相連的三角面構(gòu)成。三維空間中,構(gòu)成這些三角形的點(diǎn)和邊的集合就是Mesh。

Mesh組成:

1、頂點(diǎn)坐標(biāo)數(shù)組vertexes

2、頂點(diǎn)在uv坐標(biāo)系中的位置信息數(shù)組uvs

3、三角形頂點(diǎn)順時(shí)針或者逆時(shí)針?biāo)饕龜?shù)組triangles

4、MeshFiler組件,用于增加mesh屬性

5、MeshRender組件,增加材質(zhì)并渲染出來(lái)。

6、可能還需要每個(gè)頂點(diǎn)的法線的數(shù)組normals

/*
/// 功能:
/// 時(shí)間:
/// 版本:
*/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeshCreater : MonoBehaviour
{

    public Vector3 eulerAngles;

    [Header("斜坡尺寸")]
    public float sizeX;
    public float sizeY;
    public float sizeZ;

    [Header("斜坡后面的立方體尺寸")]
    public float planeSize;
    public float m_angle;
    public Material material;

    Vector3[] vertices = new Vector3[54];
    Vector2[] uvs = new Vector2[54];
    List<int> allTris = new List<int>();

    Vector3[] allPoint;

    GameObject gameObject;


    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            DrawMesh(m_angle);
        }
    }

    public void DrawMesh(float angle)
    {
        if (gameObject != null)
        {
            Destroy(gameObject);
        }
        sizeX = sizeY * Mathf.Tan(angle * Mathf.PI / 180);

        gameObject = new GameObject("Cube");
        gameObject.transform.eulerAngles = eulerAngles;

        var mf = gameObject.AddComponent<MeshFilter>();
        var mr = gameObject.AddComponent<MeshRenderer>();

        allPoint = new[]
        {
            //斜坡
             new Vector3(0, 0, 0),
             new Vector3(0, 0, sizeZ),
             new Vector3(sizeX, 0, sizeZ),
             new Vector3(sizeX, 0, 0),
             new Vector3(0, sizeY, 0),
             new Vector3(0, sizeY, sizeZ),

             //斜坡后的立方體
             new Vector3(0,-planeSize,0),
             new Vector3(sizeX,-planeSize,0),
             new Vector3(sizeX,-planeSize,sizeZ),
             new Vector3(0,-planeSize,sizeZ),
        };

        //斜坡
        vertices[0] = allPoint[0];  //正面
        vertices[1] = allPoint[4];
        vertices[2] = allPoint[3];


        vertices[3] = allPoint[0]; //底面1
        vertices[4] = allPoint[3];
        vertices[5] = allPoint[2];

        vertices[6] = allPoint[0];//底面1
        vertices[7] = allPoint[2];
        vertices[8] = allPoint[1];

        vertices[9] = allPoint[1];  //背面1
        vertices[10] = allPoint[4];
        vertices[11] = allPoint[0];

        vertices[12] = allPoint[4];//背面2
        vertices[13] = allPoint[1];
        vertices[14] = allPoint[5];

        vertices[15] = allPoint[5]; //反面
        vertices[16] = allPoint[1];
        vertices[17] = allPoint[2];

        vertices[18] = allPoint[2]; //斜坡1
        vertices[19] = allPoint[3];
        vertices[20] = allPoint[4];

        vertices[21] = allPoint[2]; //斜坡1
        vertices[22] = allPoint[4];
        vertices[23] = allPoint[5];


        //斜坡后的立方體
        vertices[24] = allPoint[6];
        vertices[25] = allPoint[0];
        vertices[26] = allPoint[3];

        vertices[27] = allPoint[6];
        vertices[28] = allPoint[3];
        vertices[29] = allPoint[7];

        vertices[30] = allPoint[6];
        vertices[31] = allPoint[9];
        vertices[32] = allPoint[0];

        vertices[33] = allPoint[0];
        vertices[34] = allPoint[9];
        vertices[35] = allPoint[1];

        vertices[36] = allPoint[7];
        vertices[37] = allPoint[3];
        vertices[38] = allPoint[8];

        vertices[39] = allPoint[8];
        vertices[40] = allPoint[3];
        vertices[41] = allPoint[2];

        vertices[42] = allPoint[7];
        vertices[43] = allPoint[8];
        vertices[44] = allPoint[9];

        vertices[45] = allPoint[7];
        vertices[46] = allPoint[9];
        vertices[47] = allPoint[6];

        vertices[48] = allPoint[1];
        vertices[49] = allPoint[9];
        vertices[50] = allPoint[8];

        vertices[51] = allPoint[1];
        vertices[52] = allPoint[8];
        vertices[53] = allPoint[2];


        for (int i = 0; i < vertices.Length; i++)
        {
            allTris.Add(i);
        }

        uvs[0] = new Vector2(0, 0);
        uvs[1] = new Vector2(0, 1);
        uvs[2] = new Vector2(1, 0);

        uvs[3] = new Vector2(0, 0);
        uvs[4] = new Vector2(1, 0);
        uvs[5] = new Vector2(1, 1);

        uvs[6] = new Vector2(0f, 0f);
        uvs[7] = new Vector2(1f, 1f);
        uvs[8] = new Vector2(0f, 1f);

        uvs[9] = new Vector2(0, 1);
        uvs[10] = new Vector2(1, 0);
        uvs[11] = new Vector2(0, 0);

        uvs[12] = new Vector2(1, 0);
        uvs[13] = new Vector2(0, 1);
        uvs[14] = new Vector2(1, 1);

        uvs[15] = new Vector2(0f, 1f);
        uvs[16] = new Vector2(0, 0);
        uvs[17] = new Vector2(1, 0);

        uvs[18] = new Vector2(1f, 1f);
        uvs[19] = new Vector2(1f, 0f);
        uvs[20] = new Vector2(0f, 0f);

        uvs[21] = new Vector2(1, 1);
        uvs[22] = new Vector2(0, 0);
        uvs[23] = new Vector2(0, 1);


        uvs[24] = new Vector2(1, 1);
        uvs[25] = new Vector2(0, 0);
        uvs[26] = new Vector2(0, 1);

        uvs[27] = new Vector2(1, 1);
        uvs[28] = new Vector2(0, 0);
        uvs[29] = new Vector2(0, 1);

        uvs[30] = new Vector2(1, 1);
        uvs[31] = new Vector2(0, 0);
        uvs[32] = new Vector2(0, 1);

        uvs[33] = new Vector2(1, 1);
        uvs[34] = new Vector2(0, 0);
        uvs[35] = new Vector2(0, 1);

        uvs[36] = new Vector2(1, 1);
        uvs[37] = new Vector2(0, 0);
        uvs[38] = new Vector2(0, 1);

        uvs[39] = new Vector2(1, 1);
        uvs[40] = new Vector2(0, 0);
        uvs[41] = new Vector2(0, 1);

        uvs[42] = new Vector2(1, 1);
        uvs[43] = new Vector2(0, 0);
        uvs[44] = new Vector2(0, 1);

        uvs[45] = new Vector2(1, 1);
        uvs[46] = new Vector2(0, 0);
        uvs[47] = new Vector2(0, 1);

        uvs[48] = new Vector2(1, 1);
        uvs[49] = new Vector2(0, 0);
        uvs[50] = new Vector2(0, 1);

        uvs[51] = new Vector2(1, 1);
        uvs[52] = new Vector2(0, 0);
        uvs[53] = new Vector2(0, 1);

        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.uv = uvs;
        mesh.triangles = allTris.ToArray();
        mr.material = material;
        mesh.RecalculateTangents();
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        mf.mesh = mesh;
    }

    private void OnDrawGizmos()
    {
        if (allPoint != null)
        {
            for (int i = 0; i < allPoint.Length; i++)
            {
                Gizmos.color = Color.yellow;
                Gizmos.DrawSphere(allPoint[i], 0.1f);
            }
        }
    }
}

以上就是Unity中 mesh生成斜坡的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Unity mesh斜坡的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#使用系統(tǒng)方法發(fā)送異步郵件完整實(shí)例

    C#使用系統(tǒng)方法發(fā)送異步郵件完整實(shí)例

    這篇文章主要介紹了C#使用系統(tǒng)方法發(fā)送異步郵件實(shí)現(xiàn)方法,結(jié)合完整實(shí)例形式分析了C#異步調(diào)用與郵件發(fā)送的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • C# 本地函數(shù)與 Lambda 表達(dá)式詳細(xì)介紹

    C# 本地函數(shù)與 Lambda 表達(dá)式詳細(xì)介紹

    這篇文章主要介紹了C 語(yǔ)言本地函數(shù)與 Lambda 表達(dá)式,,由于 C# 長(zhǎng)期以來(lái)一直使用 lambda,因此從差異和相似之處來(lái)看本地函數(shù)確實(shí)是有意義的,感興趣的小伙伴可以參考下面文章內(nèi)容
    2021-09-09
  • C# 泛型編譯特性對(duì)性能的影響小結(jié)

    C# 泛型編譯特性對(duì)性能的影響小結(jié)

    C#作為一種強(qiáng)類型語(yǔ)言,具有豐富的泛型支持,允許開(kāi)發(fā)者編寫(xiě)可以應(yīng)對(duì)不同數(shù)據(jù)類型的通用代碼,這篇文章主要介紹了C# 泛型編譯特性對(duì)性能的影響 ,需要的朋友可以參考下
    2023-11-11
  • C#通過(guò)JObject解析json對(duì)象

    C#通過(guò)JObject解析json對(duì)象

    這篇文章介紹了C#通過(guò)JObject解析json對(duì)象的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • C#中List.Contains(T)失效的解決方法

    C#中List.Contains(T)失效的解決方法

    這篇文章主要介紹了C#中List.Contains(T)失效的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-03-03
  • c#深拷貝文件夾示例

    c#深拷貝文件夾示例

    這篇文章主要介紹了c#深拷貝文件夾示例,需要的朋友可以參考下
    2014-04-04
  • C#泛型語(yǔ)法詳解

    C#泛型語(yǔ)法詳解

    本文詳細(xì)講解了C#中的泛型語(yǔ)法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • C#與C++與互操作實(shí)例講解

    C#與C++與互操作實(shí)例講解

    在本篇文章里小編給大家整理了關(guān)于C#與C++與互操作實(shí)例以及相關(guān)內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2019-08-08
  • WPF利用ScottPlot實(shí)現(xiàn)動(dòng)態(tài)繪制圖像

    WPF利用ScottPlot實(shí)現(xiàn)動(dòng)態(tài)繪制圖像

    ScottPlot是基于.Net的一款開(kāi)源免費(fèi)的交互式可視化庫(kù),支持Winform和WPF等UI框架,本文主要為大家詳細(xì)介紹了如何WPF如何使用ScottPlot實(shí)現(xiàn)動(dòng)態(tài)繪制圖像,需要的可以參考下
    2023-12-12
  • C#打印出正等腰三角形實(shí)例代碼

    C#打印出正等腰三角形實(shí)例代碼

    C#打印出正等腰三角形實(shí)例代碼,需要的朋友可以參考一下
    2013-03-03

最新評(píng)論

深州市| 郓城县| 福清市| 临武县| 永善县| 乡城县| 新建县| 大宁县| 岳普湖县| 吴川市| 海阳市| 教育| 香格里拉县| 泸水县| 徐闻县| 兖州市| 筠连县| 建昌县| 牡丹江市| 大名县| 鄂伦春自治旗| 弥勒县| 鹿邑县| 隆安县| 光泽县| 淄博市| 南木林县| 晋城| 雷山县| 临邑县| 阿拉善右旗| 梧州市| 湾仔区| 林州市| 罗江县| 陕西省| 监利县| 常宁市| 林周县| 兴和县| 临城县|