HDRIEnviron - 图1

    1. // HDRI Loader
    2. // HDRIEnviron2.osl, by Zap Andersson
    3. // Modified: 2020-01-27
    4. // Modified: 2021-03-17 by Saul Espinosa for Redshift 3d
    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. shader HDRIenv
    8. [[ string help =
    9. "<h3>HDRI Environment</h3> Environment shader with exposure "
    10. "adjustments,<br> ground projection mode, and separation between<br> "
    11. "<b>Background</b> and <b>Environment</b>",
    12. string version = "1.0.0",
    13. string category = "Environment",
    14. string label = "HDRI Environment" ]]
    15. (
    16. string HDRI=""
    17. [[ string widget="filename",
    18. string label ="HDRI",
    19. string page = "1 : Texture"
    20. ]],
    21. int Flip = 0
    22. [[ string widget = "checkBox",
    23. int connectable = 0,
    24. string page = "1 : Texture"]],
    25. int zup = 0
    26. [[ string widget = "checkBox",
    27. string label = "Z-Up",
    28. string page = "1 : Texture",
    29. int connectable = 0 ]],
    30. float Rotation = 0
    31. [[ float min = -360.0, float max = 360.0,
    32. int connectable=0, float step = 1.0,
    33. string page = "2 : Position"
    34. ]],
    35. float Height = 0
    36. [[ float min = -1.0, float max = 1.0,
    37. int connectable=0, float step = 0.001,
    38. string page = "2 : Position"
    39. ]],
    40. float TiltX = 0 + 0
    41. [[ float min = -360.0, float max = 360.0,
    42. int connectable=0, float step = 0.1,
    43. string page = "2 : Position"
    44. ]],
    45. float TiltY = 0
    46. [[ float min = -360.0, float max = 360.0,
    47. int connectable=0, float step = 0.1,
    48. string page = "2 : Position"
    49. ]],
    50. float Exposure = 0
    51. [[ float min = -10, float max = 10,
    52. string page = "3 : Adjustments",
    53. int connectable=0
    54. ]],
    55. float Gamma = 1
    56. [[ float min = -3, float max = 3,
    57. string page = "3 : Adjustments",
    58. int connectable=0
    59. ]],
    60. color Tint = 1.0
    61. [[
    62. string page = "3 : Adjustments",
    63. int connectable=0
    64. ]],
    65. color AdditionalLight = 0.0
    66. [[ string label = "Additional Light",
    67. string page = "4 : Extra Light",
    68. ]],
    69. float AdditionalLightMul = 1.0
    70. [[ string label = "Additional Light Multiplier", int connectable=0,
    71. string page = "4 : Extra Light",
    72. ]],
    73. int GroundProjection=0
    74. [[ string widget="checkBox",
    75. string page = "5 : Ground Projection",
    76. int connectable=0
    77. ]],
    78. point GroundCenter=0
    79. [[ int connectable=0 ,
    80. string page = "5 : Ground Projection"
    81. ]],
    82. float GroundRadius=1.5 // Default is in meters
    83. [[ int connectable=0, int worldunits=1,
    84. string page = "5 : Ground Projection"
    85. ]],
    86. int UseBackground= 0
    87. [[ string widget="checkBox",
    88. string page = "6 : Back-Plate",
    89. string label = "Use Background",
    90. int connectable = 0
    91. ]],
    92. color Background = 0.0
    93. [[string page = "6 : Back-Plate"]],
    94. float BackgroundMultiplier = 1.0
    95. [[ string label = "Background Multiplier",
    96. string page = "6 : Back-Plate",
    97. int connectable=0 ]],
    98. int Blur = 0
    99. [[ string widget = "checkBox",
    100. string page = "7 : Blur",
    101. int connectable = 0 ]],
    102. float BlurAmount = 1.0
    103. [[ float min = 0.0, float max = 25.0, int connectable=0,
    104. string page = "7 : Blur"
    105. ]],
    106. int BlurSamples = 16
    107. [[ int min = 1, int max = 256, int connectable=0,
    108. string page = "7 : Blur",
    109. ]],
    110. int aces = 0
    111. [[ string widget = "checkBox",
    112. string page = "8 : Extra",
    113. string label = "ACES",
    114. int connectable = 0 ]],
    115. int Clamp = 1
    116. [[ string widget = "checkBox",
    117. string page = "8 : Extra",
    118. int connectable = 0 ]],
    119. float ClampStops = 10.0
    120. [[ float min = -10.0, float max = 30.0, float step = 0.1, int connectable=0,
    121. string page = "8 : Extra",
    122. ]],
    123. // Output
    124. output color Out = 0
    125. )
    126. {
    127. matrix rs_transform =
    128. { 1, 0, 0, 0,
    129. 0, 1*zup,1-zup, 0,
    130. 0, 1-zup, 1*zup, 0,
    131. 0, 0, 0, 1 };
    132. if (UseBackground && raytype("camera"))
    133. {
    134. Out = Background * BackgroundMultiplier;
    135. return;
    136. }
    137. float U = 0.0, V = 0.0;
    138. // Change Direction World space Y up to Z up
    139. vector Direction = transform(rs_transform, I);
    140. vector OrgDir = Direction;
    141. point CP = transform("camera", point(0,0,0));
    142. // Change position world space Y up to Z up
    143. point Position = transform(rs_transform, P);
    144. // Workaround for Arnolds odd P behaviour in environments....
    145. if (Position == 0.0)
    146. Position = transform("camera", "world", point(0));
    147. int doBlur = Blur && BlurAmount > 0.0?raytype("camera"):0;
    148. int samples = doBlur?BlurSamples:1;
    149. for (int i = 0; i < samples; i++)
    150. {
    151. if (doBlur)
    152. Direction = OrgDir + (noise("hash", I, i) - 0.5) * BlurAmount / 100.0;
    153. // Ground Projection mode is on, and direction is pointing down?
    154. if (GroundProjection == 1 && Direction[2] < 0.0)
    155. {
    156. // Compute intersection with virtual ground plane
    157. float t = (GroundCenter[2] - Position[2])/Direction[2];
    158. point GP = Position + Direction * t;
    159. // Assume we are doing the ground
    160. int doGround = 1;
    161. if (doGround)
    162. {
    163. // Compute virtual projection point rays are projecting from
    164. point TP = GroundCenter + vector(0, 0, GroundRadius);
    165. // Use direction from that point to the groundplane as the
    166. // new virtual direction
    167. Direction = normalize(GP-TP);
    168. // Smoothen out the joint a bit....
    169. // Thanks to Vlado for suggestion!
    170. if (Direction[2] > -0.1)
    171. {
    172. float fac = 1.0 - Direction[2] * -10.0;
    173. fac *= fac;
    174. Direction = mix(Direction, OrgDir, fac);
    175. }
    176. }
    177. }
    178. if (TiltX != 0.0)
    179. Direction = rotate(Direction, radians(TiltX), vector(0.0), vector(1,0,0));
    180. if (TiltY != 0.0)
    181. Direction = rotate(Direction, radians(TiltY), vector(0.0), vector(0,1,0));
    182. // Compute texture UV's in a spherical environment map...
    183. U = (Rotation / 360.0) + atan2(Direction[0],Direction[1]) / (M_PI * 2.0);
    184. V = 0.5 + (asin(Direction[2]) / M_PI);
    185. // Adjust the height by sin(z)
    186. V -= Height * sqrt((1.0 - (Direction[2]*Direction[2])));
    187. // Look up the texture
    188. Out += texture(HDRI, Flip?-U:U, 1.0-V, "wrap", "periodic") * Tint;
    189. }
    190. Out /= samples;
    191. if (Clamp)
    192. Out = clamp(Out, 0.0, pow(2.0, ClampStops));
    193. else
    194. {
    195. // Just avoid negative numbers
    196. if (Out[0] < 0.0) Out[0] = 0.0;
    197. if (Out[1] < 0.0) Out[1] = 0.0;
    198. if (Out[2] < 0.0) Out[2] = 0.0;
    199. }
    200. // Gamma 2.2 for sRGB approx)
    201. float gammapower = Gamma;
    202. // Apply overall Exposure and Contrast
    203. Out = pow(Out, gammapower) * pow(2.0, Exposure) + (AdditionalLight * AdditionalLightMul);
    204. // ACES sRGB Transform
    205. matrix aces_tm = matrix(
    206. 0.6131, 0.0701, 0.0206, 0,
    207. 0.3395, 0.9164, 0.1096, 0,
    208. 0.0474, 0.0135, 0.8698, 0,
    209. 0, 0, 0, 1);
    210. float r = Out[0], g = Out[1], b = Out[2];
    211. // Add Additional Light or ACES Output
    212. if (aces == 0)
    213. Out = pow(Out, gammapower) * pow(2.0, Exposure) + (AdditionalLight * AdditionalLightMul);
    214. else
    215. {
    216. Out = transform(aces_tm, vector(r,g,b));
    217. }
    218. }