-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
243 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
PregnancyPlus/PregnancyPlus.Core/tools/BindPose/BindPose.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using UnityEngine; | ||
|
||
|
||
//Contains methods to extract bindpose data from a mesh | ||
public static class BindPose | ||
{ | ||
|
||
/// <summary> | ||
/// Get a bindpose matrix of a single SMR bone | ||
/// </summary> | ||
public static Matrix4x4 GetBindPose(Matrix4x4 smrMatrix, Matrix4x4 bindPose) | ||
{ | ||
return smrMatrix * bindPose.inverse; | ||
} | ||
|
||
//Overload for the above using a SMR directly | ||
public static Matrix4x4 GetBindPose(SkinnedMeshRenderer smr, Matrix4x4 bindPose) | ||
{ | ||
return GetBindPose(smr.transform.localToWorldMatrix, bindPose); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Get the scale from the first bindpose matrix | ||
/// </summary> | ||
public static Matrix4x4 GetScale(SkinnedMeshRenderer smr) | ||
{ | ||
if (smr == null) | ||
return Matrix4x4.identity; | ||
|
||
//For a bindpose check for scale (just grab the first if any exists) | ||
var bindposes = smr.sharedMesh.bindposes; | ||
if (bindposes.Length <= 0) | ||
return Matrix4x4.identity; | ||
|
||
//Note: This assumes the scale is the same for all bindposes. It's worked so far ... | ||
return Matrix.GetScaleOnlyMatrix(bindposes[0]); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Get the worldspace rotation of a bindpose | ||
/// </summary> | ||
public static Quaternion GetRotation(SkinnedMeshRenderer smr, Matrix4x4 bindpose) | ||
{ | ||
return Matrix.GetRotation(smr.transform.localToWorldMatrix * bindpose.inverse); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Get the average rotation of all the bindposes in localspace | ||
/// </summary> | ||
public static Quaternion GetAverageRotation(SkinnedMeshRenderer smr) | ||
{ | ||
//For a bindpose check for any non 0 rotation repeated more than a few times | ||
var bindposes = smr.sharedMesh.bindposes; | ||
var totalX = 0f; | ||
var totalY = 0f; | ||
var totalZ = 0f; | ||
var totalW = 0f; | ||
|
||
//Add up all the rotations for each bindpose | ||
for (int i = 0; i < bindposes.Length; i++) | ||
{ | ||
var bindposeRotation = GetRotation(smr, bindposes[i]); | ||
//We want to ignore character rotation, so convert to local rotation | ||
var localRotation = Quaternion.Inverse(smr.transform.rotation) * bindposeRotation; | ||
|
||
//Round them to the nearest 90 degree axis since most offset rotations are at 90 degree intervals | ||
var currentRotation = Rotation.AxisRound(localRotation); | ||
totalX+=currentRotation.x; | ||
totalY+=currentRotation.y; | ||
totalZ+=currentRotation.z; | ||
totalW+=currentRotation.w; | ||
} | ||
|
||
//Compute the average rotation | ||
var averageRotation = new Quaternion( | ||
x: totalX/bindposes.Length, | ||
y: totalY/bindposes.Length, | ||
z: totalZ/bindposes.Length, | ||
w: totalW/bindposes.Length | ||
); | ||
|
||
//Round the final rotation to the nearest 90 degree axis | ||
var roundedRotation = Rotation.AxisRound(averageRotation); | ||
|
||
return roundedRotation; | ||
} | ||
} |
97 changes: 46 additions & 51 deletions
97
PregnancyPlus/PregnancyPlus.Core/tools/BindPose/Matrix.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,52 @@ | ||
using UnityEngine; | ||
|
||
namespace KK_PregnancyPlus | ||
{ | ||
|
||
//Typical matrix methods, but some of these do not exist in unity 5.2 (KK) | ||
public static class Matrix | ||
{ | ||
|
||
/// <summary> | ||
/// Returns the position Vector3 of a Matrix4x4 | ||
/// </summary> | ||
public static Vector3 GetPosition(Matrix4x4 matrix) | ||
{ | ||
return matrix.GetColumn(3); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Returns the rotation Quaternion of a Matrix4x4 | ||
/// </summary> | ||
public static Quaternion GetRotation(Matrix4x4 matrix) | ||
{ | ||
return Quaternion.LookRotation(matrix.GetColumn(2), matrix.GetColumn(1)); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Returns the scale Vector3 of a Matrix4x4 | ||
/// </summary> | ||
public static Vector3 GetScale(Matrix4x4 matrix) | ||
{ | ||
var scaleMatrix = GetScaleMatrix(matrix); | ||
return new Vector3(scaleMatrix.m00, scaleMatrix.m11, scaleMatrix.m22); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Returns the scale of a Matrix4x4, excluding position and rotation | ||
/// </summary> | ||
public static Matrix4x4 GetScaleMatrix(Matrix4x4 matrix) | ||
{ | ||
return GetPositionAndRotationMatrrix(matrix).inverse * matrix; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Get position and rotation, excluding scale of a Matrix4x4 | ||
/// </summary> | ||
public static Matrix4x4 GetPositionAndRotationMatrrix(Matrix4x4 matrix) | ||
{ | ||
return Matrix4x4.TRS(GetPosition(matrix), GetRotation(matrix), Vector3.one); | ||
} | ||
|
||
//Typical matrix methods, but some of these do not exist in unity 5.2 (KK) | ||
public static class Matrix | ||
{ | ||
/// <summary> | ||
/// Returns the position in Vector3 of a Matrix4x4 | ||
/// </summary> | ||
public static Vector3 GetPosition(Matrix4x4 matrix) | ||
{ | ||
return matrix.GetColumn(3); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Returns the rotation as Quaternion of a Matrix4x4 | ||
/// </summary> | ||
public static Quaternion GetRotation(Matrix4x4 matrix) | ||
{ | ||
return Quaternion.LookRotation(matrix.GetColumn(2), matrix.GetColumn(1)); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Returns the scale as Vector3 of a scale only Matrix4x4 | ||
/// </summary> | ||
public static Vector3 GetScale(Matrix4x4 matrix) | ||
{ | ||
var scaleMatrix = GetScaleOnlyMatrix(matrix); | ||
return new Vector3(scaleMatrix.m00, scaleMatrix.m11, scaleMatrix.m22); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Returns the scale of a Matrix4x4, excluding position and rotation | ||
/// </summary> | ||
public static Matrix4x4 GetScaleOnlyMatrix(Matrix4x4 matrix) | ||
{ | ||
return GetPositionAndRotationMatrix(matrix).inverse * matrix; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Get position and rotation, excluding scale of a Matrix4x4 | ||
/// </summary> | ||
public static Matrix4x4 GetPositionAndRotationMatrix(Matrix4x4 matrix) | ||
{ | ||
return Matrix4x4.TRS(GetPosition(matrix), GetRotation(matrix), Vector3.one); | ||
} | ||
|
||
} |
Oops, something went wrong.