Giter Site home page Giter Site logo

kosrud / dq-skinning-for-unity Goto Github PK

View Code? Open in Web Editor NEW
195.0 13.0 22.0 1.55 MB

License: BSD 3-Clause "New" or "Revised" License

C# 49.20% HLSL 4.30% ShaderLab 46.50%
compute-shaders skinnedmeshrenderer unity skinning animation dual-quaternion quaternions csharp hlsl cginc

dq-skinning-for-unity's Introduction

Dual quaternion skinning for Unity

Features

  • GPU skinning with compute shaders
  • blend shape support (calculations performed in compute shader)
  • works with any platform that supports compute shaders
  • preserves volume with deformation (look comparison)
  • zero GC allocations per frame
  • original bulging compensation method

Comparison DQ vs built-in linear

Gif Difference

Bulging compensation demo

Unity version

The script was tested with following Unity versions:

  • 2020.1.0a13.1443 (earlier versions do not support #pragma multi_compile in compute shaders)
  • 2020.2.0f1 (briefly tested - seems to work just fine)

How to set up

  • Create a skinned character with SkinnedMeshRenderer component
  • Add DualQuaternionSkinner.cs component (it will require a MeshFilter component)
  • Enable mesh Read/Write in import settings

  • All materials of the mesh should use a special shader to apply vertex positions. The shader is MadCake/Material/Standard hacked for DQ skinning
  • In order for bulging compensation to work properly, all the character's bones must have one local axis (consistent for all bones) aligned with the limb controlled by the corresponding bone.
Bones properly aligned Bones not aligned
  • Whichever local axis of the bones was aligned with the limbs must be selected in the script gui:

If bulging is increased instead of decreased, select same axis with different direction (X => Negative X)

Common problems

The script is programmed to automatically detect common setup problems. Check out the editor:

Warning

You will not see any effect in edit mode.
The scipt only works in play mode.
If you see no effect in play mode verify that you are using the right shader.

Why do i need SkinnedMeshRenderer?

My scripts uses SkinnedMeshRenderer to extract an array of bones from it. Yep, that's it.
The order of bones is unpredictable and does not depend on their hierarchy.
Only SkinnedMeshRenderer knows it    ¯\_(ツ)_/¯

After extracting the bone array in Start() my script disables SkinnedMeshRenderer component as it is no longer needed. All the animations are performed by the script. You can verify it in the editor after hitting play button.

How do I use custom shaders?

Alas it's complicated.
I added comments to "Standard hacked for DQ skinning" marking the alterations i made to the Standard shader.
You can try to do the same with your own shader to make it work with the script.

Feel free to contact me in this thread at unity forum if you need help.

I would also like to hear about your projects that use my script and your experience with it.

Known bugs

Must use cullingMode = AlwaysAnimate in Animator. Otherwise, the mesh is treated as permanently invisible.

You can write a short script that will toggle cullingMode based on visibility to get proper culling.

Performance

During my testing the amount of time spent on actual skinning was negligible compared to the amount of time extracting localToWorldMatrix from every bone in the hierarchy.

As long as you are not creating hundreds of characters with complex rigs (no matter the polycount) there should be no significant performance hit.

If anyone knows how to optimize extracting localToWorldMatrix of the bones please create an issue or message me on unity forum.

Works A LOT faster with IL2CPP, about 30% slower than built-in skinning in worst-case scenario according to my testing.

API

Documentation

Future plans

  • Reduce numthreads for compatibility
  • Add instancing support
  • Allow controlling the strength of bulging compensation with a texture
  • Implement simple runtime switching between DualQuaternionSkinned and built-in SkinnedMeshRenderer
  • Implement proper animation culling (see known bugs)
  • It might make sense to group the data from all instances of the script into one batch and run the compute shaders only once per frame regardless of how many animated characters you have

Discussion

If you have any questions, ideas, bug reports, or just want to discuss the script, you can contact me on unity forum

Bulging compensation method

The bulging compensation method used is described in the paper.

Notes:

  • In formula (6) there is a mistake, x should be replaced by w_2 (there is no variable named x)
  • Since publishing the article I found a better polynomial instead of (9), the coefficients are as follows: 2.2; -9.6; 10.4
  • In formula (12) there are weird brackets around V_bisector. This is a software bug, they should be ignored. This happens multiple times later in the article.
  • The stretched region of the model in Fig (7), that looks weird even after applying the fix (though is no longer jagged), is actually caused by using improperly prepared armature. The spine and the shoulder have different local axes aligned along the joint. This contradicts one of the requirements listed at the top of page 17.

dq-skinning-for-unity's People

Contributors

kosrud avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dq-skinning-for-unity's Issues

is workaround still needed?

this.shaderComputeBoneDQ = (ComputeShader)Instantiate(this.shaderComputeBoneDQ); // bug workaround
this.shaderDQBlend = (ComputeShader)Instantiate(this.shaderDQBlend); // bug workaround
this.shaderApplyMorph = (ComputeShader)Instantiate(this.shaderApplyMorph); // bug workaround

More information should be provided on the bug, investigate if the workaround is still needed.

Get the mesh of DQ blending skinning

I have implemented the dqbs in my own project. And I want to get the mesh of human model of dqbs in obj format. However, I found i get the obj model of lbs. How can i get the mesh of dqbs? Thank you.

dqbs in unity
export lbs in obj

boneOrientationVector should be private

/// <summary>
/// Bone orientation is required for bulge-compensation.<br>
/// Do not set directly, use custom editor instead.
/// </summary>
public Vector3 boneOrientationVector = Vector3.up;

Use private field with SerializeField and public getter/setter functions. Do not use property, use functions to emphasize that changing the value requires additional internal logic and can be expensive.

Logic for setter:

var newOrientation = (BoneOrientation)EditorGUILayout.EnumPopup("Bone orientation: ", currentOrientation);
if (this.dqs.boneOrientationVector != this.boneOrientationVectors[newOrientation])
{
this.dqs.boneOrientationVector = this.boneOrientationVectors[newOrientation];
this.dqs.UpdatePerVertexCompensationCoef();
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.