Hello Unity fellows,
I have a hard time trying to render a transparent object in the “Background” queue. I spent one day searching Unity Forums and Answers for hints, but everything I tested wasn’t successful. A bit of a context (because the problem may be complex):
I have an outside environement with sea and sky. I use the “Blended SkyBox” shader to create a skybox, and, I want to add a sun (simple quad with texture). I don’t want to use a “far away” object because it requires to have a far away clipping plane, and it is always possible to spot the trick when the sun sets above the sea if the camera is quite high (you see the sun going “through the sea”, not “behind”).
Thus, I just want to render my sun behind everything, except the sky (rendered in Queue = Background), ie using a Queue=“Background+1” tag. Problem is, my sun is round, and thus, my texture is transparent.
I use the following “simple” shader:
Shader "Custom/SunAndMoon" {
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Trans. (Alpha)", 2D) = "white" { }
}
SubShader
{
Tags {"Queue"="Background+1" } // OK for Queue=2500 or +. Below, it doesn't work.
Cull Off
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
Lighting Off
SetTexture [_MainTex]
{
constantColor [_Color]
Combine texture * constant, texture * constant
}
}
}
}
Actually this shader gives this render:
Funny thing is, if I change the Queue to “Background+1501” it works (Alpha blending is taken into account, see this render:
But it renders the sun after all Geometry, which I don’t want. If Queue is “Background +1500” or lower, Alpha Blending doesn’t work.
It seems that Queues do additionnal stuff appart from managing rendering order.
Other tests I made:
- Using a cutout (AlphaTest Greater 0.2) in Queue = Background+1 → OK, But I’d like to have the “glow” effect achieved with alpha blending
- Disabling skybox, enabling Alpha blending and Queue = Background+1 → OK
This tends to show that the problem is not really with alpha blending but rather with getting the “destination color”. With a solid clear color, it’s OK. With a skybox it’s not, we get that “dark grey color” around the sun spot. Talking about it… - …The “black” around the sun is not pure black. It’s some dark grey which might be used by unity to “clear” the viewport. It’s NOT related to ambient light, and NOT related to the “clear color” specified in the camera settings (this drives me crazy). I can see it by turning my skybox alpha to 0.
- Enabling skybox and Alpha Blending, Queue = Background+1, Disabling ZWrite (because at some point, I’ll have to)-> The sun is not visible anymore! But actually, by playing with the skybox alpha, it appears that the sun is rendered BEHIND the sky…
- Trying to use a vertex/fragment shader instead of my fixed pipeline shader seemed to work at some point. But when I restarted Unity, the same problem raises again. It may be influenced by shaders order in memory I guess.
Those results don’t make any sense at all for me. I must have missed something.
Any help from Unity gurus would be greatly appreciated.
Thanks a lot and sorry for the long explanation.