• 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
Question by CaptainFailCode · Feb 05, 2014 at 04:00 PM · shadertransparencyalpha

Alpha Transparency Shader Issue

So having some issues with my simple shader I've put together, it's really basic, and I just would like alpha transparency working correctly.

Here's my code:

 Shader "Test/AlphaFINAL" 
 {
     Properties 
     {
         _MainTex ("Base (RGB)", 2D) = "white" {}
     }
     SubShader 
     {
         Pass 
         {
             //Render Back Faces
             Cull Front
             ZWrite Off
             Blend SrcAlpha OneMinusSrcAlpha
             CGPROGRAM
             #pragma vertex vert_img
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             uniform sampler2D _MainTex;
 
             float4 frag(v2f_img i) : COLOR {
                 return tex2D(_MainTex, i.uv);
             }
             ENDCG
         }
         Pass
         {
             //Render Front Faces
             Cull Back
             ZWrite On
             Blend SrcAlpha OneMinusSrcAlpha
             CGPROGRAM
             #pragma vertex vert_img
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             uniform sampler2D _MainTex;
 
             float4 frag(v2f_img i) : COLOR 
             {
                 return tex2D(_MainTex, i.uv);
             }
             ENDCG
         }
         
     }
 }

and here's the result:

alt text

Now after a lot of reading and the such, I get this is down to the draw order, in fact that's how I got to this result in the first place. I understand that I have now drawn the back first and as it comes forward I am blending the colours of the foreground and background together.

The issue here is it doesn't entirely seem to be using the alpha the bits of the model that are supposed to be "solid" for example the face and is just adding the back over the front.

This is where I'm stuck. I want it to respect alpha transparency with blending, but then not just show everything with an alpha of > 0 through itself.

I tried turning zwrite on for the second pass, this fixes that to a degree, but then you end up with these errors.

alt text

alt text

So. Any help or enlightenment on how to fix this would be amazing as I'm kinda stuck on my learning about shaders because of it. scurries off to try and find the answer himself..

Sorry for asking tbh, I really love to work things out on my own, but... this one just stumps me and it's probably a dead simple fix.

Thank you in advance.

draworder.png (445.8 kB)
Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by SunnyChow · Feb 06, 2014 at 02:24 AM

I suggest you separate it into 2 shaders: one for solid part, one for transparent.

Unity would try to render transparent objects from back to front, but it doesn't sort every triangles individually. Therefore it's possible that a triangle in back renders over a triangle in front (transparent shader don't do z-write so it can't z-test itself).

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Adding Alpha-Transparency to Shader 0 Answers

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

Avatar T-Shirt Color With Print 3 Answers

Adding Transparency to Custom Unity Shader 1 Answer

[Solved] Vertex Color/Alpha Transparency Rendering as Opaque 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