Skip to content

Commit

Permalink
Optimized the response function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Keijiro Takahashi authored and Keijiro Takahashi committed Mar 14, 2016
1 parent 0fb9e18 commit 5ffa49a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Assets/Kino/Bloom/Bloom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void OnRenderImage(RenderTexture source, RenderTexture destination)
_material.SetFloat("_Threshold", threshold);

var knee = threshold * _softKnee + 1e-5f;
var curve = new Vector3(0.25f / knee, threshold - knee, knee);
var curve = new Vector3(threshold - knee, knee * 2, 0.25f / knee);
_material.SetVector("_Curve", curve);

var pfo = !_highQuality && _antiFlicker;
Expand Down
14 changes: 4 additions & 10 deletions Assets/Kino/Bloom/Editor/BloomGraphDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void Prepare(Bloom bloom)
}

_threshold = bloom.threshold;
_knee = bloom.softKnee * _threshold;
_knee = bloom.softKnee * _threshold + 1e-5f;
_intensity = bloom.intensity;
}

Expand Down Expand Up @@ -113,15 +113,9 @@ public void DrawGraph()

float ResponseFunction(float x)
{
if (Mathf.Abs(x - _threshold) < _knee)
{
var x2 = x - _threshold + _knee;
return 0.25f * x2 * x2 * _intensity / Mathf.Max(_knee, 1e-5f);
}
else
{
return Mathf.Max(0, x - _threshold) * _intensity;
}
var rq = Mathf.Clamp(x - _threshold + _knee, 0, _knee * 2);
rq = rq * rq * 0.25f / _knee;
return Mathf.Max(rq, x - _threshold) * _intensity;
}

#endregion
Expand Down
11 changes: 4 additions & 7 deletions Assets/Kino/Bloom/Shader/Bloom.shader
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,14 @@ Shader "Hidden/Kino/Bloom"
m = GammaToLinearSpace(m);
#endif
// Pixel brightness
half br = Brightness(m) + 1e-5;

// Over-threshold part: linear response
half rl = max(br - _Threshold, 0);
half br = Brightness(m);

// Under-threshold part: quadratic curve
half rq = br - _Curve.y;
rq = _Curve.x * rq * rq;
half rq = clamp(br - _Curve.x, 0, _Curve.y);
rq = _Curve.z * rq * rq;

// Combine and apply the brightness response curve.
m *= lerp(rl, rq, abs(br - _Threshold) < _Curve.z) / br;
m *= max(rq, br - _Threshold) / (br + 1e-5);

return EncodeHDR(m);
}
Expand Down

0 comments on commit 5ffa49a

Please sign in to comment.