当前位置:网站首页>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]);
}
}
最后画出来的圆就可任意转动了
边栏推荐
- VsCode connects to the remote server and modifies the file code
- 数码管动态显示及模块化编程
- [Jiangsu University Automation Association stm32F103c8t6] Notes [Initial 32 MCU and EXTI External Interrupt Initialization Parameter Configuration]
- Knowledge of the day: handwritten deep copy and shallow copy (solves the problem of circular references)
- QT serial and CAN dynamic real-time display the log data
- QT Weekly Skills (1) ~~~~~~~~~ Running Icon
- 洛谷一P1097 [NOIP2007 提高组] 统计数字
- [Jiangsu University of Science and Technology Automation Association stm32F103c8t6] Notes [Initial 32 MCU and TIM timing interrupt initialization parameter configuration]
- SSH-RSA密钥
- this是什么(你不知道的JS)
猜你喜欢
随机推荐
如何判断 PCB 板是否变形?
js高级学习笔记(详细)
VSCode隐藏左边活动栏
Insert map data efficiently
测试第二题
重磅揭晓!第十四届深创赛福田预选赛区暨华秋第八届硬创大赛华南分赛区晋
QT serial 2: LORA test platform based on QT and STM32H750 (1)
(*(void (*)())0)()的解读
矩阵键盘
js 替换字符串中所有 “ 引号 —— 数据处理
ES6 syntax notes (ES6~ES11)
IEEE在指定期刊下搜索相关论文
survivor区对象何时进入老年代(深入理解jvm中表述不准确的地方)
四、6、前缀、中缀、后缀表达式(逆波兰表达式)
查找Proj4js地图投影参数
QT serial and CAN dynamic real-time display the log data
TCP建立连接的过程
【Exhibition of some projects】
华秋第八届硬创大赛携手NVIDIA初创加速计划,赋能企业发展
MindSpore 提 PR 全流程









