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

How to colour each hair strand differently, depending on another object's colour at a certain vertex? #23

Open DanielaAuroch opened 1 year ago

DanielaAuroch commented 1 year ago

(Please let me know if there is a better place for this, as I wasn't able to find a section in the Unity forum for the Hair System). Unity version: 2021.3.8f1 Render Pipeline: HDRP

I have a cube with a texture with different colours, like so: image

To this cube, I am applying a Hair Instance component and I give it a material. I would like to colour the hairs according to the coloured sections of the cube. So: white hair on the white sections, light brown hair on the light brown sections.

I have found solutions online that were on C# and used ray casting, but that doesn't seem like a good approach given the immense amount of hair strands that could exist. Nor was I able to find a way to iterate through said strands.

My idea was to do this in shader graph but I'm not sure how to accomplish that.

Another important note is that the cube colours are changed by a shader. So the cube could be any colour and I'd need to pick the colour of each hair strand based on the colour applied to the cube at a certain vertex. image

pauldrummond commented 1 year ago

I tried something similar using a texture for the colours and was able to use the rootUV property in Shader Graph. How does your shader work? Is the colour applied to the vertices, or is it generated at the fragment stage? If it's a vertex colour it might be possible to pass it to the fragment stage as a custom interpolator, then use it there.

Screenshot 2022-10-24 at 5 31 45 pm

DanielaAuroch commented 1 year ago

Thank you, extremely simple and straightforward! This does work for 1 object with 1 material. The final object I'm working with has several materials targeting certain UVs so it doesn't work correctly with this solution. I'll have a think about how I'm approaching things to see if I really need several materials first. If I do still need them, I'll update here, otherwise this is what I was looking for so thanks again 😄

pauldrummond commented 1 year ago

Glad I could help. The package really needs some documentation and demo scenes. I'm worried it's going to be abandoned.

pauldrummond commented 1 year ago

A hair tutorial has been added to Unity Learn. Hopefully this repository will be updated soon.

fuglsang commented 1 year ago

Labeling as documentation request. :)