- Home /

# Transparency With Atmos Scatter Shader

So, I found this shader on the forums that PRD wrote a little while back and I'm unsure how to add transparency to this shader. I'm not very experienced with shaders, I can write some simple surface shaders but I don't know how to go about adding transparency to this one in particular. Basically I want to be able to see the stars and such from the surface of my planet instead of a black sky at night (and my planet's moon at day as well). Here's the shader:

```
Shader "Atmosphere/SkyFromAtmosphere"
{
SubShader
{
Tags { "RenderType"="Transparent" }
Pass
{
Cull Front
CGPROGRAM
#include "UnityCG.cginc"
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
uniform float3 v3LightPos; // The direction vector to the light source
uniform float3 v3InvWavelength; // 1 / pow(wavelength, 4) for the red, green, and blue channels
uniform float fOuterRadius; // The outer (atmosphere) radius
uniform float fOuterRadius2; // fOuterRadius^2
uniform float fInnerRadius; // The inner (planetary) radius
uniform float fInnerRadius2; // fInnerRadius^2
uniform float fKrESun; // Kr * ESun
uniform float fKmESun; // Km * ESun
uniform float fKr4PI; // Kr * 4 * PI
uniform float fKm4PI; // Km * 4 * PI
uniform float fScale; // 1 / (fOuterRadius - fInnerRadius)
uniform float fScaleDepth; // The scale depth (i.e. the altitude at which the atmosphere's average density is found)
uniform float fScaleOverScaleDepth; // fScale / fScaleDepth
uniform float fHdrExposure; // HDR exposure
uniform float g; // The Mie phase asymmetry factor
uniform float g2; // The Mie phase asymmetry factor squared
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 t0 : TEXCOORD1;
float3 c0 : COLOR0;
float3 c1 : COLOR1;
};
float scale(float fCos)
{
float x = 1.0 - fCos;
return 0.25 * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));
}
v2f vert(appdata_base v)
{
float3 v3CameraPos = _WorldSpaceCameraPos; // The camera's current position
float fCameraHeight = length(v3CameraPos); // The camera's current height
//float fCameraHeight2 = fCameraHeight*fCameraHeight; // fCameraHeight^2
// Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
float3 v3Pos = mul(_Object2World, v.vertex).xyz;
float3 v3Ray = v3Pos - v3CameraPos;
float fFar = length(v3Ray);
v3Ray /= fFar;
// Calculate the ray's starting position, then calculate its scattering offset
float3 v3Start = v3CameraPos;
float fHeight = length(v3Start);
float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fCameraHeight));
float fStartAngle = dot(v3Ray, v3Start) / fHeight;
float fStartOffset = fDepth*scale(fStartAngle);
const float fSamples = 2.0;
// Initialize the scattering loop variables
float fSampleLength = fFar / fSamples;
float fScaledLength = fSampleLength * fScale;
float3 v3SampleRay = v3Ray * fSampleLength;
float3 v3SamplePoint = v3Start + v3SampleRay * 0.5;
// Now loop through the sample rays
float3 v3FrontColor = float3(0.0, 0.0, 0.0);
for(int i=0; i<int(fSamples); i++)
{
float fHeight = length(v3SamplePoint);
float fDepth = exp(fScaleOverScaleDepth * (fInnerRadius - fHeight));
float fLightAngle = dot(v3LightPos, v3SamplePoint) / fHeight;
float fCameraAngle = dot(v3Ray, v3SamplePoint) / fHeight;
float fScatter = (fStartOffset + fDepth*(scale(fLightAngle) - scale(fCameraAngle)));
float3 v3Attenuate = exp(-fScatter * (v3InvWavelength * fKr4PI + fKm4PI));
v3FrontColor += v3Attenuate * (fDepth * fScaledLength);
v3SamplePoint += v3SampleRay;
}
v2f OUT;
OUT.pos = mul(UNITY_MATRIX_MVP, v.vertex);
OUT.uv = v.texcoord.xy;
// Finally, scale the Mie and Rayleigh colors and set up the varying variables for the pixel shader
OUT.c0 = v3FrontColor * (v3InvWavelength * fKrESun);
OUT.c1 = v3FrontColor * fKmESun;
OUT.t0 = v3CameraPos - v3Pos;
return OUT;
}
// Calculates the Mie phase function
float getMiePhase(float fCos, float fCos2, float g, float g2)
{
return 1.5 * ((1.0 - g2) / (2.0 + g2)) * (1.0 + fCos2) / pow(1.0 + g2 - 2.0*g*fCos, 1.5);
}
// Calculates the Rayleigh phase function
float getRayleighPhase(float fCos2)
{
return 0.75 + 0.75*fCos2;
}
half4 frag(v2f IN) : COLOR
{
float fCos = dot(v3LightPos, IN.t0) / length(IN.t0);
float fCos2 = fCos*fCos;
float3 col = getRayleighPhase(fCos2) * IN.c0 + getMiePhase(fCos, fCos2, g, g2) * IN.c1;
//Adjust color from HDR
col = 1.0 - exp(col * -fHdrExposure);
return float4(col,1.0);
}
ENDCG
}
}
}
```

The best I could do was change the render type, but yeah I don't know how to do this, atm if you look up in the sky you see all of the atmospheric scattering at work but nothing beyond the atmosphere.

Did you fix it? I have the same problem, but it seems that no one in this forum knows how to get the transparency.

I converted your answer to a comment, as it didn't include a solution. Please try and use the comment button in the future, its under each question, answer, and comment.

**Answer** by toddisarockstar
·
Apr 09, 2017 at 10:11 PM

this answer works well:

http://answers.unity3d.com/questions/244837/shader-help-adding-transparency-to-a-shader.html

Yep but only for texture shaders... here's the full project so you can test by yourself: https://www.dropbox.com/s/d99beibmthmbgbf/GPU_GE$$anonymous$$S_atmosphericScattering.zip?dl=0

It's the scene "FromAtmosphere".

**Answer** by vistaero
·
Jan 28, 2018 at 07:00 AM

Replace:

```
Tags { "RenderType"="Transparent" }
```

By:

```
Tags { "Queue"="Transparent" "RenderType"="Transparent" }
Blend One One
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Shader that makes object transparent as light hits it 0 Answers

Fading objects to transparent - Apply shader with a script? 2 Answers

Transparent + affected by light shader 1 Answer

What is happening with sprite transparency in the standard shader in unity 5.6? 1 Answer

Shader that renders pixel with highest alpha value? 0 Answers