using UnityEngine;
using System.Collections;

public class SlerpImp
{
static float Dot(Quaternion a, Quaternion b)
{
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}

static Quaternion Lerp(Quaternion a, Quaternion b, float t)
{
return new Quaternion(a.x * (1 -t) + b.x * t,
a.y * (1 -t) + b.y * t,
a.z * (1 -t) + b.z * t,
a.w * (1 -t) + b.w * t);
}

static Quaternion Inverse(Quaternion a)
{
a.x = -a.x;
a.y = -a.y;
a.z = -a.z;

return a;
}

public static Quaternion Slerp1(Quaternion a, Quaternion b, float t)
{
t = Mathf.Clamp01 (t);

float similar = Dot(a,b);
float sign = 1.0f;
if(similar >= 1f - Mathf.Epsilon)
return a == Quaternion.identity ? Quaternion.identity : Lerp(a,b,t);
else if (similar < 0f)
{
a.x = -a.x;
a.y = -a.y;
a.z = -a.z;
a.w = -a.w;
sign = -1f;
}

Quaternion result = Quaternion.identity;

// 0-pi, otherwise, inverse the xyz axis space
float aw = Mathf.Acos(a.w);
float bw = Mathf.Acos(b.w);

float saw = new Vector3(a.x, a.y, a.z).magnitude;
float sbw = new Vector3(b.x, b.y, b.z).magnitude;

aw = saw != 0.0f ? aw * (1 - t) / saw : 0.0f;
bw = sbw != 0.0f ? bw * t / sbw : 0.0f;
result.x = a.x * aw + b.x * bw;
result.y = a.y * aw + b.y * bw;
result.z = a.z * aw + b.z * bw;

Vector3 v = new Vector3(result.x, result.y, result.z);
float theta = v.magnitude;
if (theta == 0f)
return Quaternion.identity;

float sintheta = sign * Mathf.Sin(theta) / theta;
result.x *= sintheta;
result.y *= sintheta;
result.z *= sintheta;
result.w = sign * Mathf.Cos(theta);

return result;
}

public static Quaternion Slerp2(Quaternion a, Quaternion b, float t)
{
t = Mathf.Clamp01 (t);

float sign = 1.0f;
float similar = Dot(a,b);
if(similar >= 1f - Mathf.Epsilon)
return a == Quaternion.identity ? Quaternion.identity : Lerp(a,b,t);
else if (similar < 0f)
{
a.x = -a.x;
a.y = -a.y;
a.z = -a.z;
a.w = -a.w;
sign = -1.0f;
}

Quaternion result = Inverse(a);
result *= b;
Vector3 v = new Vector3(result.x, result.y, result.z);
float sintheta = v.magnitude;
// 0-pi, otherwise, inverse the xyz axis space
float theta = Mathf.Acos(result.w);
theta *= t;
sintheta = sign * Mathf.Sin(theta) / sintheta;
result.x *= sintheta;
result.y *= sintheta;
result.z *= sintheta;
result.w = sign * Mathf.Cos(theta);
return a * result;
}

}

[ExecuteInEditMode]
public class SlerpTest : MonoBehaviour {

public int callTimes = 1000000;
[Range(0,1)]
public float t = 0.3f;
// Use this for initialization
void Start ()
{

int callCount = callTimes;
float time = Time.realtimeSinceStartup;
while(callCount-- > 0)
{
SlerpImp.Slerp1(this.transform.rotation, Camera.main.transform.rotation, t);
}
Debug.LogWarning("Slerp1 "+ callTimes + " calls took: " + (Time.realtimeSinceStartup - time));

callCount = callTimes;
time = Time.realtimeSinceStartup;
while(callCount-- > 0)
{
SlerpImp.Slerp2(this.transform.rotation, Camera.main.transform.rotation, t);
}
Debug.LogWarning("Slerp2 "+ callTimes + " calls took: " + (Time.realtimeSinceStartup - time));

callCount = callTimes;
time = Time.realtimeSinceStartup;
while(callCount-- > 0)
{
Quaternion.Slerp(this.transform.rotation, Camera.main.transform.rotation, t);
}
Debug.LogWarning("UnityS "+ callTimes + " calls took: " + (Time.realtimeSinceStartup - time));
}

void Update()
{
Debug.LogWarning("------------------");
Debug.LogWarning("Slerp1: " + SlerpImp.Slerp1(this.transform.rotation, Camera.main.transform.rotation, t).eulerAngles);
Debug.LogWarning("Slerp2: " + SlerpImp.Slerp2(this.transform.rotation, Camera.main.transform.rotation, t).eulerAngles);
Debug.LogWarning("UnitySLerp: " + Quaternion.Slerp(this.transform.rotation, Camera.main.transform.rotation, t).eulerAngles);
}
}

Two kinds of Quaternion SlerpImp (Unity)的更多相关文章

  1. C#程序员整理的Unity 3D笔记(十):Unity3D的位移、旋转的3D数学模型

    遇到一个想做的功能,但是实现不了,核心原因是因为对U3D的3D数学概念没有灵活吃透.故再次系统学习之—第三次学习3D数学. 本次,希望实现的功能很简单: 如在小地图中,希望可以动态画出Player当前 ...

  2. 用好lua+unity,让性能飞起来——lua与c#交互篇

    前言 在看了uwa之前发布的<Unity项目常见Lua解决方案性能比较>,决定动手写一篇关于lua+unity方案的性能优化文. 整合lua是目前最强大的unity热更新方案,毕竟这是唯一 ...

  3. [Unity Quaternion]四元数Quaternion的计算方式

    什么是Quaternion四元数 1843年,William Rowan Hamilton发明了四元数,但直到1985年才有一个叫Ken Shoemake的人将四元数引入计算机图形学处理领域.四元数在 ...

  4. 【Unity技巧】四元数(Quaternion)和旋转

    四元数介绍 旋转,应该是三种坐标变换--缩放.旋转和平移,中最复杂的一种了.大家应该都听过,有一种旋转的表示方法叫四元数.按照我们的习惯,我们更加熟悉的是另外两种旋转的表示方法--矩阵旋转和欧拉旋转. ...

  5. 【Unity】6.8 Quaternion类(四元数)

    分类:Unity.C#.VS2015 创建日期:2016-04-20 一.四元数的概念 四元数包含一个标量分量和-个三维向量分量,四元数Q可以记作: Q=[w,(x,y,z)] 在3D数学中使用单位四 ...

  6. 【Unity编程】四元数(Quaternion)与欧拉角

    版权声明:本文为博主原创文章,欢迎转载.请保留博主链接:http://blog.csdn.net/andrewfan 欧拉旋转.四元数.矩阵旋转之间的差异 除了欧拉旋转以外,还有两种表示旋转的方式:矩 ...

  7. unity, 由Matrix4x4提取Quaternion和Vector3 及 由Quaternion,Vector3构造Matrix4x4

    一,由Matrix4x4提取Quaternion和Vector3 Quaternion getRotationFromMatrix(Matrix4x4 m) {         return Quat ...

  8. unity quaternion vector

    做脚印呢 做了曲面细分和decal两种 先用正交camera生成 高度图 采样uv由pos 从world到camera space生成 unity对tessellation的支持限制还是比较大的 只能 ...

  9. Unity string 转换为 Quaternion

    public Quaternion QuaternionParse(string name) { name = name.Replace("(", "").Re ...

随机推荐

  1. python基础知识九

    sys模块 sys模块包含系统对应的功能.我们已经学习了sys.argv列表,它包含命令行参数. 命令行参数 例14.1 使用sys.argv #!/usr/bin/python # Filename ...

  2. Activity是如何挂载Pargment的Day35

    Activity是如何挂载Pargment的Day35 mebile5.0 1.Fragment优化早上任务 思路 一个主Activity 短信群发器 中秋给朋友发短信,都能够加上他们的姓名,这样他们 ...

  3. mvc5 + ef6 + autofac搭建项目(三)

    前面已经基本完成了框架的搭建,后面就是实现了,后面主要说下前端的东西bootstrap的使用和相关插件. 看图: 实现比较简单,在主页面只引入共用部分的 js等相关包,毕竟不是所有页面都需要列表以及其 ...

  4. 初学时的shell

    学习期间写过一些shell脚本, 测试过程:vi test.sh 后把程序写入其中,保存退出.然后改变文件属性:chmod +x test.sh 最后执行:./test.shfor语句测试:1)#!/ ...

  5. BFC探秘

    今天面试被问到了BFC,听到这个缩略词我是懵比的,啥东西?还是太年轻太简单啊.于是面试结束之后搜了几篇博客看了下,看完有一种豁然开朗的感觉,一些之前未能理解的CSS元素行为也知其所以然了.顺便说一下, ...

  6. Javascript面试题浅析

    分享几道JavaScript相关的面试题. 字符串反转 这这里提供了两种解题思路.如果各位读者还有其他的思路,可以分享交流! 第一方法: function reverse(str){ var sp = ...

  7. python学习笔记--随时更新

    # coding=GBK score = 90 if score >= 80: print("好") elif score >= 60: print("及格& ...

  8. 实习之vim基本学习

    最近实习学到了写vim的基本用法,记录一下 批量注释 ctrl+v进入列模式,按“I”进入插入模式,按// #等在每行开头插入注释,esc 批量去除注释 ctrl + v 进入列模式,按“x”即可. ...

  9. APACHE 与IIS同时存在的情况下,给APACHE添加反向代理 共用80端口

    一.首先打开IIS,将IIS的端口改成81,不要让IIS占用了80端口 二.打开APACHE的httpd.conf配置文件,将里面的端口配置成80 三.打开APACHE的虚拟目录配置文件,如:http ...

  10. 如何在Webstorm中添加js库 (青瓷H5游戏引擎)

    js等动态语言编码最大的缺点就是没有智能补全代码,webstorm做到了. qici_engine作为开发使用的库,如果能智能解析成提示再好不过了,经测试80%左右都有提示,已经很好了. 其他js库同 ...