/Unity-URP-Hair-Shader

Hair cards shader for unity universal render pipeline

Primary LanguageHLSLCreative Commons Zero v1.0 UniversalCC0-1.0

Unity-URP-Hair-Shader

Hair cards shader for unity universal render pipeline

Discription

This shader has been an experimental project for me and will likely not receive any further development. It is by no means a finished product and can be improved upon in many areas. Nervertheless I decided to share it for anyone to use. This may be a good starting point if you wish to create your own hair shader.

--> See shader in action: https://www.youtube.com/watch?v=EmU62lWMhSU

Installation

  • Download all files inside the FulcrumHairShader directory and put them in a folder in your project.

  • MAKE SURE that all accompanying .hlsl files are inside the same folder as the main hair shader, otherwise it will not find them.

  • Universal Render Pipeline has to be installed of course.

  • The shader has been developed and tested only on unity version 2021.3.22f1

Usage

This hair shader is not phisically arccurate and may not look correct in every lighting scenario. In my testing it looked fine most of the times but when lightsources get too bright the reflections might look a bit too bright.

It looks best if you use a normal map.

  • Opacity mask has to be baked into the Alpha channel of Albedo Texture to avoid uneccesary texture samples, you might want to bake AO in there aswell. "Anisotropic Noise" texture is used to breakup the highlight reflections a bit. An example texture is included in this repository but you can of course make your own.

To get correct looking results, you may have to play around with Smoothness and Specular color values. In most cases it would look more accurate if I had used Albedo color for reflections but that would've meant that black hair woulnd't have reflections so I went with a hybrid approach.

The Fake SSS (subsurface scattering) property visually brightends the diffuse lighting of the hair but also reduces the amount of metallness to actually get that effect.

The shader makes use of the mesh tangents. In the Import settings of your mesh make sure tangents are generated by unity or be sure that they are properly exported from your 3D software if you checked Import. I haven't done mush investigating but blender seems not to export tangents or unity doesn't import them correctly. By default unity has it set to generate in this version, so it should be fine.

Limitations

  • This shader is quite performance heavy. If you plan to use it in your project be prepared to do some optimisation on your own. Below I will list some things that could be done to improve performance.
  • As said this shader is not finished. Right now it only supports URP Forward rendering, no deffered rendering.
  • Only shader model 4.5 and upwards is supported. However it might work for lower shader models. You can test it if you manually lower the shader target in the main shader file.
  • Shadows are not influenced by the Alpha cutoff they are just straigh drawn from the hair cards mesh.
  • Alpha blending is not supported as it usually doesn't look good with hair cards and would requre manual depth sorting.
  • Backfaces do not render properly although normals and tangent are inverted for backfaces the lighting seems a bit off.
  • I haven't tested GPU insancing, it will probably not work.

== perfomance optimisations

  • without any modification to the shader your options are limited.
  • you can turn off "Use Hashing" which will avoid a psuedorandom number generation.
  • Envrionment reflections can be turned off.
  • As I'm checking for backfaces in the code I think the compiler will avoid one if statement if you set cull mode to "2" (Front) and instead duplicate an mirror hair cards in the mesh itself. In blender this is quite easy using the solidify modifier. I don't know if this will actually be worth it and it probably depends on graphics card and polycounts.

If you want to develop this shader further on your own, feel free to do so and correct my mistakes. For the most part I'm following the same structure as the other lit shaders in the urp pipeline and my shader still depends on some of the urp shader files. A lot of things have also been striped out. Performance can probably be improved a number of ways. Firstly the specular lighting function is called twice per lightsource to produce the two highlights. Removing one will speed things up.. Im using custom fresnel function with tangent insead of normal to produce the anisotropic effect. The formula for fresnel comes from blenders implmentation, you might be able to simplify it and still get the nice reflecion results. I don't see a reason why deffered rendering would not work I didn't bother porting everything to GBuffer pass but it should probably work fine. I'm also not super happy with how the hashing looks, there is probably a smarter hash function to use. The shader takes some things from the kajiya kay hair shading model however my specular function is very different.