小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

(一)mesh基礎(chǔ)

 印度阿三17 2020-02-02

概述

本篇為Unity Mesh基礎(chǔ)的第一篇,通過(guò)一個(gè)最基本的平面四邊形網(wǎng)格來(lái)進(jìn)行闡述。在此之前先對(duì)網(wǎng)格(mesh)做一個(gè)簡(jiǎn)介。網(wǎng)格為最基本的模型表達(dá),通過(guò)點(diǎn)構(gòu)成線,再由線構(gòu)成三角形,最后由三角形構(gòu)成面,然后通過(guò)材質(zhì)來(lái)進(jìn)行網(wǎng)格表面的表達(dá),如陰影,貼圖等。Unity除了ui的mesh,其他都需要MeshFilter來(lái)確定網(wǎng)格形狀,通過(guò)Material以及MeshRenderer來(lái)進(jìn)行網(wǎng)格渲染。

Mesh的組成

Mesh由頂點(diǎn)組成,再有三個(gè)頂點(diǎn)構(gòu)成。所以Mesh必須定義頂點(diǎn)以及頂點(diǎn)如何構(gòu)成三角形,這是最基本的三個(gè)要素,其次還需要定義uv來(lái)確定貼圖如何覆蓋在圖形表面,如果需要還需定義頂點(diǎn)的法線或者切向,法線跟光照有關(guān),而切向則會(huì)影響紋理凹凸問(wèn)題,本文以四邊形為例進(jìn)行闡述。

四邊形頂點(diǎn)

頂點(diǎn)個(gè)數(shù)為4,即左下,右下,右上以及左上。size為正方向尺寸。

    protected override Vector3[] Vertices
    {
        get
        {
            Vector3[] vertices = new Vector3[4];
            vertices[0] = Vector3.zero * size;
            vertices[1] = Vector3.right * size;
            vertices[2] = new Vector3(1, 1, 0) * size;
            vertices[3] = Vector3.up * size;

            return vertices;
        }
    }

三角形排列

三角形排列為int類型數(shù)組,保存頂點(diǎn)的索引號(hào),每三個(gè)組成一個(gè)三角形。下述代碼即為左下,左上,右下組成第一個(gè)三角形,剩下組成第二個(gè)三角形,排列順序?yàn)轫槙r(shí)針?lè)较颉?/p>

    protected override int[] Triangles
    {
        get
        {
            int[] triangles = new int[6]
            {
                0,3,1,1,3,2
            };

            return triangles;
        }
    }

Mesh的uv

如果給網(wǎng)格賦值貼圖,則需要定義uv,如下定義則為圖片左下角對(duì)應(yīng)mesh的左下角,右上角對(duì)應(yīng)右上角。

    protected override Vector2[] Uvs
    {
        get
        {
            Vector2[] uvs = new Vector2[4];
            uvs[0] = new Vector2(0, 0);
            uvs[1] = new Vector2(1, 0);
            uvs[2] = new Vector2(1, 1);
            uvs[3] = new Vector2(0, 1);

            return uvs;
        }
    }

完整代碼

基類

基類主要是通過(guò)OnDrawGizmos來(lái)繪制頂點(diǎn),利于更直觀觀察。

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

[RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
public class CreateMeshBase : MonoBehaviour
{
    MeshFilter meshFilter;

    protected Mesh mesh;

    protected virtual Vector3[] Vertices { get; }
    protected virtual int[] Triangles { get; }
    protected virtual Vector3[] Normals { get; }
    protected virtual Vector2[] Uvs { get; }
    protected virtual string MeshName { get; }

    protected virtual void Start()
    {
        GetMeshFilter();
    }

    protected virtual void Reset()
    {
        GetMeshFilter();
    }

    protected virtual void OnValidate()
    {
        GetMeshFilter();
    }

    void GetMeshFilter()
    {
        if (meshFilter == null)
        {
            meshFilter = GetComponent<MeshFilter>();
            mesh = new Mesh();            
        }

        mesh.triangles = null;
        mesh.uv = null;
        mesh.vertices = null;

        mesh.name = MeshName;
        mesh.vertices = Vertices;
        mesh.triangles = Triangles;
        mesh.uv = Uvs;

        meshFilter.mesh = mesh;
    }

    private void OnDrawGizmos()
    {
        if (Vertices == null) return;

        Gizmos.color = Color.red;
        Gizmos.DrawSphere(Vector3.zero, 0.5f);

        Gizmos.color = Color.blue;

        for (int i = 0; i < Vertices.Length; i  )
        {
            Gizmos.DrawSphere(Vertices[i], 0.3f);
        }
    }
}

Quad參數(shù)

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

public class CreateQuad : CreateMeshBase
{
    public int size = 5;

    protected override Vector3[] Vertices
    {
        get
        {
            Vector3[] vertices = new Vector3[4];
            vertices[0] = Vector3.zero * size;
            vertices[1] = Vector3.right * size;
            vertices[2] = new Vector3(1, 1, 0) * size;
            vertices[3] = Vector3.up * size;

            return vertices;
        }
    }

    protected override int[] Triangles
    {
        get
        {
            int[] triangles = new int[6]
            {
                0,3,1,1,3,2
            };

            return triangles;
        }
    }

    protected override string MeshName
    {
        get
        {
            return "Simple quad";
        }
    }

    protected override Vector2[] Uvs
    {
        get
        {
            Vector2[] uvs = new Vector2[4];
            uvs[0] = new Vector2(0, 0);
            uvs[1] = new Vector2(1, 0);
            uvs[2] = new Vector2(1, 1);
            uvs[3] = new Vector2(0, 1);

            return uvs;
        }
    }
}

使用

使用時(shí)新建一個(gè)空游戲物體,掛載上述腳本,然后新建材質(zhì),并將材質(zhì)賦值給MeshRenderer即可。

來(lái)源:https://www./content-4-629601.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多