Skip to content

Commit

Permalink
Minor shader optimizations, improved dithering (8x8)
Browse files Browse the repository at this point in the history
  • Loading branch information
SlightlyMad committed Jun 5, 2016
1 parent d8db1e1 commit 62d15cc
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 9 deletions.
84 changes: 82 additions & 2 deletions Assets/Scripts/VolumetricLightRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,17 @@ private void GenerateDitherTexture()
return;
}

int size = 8;
#if DITHER_4_4
size = 4;
#endif
// again, I couldn't make it work with Alpha8
_ditheringTexture = new Texture2D(4, 4, TextureFormat.RGBA32, false);
Color32[] c = new Color32[4 * 4];
_ditheringTexture = new Texture2D(size, size, TextureFormat.Alpha8, false, true);
_ditheringTexture.filterMode = FilterMode.Point;
Color32[] c = new Color32[size * size];

byte b;
#if DITHER_4_4
b = (byte)(0.0f / 16.0f * 255); c[0] = new Color32(b, b, b, b);
b = (byte)(8.0f / 16.0f * 255); c[1] = new Color32(b, b, b, b);
b = (byte)(2.0f / 16.0f * 255); c[2] = new Color32(b, b, b, b);
Expand All @@ -469,6 +475,80 @@ private void GenerateDitherTexture()
b = (byte)(7.0f / 16.0f * 255); c[13] = new Color32(b, b, b, b);
b = (byte)(13.0f / 16.0f * 255); c[14] = new Color32(b, b, b, b);
b = (byte)(5.0f / 16.0f * 255); c[15] = new Color32(b, b, b, b);
#else
int i = 0;
b = (byte)(1.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(49.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(13.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(61.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(4.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(52.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(16.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(64.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);

b = (byte)(33.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(17.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(45.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(29.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(36.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(20.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(48.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(32.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);

b = (byte)(9.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(57.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(5.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(53.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(12.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(60.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(8.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(56.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);

b = (byte)(41.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(25.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(37.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(21.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(44.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(28.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(40.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(24.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);

b = (byte)(3.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(51.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(15.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(63.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(2.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(50.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(14.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(62.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);

b = (byte)(35.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(19.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(47.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(31.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(34.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(18.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(46.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(30.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);

b = (byte)(11.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(59.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(7.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(55.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(10.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(58.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(6.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(54.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);

b = (byte)(43.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(27.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(39.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(23.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(42.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(26.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(38.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
b = (byte)(22.0f / 65.0f * 255); c[i++] = new Color32(b, b, b, b);
#endif

_ditheringTexture.SetPixels32(c);
_ditheringTexture.Apply();
Expand Down
25 changes: 19 additions & 6 deletions Assets/Shaders/VolumetricLight.shader
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,23 @@ Shader "Sandbox/VolumetricLight"
//-----------------------------------------------------------------------------------------
// MieScattering
//-----------------------------------------------------------------------------------------
float MieScattering(float cosAngle)
float MieScattering(float cosAngle, float4 g)
{
return _MieG.w * (_MieG.x / (pow(_MieG.y - _MieG.z * cosAngle, 1.5)));
return g.w * (g.x / (pow(g.y - g.z * cosAngle, 1.5)));
}

//-----------------------------------------------------------------------------------------
// RayMarch
//-----------------------------------------------------------------------------------------
float4 RayMarch(v2f i, float3 rayStart, float3 rayDir, float rayLength)
{
#ifdef DITHER_4_4
float2 interleavedPos = (fmod(floor(i.pos.xy), 4.0));
float offset = tex2D(_DitherTexture, interleavedPos / 4.0 + float2(0.5 / 4.0, 0.5 / 4.0)).w;
#else
float2 interleavedPos = (fmod(floor(i.pos.xy), 8.0));
float offset = tex2D(_DitherTexture, interleavedPos / 8.0 + float2(0.5 / 8.0, 0.5 / 8.0)).w;
#endif

int stepCount = _SampleCount;

Expand Down Expand Up @@ -280,21 +285,29 @@ Shader "Sandbox/VolumetricLight"
float scattering = _VolumetricLight.x * stepSize * density;
extinction += _VolumetricLight.y * stepSize * density;// +scattering;

float4 light = atten * _LightColor * scattering * exp(-extinction);
float4 light = atten * scattering * exp(-extinction);

//#if PHASE_FUNCTOIN
#if !defined (DIRECTIONAL) && !defined (DIRECTIONAL_COOKIE)
// phase functino for spot and point lights
float3 tolight = normalize(currentPosition - _LightPos.xyz);
cosAngle = dot(tolight, -rayDir);
#endif
light *= MieScattering(cosAngle);
light *= MieScattering(cosAngle, _MieG);
#endif
//#endif

vlight += light;

currentPosition += step;
}

#if defined (DIRECTIONAL) || defined (DIRECTIONAL_COOKIE)
// apply phase function for dir light
vlight *= MieScattering(cosAngle, _MieG);
#endif

// apply light's color
vlight *= _LightColor;

vlight = max(0, vlight);
#if defined (DIRECTIONAL) || defined (DIRECTIONAL_COOKIE) // use "proper" out-scattering/absorption for dir light
vlight.w = exp(-extinction);
Expand Down
2 changes: 1 addition & 1 deletion ProjectSettings/QualitySettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ QualitySettings:
shadowResolution: 2
shadowProjection: 1
shadowCascades: 4
shadowDistance: 300
shadowDistance: 1000
shadowNearPlaneOffset: 2
shadowCascade2Split: 0.33333334
shadowCascade4Split: {x: 0.06666667, y: 0.19999999, z: 0.46666664}
Expand Down

0 comments on commit 62d15cc

Please sign in to comment.