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 delogan · May 28, 2013 at 05:07 PM · shaderalphashaderlabgrab

Grab Pass alternative? Frag shader that applies alpha values behind it to itself.

Hi!

I was wondering if there was a more efficient way to utilize a grab pass? Current model makes it very expensive and adds a draw call for every use. I am not very experienced in shaders, and I just need to implement a simple frag shader that assumes the alpha value of every object behind it (think grab pass grayscale alpha channel):

Let's say we have a scene like this, with a plane that has our shader+material combo, and a camera that points perpendicular to our plane:

http://i.imgur.com/eF5mn7x.jpg

Now, if we were to add a semi-transparent materials to our objects, we want the plane to see whats behind it (relatively to the Main Camera, of course), and set the total alpha value through a fragment shader to itself, like this:

http://i.imgur.com/ykyPokv.jpg

The resulting mask is a grayscale alpha channel:

http://i.imgur.com/C72Pq3r.jpg

And the final result should look like this from the Scene view:

http://i.imgur.com/8gUwtVL.jpg

It is a very simple concept, but resulted grab pass shader was clunky and required layering to get the correct alpha (am I doing something wrong?). That is why I need just a simple frag shader that will allow me to modify/recalculate the alpha.

So how does that data input works in a shader? I always thought of shaders as a simple texture processor, but with grab pass it seems way more than just that. I have several books about shaders, including the cg prog tutorial by nvidia, and none of them explain how does data get in and out in simple terms. Or how the color calculations are made. Or detailed explanation of texture combiners, etc etc.

Please help or give some advice to a shader beginner! Would GREATLY appreciate your help. Long live Unity!

Comment
Add comment · Show 3
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 Owen-Reynolds · May 28, 2013 at 05:26 PM 0
Share

The usual way shaders work is they don't have any access to the "existing" pixel. Your shader code can set your pixel's depth, depth test and alpha. But that's it. The non-programmable hardware looks up the old pixels and decides whether to use/not-use/blend.

That's what the grab pass is for. The "existing" pixel, which is now merely a texture, is now lookupable.

avatar image delogan · May 28, 2013 at 06:32 PM 0
Share

What if I put the 100% transparent plane in the very back; It will eventually get the info i need at the frag stage, wouldn't it? Afterwards I can layer it appropriately and with queue/offsets I can pass the alpha layer to my texture?

Will that work, Owen?

avatar image Owen-Reynolds · May 29, 2013 at 03:29 AM 0
Share

If you draw a screen-sized plane last, sure, the old pixels will already be computed, but you still aren't allowed to look at them (without a grab pass.)

If this was a CPU, you could figure out a way to look up old work, but graphics cards just don't work that way (if they did, no one would ever need a grab-pass.)

I thought the OpenGL red book had the best explanation, or look up "GPU pipeline" for all the steps.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jde · Aug 27, 2013 at 02:15 PM

Looks like your best bet would be to use a RenderTexture combined with Camera.RenderWithShader instead of a GrabPass. The replacement shader used by RenderWithShader could be one which only displays the alpha channels of rendered objects.

The resulting RenderTexture would then be a greyscale mask image (like the one you show above) which you could use in your main shader if you plug it in to a texture slot via script.

Comment
Add comment · 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

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

15 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

Related Questions

When doing two passes on different alphas, shader gets overwritten. 0 Answers

custom Standard Shader alpha issues. 1 Answer

How to first draw all objects using one pass, then using another pass? 0 Answers

Shader compiler: internal error compiling shader snippet type=0 platform=0: 1 Answer

Altering color / alpha on a Game Object 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