Unity-Technologies / com.unity.demoteam.hair

An integrated solution for authoring / importing / simulating / rendering strand-based hair in Unity.
Other
719 stars 97 forks source link

Movement of Hair is jittery #4

Closed qlee01 closed 1 year ago

qlee01 commented 2 years ago

Moving the Gameobject with HairInstance results in jittery movement of Hair. I first thought it is due to me using FixedUpdate to move my physics based Game3Objects / Rigidbodies, but I changed the Hair code to be executed in FixedUpdate, and issue remains. Also changing the simulation frequency does not resolve this.

qlee01 commented 1 year ago

After more investigation, changing the code to execute in "FixedUpdate" does resolve the issue. Please, add an option to execute either in (Late)Update or in FixedUpdate, later is absolutely necessary to use the Hair in a Physics based environment.

fingerx commented 1 year ago

@qlee01 thanks job,want to know more detials for fixed this,thanks

qlee01 commented 1 year ago

@qlee01 thanks job,want to know more detials for fixed this,thanks

can you explain better what you mean? What I describe in this issue is that the Hair does not play nicely if you update the position within FixedUpdate, as the class "HairInstance" does update on LateUpdate. In order to fix this, HairInstance.cs needs to be changed to make it switchable whether the hair gets updated during LateUpdate or FixedUpdate. This is a usual thing in many components / libraries for like animation, cloth, and hair simulation, as in some projects everything is driven by Physics, and in this case updated in FixedUpdate.

fingerx commented 1 year ago

ok thanks for replay, i understand. I didn't know to modify that script before,but now understand. thanks agian. I would like to ask another question. The character uses abc file to generate a file and attach it to the character, but when the character moves, the hair is not correct and will fly around. I try many parameters settting , but there is no effect, but static objects will ok. thanks

qlee01 commented 1 year ago

ok thanks for replay, i understand. I didn't know to modify that script before,but now understand. thanks agian. I would like to ask another question. The character uses abc file to generate a file and attach it to the character, but when the character moves, the hair is not correct and will fly around. I try many parameters settting , but there is no effect, but static objects will ok. thanks

we should not use the Github issues to discuss general use of the library, especially not in a thread with completely different topic, as this will be no help for the developers of the library. Your best bet would be to ask the question on Unity forum.

fingerx commented 1 year ago

@qlee01 thanks,I think the unity demoteam ditched the actual users, thanks for the advice

fuglsang commented 1 year ago

Hi. The hair simulation is stepped [minSteps, maxSteps] for every rendered frame, with the simulation "time step" being defined by the frequency. Currently, if you want to ensure that it steps every visual frame when the simulation frequency is lower than the render frequency, currently you have to set minSteps to 1. This will change in the future.

qlee01 commented 1 year ago

@fuglsang I actually did set it to 1 already at simulation frequency of 60 Hz; still I get jittery movement. This seems to be more prominent whenever the GPU load gets higher.

fuglsang commented 1 year ago

@qlee01 Hmm, I see -- that does not sound right. Please post the steps to reproduce this behavior, so I can take a closer look at what is happening there.

qlee01 commented 1 year ago

@qlee01 Hmm, I see -- that does not sound right. Please post the steps to reproduce this behavior, so I can take a closer look at what is happening there.

This is not easy. In my scenario, this happens all the time, no matter what parameters I choose. The prerequisites:

I basically tried all thinkable approaches to minimize jitter, but it seems something with position or rotation interpolation seems to be weird inside the Hair package. I also don't think it is based on collision, but with collision on it can become worse than without.

qlee01 commented 1 year ago

@fuglsang I had a bit deeper look. Seems the collider are making things much worse. I think it would be good to normalize the collision response by delta time, I added something like this to test it out, and in my case gives better results:

void ApplyCollisionConstraint(inout float3 p)
{
    float3 d = 0.0;
    SolveCollisionConstraint(1.0, p, d);
    p += d * _DT * _CONSTANT_COLLISION_RESPONSE;
}

void ApplyCollisionFrictionConstraint(const float friction, const float3 x0, inout float3 p)
{
    float3 d = 0.0;
    SolveCollisionFrictionConstraint(friction, x0, 1.0, p, d);
    p += d * _DT * _CONSTANT_COLLISION_RESPONSE;
}

EDIT: The constant factor might not be needed, as we can compensate via "collision margin".

qlee01 commented 1 year ago

@fuglsang I was finally able to resolve all jittery issues, but only by changing the code of HairInstance, and putting an execution order to the meta file. I would suggest the following easy solution: Offer an "explicit" update mode, where we are able to call the update manually, instead of relying on update happening on "LateUpdate" with no possibility to change the execution order. This would give highest flexibility, and with this all synchronization issues would be easily solvable without changing the code.

fuglsang commented 1 year ago

@qlee01 Thanks for the update! I've added an option to let application code explicitly trigger updates/dispatch -- I hope this fits your needs, otherwise please reopen the issue. :)

qlee01 commented 1 year ago

@fuglsang great, thanks, will test it out asap.

qlee01 commented 1 year ago

did a test, works as expected. FYI, I still need to add the little change with _DT (delta time) in the constraints compute shader to get rid of artifacts. I guess it's due to the issues with Mesh2SDF producing artifacts on more complex meshes.

I use the new update method like this, hope this is correct (sorry for the formatting, did not find an option for line breaks):

void UpdateHair(float deltaTime) { var cmd = CommandBufferPool.Get(); { if (hairInstance.DispatchUpdate(cmd, CommandBufferExecutionFlags.None, deltaTime)) { Graphics.ExecuteCommandBuffer(cmd); } } CommandBufferPool.Release(cmd); }