HableCurve.cs 13 KB

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