• 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 /
  • Help Room /
avatar image
Question by Michionlion · Jun 17, 2016 at 05:53 AM · shadermaterialalphaskyboxblending

Skybox not correctly alpha blending with custom Shader

I have two custom shaders, a skybox shader, and a billboard shader - eventually a corona for a star. Some parts of the Corona shader have to be transparent, however when outputting transparency from the fragment, it is only used when the color is against some other object, but not the skybox. If ZWrite is On, then fully opaque color is drawn, and if ZWrite is Off, nothing. Blending is Blend SrcAlpha OneMinusSrcAlpha. Below is the code for Star_Corona, and vr_skybox. Incidentally, the problem is solved when clearing the camera from a color instead of the skybox - then, the correct alpha is applied.

EDIT: I should add that the alpha is rendered correctly in the scene view, just not in the game view. Scene View (Correct, though a different angle): alt text

Game View (Incorrect, still incorrect if viewed from any angle): alt text

Code:

Shader "Custom/Star_Corona" { Properties { } SubShader {

         Tags
     {
         "Queue"="Transparent"
         "RenderType"="Transparent"
     }
     Cull Off
     Blend SrcAlpha OneMinusSrcAlpha
     ZWrite Off
 
         Pass
         {
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
 
             struct v2f
             {
                 float4 pos : SV_POSITION;
                 float2 uv : TEXCOORD0;
                 float4 vertex : COLOR0;
             };
             
             v2f vert (appdata_base v)
             {
                 v2f o;
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.uv = v.texcoord;
                 o.vertex = v.vertex;
                 return o;
             }
             
             float4 frag (v2f i) : COLOR
             {
                 float dist = length(i.vertex)*3;
                 float brightness = (1.0/(dist*dist) - 0.1) * 1;
 
 
 
                 return float4(1,0,0,0.5);
 
             }
             ENDCG
         }
     }
 }



Incidentally, this is Copyright Valve, but you can get it for free from the Asset Store - under "TheLabRenderer".

 Shader "Valve/vr_skybox"
 {
     Properties
     {
         _Tint( "Tint Color", Color ) = ( .5, .5, .5, .5 )
         [Gamma] _Exposure( "Exposure", Range( 0, 8 ) ) = 1.0
         _Rotation( "Rotation", Range( 0, 360 ) ) = 0
         [NoScaleOffset] _FrontTex( "Front [+Z]   (HDR)", 2D) = "grey" {}
         [NoScaleOffset] _BackTex( "Back [-Z]   (HDR)", 2D) = "grey" {}
         [NoScaleOffset] _LeftTex( "Left [+X]   (HDR)", 2D) = "grey" {}
         [NoScaleOffset] _RightTex( "Right [-X]   (HDR)", 2D) = "grey" {}
         [NoScaleOffset] _UpTex( "Up [+Y]   (HDR)", 2D) = "grey" {}
         [NoScaleOffset] _DownTex( "Down [-Y]   (HDR)", 2D) = "grey" {}
     }
 
     SubShader
     {
         Tags
         {
             "Queue"="Background"
             "RenderType"="Background"
             "PreviewType"="Skybox"
         }
         Cull Off
         ZWrite Off
         //doesn't change anything, messed with Zero and One in both spots, no fix
         //Blend SrcAlpha OneMinusSrcAlpha
     
         CGINCLUDE
             #pragma target 5.0
             #pragma only_renderers d3d11
             #pragma exclude_renderers gles
 
             //-------------------------------------------------------------------------------------------------------------------------------------------------------------
             #include "UnityCG.cginc"
             #include "vr_utils.cginc"
 
             //-------------------------------------------------------------------------------------------------------------------------------------------------------------
             float4 _Tint;
             float _Exposure;
             float _Rotation;
             
             #define g_vTint _Tint
             #define g_flExposure _Exposure
             #define g_flRotation _Rotation
         
             //-------------------------------------------------------------------------------------------------------------------------------------------------------------
             float4 RotateAroundYInDegrees( float4 vPositionOs, float flDegrees )
             {
                 float flRadians = flDegrees * UNITY_PI / 180.0;
                 float flSin, flCos;
                 sincos( flRadians, flSin, flCos );
                 float2x2 m = float2x2( flCos, -flSin, flSin, flCos );
                 return float4( mul( m, vPositionOs.xz ), vPositionOs.yw ).xzyw;
             }
             
             //-------------------------------------------------------------------------------------------------------------------------------------------------------------
             struct VS_INPUT
             {
                 float4 vPositionOs : POSITION;
                 float2 vTexcoord : TEXCOORD0;
             };
 
             struct PS_INPUT
             {
                 float4 vPositionPs : SV_POSITION;
                 float2 vTexcoord : TEXCOORD0;
             };
 
             struct PS_OUTPUT
             {
                 float4 vColor : SV_Target0;
             };
 
             //-------------------------------------------------------------------------------------------------------------------------------------------------------------
             PS_INPUT SkyboxVs( VS_INPUT v )
             {
                 PS_INPUT o;
                 o.vPositionPs.xyzw = mul( UNITY_MATRIX_MVP, RotateAroundYInDegrees( v.vPositionOs.xyzw, g_flRotation ) );
                 o.vTexcoord.xy = v.vTexcoord.xy;
                 return o;
             }
 
             //-------------------------------------------------------------------------------------------------------------------------------------------------------------
             PS_OUTPUT SkyboxPs( PS_INPUT i, sampler2D faceSampler, float4 faceSamplerDecode )
             {
                 float4 vSkyboxTexel = tex2D( faceSampler, i.vTexcoord.xy ).rgba;
                 float3 vSkyboxLinearColor = DecodeHDR( vSkyboxTexel.rgba, faceSamplerDecode.rgba );
 
                 PS_OUTPUT o;
 
                 o.vColor.rgb = saturate( vSkyboxLinearColor.rgb * g_vTint.rgb * unity_ColorSpaceDouble.rgb * g_flExposure );
                 o.vColor.a = 1.0;
 
                 // Dither to fix banding artifacts
                 o.vColor.rgb += ScreenSpaceDither( i.vPositionPs.xy );
 
                 return o;
             }
 
         ENDCG
 
         Pass
         {
             CGPROGRAM
                 #pragma vertex SkyboxVs
                 #pragma fragment MainPs
                 sampler2D _FrontTex;
                 float4 _FrontTex_HDR;
                 PS_OUTPUT MainPs( PS_INPUT i ) { return SkyboxPs( i, _FrontTex, _FrontTex_HDR ); }
             ENDCG 
         }
 
         Pass
         {
             CGPROGRAM
                 #pragma vertex SkyboxVs
                 #pragma fragment MainPs
                 sampler2D _BackTex;
                 float4 _BackTex_HDR;
                 PS_OUTPUT MainPs( PS_INPUT i ) { return SkyboxPs( i, _BackTex, _BackTex_HDR ); }
             ENDCG 
         }
 
         Pass
         {
             CGPROGRAM
                 #pragma vertex SkyboxVs
                 #pragma fragment MainPs
                 sampler2D _LeftTex;
                 float4 _LeftTex_HDR;
                 PS_OUTPUT MainPs( PS_INPUT i ) { return SkyboxPs( i, _LeftTex, _LeftTex_HDR ); }
             ENDCG
         }
         Pass
         {
             CGPROGRAM
                 #pragma vertex SkyboxVs
                 #pragma fragment MainPs
                 sampler2D _RightTex;
                 float4 _RightTex_HDR;
                 PS_OUTPUT MainPs( PS_INPUT i ) { return SkyboxPs( i, _RightTex, _RightTex_HDR ); }
             ENDCG
         }    
         Pass
         {
             CGPROGRAM
                 #pragma vertex SkyboxVs
                 #pragma fragment MainPs
                 sampler2D _UpTex;
                 float4 _UpTex_HDR;
                 PS_OUTPUT MainPs( PS_INPUT i ) { return SkyboxPs( i, _UpTex, _UpTex_HDR ); }
             ENDCG
         }    
         Pass
         {
             CGPROGRAM
                 #pragma vertex SkyboxVs
                 #pragma fragment MainPs
                 sampler2D _DownTex;
                 float4 _DownTex_HDR;
                 PS_OUTPUT MainPs( PS_INPUT i ) { return SkyboxPs( i, _DownTex, _DownTex_HDR ); }
             ENDCG
         }
     }
 }
 


scene.png (122.8 kB)
game.png (97.0 kB)
Comment

People who like this

0 Show 2
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 Namey5 · Jun 18, 2016 at 06:30 AM 3
Share

When working with blending, make sure the material queue is correct. A common bug in Unity 5 resets the material queue back to 2000, so you should check that your blended material's queue is 3000.

http://docs.unity3d.com/Manual/InspectorOptions.html

Go into debug mode, select the material and check this.

avatar image Lupod Namey5 · Mar 05, 2017 at 04:48 PM 0
Share

I had the same problem as the OP and it was indeed caused by the rendering queue set to 2000. Thanks for that hint!

1 Reply

  • Sort: 
avatar image

Answer by LilaQ · Sep 01, 2016 at 11:40 PM

@Namey5 This is an awesome tip. You just saved my shader. Had the exact same problem as OP, so in my opinion this should be accpeted as a correct answer.

Debug-Mode in Inspector -> Correct "Custom Render Queue" from 2000 to 3000

Comment

People who like this

0 Show 1 · 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 Namey5 · Sep 02, 2016 at 05:42 AM 0
Share

Yeah, it's funny because I've been answering stuff on here for a while now, and most of the shader problems revolve around this single bug. I encountered it a lot myself, to an incredibly frustrating level, so it's always the first thing I say whenever someone is working with blending of any kind.

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 by June 9. 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

78 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

Related Questions

Shader Transparency Issues 1 Answer

Materia Drag & Drop Bug 0 Answers

Rotating and lerping to another material with custom shader. 0 Answers

Shader For Welding Simulation? (With References) 0 Answers

Transparent textures are still slightly darker, how do I achieve 100% 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