当前位置:网站首页>Unity Gizmos扩展:线框圆
Unity Gizmos扩展:线框圆
2022-07-30 05:47:00 【Pure_*】
Unity Gizmos扩展:画一个线框圆
定义
圆由圆心位置与半径决定的
在Unity中画圆,由于需要跟随对象转动而转动,除了圆心位置和半径,还需要提供两个方向向量
有两条线,才有了决定一个面的基础
为了方便,这两个方向需要相互垂直(也可以不垂直,但是懒啊),形成一个平面坐标轴
算法步骤
- 如图所示,以
forward(蓝色箭头)和right(红色箭头)这两个方向决定的圆为例

- 计算下一个点的坐标,如图,根据
forward方向和半径,可以算出A点的坐标
P o s A = P o s c e n t e r + f o r w a r d ∗ r a d i u s Pos_{A}=Pos_{center}+forward*radius PosA=Poscenter+forward∗radius - 根据每次转动角度
delta,计算出B点相对于圆心的偏移量
O f f s e t r i g h t = r a d i u s ∗ sin ( d e l t a ) ∗ r i g h t O f f s e t f o r w a r d = r a d i u s ∗ cos ( d e l t a ) ∗ f o r w a r d \begin{aligned}Offset_{right}=radius*\sin(delta)*right \\ Offset_{forward}=radius*\cos(delta)*forward\end{aligned} Offsetright=radius∗sin(delta)∗rightOffsetforward=radius∗cos(delta)∗forward - 最后以将偏移值与圆心坐标相加,即可得到B的坐标
P o s B = P o s c e n t e r + O f f s e t f o r w a r d + O f f s e t r i g h t Pos_{B}=Pos_{center}+Offset_{forward}+Offset_{right} PosB=Poscenter+Offsetforward+Offsetright
- 其他的点的坐标依次类推,直到转满360度。
代码
代码就是根据上述算法步骤来的,需要注意的是
根据stepAngle的大小进行每一次的转动的,如果后续累加的angle > 360,如果还是用通用公式计算,会导致这个圆不能闭合,因此在angle > 360时,最后一个点的值应该为第一个点startPos的值
/// <summary>
/// 画一个三维可转动圆形框架,平面取决于forward与right共同所在面
/// </summary>
/// <param name="center">圆心</param>
/// <param name="forward">扇形圆弧凸的方向</param>
/// <param name="right">垂直于forward</param>
/// <param name="radius">半径</param>
/// <param name="divide">分多少块</param>
public static void DrawWireCircle(Vector3 center, Vector3 forward, Vector3 right, float radius, int divide = 16)
{
radius = Mathf.Abs(radius);
forward = forward.normalized;
right = right.normalized;
divide = Mathf.Abs(divide);
if (radius == 0 || divide < 3 || Vector3.Dot(forward, right) > 0.0001f) return;
if(divide > 32) divide = 32;
List<Vector3> vertices = new List<Vector3>();
Vector3 startPos = center + forward * radius;
vertices.Add(startPos);
float stepAngle = 360.0f / divide;
float angle = 0f;
while (angle < 360)
{
angle += stepAngle;
if (angle <= 360)
{
float x = radius * Mathf.Cos(Mathf.Deg2Rad * angle);
float y = radius * Mathf.Sin(Mathf.Deg2Rad * angle);
Vector3 vertex = center + right * y + forward * x;
vertices.Add(vertex);
}
else
{
vertices.Add(startPos);
}
}
for (int i = 1; i < vertices.Count; i++)
{
Gizmos.DrawLine(vertices[i - 1], vertices[i]);
}
}
最后画出来的圆就可任意转动了
边栏推荐
- [Punctuality Atom] Learning and use of IIC (unfinished...)
- 《C陷阱和缺陷》void (*signal(int , void(*)(int)))(int)的深刻解读
- Cannnot download sources不能下载源码百分百超详细解决方案
- 2020-09-03 Solve the very slow installation of pip install [Errno 101] Network unreachable problem
- 华秋第八届硬创大赛携手NVIDIA初创加速计划,赋能企业发展
- PCB 一分钟科普之你真的懂多层板吗?
- 超详细的PCB高可靠辨别方法
- 顺序二叉树---实现数组的二叉树前序遍历输出
- ------实现二叉搜索树BST
- ---------手撕二叉树,完成二叉树的前中后序遍历,以及前中后序查找
猜你喜欢

数码管动态显示及模块化编程

VSCode hides the left activity bar

js advanced study notes (detailed)

jvm之逃逸分析

【markdown常用用法】

Explore the efficiency of make_shared

jvm之方法区

Diwen serial screen production (serialization 1) ===== preparation work

VsCode打开终端的方法
![[Punctuality Atom] Simple application of sys.c, sys.h bit-band operations](/img/7f/d9f480ab9a1e542e4fa1fda7978e4c.png)
[Punctuality Atom] Simple application of sys.c, sys.h bit-band operations
随机推荐
Antd简单启动一个企业级项目
xxx is not in the sudoers file.This incident will be reported错误
Cannnot download sources不能下载源码百分百超详细解决方案
---------手撕二叉树,完成二叉树的前中后序遍历,以及前中后序查找
四、6、前缀、中缀、后缀表达式(逆波兰表达式)
Insertion Sort in Classic Sort
lcd1602调试
i++与 ++i 的区别
2021 soft exam intermediate pass
ipconfig Command Guide
题解——汉明距离
测试第二题
-----博客声明
删除当前路径下含某个关键字的所有文件
51数码管显示
led闪烁
openssl 1.1.1 compile statement
QT weekly skills (3)~~~~~~~~~ serial port addition
Kunlun state screen production (serial 3) - based article (button serial port to send)
二进制到汇编:进制,原码反码补码,位运算,通用寄存器,内存一套打通