参考Unity粒子系统的StretchedBillboard,在保持模型的右朝向不变的情况下,尽量朝向摄像机
转化为数学问题: 一个向量在一个面上的投影
基础的Billboard的方向应为: 从中点指向摄像机
限制的平面为以right为法向量的平面
算法
x
24
1
Vector3 rightward = transform.parent.forward;
2
if (isShowLeft) rightward = -rightward;
3
Vector3 forward = (Camera.current.transform.position - transform.position).normalized;
4
if (isShowBack) forward = -forward;
5
6
float cosVal = Vector3.Dot(rightward, forward);
7
if (Mathf.Abs(cosVal) >= 0.999999f) return;
8
9
rightward = rightward * cosVal;
10
forward = forward - rightward;
11
12
//不能用forward改变,不能保证right朝向 有时候会反过来
13
if (cosVal > 0)
14
transform.rotation = Quaternion.LookRotation(forward, Vector3.Cross(rightward, forward));
15
else
16
transform.rotation = Quaternion.LookRotation(forward, Vector3.Cross(forward, rightward));
17
18
//角度旋转
19
if (billboardAngle != 0)
20
{
21
Vector3 oriAngle = transform.eulerAngles;
22
oriAngle.x += billboardAngle;
23
transform.eulerAngles = oriAngle;
24
}
效果: 白色为粒子系统的StretchedBillboard 红色为仿制的StretchedBillboard
最终计算朝向