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):
Game View (Incorrect, still incorrect if viewed from any angle):
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
}
}
}
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/$$anonymous$$anual/InspectorOptions.html
Go into debug mode, select the material and check this.
I had the same problem as the OP and it was indeed caused by the rendering queue set to 2000. Thanks for that hint!
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
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.
Your answer
Follow this Question
Related Questions
Shader Transparency Issues 1 Answer
Materia Drag & Drop Bug 0 Answers
Rotating and lerping to another material with custom shader. 0 Answers
Add contrast to projector/light? OR add fade in/out to projector/multiply 1 Answer
LWRP - Proper way to set up LWRP Standard and Simple lighting shaders? 0 Answers