U3D mesh construction

Source: Internet
Author: User

The organization method of U3D mesh. The default mesh construction method of U3D is triangle, that is, the index points to three points, so these three points will be connected to a triangle,

Before that, you must apply for many vertices, UV, and normal, and then set points and indexes. Note that the U3D area must be drawn counterclockwise to display the surface. Otherwise, the area is not displayed, the following code records

 

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;        }        }}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.