• 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
0
Question by ProperNewt · Mar 12, 2018 at 11:06 PM · shadersshader writing

How do I make a z-test shader like this one?

I don't know much about shaders and not sure what to call the effect but I essentially want to ztest and check if there's something blocking the view of an object. If so I want to display a flat silhouette of the object with a transparency option. I've seen a few examples that render an opaque colour but it's not quite what I want.

An example can be seen in the Mario + Rabbids game. I don't know if you can tell in this screenshot.

alt text

mario-rabbids-shader-example.jpg (166.8 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Mar 13, 2018 at 12:48 AM

I'm not sure if i understood what you want, but i guess it's something like this:

Here's the shader (I added it to pastebin as well so you can instant view it)

I just created a new standard surface shader and basically copied the CG code to make shaderlab create a seperate pass. As you can see i just set the rendertype and queue to Transparent, removed the shadow tag from the pragma and added the "alpha:blend" tag and added those lines before the "second pass":

 ZTest Greater
 Blend SrcAlpha OneMinusSrcAlpha 
 ZWrite Off

It will only render the object if the ztest would actually fail. It uses ordinary source alpha blending and disables zwriting (since we only draw on top of existing geometry).


I've added another Color property called "_Silhouette" which i used in the second pass as color, The whole surface function for the second pass just consists of

 o.Albedo = _Silhouette.rgb;
 o.Alpha = _Silhouette.a;

That's all.


edit
Ok since several overlapping object (or complex objects with overlap) will of course "stack", we can use the stencil buffer to get a uniform color, even when mutliple objects overlap. However for this we need basically 3 parts:

  • The shader for the actual objects need to write to the stencil buffer in case their ztest fails.

  • Another shader that will only render our silhouette color in the regions the stencil buffer has been set to a certain value

  • We need a script (or any other solution) that renders a full screen quad with the shader from point 2 after the camera has finished rendering.

The result looks like this:


However this approach has other problems if you have self intersecting / overlapping meshes since they can't be sorted from far to near by unity. This would result in some areas on the character being "shaded" which are not covered by other objects but just by itself. This can't be prevented with this approach. To fix this you would need to use 3 different shaders. One for occluders and one for the objects that you want to see through the occluders and the fullscreen quad. The drawing order has to be:

  • Objects which should be seen through the occluders

  • Occluders

  • fullscreen quad

The objects would write a value of 1 into the stencil buffer. The occluder shader would have a second pass which only renders if the stencil value is 1 and in that case increment the stencil value. The fullscreen quad would then only shade the areas with a value of 2.


Though this approach heavily limits your flexiblity since you need to enforce a particular rendering order and you have to specifically have a special shader on all occluders. So it would heavily affect the level design.

Comment
Add comment · Show 1 · Share
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 ProperNewt · Mar 13, 2018 at 10:48 PM 0
Share

I've realised that this is too complex for what it's worth and I don't know nearly enough about anything you're saying.

avatar image
0

Answer by ProperNewt · Mar 13, 2018 at 04:26 PM

Thanks. Any idea how I can flatten the geometry into a silhouette?

When you put it on a more complex object than a cube you can see all the geometry overlapping itself. e.g:

alt text

Here's how I'd like it to look:

alt text


asset-2.png (160.9 kB)
asset-1.png (149.8 kB)
Comment
Add comment · Show 1 · Share
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 Bunny83 · Mar 13, 2018 at 07:24 PM 0
Share

Well, that's not that trivial with a simple ZTest. However if you don't use the stencil buffer for anything else yet you can just render your object(s) into the stencil buffer where their ztest fails and finally render a fullscreen quad with your desired color only where you have something in the stencil buffer.


I'll edit my answer

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

82 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shader Color 1 Answer

CRT shader but NOT for camera 0 Answers

Shader graph is broken pink and after installed packages a lot of errors. How can I resolve all this ? 0 Answers

Dissolve Shader Problem 1 Answer

Can one write glsl shaders in Unity 2017? I cant make this shader work. The expected color should be red but I get black. 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