Skip to content

Commit

Permalink
code cleanup, better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
MadCake committed May 4, 2020
1 parent 078e90c commit aaad402
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
6 changes: 5 additions & 1 deletion Code/AssetPostProcessorReorderBones.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections.Generic;
using System.Linq;

#if UNITY_EDITOR

/// <summary>
/// Sorts bone indexes in imported meshes.<br>
/// SkinnedMeshRenderer requires bone indexes to be sorted based on hierarchy.
Expand Down Expand Up @@ -75,4 +77,6 @@ private static int CompareTransform(Transform A, Transform B)

return 0;
}
}
}

#endif
33 changes: 16 additions & 17 deletions Code/DQ skinning/DualQuaternionSkinner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ struct DualQuaternion
/// </summary>
public bool started { get; private set; } = false;

DualQuaternion[] poseDualQuaternions;
Matrix4x4[] poseMatrices;

ComputeBuffer bufPoseMatrices;
Expand Down Expand Up @@ -360,7 +359,6 @@ void GrabMeshFromSkinnedMeshRenderer()

this.shaderDQBlend.SetInt("textureWidth", textureWidth);

this.poseDualQuaternions = new DualQuaternion[this.mf.mesh.bindposes.Length];
this.poseMatrices = new Matrix4x4[this.mf.mesh.bindposes.Length];

// initiate textures and buffers
Expand Down Expand Up @@ -415,8 +413,6 @@ void GrabMeshFromSkinnedMeshRenderer()
for (int i = 0; i < vertInfos.Length; i++)
{
vertInfos[i].position = vertices[i];
vertInfos[i].normal = normals[i];
vertInfos[i].tangent = tangents[i];

vertInfos[i].boneIndex0 = boneWeights[i].boneIndex0;
vertInfos[i].boneIndex1 = boneWeights[i].boneIndex1;
Expand All @@ -439,6 +435,22 @@ void GrabMeshFromSkinnedMeshRenderer()
vertInfos[i].compensation_coef = Vector3.Cross(toBone, boneDirection).magnitude;
}

if (normals.Length > 0)
{
for (int i = 0; i < vertInfos.Length; i++)
{
vertInfos[i].normal = normals[i];
}
}

if (tangents.Length > 0)
{
for (int i = 0; i < vertInfos.Length; i++)
{
vertInfos[i].tangent = tangents[i];
}
}

this.bufVertInfo.SetData(vertInfos);
this.shaderDQBlend.SetBuffer(this.kernelHandleDQBlend, "vertex_infos", this.bufVertInfo);

Expand Down Expand Up @@ -579,19 +591,6 @@ void Update()

for (int i = 0; i < this.bones.Length; i++)
{
this.poseDualQuaternions[i].rotationQuaternion = this.bones[i].rotation;

Vector3 pos = this.bones[i].position;

// could use float3 instead of float4 for position but NVidia says structures not aligned to 128 bits are slow
// https://developer.nvidia.com/content/understanding-structured-buffer-performance
this.poseDualQuaternions[i].position = new Vector4(
pos.x,
pos.y,
pos.z,
0
); // not a proper quaternion, just a position. shader handles the rest

this.poseMatrices[i] = this.bones[i].localToWorldMatrix;
}

Expand Down
4 changes: 4 additions & 0 deletions Code/DQ skinning/Inspector/DualQuaternionSkinnerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System.Collections.Generic;

#if UNITY_EDITOR

/// <summary>
/// Provides custom inspector for {@link DualQuaternionSkinner}
/// </summary>
Expand Down Expand Up @@ -178,3 +180,5 @@ bool CheckProblems()
return false;
}
}

#endif

0 comments on commit aaad402

Please sign in to comment.