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 The-Oddler · Jul 25, 2012 at 02:43 PM · shadertransparent

Transparent Solid Color Shader

Hi,

I know nothing of making shaders in Unity, and I'm trying to make a Solid Color shader which is transparent. I currently have the following, which works:

  Properties
  {
       _Color ("Color", Color) = (1,1,1,.5)
  }
  SubShader
  {
       Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
       LOD 100
  
       ZWrite On
       Blend SrcAlpha OneMinusSrcAlpha 
  
       Pass
       {
            Lighting Off
            Color [_Color]
       }
  }

The problem is though that you can see parts of the model itself that are behind it: alt text

Anyone knows how to fix this? I need/want a solid color, without those bright spots.

Thanks for the help!

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
2
Best Answer

Answer by ScroodgeM · Jul 27, 2012 at 09:30 PM

shader below is do what you want. but your platform should support GrabPass technology. this can be not supported by some mobile platforms.

i can propound 2 more ways without grabpass:

  • create a 2-pass shader that first draws only to alpha-channel and the second draws just color based on alpha value, this can be achieved by blending options

  • create a object mask using render-texture and put it to main camera on top. this way is same as previous but uses render texture instead of shader calculations and blending

anyway issue is not so simple. with a just default blending two surfaces will be drawn twice and it will look not same as single surface.

shader

Shader "Unity Answers/Solid Transparency Color" { Properties { _Color ("Color", Color) = (0.5, 0.5, 0.5, 0.5) } SubShader { Tags {"Queue" = "Transparent"} ZWrite Off GrabPass { } Pass { Fog { Mode Off } Blend SrcAlpha OneMinusSrcAlpha

         CGPROGRAM

         #pragma vertex vert
         #pragma fragment frag
 
         fixed4 _Color;
         sampler2D _GrabTexture;

         struct appdata
         {
             float4 vertex : POSITION;
         };
         struct v2f
         {
             float4 pos : SV_POSITION;
             float4 uv : TEXCOORD0;
         };
         v2f vert (appdata v)
         {
             v2f o;
             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
             o.uv = o.pos;
             return o;
         }
         half4 frag(v2f i) : COLOR
         {
             float2 coord = 0.5 + 0.5 * i.uv.xy / i.uv.w;
             fixed4 tex = tex2D(_GrabTexture, float2(coord.x, 1 - coord.y));
             return fixed4(lerp(tex.rgb, _Color.rgb, _Color.a), 1);
         }
         ENDCG
     }
 }

}

Comment
Add comment · Show 9 · 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 The-Oddler · Jul 27, 2012 at 11:27 PM 0
Share

Thanks! I get an error "No subshader can run on this graphics card" though. Any idea? I'm using Unity Indie, can this be a problem?

avatar image ScroodgeM · Jul 28, 2012 at 06:34 AM 0
Share

AFAI$$anonymous$$ shaders are not depends on Unity license. so this is a hardware limits. which hardware do you use? graphic adapter model? and try menu "edit -> graphic emulation" - does it affect shader? which modes? i think GrabPass{} can be not supported y some cards

avatar image The-Oddler · Jul 28, 2012 at 09:54 AM 0
Share

I currently have a Nvidia 460gtx. I already tried changing the graphic emulation, though that didn't change anything (currently it's at none.) And what do you mean by "which modes" ? Btw, thanks a lot for helping.

avatar image ScroodgeM · Jul 28, 2012 at 10:46 AM 0
Share

that's very strange. shader model 2.0 support must be enough to run this shader. your card supports up to shader model 5.0. did you edit this shader? try to use it as is. also, try to switch to fantastic mode in quality settings. if it still doesn't work, try to remove 'GrabPass {}' line. shader will not work properly then, but should draw an object (without transparency). it should help to detect what's wrong in shader. write response here.

avatar image The-Oddler · Jul 28, 2012 at 11:56 AM 0
Share

Ok I tried all what you said. Changing the quality settings doesn't do anything. Removing the GrabPass removes the error, though as you said it's not transparant.

Show more comments
avatar image
4

Answer by Pawl · May 04, 2016 at 12:57 AM

This is an old question, but I implemented the two pass shader (with texturing) suggested by @ScroodgeM and it works great. This was tested on Unity 4 and the vertex/fragment shaders were based on the samples from the Unity docs here.

The secret ShaderLab sauce is ColorMask and Blend, as seen on the stoned dino below. Enjoy! =)

Before

After

 Shader "Custom/FlatTransparent" {
     Properties {
         _Color("Main Color", Color) = (1,1,1,1)
         _MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
     }
 
     SubShader {
         Tags{ "Queue" = "Transparent" "IgnoreProjector" = "False" "RenderType" = "Transparent" }
 
         /////////////////////////////////////////////////////////
         /// First Pass
         /////////////////////////////////////////////////////////
 
         Pass {
             // Only render alpha channel
             ColorMask A
             Blend SrcAlpha OneMinusSrcAlpha
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             fixed4 _Color;
 
             float4 vert(float4 vertex : POSITION) : SV_POSITION {
                 return mul(UNITY_MATRIX_MVP, vertex);
             }
 
             fixed4 frag() : SV_Target {
                 return _Color;
             }
 
             ENDCG
         }
 
         /////////////////////////////////////////////////////////
         /// Second Pass
         /////////////////////////////////////////////////////////
 
         Pass {
             // Now render color channel
             ColorMask RGB
             Blend SrcAlpha OneMinusSrcAlpha
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             sampler2D _MainTex;
             fixed4 _Color;
 
             struct appdata {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             struct v2f {
                 float2 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
             };
 
             v2f vert(appdata v) {
                 v2f o;
                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.uv = v.uv;
                 return o;
             }
 
             fixed4 frag(v2f i) : SV_Target{
                 fixed4 col = _Color * tex2D(_MainTex, i.uv);
                 return col;
             }
             ENDCG
         }
     }
 
     Fallback "Diffuse"
 }
 



before.png (362.1 kB)
after.png (373.1 kB)
Comment
Add comment · Show 3 · 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 allenwp · Jul 01, 2016 at 07:59 PM 0
Share

Quick heads up to future readers: This method works well with a single mesh, but doesn't work with a model that's made up of multiple meshes.

avatar image BdarVirtu allenwp · Nov 20, 2020 at 03:03 PM 0
Share

still a pretty amazing result. 2020 and still works!

avatar image bnuernberger · Feb 27, 2017 at 06:50 PM 0
Share

I also found another solution posted here: http://wiki.unity3d.com/index.php?title=AlphaVertexLitZ. Is there any advantages to using @Scroodge$$anonymous$$'s approach compared to first rendering only into the depth buffer? Is this basically the same thing? Is one faster than the other?

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

10 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

Related Questions

About shader! 0 Answers

Shader: Masked Tint with Transparency 0 Answers

Shader Warning Problem 0 Answers

Transparent shaders render as fullbright 0 Answers

Rendering an entire object as transparent 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