UberColorCorrect - 图1

    1. // Color Correction
    2. // ColorCorrect.osl, by Zap Andersson
    3. // Modified: 2021-12-20 by Saul Espinosa for Redshift 3D
    4. // Removed Max UI specific options and added Page grouping Metadata
    5. // Copyright 2020 Autodesk Inc, All rights reserved. This file is licensed under Apache 2.0 license
    6. // https://github.com/ADN-DevTech/3dsMax-OSL-Shaders/blob/master/LICENSE.txt
    7. float remap(color In, int mapping)
    8. {
    9. if (mapping < 3)
    10. return In[mapping];
    11. if (mapping < 6)
    12. return 1.0 - In[mapping-3];
    13. if (mapping == 6) return luminance(In);
    14. if (mapping == 7) return 1.0 - luminance(In);
    15. if (mapping == 8) return 0.0;
    16. return 1.0;
    17. }
    18. shader UberColorCorrect
    19. [[ string label = "Color Correction",
    20. // string category = "Color Correction",
    21. string version = "1.5" ]]
    22. (
    23. color Input = 0.18 [[string page = "Input"]],
    24. int Bypass = 0 [[ string widget = "checkBox", string page = "Input" ]],
    25. float Overall = 1.0 [[ float min = 0.0, float max = 1.0, string page = "Input" ]],
    26. int HueRange = 0 [[ string widget = "checkBox", string page = "Hue Ranges" ]],
    27. color AffectHue = color(1.0, 0.4, 0.3) [[string page = "Hue Ranges"]],
    28. float HueRangeWidth = 10 [[ float min = 0.0, float max = 360, float step = 1.0, string page = "Hue Ranges" ]],
    29. float HueRangeSoftness = 10 [[ float min = 0.0, float max = 360, float step = 1.0, string page = "Hue Ranges" ]],
    30. int ChannelMapping = 0 [[ string widget = "checkBox", string page = "Channels" ]],
    31. int RedMapping = 0
    32. [[
    33. int connectable = 0,
    34. string page = "Channels",
    35. string widget = "mapper",
    36. string options =
    37. "Red:0|Green:1|Blue:2|"
    38. "Red (Inverse):3|Green (Inverse):4|Blue (Inverse):5|"
    39. "Luminance:6|Luminance (Inverse):7|Zero:8|One:9" ]],
    40. int GreenMapping = 1
    41. [[ int connectable = 0,
    42. string page = "Channels",
    43. string widget = "mapper",
    44. string options =
    45. "Red:0|Green:1|Blue:2|"
    46. "Red (Inverse):3|Green (Inverse):4|Blue (Inverse):5|"
    47. "Luminance:6|Luminance (Inverse):7|Zero:8|One:9" ]],
    48. int BlueMapping = 2
    49. [[ int connectable = 0,
    50. string page = "Channels",
    51. string widget = "mapper",
    52. string options =
    53. "Red:0|Green:1|Blue:2|"
    54. "Red (Inverse):3|Green (Inverse):4|Blue (Inverse):5|"
    55. "Luminance:6|Luminance (Inverse):7|Zero:8|One:9" ]],
    56. int BrightnessAdjustment = 1 [[ string widget = "checkBox", string page = "Value Adjustments" ]],
    57. int Invert = 0 [[ string widget = "checkBox", int connectable=0, string page = "Value Adjustments" ]],
    58. float Brightness = 1.0 [[string page = "Value Adjustments"]],
    59. float Contrast = 1.0 [[string page = "Value Adjustments"]],
    60. float Lift = 0.0 [[string page = "Value Adjustments"]],
    61. float Pivot = 0.5 [[string page = "Value Adjustments"]],
    62. int InverseGamma = 0 [[ string widget = "checkBox", int connectable=0, string page = "Value Adjustments" ]],
    63. float Gamma = 1.0 [[string page = "Value Adjustments"]],
    64. int ColorAdjustment = 0 [[ string widget = "checkBox", int connectable=0, string page = "Color Adjustments" ]],
    65. float HueShift = 0.0 [[ float min = -1.0, float max = 1.0, string page = "Color Adjustments" ]],
    66. float Saturation = 1.0 [[ float min = 0.0, float max = 10.0, string page = "Color Adjustments" ]],
    67. color TintColor = 1.0 [[ string page = "Color Adjustments"]],
    68. float TintStrength = 0.0 [[ float min = 0.0, float max = 1.0, string page = "Color Adjustments" ]],
    69. int OutputTint = 0 [[ string widget = "checkBox", int connectable=0, string page = "Output Adjustments" ]],
    70. color Shadows = 0.0 [[ string page = "Output Adjustments"]],
    71. color Midtones = 0.2 [[ string page = "Output Adjustments"]],
    72. color Highlights = 1.0 [[ string page = "Output Adjustments"]],
    73. float MidtoneLum = 0.2 [[ float min = 0.0, float max = 1.0, string page = "Output Adjustments" ]],
    74. int Clamp = 1 [[ string widget = "checkBox", int connectable=0, string page = "Output Clamp" ]],
    75. float ClampLow = 0.0 [[string page = "Output Clamp"]],
    76. float ClampHi = 1.0 [[string page = "Output Clamp"]],
    77. // Outputs
    78. output color Out = 0.0,
    79. output float R = 0.0,
    80. output float G = 0.0,
    81. output float B = 0.0
    82. )
    83. {
    84. float effect = Overall;
    85. Out = Input;
    86. if (!Bypass)
    87. {
    88. // Apply only to hue range
    89. if (HueRange)
    90. {
    91. // Tramsform to HSV space
    92. color hsv = transformc("rgb", "hsv", Input);
    93. // Tramsform to HSV space
    94. color hsv2 = transformc("rgb", "hsv", AffectHue);
    95. float huedifference = fabs(hsv[0] - hsv2[0]);
    96. // If distance in hue is larger than half
    97. // the color wheel, measure the other way around
    98. if (huedifference > 0.5)
    99. huedifference = 1.0 - huedifference;
    100. float HRW = HueRangeWidth / 360.0 / 2.0;
    101. float HRS = HueRangeSoftness / 360.0;
    102. // Are we outside the hue range? Then we need to modulate
    103. // the effect value to apply the effect less (or not at all)
    104. if (huedifference > HRW)
    105. {
    106. // Outside range - apply softness (if any)
    107. if (HRS > 0.0)
    108. effect *= smoothstep(0.0, 1.0, 1.0 - (huedifference - HRW) / HRS);
    109. else
    110. effect = 0.0; // No softness - just hard cut effect to zero
    111. }
    112. }
    113. // Apply the channel mapping
    114. if (ChannelMapping)
    115. {
    116. Out[0] = remap(Input, RedMapping);
    117. Out[1] = remap(Input, GreenMapping);
    118. Out[2] = remap(Input, BlueMapping);
    119. }
    120. // Brightness adjustment section
    121. if (BrightnessAdjustment)
    122. {
    123. // Invert, if requested
    124. if (Invert) Out = 1.0 - Out;
    125. // Multiply by Brightness
    126. Out = Out * Brightness;
    127. // Scale around Pivot by Contrast
    128. Out = ((Out - Pivot) * Contrast) + Pivot;
    129. // Add Lift
    130. Out = Out + Lift;
    131. // Apply Gamma
    132. if (Gamma != 1.0)
    133. Out = pow(max(0.0, Out), InverseGamma?1.0/Gamma:Gamma);
    134. }
    135. // Color adjustment section
    136. if (ColorAdjustment)
    137. {
    138. // Tramsform to HSV space
    139. color hsv = transformc("rgb", "hsv", Out);
    140. hsv[0] += HueShift;
    141. hsv[1] *= Saturation;
    142. // Convert back to RGB
    143. Out = transformc("hsv", "rgb", hsv);
    144. // Mix with tint color scaled by value
    145. Out = mix(Out, TintColor * hsv[2], TintStrength);
    146. }
    147. // Output Tint Section
    148. if (OutputTint)
    149. {
    150. color below = clamp((Out / MidtoneLum), 0.0, 1.0);
    151. color above = clamp((Out - MidtoneLum) / (1.0 - MidtoneLum), 0.0, 1.0);
    152. Out = mix(Shadows, mix(Midtones, Highlights, above), below);
    153. }
    154. if (Clamp)
    155. Out = clamp(Out, ClampLow, ClampHi);
    156. // Do the overall mix in of the effect
    157. Out = mix(Input, Out, effect);
    158. }
    159. // Output individual channels for convenience
    160. R = Out[0];
    161. G = Out[1];
    162. B = Out[2];
    163. }