[Mono] implement Transform.InterpolateWith()

This commit is contained in:
Kelly Thomas 2018-09-15 10:26:11 +08:00
parent 43a1b93d71
commit d4b2423428
3 changed files with 54 additions and 0 deletions

3
.gitignore vendored
View file

@ -91,6 +91,9 @@ bld/
*.debug
*.dSYM
# Visual Studio cache/options directory
.vs/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*

View file

@ -165,6 +165,38 @@ namespace Godot
);
}
internal Quat RotationQuat()
{
Basis orthonormalizedBasis = Orthonormalized();
real_t det = orthonormalizedBasis.Determinant();
if (det < 0)
{
// Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
orthonormalizedBasis = orthonormalizedBasis.Scaled(Vector3.NegOne);
}
return orthonormalizedBasis.Quat();
}
internal void SetQuantScale(Quat quat, Vector3 scale)
{
SetDiagonal(scale);
Rotate(quat);
}
private void Rotate(Quat quat)
{
this *= new Basis(quat);
}
private void SetDiagonal(Vector3 diagonal)
{
_x = new Vector3(diagonal.x, 0, 0);
_y = new Vector3(0, diagonal.y, 0);
_z = new Vector3(0, 0, diagonal.z);
}
public real_t Determinant()
{
return this[0, 0] * (this[1, 1] * this[2, 2] - this[2, 1] * this[1, 2]) -

View file

@ -20,6 +20,25 @@ namespace Godot
return new Transform(basisInv, basisInv.Xform(-origin));
}
public Transform InterpolateWith(Transform transform, real_t c)
{
/* not sure if very "efficient" but good enough? */
Vector3 sourceScale = basis.Scale;
Quat sourceRotation = basis.RotationQuat();
Vector3 sourceLocation = origin;
Vector3 destinationScale = transform.basis.Scale;
Quat destinationRotation = transform.basis.RotationQuat();
Vector3 destinationLocation = transform.origin;
var interpolated = new Transform();
interpolated.basis.SetQuantScale(sourceRotation.Slerp(destinationRotation, c).Normalized(), sourceScale.LinearInterpolate(destinationScale, c));
interpolated.origin = sourceLocation.LinearInterpolate(destinationLocation, c);
return interpolated;
}
public Transform Inverse()
{
Basis basisTr = basis.Transposed();