- Home /
Cant seem to render alpha surface shader BEHIND geometry
Hello there. I'm attempting to add a spherical 'glow' behind my planets in a space exploration game. There are 2 issues:
1) I can't seem to get the effect to render before (behind) the geometry in my scene. I've tried setting the Tag as "Queue"="Background", "Queue"="Geometry - 1" in combination with both "RenderType"="Opaque" and "RenderType"="Transparent", all without luck. The planet surface is rendering in the Geometry queue and I've also tried adding a 'forward' offset to that. Both the planet surface and the atmosphere glow seem to be jostling for render position, creating a frantic, moving mosaic. That looks real bad.
2) When I include this line...
#pragma surface surf Lambert alpha:fade
..I get the alpha effect, but then the entire object disappears at distance. This being a space game, the distances are large, but I'm talking about a few thousand units. When I don't include the 'alpha:fade' part, the alpha does not render (everything is opaque).
Here's the surface shader, which gets passed an atmosphere tint Color and an atmosphere density float.
Shader "Custom/planet glow" {
Properties {
_glowTint ("Glow Tint", Color) = (0.5,0.8,1,1)
_atmosphereMix ("Atmosphere Mix", float) = 0
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Background" }
Cull Front // I want only the far faces to render (behind the actual planet surface)
CGPROGRAM
#pragma surface surf Lambert alpha:fade
#pragma target 4.0
struct Input {
float3 viewDir;
};
fixed4 _glowTint;
float _atmosphereMix;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = _glowTint * _atmosphereMix;
float cameraNormalDP = saturate(dot( normalize(IN.viewDir), -o.Normal ) * 4.5);
o.Alpha = cameraNormalDP * cameraNormalDP * cameraNormalDP; // makes the edge fade 'faster'
o.Emission = _glowTint * _atmosphereMix;
}
ENDCG
}
FallBack "Diffuse"
}
If anyone has any pointers as to what I'm missing, that would be great! Cheers.
I should add that I have since been trying various combinations of settings for ZTest and ZWrite in both this shader and the shader for the planet surface. Oddly, when I set the planet to ZTest Always (which, apparently, is supposed to render the pixel, irrespective of depth), I'm still getting the crazy interference mosaic. If anyone could help, that would be a brain saver!
I have now also tried 'Offset value, value' in the glow shader, to put it 'behind' the planet surface. This partially works (SO$$anonymous$$E z-fighting artifacts are still visible though) AND I have tried instantiating the glow at 3x the size and positioning it 3x as far away as the planet sphere. This, bizarrely has also not worked! I have tried this method, with both shaders' RenderTypes and Queues different AND the same. Anyone know what might be going on?
Answer by FortisVenaliter · Jan 27, 2016 at 07:49 PM
Are you using a sphere or a plane to draw the glow? If it's a sphere, try turning it inside out. That way the front will be culled, but the back will be rendered, behind the planet. If it's a plane, put it physically behind the planet.
I'm using a sphere; the shader above culls front faces and inverts the normals (at line 23) - so, basically, exactly what you're suggesting. I'm still getting z-fighting (or, at best, some of the planet surface polygons are flickering). I have even tried positioning the 'glow' sphere further away from the camera than the planet surface sphere (and expanding its radius by the same proportion) and STILL I'm getting z-fighting. But thanks for the reply. If you have any other ideas, it would be great to hear them!
Then your camera near and far planes are simply too far apart. You need to increase the near-clip or utilize multiple cameras.
Your answer
![](https://koobas.hobune.stream/wayback/20220612061655im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distant object won't render in 'distance camera' 0 Answers
How do I add alpha writes to this shader? 0 Answers
RenderTexture Painting - color blending issues 1 Answer
What happens when the alpha value is outside the 0..1 range in surface shader lighting models? 0 Answers
Luminance to Alpha 0 Answers