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
1
Question by VivienS · Mar 16, 2011 at 04:21 PM · shaderalphapost-process-effectalpha-channel

Shader that draws alpha independent of transparency?

Hi there.

Is it possible to write a shader that does not render its own transparency into the alpha channel of the camera image, but some custom value?

I'm using the glow post processing effect that adds a glow based on the alpha channel that is rendered in the scene (The value that can be seen if you switch the view port in Unity from RGB to Alpha in the top left dropdown list). I also use semi-transparent objects who's glow behavior is hard to control: They can not carry a complete glow when they are semi transparent, because alpha 1 = full glow but also full opacity.

Maybe someone can also shed some light into the relation between the rendered image's alpha channel ("alpha-buffer"?) and alpha value of a shader?

Thank you!


Take this simple vertex lit transparent ShaderLab code for instance:

Shader "Transparent/Vertex Lit Alpha" {
Properties {
    _Color ("Color", Color) = (1,1,1,1)
    _Emissive ("Emissive", Color) = (0,0,0,0)
    _MainTex ("Texture", 2D) = "white" {}
}
SubShader {
    Tags { "RenderType"="Transparent" "Queue"="Transparent" }
    Blend SrcAlpha OneMinusSrcAlpha
    Pass {
        Lighting Off
        Material {
            Diffuse [_Color]
            Ambient (1,1,1,1)
            Emission [_Emissive]
        }
        Lighting On
        SeparateSpecular On
        SetTexture [_MainTex] {
            Combine texture * primary DOUBLE, texture * primary
        }
    }
}
}

If I understand correctly, the statements

Diffuse [_Color]

and

Combine texture * primary DOUBLE, texture * primary

together define the transparency as well as the rendered alpha of the object. If I wanted an additional property like

_RenderedAlpha ("RenderedAlpha", Range(0,1)) = 0.5

that should not influence the object's transparency, but only the level of gray that is visible if I switch from RGB to alpha view... how could I go about doing that?

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 Jessy · Mar 16, 2011 at 04:52 PM

Yes. Every single shader explicitly states what you're rendering into the alpha channel. You can then see what that is, if you switch the Scene view mode to Alpha instead of RGB.

If you have a specific shader you need edited, post it here. I'll see if I can do anything with it. If it's a surface shader, I'll have no idea, though.

Edit: There's no way to do exactly what you're saying, with a shader that alpha blends. I'll show you what options you have.

First, some tips as to why this base looks different than yours: The alpha component of light isn't passed into a fixed function shader - it's always one. Don't bother multiplying by one. Also, don't bother turning lighting off before turning it on - this has no effect. You have no specular component to your lighting, so don't bother using SeparateSpecular.

SubShader { Tags {"RenderType"="Transparent" Queue = Transparent} Blend SrcAlpha OneMinusSrcAlpha

 Lighting On
 Material {
     Diffuse[_Color]
     Ambient (1,1,1,1)
     Emission[_Emissive]
 }

 Pass {
     SetTexture [_MainTex] {Combine texture * primary Double, texture}
 }

}

So, the first thing you can do, is use ColorMask RGB, in which case you rely on what has already been rendered, for alpha. Unity's built-in opaque shaders render 1, for alpha, so this seems like it will get you the effect you want. Unity's built-in transparent shaders take this approach. (This method is slow on POWERVR hardware and the following technique is better there.)

The other thing you can do, is blend the alpha you're rendering, with the alpha in the color buffer, as described here. That doesn't allow for much control, but it's more than than the equivalent Blend SrcColor OneMinusSrcColor of ColorMask RGBA or Blend Zero One of ColorMask RGB.

If neither of these things gets you what you need, then your only option is to use two passes, with ColorMask A in the first one, and mangle the alpha channel as you see fit, now having explicit control of what DstAlpha is.

Comment
Add comment · Show 2 · 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 VivienS · Mar 17, 2011 at 11:00 AM 0
Share

Hi there! Thanks for the quick reply. I extended my post for the shader code

avatar image Jessy · Mar 17, 2011 at 01:29 PM 0
Share

Edited in response.

avatar image
0

Answer by Pawl · May 04, 2016 at 01:02 AM

I came across this thread (and others) in search of a solution. An example of how to use ColorMask and Blend as @Jessy suggested can be found in this answer I posted here.

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

1 Person is following this question.

avatar image

Related Questions

sprite.associatedAlphaSplitTexture with RGB Crunched ETC1 0 Answers

Shader to render RenderTexture plus alpha? 1 Answer

Unlit Shader with alpha mask 2 Answers

Cutout Shader getting Opaque when turning alpha off and on again 1 Answer

How should I add an alpha mask AND an alpha multiplier into a shader? 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