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
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.

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

How to efficiently render a 2D grid? 1 Answer

Alpha vs non-alpha unlit shaders 0 Answers

See-through hole via shaders on a 2D plane 2 Answers

Hiding part of mesh 0 Answers

Custom shader of sprite results in black alpha. 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