Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Waliactico · Oct 18, 2015 at 09:10 AM · shadershaderstransparencyshader programmingblending

Silhouette overlay shader

I'm trying to make a shader to use when characters are behind walls, to draw a transparent silhouette (blending a solid color with the wall). The problem that I have is that my characters are modelled with more than 1 mesh, so even if I make the shader to avoid rendering, and thus blending, hidden parts, when it renders another mesh from the same character, the blending inevitably occurs, as you can see in the picture attached. The hierarchy of my model also looks like in the other picture.

alt text

Now, to the core of the problem. I thought I could write the stencil buffer, and then the other meshes of the same character would ignore the parts already drawn, but that doesn't work if there are other characters overlapping that character, they would not render the silhouette, resulting in fake visual depth order, like this:

alt text

This is the shader to get this result:

 SubShader {
             Tags { "Queue" = "Transparent+100" "IgnoreProjector"="True" "RenderType"="Transparent" }
             LOD 100
             ColorMask RGB
             Lighting Off
       
             Pass {
                //the first time the silhouette is rendered, writes 10 to the stencil. Other passes won't render, because stencil doesn't pass (Comp NotEqual)
                 Stencil {
                     Ref 10
                     Comp NotEqual
                     Pass Replace
                 }
           
                 ZTest Greater  //to draw only behind walls
                 ZWrite Off
                 Blend SrcAlpha OneMinusSrcAlpha
               
                 CGPROGRAM
                 #pragma vertex vert
                 #pragma fragment frag
                 #include "UnityCG.cginc"
               
                 uniform float4 _ColorOverlay;
                
                 struct vertexOutput { float4 pos: POSITION; };
               
                 vertexOutput vert ( appdata_base v ) {  
                     vertexOutput output;
                     output.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                     return output;
                 }
                 float4 frag ( vertexOutput input ) : COLOR {
                     float4 lColor     = _ColorOverlay;                  
                     lColor.a = 0.5;                  
                     return lColor;
                 }
                 ENDCG
             }

Then what I tried was to "reset" the stencil buffer after rendering the mesh, with an extra pass like this:

 Pass {
                 ZTest Always
                 Colormask 0
                 ZWrite Off
               
                 Stencil {
                     Ref 10
                     Comp Equal
                     Pass Zero
                 }
             }

...but then, what I got is the first result I showed.

So... what can I do to get the characters with all its meshes in a flat color, and in the correct depth order?

Any help would be appreciated, shader gurus! Thanks for your time!

sil1.png (81.2 kB)
sil3.png (88.9 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mehrdad995 · Dec 12, 2016 at 12:57 PM 1
Share

just wanted to thank you. your stencil combo saved my day for making fake shadows created with 3d mesh which will ignore overlapping on each other.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

See backside of a transparent emissive shader? 0 Answers

post shader: reconstruct worldspace plane coordinates 1 Answer

Receiving Shadows On a Tranparent Shader 1 Answer

Is it possible to avoid writing to certain g-buffers? 1 Answer

Shader issue with a 170 upper angle 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges