- Home /
Atmospheric Scattering Shader Glitch on Floating Origin Late Update
Hi All,
Ive got an atmospheric scattering shader on a planet which is moved by a floating origin script. The floating origin checks the main camera position and moves everything back to the origin when the distance to the origin exceeds a threshold.
When the floating origin script moves everything the atmospheric scattering shader hiccups and creates a frame that looks like a short flash. Example video below.
https://www.youtube.com/watch?v=HoLRgvRIbMc
The atmospheric shader is initialized every Update with new sun/camera positions and the floating origin happens in LateUpdate once the threshold is exceeded. I am not entirely sure how the shader produces this one bad frame after the floating origin LateUpdate but I would love everyone's thoughts!
The Atmospheric mono script controlling the shader:
public class Atmosphere : MonoBehaviour
{
public GameObject m_sun;
public Material m_groundMaterial;
public Material m_skyMaterial;
public float m_hdrExposure = 0.8f;
public Vector3 m_waveLength = new Vector3(0.65f, 0.57f, 0.475f); // Wave length of sun light
public float m_ESun = 20.0f; // Sun brightness constant
public float m_kr = 0.0025f; // Rayleigh scattering constant
public float m_km = 0.0010f; // Mie scattering constant
public float m_g = -0.990f; // The Mie phase asymmetry factor, must be between 0.999 to -0.999
//Dont change these
private const float m_outerScaleFactor = 1.025f; // Difference between inner and ounter radius. Must be 2.5%
private float m_innerRadius; // Radius of the ground sphere
private float m_outerRadius; // Radius of the sky sphere
private float m_scaleDepth = 0.25f; // The scale depth (i.e. the altitude at which the atmosphere's average density is found)
private bool Waiting;
void Start()
{
Waiting = false;
//Get the radius of the sphere. This presumes that the sphere mesh is a unit sphere (radius of 1)
//that has been scaled uniformly on the x, y and z axis
float radius = transform.localScale.x;
m_innerRadius = radius;
//The outer sphere must be 2.5% larger that the inner sphere
m_outerRadius = m_outerScaleFactor * radius;
InitMaterial(m_groundMaterial);
InitMaterial(m_skyMaterial);
}
void Update()
{
InitMaterial(m_groundMaterial);
InitMaterial(m_skyMaterial);
}
void InitMaterial(Material mat)
{
Vector3 invWaveLength4 = new Vector3(1.0f / Mathf.Pow(m_waveLength.x, 4.0f), 1.0f / Mathf.Pow(m_waveLength.y, 4.0f), 1.0f / Mathf.Pow(m_waveLength.z, 4.0f));
float scale = 1.0f / (m_outerRadius - m_innerRadius);
//Hungarian notation, yuck.
mat.SetVector("v3LightPos", m_sun.transform.forward * -1.0f);
mat.SetVector("v3InvWavelength", invWaveLength4);
mat.SetFloat("fOuterRadius", m_outerRadius);
mat.SetFloat("fOuterRadius2", m_outerRadius * m_outerRadius);
mat.SetFloat("fInnerRadius", m_innerRadius);
mat.SetFloat("fInnerRadius2", m_innerRadius * m_innerRadius);
mat.SetFloat("fKrESun", m_kr * m_ESun);
mat.SetFloat("fKmESun", m_km * m_ESun);
mat.SetFloat("fKr4PI", m_kr * 4.0f * Mathf.PI);
mat.SetFloat("fKm4PI", m_km * 4.0f * Mathf.PI);
mat.SetFloat("fScale", scale);
mat.SetFloat("fScaleDepth", m_scaleDepth);
mat.SetFloat("fScaleOverScaleDepth", scale / m_scaleDepth);
mat.SetFloat("fHdrExposure", m_hdrExposure);
mat.SetFloat("g", m_g);
mat.SetFloat("g2", m_g * m_g);
mat.SetVector("v3LightPos", m_sun.transform.forward * -1.0f);
mat.SetVector("v3Translate", transform.localPosition);
}
}
The shader itself:
Shader "Custom/SkyAtmosphere"
{
SubShader
{
Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True" }
Pass
{
//Blend SrcAlpha OneMinusSrcAlpha
Blend One One
Cull Front
CGPROGRAM
#include "UnityCG.cginc"
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
uniform float3 v3Translate; // The objects world pos
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 - v3Translate; // 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(unity_ObjectToWorld, v.vertex).xyz - v3Translate;
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 = UnityObjectToClipPos(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);
//float num =(col.r + col.g + col.b);
return float4(col,1.0);
}
ENDCG
}
}
}
The Floating Origin Script can be found here: http://wiki.unity3d.com/index.php/Floating_Origin
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Changing material in code doesn't update shader 3 Answers
Dymanic Mesh Hiding 2 Answers
C#-script to shader list variable 2 Answers