尽管unity3D中直接有模块画圆柱体,但画圆面仍旧需要自己使用mesh来绘制。使用mesh画图一般分为四个步骤。

1.顶点

2.三角形

3.UV

4.负载属性与mesh

自己绘制一个简单的平面代码如下:

Mesh Createmeshp(float radius, float Height){//vertices:int p_vertices_count = 4;Vector3[] p_vertices = new Vector3[p_vertices_count];p_vertices[0] = new Vector3(-radius, -Height / 2, radius);p_vertices[1] = new Vector3(-radius, Height / 2, radius);p_vertices[2] = new Vector3(radius, -Height / 2, radius);p_vertices[3] = new Vector3(radius, Height / 2, radius);//trianglesint[] p_triangles = new int[6];p_triangles[0] = 0;p_triangles[1] = 1;p_triangles[2] = 2;p_triangles[3] = 3;p_triangles[4] = 2;p_triangles[5] = 1;//uv:Vector2[] p_uvs = new Vector2[4];float p_uvSetup = 1.0f ;p_uvs[0] = new Vector2(p_uvSetup * 0, 1);p_uvs[1] = new Vector2(p_uvSetup * 0, 0);p_uvs[2] = new Vector2(p_uvSetup * 1, 1);p_uvs[3] = new Vector2(p_uvSetup * 1, 0);//负载属性与meshMesh mesh = new Mesh();mesh.vertices = p_vertices;mesh.triangles = p_triangles;mesh.uv = p_uvs;return mesh;}

绘制结果如下图所示:

而圆柱面和平面最大的区别就是面是首尾相连的,所以在画三角形时应该分别考虑连续的部分和首尾相连的部分,比如考虑由四个平面组成的首尾相连的长方体侧面,他们可以按如下代码绘制:

 Mesh Createmesh(float radius, float Height){int s_vertices_count = 8;Vector3[] s_vertices = new Vector3[s_vertices_count];s_vertices[0] = new Vector3(-radius, -Height / 2, -radius);s_vertices[1] = new Vector3(-radius, Height / 2, -radius);s_vertices[2] = new Vector3(-radius, -Height / 2, radius);s_vertices[3] = new Vector3(-radius, Height / 2, radius);s_vertices[4] = new Vector3(radius, -Height / 2, radius);s_vertices[5] = new Vector3(radius, Height / 2, radius);s_vertices[6] = new Vector3(radius, -Height / 2, -radius);s_vertices[7] = new Vector3(radius, Height / 2, -radius);//trianglesint tri_num = 8;int tri_cout = tri_num * 3;int[] s_triangles = new int[tri_cout];for (int i = 0, vi = 0; i < tri_cout - 6; i += 6, vi += 2){s_triangles[i] = vi;s_triangles[i + 1] = vi + 1;s_triangles[i + 2] = vi + 2;s_triangles[i + 3] = vi + 3;s_triangles[i + 4] = vi + 2;s_triangles[i + 5] = vi + 1;}s_triangles[tri_cout - 6] = 6;s_triangles[tri_cout - 5] = 7;s_triangles[tri_cout - 4] = 0;s_triangles[tri_cout - 3] = 1;s_triangles[tri_cout - 2] = 0;s_triangles[tri_cout - 1] = 7;//uv:Vector2[] s_uvs = new Vector2[s_vertices_count];float s_uvSetup = 1.0f / 4;int iduv = 0;for (int i = 0; i < s_vertices_count; i = i + 2){s_uvs[i] = new Vector2(s_uvSetup * iduv, 1);s_uvs[i + 1] = new Vector2(s_uvSetup * iduv, 0);iduv++;}//负载属性与meshMesh mesh = new Mesh();mesh.vertices = s_vertices;mesh.triangles = s_triangles;mesh.uv = s_uvs;return mesh;}

结果显示如下:

由于圆柱面地面是个圆,还得需要按照一定的规律来计算圆周上的各点

 Mesh CreateMesh(float radius, int segments, float Height){//vertices:int vertices_count = Segments * 2;Vector3[] vertices = new Vector3[vertices_count];//vertices[0] = Vector3.zero;float angledegree = 360.0f;float angleRad = Mathf.Deg2Rad * angledegree;float angleCur = angleRad;float angledelta = angleRad / Segments;for (int i = 0; i < vertices_count; i++){float cosA = Mathf.Cos(angleCur);float sinA = Mathf.Sin(angleCur);vertices[i] = new Vector3(Radius * cosA, Height / 2, Radius * sinA);i++;vertices[i] = new Vector3(Radius * cosA, -Height / 2, Radius * sinA);angleCur -= angledelta;}//trianglesint triangle_count = segments * 3 * 2;int[] triangles = new int[triangle_count];int vert = 0;int idx = 0;for (int i = 0; i < segments - 1; i++)     //因为该案例分割了60个三角形,故最后一个索引顺序应该是:0 60 1;所以需要单独处理{triangles[idx++] = vert + 1;triangles[idx++] = vert;triangles[idx++] = vert + 3;triangles[idx++] = vert;triangles[idx++] = vert + 2;triangles[idx++] = vert + 3;vert += 2;}triangles[triangle_count - 6] = vertices_count - 1;triangles[triangle_count - 5] = vertices_count - 2;triangles[triangle_count - 4] = 1;triangles[triangle_count - 3] = vertices_count - 2;triangles[triangle_count - 2] = 0;triangles[triangle_count - 1] = 1;                           //为了完成闭环,将最后一个三角形单独拎出来//uv:Vector2[] uvs = new Vector2[vertices_count];float uvSetup = 1.0f / Segments;int iduv = 0;for (int i = 0; i < vertices_count; i = i + 2){uvs[i] = new Vector2(uvSetup * iduv, 1);uvs[i + 1] = new Vector2(uvSetup * iduv, 0);iduv++;}//负载属性与meshMesh mesh = new Mesh();mesh.vertices = vertices;mesh.triangles = triangles;mesh.uv = uvs;return mesh;}

结果如下:

侧面

俯视

附整体代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class NewBehaviourScript : MonoBehaviour
{public float Radius = 10f;    //半径  public int Segments = 600;   //分割数  public float Height = 2;           ///3.1415926f;private MeshFilter meshFilter;void Start(){//Debug.Log("hello unity: ");meshFilter = GetComponent<MeshFilter>();//圆柱,长方体,平面meshFilter.mesh = CreateMesh(Radius, Segments,Height);//meshFilter.mesh = Createmesh(Radius,  Height);//meshFilter.mesh = Createmeshp(Radius, Height);}Mesh CreateMesh(float radius, int segments, float Height){//vertices:int vertices_count = Segments * 2;Vector3[] vertices = new Vector3[vertices_count];//vertices[0] = Vector3.zero;float angledegree = 360.0f;float angleRad = Mathf.Deg2Rad * angledegree;float angleCur = angleRad;float angledelta = angleRad / Segments;for (int i = 0; i < vertices_count; i++){float cosA = Mathf.Cos(angleCur);float sinA = Mathf.Sin(angleCur);vertices[i] = new Vector3(Radius * cosA, Height / 2, Radius * sinA);i++;vertices[i] = new Vector3(Radius * cosA, -Height / 2, Radius * sinA);angleCur -= angledelta;}//trianglesint triangle_count = segments * 3 * 2;int[] triangles = new int[triangle_count];int vert = 0;int idx = 0;for (int i = 0; i < segments - 1; i++)     //因为该案例分割了60个三角形,故最后一个索引顺序应该是:0 60 1;所以需要单独处理{triangles[idx++] = vert + 1;triangles[idx++] = vert;triangles[idx++] = vert + 3;triangles[idx++] = vert;triangles[idx++] = vert + 2;triangles[idx++] = vert + 3;vert += 2;}triangles[triangle_count - 6] = vertices_count - 1;triangles[triangle_count - 5] = vertices_count - 2;triangles[triangle_count - 4] = 1;triangles[triangle_count - 3] = vertices_count - 2;triangles[triangle_count - 2] = 0;triangles[triangle_count - 1] = 1;                           //为了完成闭环,将最后一个三角形单独拎出来//uv:Vector2[] uvs = new Vector2[vertices_count];float uvSetup = 1.0f / Segments;int iduv = 0;for (int i = 0; i < vertices_count; i = i + 2){uvs[i] = new Vector2(uvSetup * iduv, 1);uvs[i + 1] = new Vector2(uvSetup * iduv, 0);iduv++;}//负载属性与meshMesh mesh = new Mesh();mesh.vertices = vertices;mesh.triangles = triangles;mesh.uv = uvs;return mesh;}Mesh Createmesh(float radius, float Height){int s_vertices_count = 8;Vector3[] s_vertices = new Vector3[s_vertices_count];s_vertices[0] = new Vector3(-radius, -Height / 2, -radius);s_vertices[1] = new Vector3(-radius, Height / 2, -radius);s_vertices[2] = new Vector3(-radius, -Height / 2, radius);s_vertices[3] = new Vector3(-radius, Height / 2, radius);s_vertices[4] = new Vector3(radius, -Height / 2, radius);s_vertices[5] = new Vector3(radius, Height / 2, radius);s_vertices[6] = new Vector3(radius, -Height / 2, -radius);s_vertices[7] = new Vector3(radius, Height / 2, -radius);//trianglesint tri_num = 8;int tri_cout = tri_num * 3;int[] s_triangles = new int[tri_cout];for (int i = 0, vi = 0; i < tri_cout - 6; i += 6, vi += 2){s_triangles[i] = vi;s_triangles[i + 1] = vi + 1;s_triangles[i + 2] = vi + 2;s_triangles[i + 3] = vi + 3;s_triangles[i + 4] = vi + 2;s_triangles[i + 5] = vi + 1;}s_triangles[tri_cout - 6] = 6;s_triangles[tri_cout - 5] = 7;s_triangles[tri_cout - 4] = 0;s_triangles[tri_cout - 3] = 1;s_triangles[tri_cout - 2] = 0;s_triangles[tri_cout - 1] = 7;//uv:Vector2[] s_uvs = new Vector2[s_vertices_count];float s_uvSetup = 1.0f / 4;int iduv = 0;for (int i = 0; i < s_vertices_count; i = i + 2){s_uvs[i] = new Vector2(s_uvSetup * iduv, 1);s_uvs[i + 1] = new Vector2(s_uvSetup * iduv, 0);iduv++;}//负载属性与meshMesh mesh = new Mesh();mesh.vertices = s_vertices;mesh.triangles = s_triangles;mesh.uv = s_uvs;return mesh;}Mesh Createmeshp(float radius, float Height){int p_vertices_count = 4;Vector3[] p_vertices = new Vector3[p_vertices_count];p_vertices[0] = new Vector3(-radius, -Height / 2, radius);p_vertices[1] = new Vector3(-radius, Height / 2, radius);p_vertices[2] = new Vector3(radius, -Height / 2, radius);p_vertices[3] = new Vector3(radius, Height / 2, radius);//trianglesint[] p_triangles = new int[6];p_triangles[0] = 0;p_triangles[1] = 1;p_triangles[2] = 2;p_triangles[3] = 3;p_triangles[4] = 2;p_triangles[5] = 1;//uv:Vector2[] p_uvs = new Vector2[4];float p_uvSetup = 1.0f ;p_uvs[0] = new Vector2(p_uvSetup * 0, 1);p_uvs[1] = new Vector2(p_uvSetup * 0, 0);p_uvs[2] = new Vector2(p_uvSetup * 1, 1);p_uvs[3] = new Vector2(p_uvSetup * 1, 0);//负载属性与meshMesh mesh = new Mesh();mesh.vertices = p_vertices;mesh.triangles = p_triangles;mesh.uv = p_uvs;return mesh;}// Update is called once per framevoid Update(){}
}

unity3D中使用mesh画圆柱面相关推荐

  1. Unity3D中使用mesh collider和box collider的区别

    Unity3D中使用mesh collider和box collider的区别 踩坑过程记录. 设备是HTC的VIVE 和 Unity 5.x CPU: Intel Xeon Silver 4116 ...

  2. unity 画球面_unity中实现Mesh画球体、半球体、四分之一球体以及任意弧面

    感谢两篇文章: mesh绘制模型:https://blog.csdn.net/qq_29579137/article/details/77369734?depth_1-utm_source=distr ...

  3. sketchup画圆柱面

    1.画好一个圆面 2.使用选择工具选中圆面的边,右键拆分,输入14(14等分为例子) 3.推拉 4.使用橡皮擦将图调整至此形状 5.选中下方圆形底 右键查找中心,会多出如图中的中心点 6橡皮擦到只剩一 ...

  4. matlab中做出球面和圆柱面,如何用MATLAB在直角坐标系下绘制球面x^2+y^2+z^2=4被柱面(x-1)^2+y^2=1截得的部分曲面.急!谢谢...

    答:clear;clc; r=1;%r的值自己改 %柱面部分 t=linspace(0,2*pi,37); q=linspace(-1,1,11); [tt,qq]=meshgrid(t,q); x= ...

  5. 【坑】html5中使用canvas画圆,弧度和角度傻傻分不清楚

    问题导入 <body> <canvas id="myCanvas" width="400" height="400" st ...

  6. 如何通过Matplotlib画圆

    前言 前两天因为需要,尝试着用Matplotlib 中的pyplot画圆我画圆的方法是通过圆的方程来画的,另外在图中做了必要的说明.半径为1,圆心为(2,2),方程分为两个,话不多说,直接看代码 画圆 ...

  7. 在Unity3D中利用描点法画圆

    直接法: 调用函数即可 //N为描点的个数,CirclePoint圆心距离,radius半径, mycolor为颜色 void MyDrawCircles(int N, Vector3 CircleP ...

  8. 问题四十一:怎么用ray tracing画任意圆柱面(generalized cylinder)

    我们之前在"35.2"章节中画过椭圆柱面: 我们还在"36.4"章节中画过圆柱面的Inverse Mapping图: 但是,这些柱面都是:底面与ZOX平面平行, ...

  9. WebGIS中基于AGS的画圆查询简析以及通过Polygon来构造圆的算法

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 某个项目需求中需要在前端进行画圆查询,将圆范围上的多边形要素 ...

最新文章

  1. JDK 5.0 注解的使用
  2. visual assist破解
  3. 开启注解缓存_Spring Boot 2.x基础教程:进程内缓存的使用与Cache注解详解
  4. springboot中aop的应用场景_自然语言处理工具包 HanLP在 Spring Boot中的应用
  5. 报错:failed to get the task for process XXX(解决方案)
  6. SAP Spartacus Cart UI 修改 quantity 字段后的 Patch 请求遇到 400 错误 - IllegalArgumentError
  7. 诸暨机器人餐厅价格_现场 | 一家尝出“锅气”的餐厅 探店机器人餐厅
  8. 三因素方差分析_详解方差分析表(ANOVA)(一) —— 线性回归与矩阵代数.回顾
  9. 删缓存,数据库更新谁先执行,及延时双删
  10. 关于opencv标定的一些疑问,首先声明这篇文章转载于纸异兽,由于暂时联系不到他本人,只好请教各位了。有些问题想请教,各位大神可以留下联系方式帮我解决,万分感谢...
  11. 服务器sas硬盘转sata硬盘6,SAS接口(sas硬盘改sata家用)
  12. 使用 RabbitMQ 实现 RPC
  13. PAT考前准备篇:目标满分
  14. 虚拟磁盘管理 无法启动服务器,运行虚拟磁盘管理器提示"RPC服务器不可用"分析与解决方案...
  15. 游戏项目框架(属性名+方法名)
  16. docker镜像指定安装源_如何修改docker pull镜像源
  17. ORA-00911: 无效字符 细节一定要注意
  18. MIMIC-IV-v2.0安装教程
  19. asio(三)、bind
  20. GPRS、433、Wifi、Zigbee模块概念和区别

热门文章

  1. 万万没想到之抓捕孔连顺问题的一些思考
  2. FCFS算法java实现
  3. Java-Sec-Code 环境搭建
  4. System Management Bus
  5. 后乔布斯时代结束 蒂姆库克思路渐现
  6. linux 用户操作神迹,《不朽的神迹》操作技巧篇
  7. Unity Transparent Video | 用 VideoPlayer 或 AVPro 播放透明影片
  8. easypoi教程_Easy_Poi使用教程
  9. Windows主机信息搜集
  10. 最多4位数的时间的加减法