使用unity的mesh绘制三菱柱的碰撞体(3d三角形)
来源:互联网 发布:高中免费听课软件 编辑:程序博客网 时间:2024/04/25 19:51
因为项目中有很多类似三菱柱的物体,如果使用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}
对于绘制出来的下面有一个图便于理解:
然后就是根据这个图的示意来绘制三菱柱了。
完!
阅读全文
0 0
- 使用unity的mesh绘制三菱柱的碰撞体(3d三角形)
- 使用unity绘制旋转的三角形
- [Unity 3D] Unity 3D 里的碰撞检测
- Unity 3D 里的碰撞检测
- 【Unity】3D碰撞物体的设置
- Unity动态绘制曲线Mesh的代码
- Unity 3D 创建Mesh(一)
- Unity 3D 创建Mesh(二)
- Unity 3D 创建Mesh(一)
- Unity 3D 创建Mesh(二)
- 初始Unity 3D——碰撞事件的监听
- unity 在Mesh中的顶点或者mesh 三角形的中点生成物体
- 3D编程-绘制最简单的三角形
- 初试Unity 3D——刚体与碰撞体的比较
- 【Unity】Unity中获得碰撞体碰撞的位置
- 3D mesh 的质量评估(Perceptual Metrics Quality)
- Unity 3D mesh制作地形工具
- unity射线碰撞检测+LayerMask的使用
- java
- 关于ng-file-upload插件反应慢的问题
- 1、Kafka简介
- 147. Insertion Sort List
- svg转成字体
- 使用unity的mesh绘制三菱柱的碰撞体(3d三角形)
- Bazel安装
- linux离线搭建Python环境及安装numpy、pandas
- StringUtils 解析公共类com.alibaba.dubbo.common.utils
- 五、JSP之JSP指令与动作元素
- cache的write through和write back
- 一个简单的前端获取手机验证码实现
- SDUT-1524 回文串判定
- 【性能测试】-Loadrunner脚本调试和回放常见问题及解决方案