Search Unity

HairWorks Port For Unity

Discussion in 'General Discussion' started by theprotonfactor, Nov 8, 2015.

  1. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    Hey guys, in case any of you haven't heard the news, Unity Japan has developed a Nvidia HairWorks port for Unity. It's been stagnant for a while now so I've continued their work. You can grab it from my GitHub page:

    https://github.com/theprotonfactor/NVIDIAHairWorksIntegration

    So what features does mine have that theirs doesn't ?
    Mine supports the ability to:
    Light hair with spot lights.
    Change hair length and length noise.
    Change hair alpha falloff.
    Add specular glint.
    Make use of the built in light probes feature for global illumination.
    Make use of textures for root color, tip color, specularity and many more.
    Shadow casting and receiving (coming soon).
    Self shadowing (coming soon).
    And it has a better overall look thanks to some shader modifications.

    I have also started work on a hair exporter for Blender that exports to the HairWorks format (.apx) which you guys can grab here :

    https://github.com/theprotonfactor/HairWorks-Exporter-For-Blender

    All relevant info in the readme.

    and also a video tutorial:



    I'm posting this so that the community can try out the features and hopefully report back to help improve it. Take note that it is a work in progress and it's still slow and unoptimized.
     
    Last edited: Dec 8, 2015
    twobob, aaiman, Glader and 5 others like this.
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I'm assuming this works similarly to PhysX in that it's CPU Based (so not limited to NVidia hardware), is that correct?
     
  3. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    Hey Dustin,

    Yes, it's not limited to Nvidia hardware, but no it's not CPU accelerated. I believe the implementation works via compute shaders. So it's most certainly GPU accelerated, but it should run on AMD and Nvidia hardware. The only limitation to compute shaders in Unity I believe is that it's DX11 exclusive. So it might in fact be Windows exclusive. I'm not 100% sure though, I've never tested compute shaders on Mac or Linux.
     
    Last edited: Nov 8, 2015
  4. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    I've been using the plugin for about a month and a half now, and it works. Only thing I really want though is for the hair shadows to work correctly in Unity. So far, they havn't updated the github plugin to be able to do shadows with the hair (in Unity).

    If anyone knows how to write an HLSL shader that will do shadows with the plugin, that would be great. Until then, we have to wait for Unity Japan to update it themselves. I mean the fact that it works at all is a huge plus of course.
     
  5. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    I might have a stab at it, but I think the problems might have to do with the alpha sorting which is something that's also troubling the tressFX port for Unity. That reminds me that I should probably look into making a tressFX exporter for Blender as well for those who would prefer it over HairWorks.
     
  6. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    That would be awesome Protonfactor. Right now, I'm having to turn off all shadow creators and receivers in Unity, otherwise the Nivida Hairworks assets start blinking like they're missing rendering frames for some reason due to shadows.
    All in all, shadows are causing problems with the hairworks assets. My guess is, if the plugin/shader is updated to work with shadows, that these problems will disappear. It doesn't sound easy though. I'm rooting for you :)
     
  7. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Compute shaders will be available on OpenGL platforms once UT upgrades to 4.x, which is 5.3 IIRC.
     
    Arkade likes this.
  8. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    Then it's most definitely to do with the transparency/alpha. Some time ago I was modifying The Blacksmith's soft shadows and I managed to temporarily break it and that also caused flickering on the Blacksmith hair shader. I'll most probably take a look at the HairWorks shaders in a week or so.
     
    Arkade likes this.
  9. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    That's awesome news! Looks like we might finally have a free hair solution that works with all software and hardware configurations!
     
    hopeful likes this.
  10. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    Actually scratch that guys...I've taken a look at the plugin and it's going to be windows only as far as I can tell even when 5.3 ships. The problem is that the entire rendering process of the hair is handled plugin side and it's coded for DX11.

    The actual shader is also a binary hlsl file and the compute shaders are embedded in the plugin which means at no point does it touch shaderlab or Unity's internal rendering system and thus unity can't cross compile to OpenGL. So the only way this will work on OpenGL platforms is if the entire plugin is rewritten for OpenGL.

    So for the windows users I've checked the possibility of shadows and it might just be possible. The trick is going to be getting the generated depth map out of hlsl and into unity's internal shadow maps. One thing I can do while I ponder that is to add some nice soft self-shadowing to the hair because that can be done purely in the hlsl. Might also fix the flickering.
     
    Velo222 and Arkade like this.
  11. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    655
    Hmm... how do you tell this forum software you like the good bits of the post but don't like the disappointing parts? ;-)
     
    theprotonfactor likes this.
  12. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Quote the bit you like and then "Like" your own post. Scratch that, guess you can't like your own post.
     
    theANMATOR2b likes this.
  13. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    That would be awesome Protonfactor. I know very little about shaders, but from what you're saying it sounds like the shaders/plugin is not very Unity friendly. If you're able to add self-shadowing that would be a huge step forward in my opinion (as well as if you're able to get rid of the flickering). That would be great.

    It's a bit disappointing that it has to remain Windows only right now. But even Windows only is better than nothing, for sure. Do you think this could change in the future though? Like months, or are we talking years? Windows is the main platform I am personally developing for, but I'd love to be able to include Mac and Linux users eventually.

    Anyways, thanks for the update Proton. I'm glad you're able to make some sense of it. And good luck to you! Keep us posted.
     
  14. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    Well I do know 2 Nvidia devs that manage all of the GameWorks integrations for Unreal Engine 4 (yes, I occasionally go to the dark side) and they recently made an OpenGL port for their voxel GI. So they are certainly looking at getting everything cross-platform but when and if they make a port for hairwokrs I can't say ( I'll ask though). You should note that even if they do then someone will still have to rewrite the unity plugin and that likely won't be me, I'm not good with integrating features if I don't have the engine's source code. *looks passive-aggressively at Unity* The Unreal hairworks port doesn't have any of these problems because it's just injected straight into the source code. Anyway, when I've made some progress I'll share it. I'll probably make a new GitHub repo for my build.
     
  15. shubniggurath

    shubniggurath

    Joined:
    May 10, 2015
    Posts:
    10
    hello, I have a problem using the API, not generating the effect of hair I also id the shader variables, asset_id, instance_id show a null not know if this is normal, any suggestions?
    http://imgur.com/y50qzlF
     
  16. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    Hey, do you have a mesh renderer and mesh filter in there? Also did you download the HairWorks sdk and paste the .dll the readme mentions into the correct folder?
     
    Last edited: Nov 16, 2015
  17. shubniggurath

    shubniggurath

    Joined:
    May 10, 2015
    Posts:
    10
    hello, if I have a mesh renderer and mesh filter and I have the .dll in the correct folder.

    I leave the project into a link if like to review, if you have an example of working in unity Hairworks you can provide me grateful.
    https://mega.nz/#!6thD2LAJ!H64IS11WVZ9tCdXF2sgwJ9zRq3n6GpVYd8foX9WMwAU
     
  18. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    Hey, I think I've found your problem. Looks like your .dll is corrupt because when I pasted mine over it the hair appeared. I also made a modification to the HairInstance script that adds a reset button that will set the mesh renderer and filter to the standard 'probe' filter it comes with. I also removed the Manjaladon and the other stuff you had in the scene because it was causing crashes, I think you just have to much hair there for my poor machine. So you'll have to redownload the .dll from Nvidia, I'm not allowed to redistribute it. I'll attach the updated scripts, I'll also include the changes in my GitHub build when I release my version
     
    Last edited: Nov 17, 2015
    MTDues likes this.
  19. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    Paste the Editor script in HairWorksIntegration\Editor and the other one in HairWorksIntegration\Scripts. You should then see a reset button on the hair instance. It's not necessary to get it working but if you mess up your renderer and filter it will just set it back to default. Other than that you just have to replace your .dll
     

    Attached Files:

  20. shubniggurath

    shubniggurath

    Joined:
    May 10, 2015
    Posts:
    10

    thanks for your help, it works by changing the .dll just need to accommodate the mesh hair, since hair is messy and out of mesh.
     
  21. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    So this is just a quick update...I've managed to kinda stop the flickering in the editor, but not at runtime. So it's not quite fixed yet, but now I can at least look at it without hurting my eyes. The flickering occurs in the editor when it's updated but not when the viewport is redrawn. I've modified it in the editor so that persistent updating is no longer required. I'll upload the new repo if I can get it fully fixed or until I add shadow support, whichever comes first.
     
    Last edited: Nov 18, 2015
  22. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Progress, nice Protonfactor. Thanks for the update.

    I really hope you're able to get it fully fixed, especially for an actual game build (runtime). I was just about to ask you for an update on this thread, but you beat me to it. You're basically our only hope right now, unless Unity Japan re-writes the entire thing soon. :)
     
  23. edwon

    edwon

    Joined:
    Apr 24, 2011
    Posts:
    266
    Hi Everyone,

    I've been trying to get hairworks working as well, specifically for VR, which would be amazing I think. The only problem is the hair is only rendering to one eye on the oculus. Anybody have any ideas on how to get hairworks working with VR?
     
  24. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    Did you remove render textures on the main camera?
     
  25. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    I've found the function that causes the flickering. Now to find out what the hell is wrong with it.
     
    Velo222 likes this.
  26. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    Hey guys, I think I've found the culprit in the flickering but I want someone to test it out just to be sure since I've already modified it considerably. In the params section of your Hair Instance in the inspector there should be a field called Bend Stiffness. Set this to anything above zero and see if the flickering stops. You should still see some holes in the hair though. And make sure simulate is ticked else it will keep flickering.
     
    Last edited: Nov 21, 2015
  27. hopeful

    hopeful

    Joined:
    Nov 20, 2013
    Posts:
    5,682
    For some reason I read that as:

     
    twobob and PROTOFACTOR_Inc like this.
  28. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437

    Purely in the Editor Protonfactor, or test in an actual game build? I need to look at my game again when I get time, and I'll let you know. I only get the flickering when I have other shadow creators on in my scene, so I'll have to turn on some shadows and see what happens.
     
  29. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437

    Hey Proton,

    I did a couple tests so just letting you know some very basic results. Overall, I'm still kind of puzzled.


    First Test
    So first test settings: Bend Stiffness = 0.1, Simulate = On (true), Use Collision = On (true).

    First test, during the game build I did see some flickering, mainly towards the beginning of the game, and when the objects with the hairworks instances are first instantiated into the game. However as I spent more time in the game, the flickering did not happen very often at all -- but it still happened once or twice.



    Second Test
    Second test settings: Bend Stiffness = 1.0, Simulate = On (true), Use Collision = Off (false)

    In the editor, with these settings, I saw no flickering at all, but this is in the editor only. I don't remember (in the editor) with the first settings, if I saw any flickering at all, but I don't think I did either. The editor seems to have no flickering problem whatsoever. However, the very first time I built and ran the game with the second test settings the game completely crashed! So, I ran the game again, and the second time I saw flickering with the first hair instance object instantiated -- but only for about 10 seconds. After this period, I saw no flickering at all. It's almost as if the game "gets used to" the hair and after some time it stabilizes -- which I can't think of a reason why that would be the case.


    Anyways, there are some more scenarios I'd like to test out, and if I do, I'll let you know as well. Let me know if you have any ideas or if you're able to resolve it any further.

    Thanks.
     
  30. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    This is quite interesting...I have just built the default scene and I have no flickering issues whatsoever even with the bend stiffness set to zero, but it still flickers for me when I play in the editor. This was with an entirely new shader so that might be the reason. Fingers crossed that this might the fix we're waiting for!

    I should probably explain things a little bit at this stage. The reason I asked you to test the bend stiffness was because I noted that if you remove the function that updates the hair simulation from update the flickering goes away. This simple test already indicated to me that the problem didn't lie with the actual rendering of the hair but that the updating of the position of the hair was going haywire somewhere and if invalid positions get sent to the shader it simply won't be drawn. Setting bend stiffness resolved the issue for me, but that's more of a workaround than a fix. With this new shader that seems to have stopped without upsetting the simulation.

    What I'm worried about though is that you say it flickers for you when shadow casting is enabled because with me it happens regardless. Is there any way you could send a small scene my way to test it with the new shader? I would just attach it but for it to work I had to modify all the scripts in the scene as well as all the cpp files in the plugin solutuion. Also are you using the latest version of unity because that's what I'm on.
     
  31. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Actually that is very interesting. Let me tell you this, I have a script that I'm running on an object that contains the hair asset. This script overrides my object's animations in the "LateUpdate" function by positioning it's feet to a point on the terrain. Every once inawhile, the way my script calculates the object's feet position will actually calculate a number that is not possible (i.e. NaN -- not a number). When this happens, the hair asset completely disappears from my object! Sometimes it comes back, and sometimes it doesn't lol. I don't think my feet position script causes the flickering by itself....but I think it shows what happens when the location of something is way off.

    I too, noticed that it could be a positioning issue -- and I think you're on to something. That being said, I could be slightly wrong about it being a "shadows only" issue. However, at first, is seemed like it was the shadows causing it for some reason.

    In any case, I'll see if I can work up a small test scene for you. I'm currently using Unity version 5.2.1p2.
     
  32. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    Hey guys, new shader coming atcha...features 3 new features. Ability to change hair length in game (controlled by length scale and length noise in params). Slight specular imporovement. Glint support, a feature that adds bright sparkles to hair, controlled by glint strength, scale and exponent (only negative seems to work). Attached is an image with over exaggerated effects to demonstrate and the new shader. I had problems using the "Load Hair Shader" button so just replace the one in Streaming Assets and click on "Reload Hair Shader". If the hair disappears then just press play in the editor and it should be back. Doesn't entirely fix the flickering though. Texture support coming soon. Shadows are currently in "Development time long or uncertain" to quote the unity roadmap. forgive me I had to :) Enjoy!
     

    Attached Files:

    Velo222 likes this.
  33. edwon

    edwon

    Joined:
    Apr 24, 2011
    Posts:
    266
    Wow, looks awesome theprotonfactor, have you tried rendering in VR with Oculus DK2?
     
  34. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    No, unfortunately I won't have access to an oculus for the next 2 months. If possible I'll look to add VR support. I saw you were having some trouble with it rendering in only one eye. Could you perhaps elaborate further? Is the scene displaying without the hair or is there nothing in the other eye?
     
  35. edwon

    edwon

    Joined:
    Apr 24, 2011
    Posts:
    266
    The hair and scene are rendering the left eye, but in the right eye, the scene is rendering, but the hair is not.

    A friend of mine said this "My guess is that it implements some camera space calculations on OnWillRender() and doesn’t expect to get called twice in one frame." I wonder if that's the source of the issue?
     
  36. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    In that case, your friend is absolutely correct.
    That is indeed the exact problem, I'll look into fixing it, but you or someone else would naturally have to test it.
     
  37. edwon

    edwon

    Joined:
    Apr 24, 2011
    Posts:
    266
    Sure thing! Let me know when it's ready to test. Are you putting your work into a github project somewhere?
     
  38. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    That's the plan yes. I'm working on a build with textures and shadows. I get the feeling that modifying the original build for VR will be done before the others, so the moment I've got something to show for you I'll upload the repo and that will then act as a base for the other features. I'll keep you posted.
     
  39. Deleted User

    Deleted User

    Guest

    hopeful likes this.
  40. edwon

    edwon

    Joined:
    Apr 24, 2011
    Posts:
    266
  41. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    Alright, I might have something that could render in both eyes for the native oculus integration that ships from unity 5.1 onwards. Is that what you're using or are you on the legacy external plugin?
     
  42. edwon

    edwon

    Joined:
    Apr 24, 2011
    Posts:
    266
    Yep I'm using the latest Unity (5.2.3)
     
  43. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    OK, so I've tried to modify the hairworks integration in a way that it won't affect non VR users. I'll upload a repo soon, with a translated readme and the new shader. Now, with regards to oculus support, if you want to enable rendering in both eyes you'll hopefully just have to slap a script called Hair VR onto your main camera. I'm working under the assumption that the camera's OnPreRender () and OnImageRender () get called twice per frame. If it doesn't, this won't work at all. Also this is about the simplest solution I could think of, which means I'm actually a little sceptical if it will work, but give it a shot. For people who don't want VR just don't put that script on your camera and it shouldn't affect the hair or your performance whatsoever. Lastly, the VR script should be more performance heavy, it will draw the hair twice (once for each eye) and make sure to tick view frustum culling in the hair instance params or else it will draw even when off camera, because the right eye draws in on image render which gets called every frame rather than in on will render object which only gets called when the object is visible.
     
  44. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    @edwon perhaps there's an even more simple solution : Open your hair instance script and navigate to the OnWillRenderObject() function. Delete the if statement at the top and it's brackets then see if it renders both eyes.
     
  45. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    GitHub repo should be up soon. Managed to sneak in spot light support and hair now turns black when out of light range. This obviously won't be much useful in the case of spot lights, but should be useful for point lights. I'm toying with the idea of using GI when it's out of light range... or within for that matter. The question is how...Light Probes maybe? See the attached image for the new spot light support. Enjoy guys! I'm getting git ready for the upload as I type this.
     

    Attached Files:

  46. edwon

    edwon

    Joined:
    Apr 24, 2011
    Posts:
    266
    Wow, awesome work proton, excited to see the repo!
     
  47. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    So, apparently my internet connection and GitHub both conspired against me, but here it is. No source code for now, still have to clean up non-functional / experimental code. You guys will naturally have to paste your GFSDK_HairWorks.win64.dll into the x86_64 folder as I'm not allowed to redistribute it. To port it to your project replace the .cso file in Streaming Assets and the HairWorks.dll in the x86_64 folder as well as all the scripts in the Hairwokrs Scripts folder. Enjoy!

    https://github.com/theprotonfactor/NVIDIAHairWorksIntegration
     
    twobob likes this.
  48. edwon

    edwon

    Joined:
    Apr 24, 2011
    Posts:
    266
    Ok I downloaded the rep, and imported my GFSDK_HairWorks.win64.dll from the Hairworks zip. And sadly, I don't see any hair in the test scene. VR is disabled, so I'm just testing on a normal screen.
     
  49. theprotonfactor

    theprotonfactor

    Joined:
    Oct 10, 2014
    Posts:
    140
    Weird...just downloaded it and it works for me. Does the reload buttons in the instance do anything? Did you restart the editor or press play? I should also mention that I pasted the .dll in before I even opened the project for the first time.
     
  50. edwon

    edwon

    Joined:
    Apr 24, 2011
    Posts:
    266
    Did all of the above, still nothing. Is there anything else I need to do or check in terms of setup?

    P.S. I tried the Fur Viewer and it's working fine on my machine.