Del*_*eak 1 c# unity-game-engine
为了我的研究,我需要知道 Unity 到底是如何实现 Vector3 的 Slerp 函数的。
\nUnity3D文档描述,输入向量被视为方向而不是空间中的点。但它并没有解释,如果实习生使用了四元数。
\nUnity3D-C#-Reference在这里提到了 Vector3.Slerp :
\n[FreeFunction("VectorScripting::Slerp", IsThreadSafe = true)] extern public static Vector3 Slerp(Vector3 a, Vector3 b, float t);\nRun Code Online (Sandbox Code Playgroud)\n但是,我在任何地方都找不到定义。我认为它\xc2\xb4s 是一个C++参考。Unity 的 C++ 代码只有在获得许可证后才可用(据我所知)。
\n有人可以帮我确定这个问题吗?我需要知道的是Unity3D内部是否使用Vector3.Slerp(Vector3, Vector3, float)的四元数。
\n预先感谢您的帮助。
\n我当然不确定,因为我们没有这些内部方法的源代码,但我很确定他们不会使用Quaternion性能相当低下的方法,而是使用纯粹且简单的基于浮点的数学,如正弦、余弦等。这c#看起来有点类似于这里提到的解决方案
Run Code Online (Sandbox Code Playgroud)Vector3 Slerp(Vector3 start, Vector3 end, float percent) { // Dot product - the cosine of the angle between 2 vectors. float dot = Vector3.Dot(start, end); // Clamp it to be in the range of Acos() // This may be unnecessary, but floating point // precision can be a fickle mistress. Mathf.Clamp(dot, -1.0f, 1.0f); // Acos(dot) returns the angle between start and end, // And multiplying that by percent returns the angle between // start and the final result. float theta = Mathf.Acos(dot) * percent; Vector3 RelativeVec = end - start * dot; RelativeVec.Normalize(); // Orthonormal basis // The final result. return ((start*Mathf.Cos(theta)) + (RelativeVec * Mathf.Sin(theta))); }
虽然他们的当然是在底层c++环境中并且不会使用Mathf,因此性能应该更好一点。
| 归档时间: |
|
| 查看次数: |
4319 次 |
| 最近记录: |