u3d mesh構造

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   tar   

u3d  mesh的組織方法  ,u3d 預設的mesh構造方法是三角形,即index指向三個點,那麼這三個點就會被串連成三角形,

那麼之前必須先申請很多點,uv,法線,然後設定點,index即可,有一點需要注意就是 ,u3d裡面必須逆時針繪製才會顯示面,否則不顯示,以下代碼先記錄

 

using UnityEngine;using System.Collections;public class LiDiBoxOne : MonoBehaviour {    private GameObject TObj;        public Material Tmeterial;    public float THeight;    private Mesh teMesh;    private int MNUMPOINT = 12100;    private Vector3[] vertex;    private Vector2[] uvs;    private int[] trangles;    private Vector4[] tangents;       private Vector3 m_OldPos;    private Vector3 m_OldLeftPos;    private Vector3 m_OldRightPos;    private int m_Step;    // Use this for initialization    void Start()    {        m_Step = 0;        m_OldPos = new Vector3(0, 0, 0);        TObj = new GameObject();        TObj.name = "abc";        TObj.transform.position = new Vector3(0, 0, 0);        vertex = new Vector3[MNUMPOINT];        uvs = new Vector2[MNUMPOINT];        tangents = new Vector4[MNUMPOINT];        trangles = new int[MNUMPOINT *12];        TObj.gameObject.AddComponent<MeshFilter>();        TObj.gameObject.AddComponent("MeshRenderer");        if (Tmeterial)            TObj.renderer.material = Tmeterial;        else            TObj.renderer.material.color = Color.white;        teMesh = new Mesh();            }    // Update is called once per frame    void Update()    {        LidiNewAction();            }    void LidiOldAction()    {        Vector3 mTempPositionCar = this.transform.position;        Vector3 mTempPosition = new Vector3(mTempPositionCar.x, THeight, mTempPositionCar.z);        if (Vector3.Distance(m_OldPos, mTempPosition) > 4.0f)        {            Vector3 m_leftstep = this.transform.right * 4.50f;            Vector3 m_left = mTempPosition - m_leftstep;            Vector3 m_right = mTempPosition + m_leftstep;            float PointDis;            for (int j = 0; j < 2; ++j)            {                if (j == 0)                {                    vertex[m_Step * 2 + j] = new Vector3(m_left.x, THeight, m_left.z);                    PointDis = 0;                    uvs[m_Step * 2 + j] = new Vector2(0.0f, m_Step);                    m_OldLeftPos = new Vector3(m_left.x, THeight, m_left.z);                }                else                {                    vertex[m_Step * 2 + j] = new Vector3(m_right.x, THeight, m_right.z);                    uvs[m_Step * 2 + j] = new Vector2(1.0f, m_Step);                    m_OldRightPos = new Vector3(m_right.x, THeight, m_right.z);                }                tangents[m_Step * 2 + j] = new Vector4(0, 1, 0, 1);            }            if (m_Step > 0)            {                int ggtemp = m_Step - 1;                trangles[ggtemp * 6 + 0] = ggtemp * 2;                trangles[ggtemp * 6 + 1] = ggtemp * 2 + 2;                trangles[ggtemp * 6 + 2] = ggtemp * 2 + 1;                trangles[ggtemp * 6 + 3] = ggtemp * 2 + 2;                trangles[ggtemp * 6 + 4] = ggtemp * 2 + 3;                trangles[ggtemp * 6 + 5] = ggtemp * 2 + 1;            }            teMesh.vertices = vertex;            teMesh.uv = uvs;            teMesh.triangles = trangles;            teMesh.RecalculateNormals();            teMesh.RecalculateBounds();            teMesh.tangents = tangents;            TObj.GetComponent<MeshFilter>().mesh = teMesh;            m_Step++;            m_OldPos = mTempPosition;        }            }    void LidiNewAction()    {        Vector3 mTempPosition = this.transform.position;        Vector3 m_leftstep = this.transform.right * 6.50f;        Vector3 m_leftPos = mTempPosition - m_leftstep;        Vector3 m_rightPos = mTempPosition + m_leftstep;        float disleft = Vector3.Distance(m_leftPos, m_OldLeftPos);        float disright = Vector3.Distance(m_rightPos, m_OldRightPos);        if (disleft > 1.5250f || disright > 1.525f)        {            for (int j = 0; j < 3; ++j)            {                if (j == 0)                {                    vertex[m_Step * 3 + j] = new Vector3(m_leftPos.x, THeight, m_leftPos.z);                    uvs[m_Step * 3 + j] = new Vector2(0.0f, m_Step *0.25f);                }                if (j == 1)                {                    vertex[m_Step * 3 + j] = new Vector3(mTempPosition.x, THeight, mTempPosition.z);                    uvs[m_Step * 3 + j] = new Vector2(0.5f, m_Step *0.25f);                }                if (j == 2)                {                    vertex[m_Step * 3 + j] = new Vector3(m_rightPos.x, THeight, m_rightPos.z);                    uvs[m_Step * 3 + j] = new Vector2(1.0f, m_Step *0.25f);                }                // tangents[m_Step * 2 + j] = new Vector4(0, 1, 0, 1);            }            if (m_Step > 0)            {                int ggtemp = m_Step - 1;                trangles[ggtemp * 12 + 0] = ggtemp * 3;                trangles[ggtemp * 12 + 1] = ggtemp * 3 + 3;                trangles[ggtemp * 12 + 2] = ggtemp * 3 + 4;                trangles[ggtemp * 12 + 3] = ggtemp * 3 + 0;                trangles[ggtemp * 12 + 4] = ggtemp * 3 + 4;                trangles[ggtemp * 12 + 5] = ggtemp * 3 + 1;                trangles[ggtemp * 12 + 6] = ggtemp * 3 + 1;                trangles[ggtemp * 12 + 7] = ggtemp * 3 + 4;                trangles[ggtemp * 12 + 8] = ggtemp * 3 + 2;                trangles[ggtemp * 12 + 9] = ggtemp * 3 + 2;                trangles[ggtemp * 12 + 10] = ggtemp * 3 + 4;                trangles[ggtemp * 12 + 11] = ggtemp * 3 + 5;            }            teMesh.vertices = vertex;            teMesh.uv = uvs;            teMesh.triangles = trangles;            teMesh.RecalculateNormals();            teMesh.RecalculateBounds();            teMesh.tangents = tangents;            TObj.GetComponent<MeshFilter>().mesh = teMesh;            m_Step++;            m_OldLeftPos = m_leftPos;            m_OldRightPos = m_rightPos;            m_OldPos = mTempPosition;        }        }}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.