Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Zst · Apr 11, 2019 at 12:02 AM · shadertransparencyalpha-channel

Custom Transparent shader is not transparent

I am writing a, hopefully, simple shader which will allow me to do 2 things. The first is to layer several textures together using each texture's own alpha as a mask. This part works. The second thing is to allow me to use an entirely separate texture for the geometry's transparency. I have copy/pasted example code into my shader and it does not seem to respond to changes in alpha at all. When I change my base texture to something with an alpha, that alpha is outlined by an orange line in the unity editor. However I am not passing that texture's alpha to the SurfaceOutputStandard object. Regardless, the geometry is never transparent, and changing the blending has no affect except when it's "One One". All I need to wrap up this problem is to be able to use a 4th and separate texture's alpha for the geometry's transparency.

 Shader "Custom/multimage"
 {
     Properties
     {
         _Color ("Base Color", Color) = (1,1,1,1)
         _MainTex ("Base", 2D) = "white" {}
         _2ndColor ("2nd Color", Color) = (1,1,1,1)
         _Detail ("Detail", 2D) = "white"{}
         _Detail2("Detail2", 2D) = "white"{}
         _AlphaTex("Alpha Map", 2D) = "white"{}
         _Glossiness ("Smoothness", Range(0,1)) = 0.5
         _Metallic ("Metallic", Range(0,1)) = 0.0
     }
     SubShader
     {
             Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True" }
             ZWrite Off
             
             Blend SrcAlpha OneMinusSrcAlpha
 
         CGPROGRAM
             // Physically based Standard lighting model, and enable shadows on all light types
             #pragma surface surf Standard fullforwardshadows fragment frag
             //#pragma fragment frag
 
             // Use shader model 3.0 target, to get nicer looking lighting
             #pragma target 3.0
 
             sampler2D _MainTex;
             sampler2D _Detail;
             sampler2D _Detail2;
             sampler2D _AlphaTex;
 
             struct Input
             {
                 float2 uv_MainTex;
                 float2 uv_Detail;
                 float2 uv_Detail2;
                 float2 uv_alpha;
             };
 
             half _Glossiness;
             half _Metallic;
             fixed4 _Color;
             fixed4 _2ndColor;
             fixed4 _AlphaMap;
 
             void surf(Input IN, inout SurfaceOutputStandard o)
             {
                 // Albedo comes from a texture tinted by color
                 fixed4 c0 = tex2D(_MainTex, IN.uv_MainTex) * _Color;
                 fixed4 c1 = tex2D(_Detail, IN.uv_Detail) * _2ndColor;
                 fixed4 c2 = tex2D(_Detail2, IN.uv_Detail2) * _2ndColor;
                 fixed4 a = tex2D(_AlphaTex, IN.uv_alpha);
 
                 //Each texture is blended with the base layer in turn
                 c0.rgb = c1.a * c1.rgb + (1 - c1.a) * c0.rgb;
                 c0.rgb = c2.a * c2.rgb + (1 - c2.a) * c0.rgb;
 
                 //final rgb output is all the layers blended with their respective transparencies
                 o.Albedo = c0.rgb;
                 // Metallic and smoothness come from slider variables
                 o.Metallic = _Metallic;
                 o.Smoothness = _Glossiness;
                 //The final alpha OUGHT to be the AlphaMap's alpha
                 o.Alpha = a.a;
             }
         ENDCG
     }
     FallBack "Diffuse"
 }
 
Comment
Add comment · Show 1
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 Zst · Apr 11, 2019 at 12:53 AM 0
Share

Is the problem that I cannot process more than 3 textures at a time? I still can't get traditional transparency working. Given my specific use I am fine with using clip() to get a transparent cutout, but it never worked. Using one of the first 3 textures does work. It will impact my design if I cannot use 4 texture's however. So if I could use a single channel texture, perhaps, which won't count towards the limit that would work.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Apr 11, 2019 at 01:12 AM

Your shader is not a transparent shader at all. All you did is specify the renderqueue and the rendertype tag. Those only control when this shader is actually rendered during the rendering stage. Those do not affect the actual shader behaviour. Please read the Writing Surface Shaders documentation. Your pragma directive needs the "alpha" directive. This will essentially enables alpha blending for this shader.


ps: you may also want to read about sub shader tags to understand their usage. Generally the whole Shader Reference category (on the left) contains all you need to know about Unity's shaderlab syntax.

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

166 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 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 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 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 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 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 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 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 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 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 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 avatar image

Related Questions

Change alpha in runtime of gameObject with mobile shader 1 Answer

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

Transparency and blending issues with custom-made shader 1 Answer

How can i add transparency to this shader? 1 Answer

Transperency not working correctly on imported objects. 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