• 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 image28 · Sep 21, 2014 at 11:14 PM · shaderalpha-blend

Alpha Sorting Issues

Been working on this LOD Fading Shader for a little while now, and the LOD Fading part works great but some tree branches show a clear texture that covers the branches below it.

Also trying to write the shader in one pass so it works with batching..

Code and Picture Below

   Shader "FX/LODFade" {
     Properties {
         _MainTex ("Texture", 2D) = "white" {}
         _FadeIn ("FadeIn",Float) = 0.0
         _FadeOut ("FadeOut",Float) = 0.0
         _FadeAmount ("FadeAmount",Float) = 0.0
     }
     SubShader {
         Tags {"Queue"="Transparent"  "RenderType"="Transparent" "IgnoreProjector"="True" }
  
            ZWrite On
         //  ZTest Equal
          Blend SrcAlpha OneMinusSrcAlpha 
           Cull Off
            
         Pass {
  
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
  
             sampler2D _MainTex;
             float4 _MainTex_ST;
             uniform float _FadeIn;
             uniform float _FadeOut;
             uniform float _FadeAmount;
            
  
             struct a2v
             {
                 float4 vertex : POSITION0;
                 float3 normal : NORMAL;
                 float4 texcoord : TEXCOORD0;
  
             };
  
             struct v2f
             {
                 float4 pos : POSITION0;
                 float2 uv : TEXCOORD1;
                 float4 color : COLOR0;
             };
  
             v2f vert (a2v v)
             {
                 v2f o;
                 
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.uv = TRANSFORM_TEX (v.texcoord, _MainTex); 
 
                   float viewZ = distance(_WorldSpaceCameraPos, mul(_Object2World, v.vertex));
                           
                   // Fade In/Out LOD0
                   o.color.a = 1.0;
                   
                   if ( _FadeIn != 0 && viewZ >= _FadeIn-_FadeAmount/2 && viewZ < _FadeIn+_FadeAmount/2 )
                 {
                       o.color.a = saturate((1.0f/_FadeAmount)*(viewZ-(_FadeIn-_FadeAmount/2)));
                   }
                   
                   if ( viewZ >= _FadeOut-_FadeAmount/2 && viewZ < _FadeOut+_FadeAmount/2 )
                   {
                       o.color.a = saturate ((1.0-(1.0f/_FadeAmount)*(viewZ-(_FadeOut-_FadeAmount/2))));
                   }
 
                 return o;
             }
  
             float4 frag(v2f i) : COLOR
             {
                 float4 c = tex2D (_MainTex, i.uv);
                 //if ( i.color.a != 1.0 )
                 //    c.a = c.a * i.color.a;
                     
                 
                 return c;
  
             }
  
             ENDCG
         }
     }
     FallBack "Diffuse"
 }

Z Sorting Error

alpha-sorting.png (57.7 kB)
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

mobile particles additive vs alpha blended performance ? 1 Answer

iPhone Shader - Unlit, 2 textures (alpha blend), Base needs tint. 1 Answer

Is there a way to create an fading alpha-cutoff effect? 0 Answers

Assigning a texture to a black & white "alpha mask" 2 Answers

Use of Clip () in alpha cutouts? 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