• 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 eduzanni · May 28, 2014 at 01:35 AM · 2dshadermeshalpha

Cancel out mesh alpha

Hello everyone!

I'm rendering a mesh programatically and I would like to know if there's a way to make another mesh, that's in front of my first one, act like an "x-ray screen" of sorts and cancel out the alpha for the underlying mesh.

To clarify the question further: I have mesh 1 and mesh 2. Mesh 1 is a black mesh that hides objects behind it. Mesh 2 is in front of mesh 1 on the player perspective. I want to make it so that where mesh 2 intersects or projects against mesh 1, mesh 1 is fully transparent.

I'm in 2D btw. The mesh is supposed to be the player's current field of vision.

Can it be done?

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 christoph_r · May 28, 2014 at 12:13 PM 0
Share

Sounds a bit complex. Have you considered a bit of boolean operator-style tricking? I.e. you assign a value to each pixel of the screen for how much of mesh 1 is visible and how much of mesh 2 is visible (depending on their respective transparency texture), invert that pixel map for mesh 2, multiply those, crop the resulting image to the size and position of mesh 1's supposed transparency texture and use that? I'm not quite sure I'm getting exactly what you're looking for, though.

1 Reply

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

Answer by MakeCodeNow · May 28, 2014 at 08:26 PM

You need some way to test this intersection, and there are two per-pixel options, depth and stencil. Either one can work, depending on how your scene is setup, but stencil may be the better approach. You'll need to render the base object and the mask first and then the rest of the scene. If the base object is partially transparent outside the mask, then that is trickier. One option would be to render the object and the mask to a render target and then use that render target where you currently use the base sprite.

If you want to take a geometric approach, there are some really good C# mesh triangulation libraries that can handle holes. Check out poly2tri and Triangle.NET.

Comment
Add comment · Show 4 · 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 eduzanni · May 29, 2014 at 03:56 AM 0
Share

I tried Poly2tri, but I couldn't find a way to get it to generate the triangle index array I need, so I thought a masking approach would be better. I'm not dealing with partial transparency or anything. I just want to render a white mask to cancel a black mesh in order to make scenery visible/invisible.

Stencil and depth aren't shaders in Unity are they? What are they and how do I go about using them?

avatar image MakeCodeNow · May 29, 2014 at 03:14 PM 0
Share

I was referring to the depth and stencil buffers, and to depth and stencil writing and testing. You can control depth testing and both stencil writing and testing in the Unity shader files. You can read about these techniques and how to use them if you read up on depth and stencil testing. For your case, I think you should focus on stencil testing, as that's really what you are doing, using one object to stencil out another object.

avatar image eduzanni · May 29, 2014 at 05:45 PM 0
Share

Cool. Can it be done with the free version of Unity?

avatar image MakeCodeNow · May 29, 2014 at 06:30 PM 0
Share

Not sure. That can be part of your research.

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

22 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

Related Questions

Shader for mesh to project onto sprite? 0 Answers

How can I make a part of a mesh to be invisible - ie: see through parts of walls... 2 Answers

I want to fade out this Texture Masking Shader (using Rendertextures) and I need your help 3 Answers

Shader vs Sprites - Sorting layer (ZWrite Off not working) 1 Answer

How to apply a gradient to a 2D mesh? 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