- Home /
Stencil shader object "colliding" with outside of mesh
Hi, sorry for the weird title, I don't really know how to explain this since I'm new to programming and working with shaders. So what I'm trying to do is make it so the liquid mesh only looks like it is inside of the bottle. If I scale the liquid mesh to far it looks like the mesh is outside of the bottle. I was wondering if there is a fix for this to make it look like the mesh is still in the bottle without having to scale the liquid down. Sorry if there is a really obvious fix but i've been stuck on this for like 3 days and have tried anything I could think of.
Answer by Piyush_Pandey · Aug 03, 2018 at 09:28 AM
I think you want the water to look like it is inside your bottle with some width for glass thickness. You can achieve this by below steps:-
1) Make mesh just like the bottle. Add a material to it. To this material, you need to add new custom shader code { IMPORTANT when you add this Material with this shader, it will stop rendering on the scene. Do not think something went wrong. We want a masker which is just like a water bottle but a bit thinner so that we can see the glass thickness}
SubShader
{
Tags
{
"RenderType"="Transparent"
"Queue"="Transparent"
}
Cull Off
Lighting Off
ZWrite Off
Blend Zero OneMinusSrcAlpha
Pass
{
Stencil
{
Ref 6
Comp always
Pass replace
}
// blending zero to one will not render the object to the screen hence you can use this as a masker which is not rendered on the screen also
Blend Zero One
***rest of the shader code***
}
}
2) Now to your liquid , you have to add a new material. Add a new custom shader to this material with this code
SubShader
{
Tags
{
"RenderType"="Transparent"
"Queue"="Transparent+1"
}
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
Pass
{
Stencil
{
Ref 6
Comp equal
ZFail decrWrap
}
***rest of the shader code**
}
}
Your answer
Follow this Question
Related Questions
Use stencil buffer to hide objects with same shader? 1 Answer
Using stencil buffer as form of zwrite? 0 Answers
Looking for Stencil Buffer examples/tutorials 1 Answer
Showing hidden units - using stencil buffer with standard shader and deferred rendering 0 Answers
Shadow Support in Custom Shader 0 Answers