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
3
Question by deus_duke · Apr 19, 2012 at 08:13 PM · shadertransparencyalphacgunlit

How do I add transparency to a cg vertex lit shader

I have a scrolling background shader that I am trying to add transparency two, but I can't seem to get it working. Can anyone help me out with this?

 Shader "BRS/Environment/Scrolling Background with Alpha" {
     Properties {
         _MainTex ("Base layer (RGB)", 2D) = "white" {}
         //_DetailTex ("2nd layer (RGB)", 2D) = "white" {}
         _ScrollX ("Base layer Scroll speed X", Float) = 1.0
         _ScrollY ("Base layer Scroll speed Y", Float) = 0.0
         //_Scroll2X ("2nd layer Scroll speed X", Float) = 1.0
         //_Scroll2Y ("2nd layer Scroll speed Y", Float) = 0.0
         _Intensity ("Intensity", Float) = 0.5
         _Alpha ("Alpha", Range(0.0, 1.0)) = 1.0
     }
     
     SubShader {
         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
         
         Lighting Off 
         Fog { Mode Off }
         ZWrite Off
         
         LOD 100
         
             
         CGINCLUDE
         #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
         #include "UnityCG.cginc"
         sampler2D _MainTex;
         //sampler2D _DetailTex;
     
         float4 _MainTex_ST;
         //float4 _DetailTex_ST;
         
         float _ScrollX;
         float _ScrollY;
         //float _Scroll2X;
         //float _Scroll2Y;
         float _Intensity;
         float _Alpha;
         
         struct v2f {
             float4 pos : SV_POSITION;
             float2 uv : TEXCOORD0;
             //float2 uv2 : TEXCOORD1;
             fixed4 color : TEXCOORD1;        
         };
     
         
         v2f vert (appdata_full v)
         {
             v2f o;
             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
             o.uv = TRANSFORM_TEX(v.texcoord.xy,_MainTex) + frac(float2(_ScrollX, _ScrollY) * _Time);
             //o.uv2 = TRANSFORM_TEX(v.texcoord.xy,_DetailTex) + frac(float2(_Scroll2X, _Scroll2Y) * _Time);
             o.color = fixed4(_Intensity, _Intensity, _Intensity, _Alpha);
     
             return o;
         }
         ENDCG
     
     
         Pass {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma fragmentoption ARB_precision_hint_fastest        
             fixed4 frag (v2f i) : COLOR
             {
                 fixed4 o;
                 fixed4 tex = tex2D (_MainTex, i.uv);
                 //fixed4 tex2 = tex2D (_DetailTex, i.uv2);
                 
                 //o = (tex * tex2) * i.color;
                 o = tex * i.color;
                 
                 return o;
             }
             ENDCG 
         }    
     }
     }
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
8
Best Answer

Answer by aldonaletto · Apr 20, 2012 at 04:03 AM

Add the blend mode after ZWrite Off:

     ...
     ZWrite Off
     Blend SrcAlpha OneMinusSrcAlpha
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 deus_duke · Apr 25, 2012 at 06:39 AM 0
Share

I ended up rewriting the shader, but your answer was what I needed thanks!

avatar image z4rd0z · Jun 16, 2012 at 07:49 PM 0
Share

Hey Deus,

Do you think you could post your functional shader code? I'm having a similar problem. Thanks!

avatar image Bunny83 · Jun 16, 2012 at 07:50 PM 0
Share

@z4rd0z: Your post is not an answer, so don't post it as answer.

I've converted it into a comment.

avatar image x5lcfd · Jun 04, 2016 at 01:57 PM 0
Share

thank you so much, it works for me too.

avatar image
0

Answer by deus_duke · Jun 18, 2012 at 05:53 PM

Sure. Instead of multiplying to overlay a texture with alpha, you need to use lerp.

 // - Unlit

// - Scroll 3 layers /w Multiplicative op

Shader "BRS/Environment/Scroll 3 Layers" { Properties { _MainTex ("Base layer (RGB)", 2D) = "white" {} _FarTex ("2nd layer (RGB)", 2D) = "black" {} _NearTex ("2nd layer (RGB)", 2D) = "black" {} _ScrollX ("Base layer Scroll speed X", Float) = 0.1 _ScrollY ("Base layer Scroll speed Y", Float) = 0.0 _Scroll2X ("2nd layer Scroll speed X", Float) = 0.5 _Scroll2Y ("2nd layer Scroll speed Y", Float) = 0.0 _Scroll3X ("3nd layer Scroll speed X", Float) = 1.0 _Scroll3Y ("3nd layer Scroll speed Y", Float) = 0.0 _ColorIntensity ("Layer Multiplier", Float) = 0.5 }

 SubShader{
     Tags {"Queue"="Background" "IgnoreProjector"="True" "RenderType"="Background"}
     LOD 100
     
     ZWrite Off
     Lighting Off

     CGPROGRAM
     #pragma surface surf NoLighting

     half4 LightingNoLighting (SurfaceOutput s, half3 lightDir, half atten) {
         half4 c;
         c.rgb = s.Albedo;
         return c;
     }

     struct Input {
         float2 uv_MainTex;
         float2 uv_FarTex;
         float2 uv_NearTex;
     };
     
     sampler2D _MainTex;
     sampler2D _FarTex;
     sampler2D _NearTex;
     
     half _ScrollX;
     half _ScrollY;
     half _Scroll2X;
     half _Scroll2Y;
     half _Scroll3X;
     half _Scroll3Y;
     
     fixed _ColorIntensity;
     
     void surf (Input IN, inout SurfaceOutput o) {
         float2 mainTex_uv = IN.uv_MainTex;
         float2 farTex_uv = IN.uv_FarTex;
         float2 nearTex_uv = IN.uv_NearTex;
         
         mainTex_uv.x += _ScrollX * _Time.x;
         mainTex_uv.y += _ScrollY * _Time.x;
         
         farTex_uv.x += _Scroll2X * _Time.x;
         farTex_uv.y += _Scroll2Y * _Time.x;
         
         nearTex_uv.x += _Scroll3X * _Time.x;
         nearTex_uv.y += _Scroll3Y * _Time.x;
         
         float4 mainTex = tex2D (_MainTex, mainTex_uv);
         float4 farTex = tex2D (_FarTex, farTex_uv);
         float4 nearTex = tex2D (_NearTex, nearTex_uv);
         
         float3 previous = lerp(mainTex.rgb, farTex.rgb, farTex.a);
         o.Albedo = lerp(previous.rgb, nearTex.rgb, nearTex.a) * _ColorIntensity;
     }
     
     ENDCG
 }

}

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Add transparency to mask shader 1 Answer

Add Color Property to Unlit Alpha? 1 Answer

Transparent/Diffuse-shader always transparent by default (alpha)? 1 Answer

An Outline like the Editor does in game, with transparency 1 Answer

Is it possible to create a blur effect based on the alpha of a transparent texture on a plane? 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