首页 > 代码库 > u3d mesh构造
u3d mesh构造
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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。