Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
5
Question by Moesh · Jun 28, 2015 at 09:15 PM · shaderspriteshadersreferencestencil

Is it possible to have multiple stencil reference values in one shader?

I'm fairly new to Unity and C#, let alone tampering with shaders. At the moment I have the result I want, but I think it can be done more neatly/optimized.

I have multiple identical shaders with each its own stencil reference value. Is there a way to combine them, as in having multiple reference values in one shader or another way of optimizing my shaders?


I'm not experienced enough to create my own shader, so I just tried editing an existing shader (Sprite-Diffuse) and applied that to my sprites.

What I have are multiple layers of sprites (e.g. body, hair, clothing, etc) all forming one character. When the character collides with another sprite (e.g. blood), I want the second sprite to stick onto the character on the right layer and get cut off at the edges of the sprite.

I now have a mask shader with a reference value in the stencil buffer for each layer (body, hair, clothing) and use the same reference value in the shader of the other sprite

In the mask shader I have:

 Stencil
 {
     Ref 1
     Comp always
     Pass replace
 }

and in the void surf of that shader:

 fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
 if (c.a<0.1) discard;

in the shader of the particles I have the same reference value in the stencil buffer, like this:

 Stencil
 {
     Ref 1
     Comp Equal
 }

Now I have multiple shaders with each its own reference value in the stencil buffer, but with the rest of the script completely identical. All works great, but I don't like to have identical scripts working together. Is there a way to have multiple reference values in the stencil buffer? Or is there an other way I could optimize my script?

Thank you for reading

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 Gunther Fox · Jul 14, 2015 at 03:52 PM 2
Share

I'd really love to know the answer to this. If you figure it out on your own, please update here!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Deferred Light Rendering: Prevent additional lighting passes on pixels in overlapping point lights 0 Answers

Are shaders more efficient than manual pixel replacement? 0 Answers

My shader doesn't work if I use the same material for multiple objects with the same texture. 1 Answer

Why Sprites-Default shader hasn't any fallback? 1 Answer

Global Transparency for sprite shader 1 Answer


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