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