HableCurve.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. using static UnityEngine.Mathf;
  2. namespace UnityEngine.Rendering
  3. {
  4. /// <summary>
  5. /// An implementation of Hable's artist-friendly tonemapping curve.
  6. /// http://filmicworlds.com/blog/filmic-tonemapping-with-piecewise-power-curves/
  7. /// </summary>
  8. public class HableCurve
  9. {
  10. /// <summary>
  11. /// Individual curve segment.
  12. /// </summary>
  13. public class Segment
  14. {
  15. /// <summary>
  16. /// The offset of the segment on the X axis.
  17. /// </summary>
  18. public float offsetX;
  19. /// <summary>
  20. /// The offset of the segment on the Y axis.
  21. /// </summary>
  22. public float offsetY;
  23. /// <summary>
  24. /// The scale of the segment on the X axis.
  25. /// </summary>
  26. public float scaleX;
  27. /// <summary>
  28. /// The scale of the segment on the Y axis.
  29. /// </summary>
  30. public float scaleY;
  31. /// <summary>
  32. /// <c>ln(A)</c> constant in the power curve <c>y = e^(ln(A) + B*ln(x))</c>.
  33. /// </summary>
  34. public float lnA;
  35. /// <summary>
  36. /// <c>B</c> constant in the power curve <c>y = e^(ln(A) + B*ln(x))</c>.
  37. /// </summary>
  38. public float B;
  39. /// <summary>
  40. /// Evaluate a point on the curve.
  41. /// </summary>
  42. /// <param name="x">The point to evaluate.</param>
  43. /// <returns>The value of the curve, at the point specified.</returns>
  44. public float Eval(float x)
  45. {
  46. float x0 = (x - offsetX) * scaleX;
  47. float y0 = 0f;
  48. // log(0) is undefined but our function should evaluate to 0. There are better ways
  49. // to handle this, but it's doing it the slow way here for clarity.
  50. if (x0 > 0)
  51. y0 = Exp(lnA + B * Log(x0));
  52. return y0 * scaleY + offsetY;
  53. }
  54. }
  55. struct DirectParams
  56. {
  57. internal float x0;
  58. internal float y0;
  59. internal float x1;
  60. internal float y1;
  61. internal float W;
  62. internal float overshootX;
  63. internal float overshootY;
  64. internal float gamma;
  65. }
  66. /// <summary>
  67. /// The white point.
  68. /// </summary>
  69. public float whitePoint { get; private set; }
  70. /// <summary>
  71. /// The inverse of the white point.
  72. /// </summary>
  73. /// <seealso cref="whitePoint"/>
  74. public float inverseWhitePoint { get; private set; }
  75. /// <summary>
  76. /// The start of the linear section (middle segment of the curve).
  77. /// </summary>
  78. public float x0 { get; private set; }
  79. /// <summary>
  80. /// The end of the linear section (middle segment of the curve).
  81. /// </summary>
  82. public float x1 { get; private set; }
  83. /// <summary>
  84. /// The three segments of the curve.
  85. /// </summary>
  86. public readonly Segment[] segments = new Segment[3];
  87. /// <summary>
  88. /// Creates a new curve.
  89. /// </summary>
  90. public HableCurve()
  91. {
  92. for (int i = 0; i < 3; i++)
  93. segments[i] = new Segment();
  94. uniforms = new Uniforms(this);
  95. }
  96. /// <summary>
  97. /// Evaluates a point on the curve.
  98. /// </summary>
  99. /// <param name="x"></param>
  100. /// <returns></returns>
  101. public float Eval(float x)
  102. {
  103. float normX = x * inverseWhitePoint;
  104. int index = (normX < x0) ? 0 : ((normX < x1) ? 1 : 2);
  105. var segment = segments[index];
  106. float ret = segment.Eval(normX);
  107. return ret;
  108. }
  109. /// <summary>
  110. /// Initializes the curve.
  111. /// </summary>
  112. /// <param name="toeStrength">The strength of the transition between the curve's toe and the curve's mid-section. A value of 0 results in no transition and a value of 1 results in a very hard transition.</param>
  113. /// <param name="toeLength">The length of the curve's toe. Higher values result in longer toes and therefore contain more of the dynamic range.</param>
  114. /// <param name="shoulderStrength">The strength of the transition between the curve's midsection and the curve's shoulder. A value of 0 results in no transition and a value of 1 results in a very hard transition.</param>
  115. /// <param name="shoulderLength">The amount of f-stops to add to the dynamic range of the curve. This is how much of the highlights that the curve takes into account.</param>
  116. /// <param name="shoulderAngle">How much overshoot to add to the curve's shoulder.</param>
  117. /// <param name="gamma">A gamma correction to the entire curve.</param>
  118. public void Init(float toeStrength, float toeLength, float shoulderStrength, float shoulderLength, float shoulderAngle, float gamma)
  119. {
  120. var dstParams = new DirectParams();
  121. // This is not actually the display gamma. It's just a UI space to avoid having to
  122. // enter small numbers for the input.
  123. const float kPerceptualGamma = 2.2f;
  124. // Constraints
  125. {
  126. toeLength = Pow(Clamp01(toeLength), kPerceptualGamma);
  127. toeStrength = Clamp01(toeStrength);
  128. shoulderAngle = Clamp01(shoulderAngle);
  129. shoulderStrength = Clamp(shoulderStrength, 1e-5f, 1f - 1e-5f);
  130. shoulderLength = Max(0f, shoulderLength);
  131. gamma = Max(1e-5f, gamma);
  132. }
  133. // Apply base params
  134. {
  135. // Toe goes from 0 to 0.5
  136. float x0 = toeLength * 0.5f;
  137. float y0 = (1f - toeStrength) * x0; // Lerp from 0 to x0
  138. float remainingY = 1f - y0;
  139. float initialW = x0 + remainingY;
  140. float y1_offset = (1f - shoulderStrength) * remainingY;
  141. float x1 = x0 + y1_offset;
  142. float y1 = y0 + y1_offset;
  143. // Filmic shoulder strength is in F stops
  144. float extraW = Pow(2f, shoulderLength) - 1f;
  145. float W = initialW + extraW;
  146. dstParams.x0 = x0;
  147. dstParams.y0 = y0;
  148. dstParams.x1 = x1;
  149. dstParams.y1 = y1;
  150. dstParams.W = W;
  151. // Bake the linear to gamma space conversion
  152. dstParams.gamma = gamma;
  153. }
  154. dstParams.overshootX = (dstParams.W * 2f) * shoulderAngle * shoulderLength;
  155. dstParams.overshootY = 0.5f * shoulderAngle * shoulderLength;
  156. InitSegments(dstParams);
  157. }
  158. void InitSegments(DirectParams srcParams)
  159. {
  160. var paramsCopy = srcParams;
  161. whitePoint = srcParams.W;
  162. inverseWhitePoint = 1f / srcParams.W;
  163. // normalize params to 1.0 range
  164. paramsCopy.W = 1f;
  165. paramsCopy.x0 /= srcParams.W;
  166. paramsCopy.x1 /= srcParams.W;
  167. paramsCopy.overshootX = srcParams.overshootX / srcParams.W;
  168. float toeM = 0f;
  169. float shoulderM = 0f;
  170. {
  171. float m, b;
  172. AsSlopeIntercept(out m, out b, paramsCopy.x0, paramsCopy.x1, paramsCopy.y0, paramsCopy.y1);
  173. float g = srcParams.gamma;
  174. // Base function of linear section plus gamma is
  175. // y = (mx+b)^g
  176. //
  177. // which we can rewrite as
  178. // y = exp(g*ln(m) + g*ln(x+b/m))
  179. //
  180. // and our evaluation function is (skipping the if parts):
  181. /*
  182. float x0 = (x - offsetX) * scaleX;
  183. y0 = exp(m_lnA + m_B*log(x0));
  184. return y0*scaleY + m_offsetY;
  185. */
  186. var midSegment = segments[1];
  187. midSegment.offsetX = -(b / m);
  188. midSegment.offsetY = 0f;
  189. midSegment.scaleX = 1f;
  190. midSegment.scaleY = 1f;
  191. midSegment.lnA = g * Log(m);
  192. midSegment.B = g;
  193. toeM = EvalDerivativeLinearGamma(m, b, g, paramsCopy.x0);
  194. shoulderM = EvalDerivativeLinearGamma(m, b, g, paramsCopy.x1);
  195. // apply gamma to endpoints
  196. paramsCopy.y0 = Max(1e-5f, Pow(paramsCopy.y0, paramsCopy.gamma));
  197. paramsCopy.y1 = Max(1e-5f, Pow(paramsCopy.y1, paramsCopy.gamma));
  198. paramsCopy.overshootY = Pow(1f + paramsCopy.overshootY, paramsCopy.gamma) - 1f;
  199. }
  200. this.x0 = paramsCopy.x0;
  201. this.x1 = paramsCopy.x1;
  202. // Toe section
  203. {
  204. var toeSegment = segments[0];
  205. toeSegment.offsetX = 0;
  206. toeSegment.offsetY = 0f;
  207. toeSegment.scaleX = 1f;
  208. toeSegment.scaleY = 1f;
  209. float lnA, B;
  210. SolveAB(out lnA, out B, paramsCopy.x0, paramsCopy.y0, toeM);
  211. toeSegment.lnA = lnA;
  212. toeSegment.B = B;
  213. }
  214. // Shoulder section
  215. {
  216. // Use the simple version that is usually too flat
  217. var shoulderSegment = segments[2];
  218. float x0 = (1f + paramsCopy.overshootX) - paramsCopy.x1;
  219. float y0 = (1f + paramsCopy.overshootY) - paramsCopy.y1;
  220. float lnA, B;
  221. SolveAB(out lnA, out B, x0, y0, shoulderM);
  222. shoulderSegment.offsetX = (1f + paramsCopy.overshootX);
  223. shoulderSegment.offsetY = (1f + paramsCopy.overshootY);
  224. shoulderSegment.scaleX = -1f;
  225. shoulderSegment.scaleY = -1f;
  226. shoulderSegment.lnA = lnA;
  227. shoulderSegment.B = B;
  228. }
  229. // Normalize so that we hit 1.0 at our white point. We wouldn't have do this if we
  230. // skipped the overshoot part.
  231. {
  232. // Evaluate shoulder at the end of the curve
  233. float scale = segments[2].Eval(1f);
  234. float invScale = 1f / scale;
  235. segments[0].offsetY *= invScale;
  236. segments[0].scaleY *= invScale;
  237. segments[1].offsetY *= invScale;
  238. segments[1].scaleY *= invScale;
  239. segments[2].offsetY *= invScale;
  240. segments[2].scaleY *= invScale;
  241. }
  242. }
  243. // Find a function of the form:
  244. // f(x) = e^(lnA + Bln(x))
  245. // where
  246. // f(0) = 0; not really a constraint
  247. // f(x0) = y0
  248. // f'(x0) = m
  249. void SolveAB(out float lnA, out float B, float x0, float y0, float m)
  250. {
  251. B = (m * x0) / y0;
  252. lnA = Log(y0) - B * Log(x0);
  253. }
  254. // Convert to y=mx+b
  255. void AsSlopeIntercept(out float m, out float b, float x0, float x1, float y0, float y1)
  256. {
  257. float dy = (y1 - y0);
  258. float dx = (x1 - x0);
  259. if (dx == 0)
  260. m = 1f;
  261. else
  262. m = dy / dx;
  263. b = y0 - x0 * m;
  264. }
  265. // f(x) = (mx+b)^g
  266. // f'(x) = gm(mx+b)^(g-1)
  267. float EvalDerivativeLinearGamma(float m, float b, float g, float x)
  268. {
  269. return g * m * Pow(m * x + b, g - 1f);
  270. }
  271. /// <summary>
  272. /// An utility class to ease the binding of curve parameters to shaders.
  273. /// </summary>
  274. public class Uniforms
  275. {
  276. HableCurve parent;
  277. internal Uniforms(HableCurve parent)
  278. {
  279. this.parent = parent;
  280. }
  281. /// <summary>
  282. /// Main curve settings, stored as <c>(inverseWhitePoint, x0, x1, 0)</c>.
  283. /// </summary>
  284. public Vector4 curve => new Vector4(parent.inverseWhitePoint, parent.x0, parent.x1, 0f);
  285. /// <summary>
  286. /// Toe segment settings, stored as <c>(offsetX, offsetY, scaleX, scaleY)</c>.
  287. /// </summary>
  288. public Vector4 toeSegmentA => new Vector4(parent.segments[0].offsetX, parent.segments[0].offsetY, parent.segments[0].scaleX, parent.segments[0].scaleY);
  289. /// <summary>
  290. /// Toe segment settings, stored as <c>(ln1, B, 0, 0)</c>.
  291. /// </summary>
  292. public Vector4 toeSegmentB => new Vector4(parent.segments[0].lnA, parent.segments[0].B, 0f, 0f);
  293. /// <summary>
  294. /// Mid segment settings, stored as <c>(offsetX, offsetY, scaleX, scaleY)</c>.
  295. /// </summary>
  296. public Vector4 midSegmentA => new Vector4(parent.segments[1].offsetX, parent.segments[1].offsetY, parent.segments[1].scaleX, parent.segments[1].scaleY);
  297. /// <summary>
  298. /// Mid segment settings, stored as <c>(ln1, B, 0, 0)</c>.
  299. /// </summary>
  300. public Vector4 midSegmentB => new Vector4(parent.segments[1].lnA, parent.segments[1].B, 0f, 0f);
  301. /// <summary>
  302. /// Shoulder segment settings, stored as <c>(offsetX, offsetY, scaleX, scaleY)</c>.
  303. /// </summary>
  304. public Vector4 shoSegmentA => new Vector4(parent.segments[2].offsetX, parent.segments[2].offsetY, parent.segments[2].scaleX, parent.segments[2].scaleY);
  305. /// <summary>
  306. /// Shoulder segment settings, stored as <c>(ln1, B, 0, 0)</c>.
  307. /// </summary>
  308. public Vector4 shoSegmentB => new Vector4(parent.segments[2].lnA, parent.segments[2].B, 0f, 0f);
  309. }
  310. /// <summary>
  311. /// An instance of the <see cref="Uniforms"/> utility class for this curve.
  312. /// </summary>
  313. public readonly Uniforms uniforms;
  314. }
  315. }