- Home /
adding multiple clothes to character,equipting multiple clothes to character problems
Hi, i am trying to add multiple clothes to my character in unity 2018.2.1f1 which works kinda ok, the problem is that the clothes underneath continuously stick through the one on top. for example i add a bra then i add a blouse, the frills from the bra keeps sticking through the blouse. if i add a pants then add a skirt over the pants, parts of the pants keeps sticking through the skirt. can anyone tell me how to fix this problem please. i'm using the clothes as prefabs. thank you in dvance,hi, I am trying to add multiple pieces of clothes to the character in unity 2018.2.1f1, the clothes actually goes onto the character but keeps sticking through each other. for example if the character has on a bra and i add a blouse, the frills from the bra sticks through the blouse. Also if i add for example a pants under a skirt, parts of the pants bulges through the skirt. can someone please assist me with a fix for this? im using the clothes as prefabs.
Answer by Zarenityx · Aug 02, 2018 at 08:16 PM
You can do this using the Stencil Buffer. It's a bit like the depth buffer, only you can write custom values and make custom tests to it.
First, Disclaimers:
This requires a separate shader, but you'll probably want that for fabric anyway (the standard shader doesn't do a great job on cloth). This also requires that you have some way to guarantee the order of clothing. A slotting system is good for this, as you can simply say that the undergarments slot is always under the pants slot, which is always under the shirt slot, which is always under the armor slot, which is always under the jacket slot, etc. Additionally, if you are using the Legacy Deferred, Legacy Light Prepass ('Legacy legacy deferred') or HDRP render pipelines, there are some reserved stencil buffer bits. You can get around this with Camera.clearStencilAfterLightingPass. If you are using the Legacy Forward, LWRP or a custom SRP that doesn't utilize stencil, you need not be concerned with this. This technique also won't work for transparent clothing, as you will just see through your entire character.
The Technique:
You'll also need to reserve a stencil value for this purpose. What you use is up to you, so I'll just call it X. Your fabric shader should include this snippet:
Ref X
Comp NotEqual
Pass Replace
This tells the shader:
---Using the value X,
---Check if the current value is not equal. If it isn't equal, render, otherwise, fail.
---If the stencil check passes, replace the value with X, therefore causing future checks to fail
Next, you will need to use Material.renderQueue to set the order in which the materials get rendered. Outer layers should get rendered first, then inner layers. This is where a slotting system comes in handy. Now, if you have a blouse, it gets rendered first, and sets a flag on the stencil buffer that says 'don't render what's underneath'. When the bra gets rendered, any pixels that the bra rendered to first won't get rendered, regardless of depth.
Reasons why this is a good option:
This has an added advantage: Stencil buffers always get checked before shading, and won't render if they fail. This saves expensive per-pixel calculations on anything that's obscured. By contrast, the usual depth buffer is sometimes checked after the fragment shader (depending on renderer and hardware), so sometimes these computations can occur for unseen pixels. As such, using this technique all over your character can help speed up rendering. Stencil buffer checks on skin, hair, props, accessories, items, etc. can all be used to make highly customizable and performant characters.
Your answer
Follow this Question
Related Questions
How to add different clothes to rigged character 1 Answer
Changing my character's clothes and environment in 2D 1 Answer
Animate Clothing Meshes With Mecanim? 1 Answer
How to fix character mesh going through clothes? 1 Answer
Can I import clothing attached to a character that has already been imported into Unity? 2 Answers