naelstrof / SkinnedMeshDecals

An example of rendering decals on SkinnedMesh Renderers in Unity.
MIT License
155 stars 24 forks source link

Support for Renderers with Multiple Materials #4

Closed Snow-Okami closed 3 years ago

Snow-Okami commented 3 years ago

Is there anyway to support this or give a hint as to how I should go about changing the code to support this? I have a model that uses multiple materials to support wear-able switching so my materials are many (about 20) instead of just one. It seems currently this only works on material index # 2 (which caught me a bit off-guard as I was expecting #0)

Later down the line I will probably have it so all the materials are baked into one when a wearable is changed as far as clothes vs skin, but the skin and clothes would still need separate materials.

naelstrof commented 3 years ago

It does in-fact support multiple materials and submeshes, though it assumes that they all share the same UV2 (meaning they can all share the same decal map).

The relevant code for this is right here: https://github.com/naelstrof/SkinnedMeshDecals/blob/master/Assets/Naelstrof/Plugins/DecalTech/Scripts/PaintDecal.cs#L172 and here: https://github.com/naelstrof/SkinnedMeshDecals/blob/master/Assets/Naelstrof/Plugins/DecalTech/Scripts/PaintDecal.cs#L231

First link shows that it iterates through all materials that have a _DecalColorMap input and sets it to the rendertexture with the decal on it.

Second link shows that it properly iterates through submeshes, re-rendering the decal to make sure no mesh was missed. (submeshes are separated by material.)

By checking Automatic Lightmap Generation in the model import settings, it should automatically create uv2's that don't overlap (though I'm not completely sure!).

Then all you have to make sure is that all the materials have a shader that takes a _DecalColorMap input, and when you go to render the decal, you must iterate through ALL the SkinnedMeshRenderer's that you wish for the decal to appear on. (Eg: You must call RenderDecal once per SkinnedMeshRenderer.)

If you want some more direct help, you can add me on discord: naelstrof#7705 and I'll try to help you! I'd love to see what you're working on. (And I'll try to repeat the problem and solution here for people who are googling.)

Snow-Okami commented 3 years ago

I thought the same thing as I was looking through the code and saw support for the multiple materials but was confused as to why it was not giving the expected results. After looking through the code more deeply (thanks for those code hints) I noticed one line that seemed not quite right:

https://github.com/naelstrof/SkinnedMeshDecals/blob/f783e873ab92f214ca45539a06f69af2b2d39d98/Assets/Naelstrof/Plugins/DecalTech/Scripts/PaintDecal.cs#L128

This break here makes it so that after finding the first decalable material, all the rest are skipped. This also explains the strange index of the one material that was working.

I deleted this line and now it is working beautifully. Although maybe 10% of the time I seem to get no response. I need to investigate this more, it may be blitting from behind and I don't see it.

Also this is an amazing asset. For the insanely fast responses I am getting from you - this should def be sold on the asset store! I am use to getting no responses from github projects for Unity especially ones that fill huge voids with no other alternatives.

naelstrof commented 3 years ago

Haha oh crap, that is definitely an erroneous break. Thanks for the find!

I'm glad you find this asset useful! I would sell it, but Unity desperately needs more open-source solutions. I can't believe it still doesn't have built-in skinned mesh renderer decal solutions in 2021. 😔

I hope by making this open-source, I can help lessen the burden for others who just want to make cool video games.