使用unity的mesh绘制三菱柱的碰撞体(3d三角形)

来源:互联网 发布:高中免费听课软件 编辑:程序博客网 时间:2022/05/27 14:52

因为项目中有很多类似三菱柱的物体,如果使用meshcollider的话,模型边的弧度会有很多碰撞线,所以就想着能不能自己绘制

一个三菱柱的碰撞体。思路是首先绘制出三菱柱,然后在添加meshcollidre,这样就能绘制出来了。

然后参考博客:http://www.cnblogs.com/JLZT1223/p/6089996.html


首先上效果图:

     


然后下面是代码:


/************************************************************************* *  FileName: DrawTriangle.cs *  Author: LaiZhangYin(Eagle)   Version: 1.0   Date: 6/21/2017 * if you have some question, please call  *  QQ/Wechat : 782966734 *************************************************************************/using UnityEngine;using System.Collections;[RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]public class DrawTriangle : BaseMonoBehaviour{    public float length = 6;    public float width = 6;    public float height = 6;    private MeshFilter meshFilter;    void Start()    {        meshFilter = GetComponent<MeshFilter>();        meshFilter.mesh = CreateTriangle(length, width, height);        if (this.gameObject.AddComponent<MeshCollider>() == null)            this.gameObject.AddComponent<MeshCollider>();        this.GetComponent<MeshRenderer>().enabled = false;    }    private Mesh CreateTriangle(float length, float width, float height)    {        int verticesCount = 4 * 6;//3* 4;        Vector3[] vertices = new Vector3[verticesCount];        vertices[0] = Vector3.zero;                 //前面左下角的点        vertices[1] = new Vector3(0, height, 0);    //前面左上角的点        vertices[2] = new Vector3(length, 0, 0);   // 前面右下角的点            vertices[3] = new Vector3(length, height, 0);//前面右上角的点        vertices[4] = Vector3.zero;//new Vector3(length, 0, width);        //后面右下角的点        vertices[5] = Vector3.zero;//new Vector3(length, height, width);    //后面右上角的点        vertices[6] = new Vector3(0, 0, width);             //后面左下角的点        vertices[7] = new Vector3(0, height, width);        //后面左上角的点        vertices[8] = vertices[6];                              //左        vertices[9] = vertices[7];        vertices[10] = vertices[0];        vertices[11] = vertices[1];        vertices[12] = vertices[2];                              //右        vertices[13] = vertices[3];        vertices[14] = vertices[6];        vertices[15] = vertices[7];        vertices[16] = vertices[1];                              //上        vertices[17] = vertices[7];        vertices[18] = vertices[3];        vertices[19] = vertices[7];        vertices[20] = vertices[2];                              //下        vertices[21] = vertices[6];        vertices[22] = vertices[0];        vertices[23] = vertices[6];                int triangleCount = 6 * 2 * 3;        int[] triangles = new int[triangleCount];        for (int i = 0, v = 0; i < triangleCount; i += 6, v += 4)        {            triangles[i] = v;            triangles[i + 1] = v + 1;            triangles[i + 2] = v + 2;            triangles[i + 3] = v + 3;            triangles[i + 4] = v + 2;            triangles[i + 5] = v + 1;        }        Mesh mesh = new Mesh();        mesh.vertices = vertices;        mesh.triangles = triangles;        return mesh;    }#if UNITY_EDITOR    //用于在编辑状态设置大小    void OnDrawGizmos()    {        meshFilter = GetComponent<MeshFilter>();        meshFilter.mesh = CreateTriangle(length, width, height);    }#endif}



对于绘制出来的下面有一个图便于理解:


然后就是根据这个图的示意来绘制三菱柱了。





完!