参考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
 
最终计算朝向






StretchedBillboard 实现的更多相关文章

随机推荐

  1. equals() 和 hashCode()

    equals() 和 hashCode()这两个方法在java.lang.Object中,所有的类都可以继承这两个方法: 但是,这两个方法在Object类中的实现一般没什么用,所以你通常需要自己重载这 ...

  2. POJ 3461 字符串出现次数 && HDU1711 字符串第一次出现的位置 模板题

      Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 48387   Accepted: 19261 Descri ...

  3. Delphi字符串加密/解密

    unit uEncrypt_Decrypt;   interface   uses SysUtils;   const XorKey: array[0..7] of Byte = ($B2, $09, ...

  4. Extjs.panel.Panel赋值的问题

    初学extjs,很是不爽.也是只有初学者才犯的错误,发出来以免再犯. 先创建一个panel var panel1 = Ext.create('Ext.panel.Panel', { id: 'p1', ...

  5. java获得文件的最后修改时间

    原文:http://www.open-open.com/code/view/1453190044980 java的File类的lastModified()方法可以返回文件的最后修改时间: String ...

  6. HttpUtils 用于进行网络请求的工具类

    原文:http://www.open-open.com/code/view/1437537162631 import java.io.BufferedReader; import java.io.By ...

  7. FM算法及FFM算法

    转自:http://tech.meituan.com/deep-understanding-of-ffm-principles-and-practices.html http://blog.csdn. ...

  8. 修正iOS从照相机和相册中获取的图片 方向

    修正iOS从照相机和相册中获取的图片 方向   修正iOS从照相机和相册中获取的图片 方向 使用系统相机拍照得到的图片的默认方向有时不是ImageOrientationDown,而是ImageOrie ...

  9. uml时序图的初印象-------Day64

    近期有好多想法迫不及待的想去实现,但是其实是在那些最開始想的很明确,感觉会没问题的地方也总是会出现故障,导致稍微有些急躁,还是要淡定啊.又到了周末.明后天要收拾东西搬家,不知道宽带能不能顺利的给挪过去 ...

  10. kernel: audit: printk limit exceeded

    问题: 小长假的第一天早上8:18一个数据,被定时任务中的脚本漏处理: 查定时任务的日志,发现调度异常 查var messages-20171231 日志信息,排查问题. http://man7.or ...