Skip to content

Commit

Permalink
Add MultiScattLUT to AtmosphericScattering
Browse files Browse the repository at this point in the history
  • Loading branch information
2223 committed Dec 21, 2020
1 parent 3bbb650 commit 7813d9d
Show file tree
Hide file tree
Showing 17 changed files with 1,498 additions and 761 deletions.
3 changes: 3 additions & 0 deletions Assets/Editor/AtmosphericScatteringEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class AtmosphericScatteringEditor : Editor
SerializedProperty ReflectionProbe;
SerializedProperty ReflectionProbeResolution;
SerializedProperty DistanceScale;
SerializedProperty MultiScatterFactor;

string[] ResolutionNames = { "32", "64", "128", "256" };
int[] Resolutions = { 32, 64, 128, 256 };
Expand Down Expand Up @@ -78,6 +79,7 @@ void OnEnable()
ReflectionProbe = serializedObject.FindProperty("ReflectionProbe");
ReflectionProbeResolution = serializedObject.FindProperty("ReflectionProbeResolution");
DistanceScale = serializedObject.FindProperty("DistanceScale");
MultiScatterFactor = serializedObject.FindProperty("MultiScatterFactor");
}

public override void OnInspectorGUI()
Expand Down Expand Up @@ -115,6 +117,7 @@ public override void OnInspectorGUI()
MieScatterCoef.floatValue = EditorGUILayout.Slider("Mie Coef (*)", MieScatterCoef.floatValue, 0, 4);
MieG.floatValue = EditorGUILayout.Slider("MieG", MieG.floatValue, 0, 0.999f);
DistanceScale.floatValue = EditorGUILayout.FloatField("Distance Scale", DistanceScale.floatValue);
MultiScatterFactor.floatValue = EditorGUILayout.FloatField("MultiScatterFactor (*)", MultiScatterFactor.floatValue);
GUILayout.Label("* - Change requires LookUp table update");
if (GUILayout.Button("Update LookUp Tables") && a.IsInitialized())
((AtmosphericScattering)target).CalculateLightLUTs();
Expand Down
35 changes: 30 additions & 5 deletions Assets/Scripts/AtmosphericScattering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public enum LightShaftsQuality
private RenderTexture _inscatteringLUT;
private RenderTexture _extinctionLUT;

private Vector2 _multiscattLUTSize = new Vector2(32, 32);
private RenderTexture _multiscattLUT;

private const int LightLUTSize = 128;

[ColorUsage(false, true, 0, 15, 0, 15)]
Expand Down Expand Up @@ -104,6 +107,7 @@ public enum LightShaftsQuality
[Range(0.0f, 0.999f)]
public float MieG = 0.76f;
public float DistanceScale = 1;
public float MultiScatterFactor = 10;

public bool UpdateLightColor = true;
[Range(0.5f, 3.0f)]
Expand Down Expand Up @@ -382,36 +386,53 @@ private void PrecomputeSkyboxLUT()
_skyboxLUT.Create();
}

#if HIGH_QUALITY
if (_skyboxLUT2 == null)
{
_skyboxLUT2 = new RenderTexture((int)_skyboxLUTSize.x, (int)_skyboxLUTSize.y, 0, RenderTextureFormat.RGHalf, RenderTextureReadWrite.Linear);
_skyboxLUT2 = new RenderTexture((int)_skyboxLUTSize.x, (int)_skyboxLUTSize.y, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
_skyboxLUT2.volumeDepth = (int)_skyboxLUTSize.z;
_skyboxLUT2.isVolume = true;
_skyboxLUT2.enableRandomWrite = true;
_skyboxLUT2.name = "SkyboxLUT2";
_skyboxLUT2.Create();
}
#endif

int kernel = ScatteringComputeShader.FindKernel("SkyboxLUT");

ScatteringComputeShader.SetTexture(kernel, "_SkyboxLUT", _skyboxLUT);
#if HIGH_QUALITY
ScatteringComputeShader.SetTexture(kernel, "_SkyboxLUT2", _skyboxLUT2);
#endif

UpdateCommonComputeShaderParameters(kernel);

ScatteringComputeShader.Dispatch(kernel, (int)_skyboxLUTSize.x, (int)_skyboxLUTSize.y, (int)_skyboxLUTSize.z);
}

private void PrecomputeMultiscattLUT()
{
if (_multiscattLUT == null)
{
_multiscattLUT = new RenderTexture((int)_multiscattLUTSize.x, (int)_multiscattLUTSize.y, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
_multiscattLUT.enableRandomWrite = true;
_multiscattLUT.name = "MultiscattLUT";
_multiscattLUT.Create();
}

int kernel = ScatteringComputeShader.FindKernel("NewMultiScattLUT");

ScatteringComputeShader.SetTexture(kernel, "_MultiscattLUT", _multiscattLUT);

UpdateCommonComputeShaderParameters(kernel);

ScatteringComputeShader.Dispatch(kernel, (int)_multiscattLUTSize.x, (int)_multiscattLUTSize.y, 1);
}

/// <summary>
///
/// </summary>
private void UpdateCommonComputeShaderParameters(int kernel)
{
ScatteringComputeShader.SetTexture(kernel, "_ParticleDensityLUT", _particleDensityLUT);
ScatteringComputeShader.SetTexture(kernel, "_MultiscattLUTTex", _multiscattLUT);

ScatteringComputeShader.SetFloat("_AtmosphereHeight", AtmosphereHeight);
ScatteringComputeShader.SetFloat("_PlanetRadius", PlanetRadius);
ScatteringComputeShader.SetVector("_DensityScaleHeight", DensityScale);
Expand All @@ -421,6 +442,8 @@ private void UpdateCommonComputeShaderParameters(int kernel)
ScatteringComputeShader.SetVector("_ExtinctionR", RayleighSct * RayleighExtinctionCoef);
ScatteringComputeShader.SetVector("_ExtinctionM", MieSct * MieExtinctionCoef);

ScatteringComputeShader.SetFloat("_MultiScatterFactor", MultiScatterFactor);

ScatteringComputeShader.SetVector("_IncomingLight", IncomingLight);
ScatteringComputeShader.SetFloat("_MieG", MieG);
}
Expand Down Expand Up @@ -490,6 +513,7 @@ private void UpdateMaterialParameters(Material material)

material.SetTexture("_SkyboxLUT", _skyboxLUT);
material.SetTexture("_SkyboxLUT2", _skyboxLUT2);
material.SetTexture("_MultiscattLUTTex", _multiscattLUT);
}

/// <summary>
Expand Down Expand Up @@ -525,6 +549,7 @@ public void CalculateLightLUTs()
_lightColorTextureTemp.ReadPixels(new Rect(0, 0, LightLUTSize, 1), 0, 0);
_directionalLightLUT = _lightColorTextureTemp.GetPixels(0, 0, LightLUTSize, 1);

PrecomputeMultiscattLUT();
PrecomputeSkyboxLUT();
}

Expand Down
19 changes: 17 additions & 2 deletions Assets/Shaders/AtmosphericScattering.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ sampler3D _SkyboxLUT2;
sampler3D _InscatteringLUT;
sampler3D _ExtinctionLUT;

sampler2D _MultiscattLUTTex;

//-----------------------------------------------------------------------------------------
// RaySphereIntersection
//-----------------------------------------------------------------------------------------
Expand Down Expand Up @@ -167,6 +169,15 @@ float3 ComputeOpticalDepth(float2 density)
return _IncomingLight.xyz * extinction;
}

float3 GetMultipleScattering(float3 position, float3 planetCenter, float3 lightDir)
{
float height = length(position - planetCenter) - _PlanetRadius;

float cosAngle = dot(normalize(position - planetCenter), lightDir.xyz);

return tex2D(_MultiscattLUTTex, float2(cosAngle * 0.5 + 0.5, (height / _AtmosphereHeight))).rgb;
}

//-----------------------------------------------------------------------------------------
// IntegrateInscattering
//-----------------------------------------------------------------------------------------
Expand All @@ -178,6 +189,7 @@ float4 IntegrateInscattering(float3 rayStart, float3 rayDir, float rayLength, fl
float2 densityCP = 0;
float3 scatterR = 0;
float3 scatterM = 0;
float3 multiScatt = 0;

float2 localDensity;
float2 densityPA;
Expand All @@ -196,7 +208,8 @@ float4 IntegrateInscattering(float3 rayStart, float3 rayDir, float rayLength, fl
float3 p = rayStart + step * s;

GetAtmosphereDensity(p, planetCenter, lightDir, localDensity, densityPA);
densityCP += (localDensity + prevLocalDensity) * (stepSize / 2.0);
float2 avgDen = (localDensity + prevLocalDensity) / 2.0;
densityCP += avgDen * (stepSize);

prevLocalDensity = localDensity;

Expand All @@ -206,6 +219,8 @@ float4 IntegrateInscattering(float3 rayStart, float3 rayDir, float rayLength, fl
scatterR += (localInscatterR + prevLocalInscatterR) * (stepSize / 2.0);
scatterM += (localInscatterM + prevLocalInscatterM) * (stepSize / 2.0);

multiScatt += (avgDen.x * _ScatteringR + avgDen.y * _ScatteringM) * GetMultipleScattering(rayStart, planetCenter, lightDir);

prevLocalInscatterR = localInscatterR;
prevLocalInscatterM = localInscatterM;
}
Expand All @@ -214,7 +229,7 @@ float4 IntegrateInscattering(float3 rayStart, float3 rayDir, float rayLength, fl
// phase function
ApplyPhaseFunction(scatterR, scatterM, dot(rayDir, -lightDir.xyz));
//scatterR = 0;
float3 lightInscatter = (scatterR * _ScatteringR + scatterM * _ScatteringM) * _IncomingLight.xyz;
float3 lightInscatter = (scatterR * _ScatteringR + scatterM * _ScatteringM + multiScatt) * _IncomingLight.xyz;
lightInscatter += RenderSun(m, dot(rayDir, -lightDir.xyz)) * _SunIntensity;
float3 lightExtinction = exp(-(densityCP.x * _ExtinctionR + densityCP.y * _ExtinctionM));

Expand Down
Loading

0 comments on commit 7813d9d

Please sign in to comment.