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 Eplus · Apr 11, 2013 at 09:38 AM · shadertransparency

Adding Transparency to Custom Unity Shader

Hello everyone,

i am having problems adding transparency to a custom shader. I have tried many methods and i still couldn't get it to work.

The main thing is i need the shader to interpolate to a lower alpha value and back without affecting it's shininess.

I managed to get it to interpolate between textures but that is the maximum of my capabilities.

Please help.

The shader is as follows (without the texture swapping to keep things simple):

 Shader "Toon/Shiny" 
 {
     Properties 
     {
         _Color ( "Main Color", Color ) = ( 1, 1, 1, 1 )
         _RimColor ( "Rim Color", Color ) = ( 0.97, 0.88, 1, 0.75 )
         _RimPower ( "Rim Power", Float ) = 2.5
         _MainTex ( "Diffuse (RGB) Alpha (A)", 2D ) = "white" {}
         _BumpMap ( "Normal (Normal)", 2D ) = "bump" {}
         _SpecularTex ( "Specular Level (R) Gloss (G) Rim Mask (B)", 2D ) = "gray" {}
         _RampTex ( "Toon Ramp (RGB)", 2D ) = "white" {}
         _Cutoff ( "Alphatest Cutoff", Range( 0, 1 ) ) = 0.5
     }
 
     SubShader
     {
         Tags { "RenderType" = "Opaque" }
         
         CGPROGRAM
         #pragma surface surf TF2 alphatest:_Cutoff
         #pragma target 3.0
 
         struct Input
         {
             float2 uv_MainTex;
             float3 worldNormal;
             INTERNAL_DATA
         };
         
         sampler2D _MainTex, _SpecularTex, _BumpMap, _RampTex;
         float4 _RimColor;
         float _RimPower;
 
         inline fixed4 LightingTF2 ( SurfaceOutput s, fixed3 lightDir, fixed3 viewDir, fixed atten )
         {
             fixed3 h = normalize ( lightDir + viewDir );
             
             fixed NdotL = dot( s.Normal, lightDir ) * 0.5 + 0.5;
             fixed3 ramp = tex2D( _RampTex, float2( NdotL * atten ) ).rgb;
             
             float nh = max ( 0, dot ( s.Normal, h ) );
             float spec = pow ( nh, s.Gloss * 128 ) * s.Specular;
             
             fixed4 c;
             c.rgb = ( ( s.Albedo * ramp * _LightColor0.rgb + _LightColor0.rgb * spec ) * ( atten * 2 ) );
             c.a = s.Alpha;
             return c;
         }
 
         void surf ( Input IN, inout SurfaceOutput o )
         {
             o.Albedo = tex2D( _MainTex, IN.uv_MainTex ).rgb;
             o.Alpha = tex2D( _MainTex, IN.uv_MainTex ).a;
             o.Normal = UnpackNormal( tex2D( _BumpMap, IN.uv_MainTex ) );
             float3 specGloss = tex2D( _SpecularTex, IN.uv_MainTex ).rgb;
             o.Specular = specGloss.r;
             o.Gloss = specGloss.g;
             
             half3 rim = pow( max( 0, dot( float3( 0, 1, 0 ), WorldNormalVector ( IN, o.Normal ) ) ), _RimPower ) * _RimColor.rgb * _RimColor.a * specGloss.b;
             o.Emission = rim;
         }
         ENDCG
     }
     Fallback "Transparent/Cutout/Bumped Specular"
 }



  • Shader originally edited from http://wiki.unity3d.com/index.php/

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by electricsauce · Apr 12, 2013 at 03:36 AM

You need to start with changing this:

Tags { "RenderType" = "Opaque" }

to

Tags { "RenderType" = "Transparent" }

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 Anxo · Jan 27, 2014 at 03:50 PM 0
Share

is that all? I added Transparent to this custom depth shader without result.

 Shader "Custom/WriteDepth" {
 
     Properties {
 
         _$$anonymous$$ainTex ("Base (RGB)", 2D) = "white" {}
 
     }
 
     SubShader {
 
         Tags { "RenderType"="Transparent" }
 
  
 
         Pass {
 
             CGPROGRA$$anonymous$$        
 
                 #pragma exclude_renderers gles flash
 
                 #pragma vertex vert
 
                 #pragma fragment frag
 
                 
 
                 #include "UnityCG.cginc"
 
                                 
 
                 struct v2f {
 
                     float4 pos : POSITION;
 
                     float2 uv : TEXCOORD0;
 
                 };
 
                 
 
                 struct fout {
 
                     half4 color : COLOR;
 
                     float depth : DEPTH;
 
                 };                
 
                
 
                 uniform sampler2D _$$anonymous$$ainTex;
 
                 
 
                 v2f vert (appdata_base v) {
 
                     v2f vo;
 
                     vo.pos = mul( UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex );
 
                     vo.uv = v.texcoord.xy;
 
                     return vo;
 
                 }
 
              
 
                 fout frag( v2f i ) {               
 
                     fout fo;
 
                     fo.color = tex2D(_$$anonymous$$ainTex, i.uv);
 
                     fo.depth = 0.2;
 
                     return fo;
 
                 }
 
             ENDCG
 
             }
 
     
 
     } 
 
     FallBack "Diffuse"
 
 }
avatar image whydoidoit · Jan 27, 2014 at 03:53 PM 0
Share

You also need a BLEND statement:

  Blend SrcAlpha One$$anonymous$$inusSrcAlpha     // Alpha blending

See this: http://docs.unity3d.com/Documentation/Components/SL-Blend.html

avatar image Anxo · Jan 27, 2014 at 04:11 PM 0
Share

Thank you for the reply but I have tried that as well with no luck. As I am searching through the posts, I am continuously adding lines but nothing so far. The depth park works well but it ignores the alpha. This is what I go so far.

 Shader "Custom/WriteDepth" {
 
     Properties {
 
         _$$anonymous$$ainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
 
     }
 
     SubShader {
 
         Tags {
             "Queue"="Transparnet"
           "RenderType"="Transparent" }
          Blend SrcAlpha One$$anonymous$$inusSrcAlpha
 
  
 
         Pass {
 
             CGPROGRA$$anonymous$$        
 
                 #pragma exclude_renderers gles flash
 
                 #pragma vertex vert
 
                 #pragma fragment frag
 
                 
 
                 #include "UnityCG.cginc"
 
                                 
 
                 struct v2f {
 
                     float4 pos : POSITION;
 
                     float2 uv : TEXCOORD0;
 
                 };
 
                 
 
                 struct fout {
 
                     half4 color : COLOR;
 
                     float depth : DEPTH;
 
                 };                
 
                
 
                 uniform sampler2D _$$anonymous$$ainTex;
 
                 
 
                 v2f vert (appdata_base v) {
 
                     v2f vo;
 
                     vo.pos = mul( UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex );
 
                     vo.uv = v.texcoord.xy;
 
                     return vo;
 
                 }
 
              
 
                 fout frag( v2f i ) {               
 
                     fout fo;
 
                     fo.color = tex2D(_$$anonymous$$ainTex, i.uv);
                     fo.color.a = fo.color.a;
                     
 
                     fo.depth = 0.2;
 
                     return fo;
 
                 }
 
             ENDCG
 
             }
 
     
 
     } 
 
     FallBack "Diffuse"
 
 }
avatar image Anxo · Jan 27, 2014 at 04:35 PM 0
Share

This shader actually does everything I need it to do but there is a grass shader that still renders on top of it. I like this shader to be on top of everything like the shader above is but with alpha.

  Shader "GUI/Textured Text Shader"
  {
      Properties {
          _$$anonymous$$ainTex ("Font Texture", 2D) = "white" {}
          _Color ("Text Color", Color) = (1,1,1,1)
      }
      
      SubShader {
          Lighting Off
          cull off
          ztest always
          Zwrite off
          Fog { $$anonymous$$ode Off }
          Tags {"Queue" = "Transparent" }
          Pass {
              Blend SrcAlpha One$$anonymous$$inusSrcAlpha
              SetTexture [_$$anonymous$$ainTex] {
                  constantColor [_Color]
                  Combine texture * constant, texture * constant
              }
          }
      }
  }

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

13 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

Related Questions

How to make a submesh from part of my character 1 Answer

A node in a childnode? 1 Answer

Alpha Transparency Shader Issue 1 Answer

Adding transparency to a shadow 1 Answer

shader Transparency 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