StretchedBillboard 实现

参考Unity粒子系统的StretchedBillboard,在保持模型的右朝向不变的情况下,尽量朝向摄像机
转化为数学问题: 一个向量在一个面上的投影
基础的Billboard的方向应为: 从中点指向摄像机
限制的平面为以right为法向量的平面
算法
Vector3 rightward = transform.parent.forward; if (isShowLeft) rightward = -rightward; Vector3 forward = (Camera.current.transform.position - transform.position).normalized; if (isShowBack) forward = -forward; float cosVal = Vector3.Dot(rightward, forward); if (Mathf.Abs(cosVal) >= 0.999999f) return; rightward = rightward * cosVal; forward = forward - rightward; //不能用forward改变,不能保证right朝向 有时候会反过来 if (cosVal > 0) transform.rotation = Quaternion.LookRotation(forward, Vector3.Cross(rightward, forward)); else transform.rotation = Quaternion.LookRotation(forward, Vector3.Cross(forward, rightward)); //角度旋转 if (billboardAngle != 0) { Vector3 oriAngle = transform.eulerAngles; oriAngle.x += billboardAngle; transform.eulerAngles = oriAngle; }
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
1146702-20180315180346848-863295694.jpg 
最终计算朝向
1146702-20180315180347236-967272717.jpg






转载于:https://www.cnblogs.com/Hichy/p/8575204.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值