JiWindowBox_Examples.zipJiWindowBox - 图1

    1. // jiWindowBox - creates a fake "room" from a single plane
    2. // jiWindowBox.osl, by Julius Ihle
    3. // windowAspect wall distortion fix by David McDonnell
    4. // UI overhaul to Qt, filenmae widget & Color grading integrated by Mads Drøschler
    5. // External Grading function from the OSL CC base node
    6. // Mads Drøschler
    7. // Modified 4/15/21 by Saul Espinosa for Redshift 3d Support
    8. // Added ACES output, Gamma, and new page/min/max metadata
    9. color Grader (color Input, color InputMin, color InputMax, vector HSV, color OutputMax, color OutputMin, float MidTones, int Clamp)
    10. {
    11. color Out;
    12. color col = (Input - InputMin) / InputMax;
    13. color hsv = transformc("rgb", "hsv", col);
    14. hsv[0] += HSV[0];
    15. hsv[1] *= HSV[1];
    16. hsv[2] *= HSV[2];
    17. col = transformc("hsv", "rgb", hsv);
    18. Out = mix(OutputMin, OutputMax, pow(col, 1.0/MidTones));
    19. if (Clamp) Out = clamp(Out, 0.0, 1.0);
    20. return Out;
    21. }
    22. // UI overhaul port to QT for 3dsmax custom User interface
    23. // Mads Drøschler
    24. shader jiWindowBox_Redshift
    25. [[ string help = "Window Box Projection Shader",
    26. string label = "Window Box" ]]
    27. (
    28. // Filename node is not needed with the string widget "filename" in max with this addition.
    29. // User can load the texture directly into the shader.
    30. // Mads Drøschler
    31. string input_tex = ""
    32. [[
    33. string widget = "filename",
    34. int connectable = 0,
    35. string page = "0 : Texture",
    36. ]],
    37. int zUpAxis = 0
    38. [[
    39. string widget = "checkBox",
    40. int connectable = 0,
    41. string page = "0 : Texture",
    42. ]],
    43. int textureFlip = 0
    44. [[
    45. string widget = "checkBox",
    46. int connectable = 0,
    47. string page = "0 : Texture",
    48. ]],
    49. int textureFlop = 0
    50. [[
    51. string widget = "checkBox",
    52. int connectable = 0,
    53. string page = "0 : Texture",
    54. ]],
    55. float Hue = 0
    56. [[
    57. int connectable = 0,
    58. string page = "1 : Color Correct",
    59. float min = 0,
    60. float max = 1
    61. ]],
    62. float Saturation = 1
    63. [[
    64. int connectable = 0,
    65. string page = "1 : Color Correct",
    66. float min = 0,
    67. float max = 1,
    68. ]],
    69. float Value = 1
    70. [[
    71. int connectable = 0,
    72. string page = "1 : Color Correct",
    73. float min = 0,
    74. float max = 100
    75. ]],
    76. float ManualGamma = 2.2
    77. [[
    78. string label="Gamma",
    79. string page = "1 : Color Correct",
    80. float min = 0,
    81. float max = 5
    82. ]],
    83. color InputMin = 0.0
    84. [[
    85. string help = "The input value that maps to the darkest output. Incoming black level. ",
    86. int connectable = 0,
    87. string page = "2 : Color Clamp",
    88. ]],
    89. color InputMax = 1.0
    90. [[
    91. string help = "The input value that maps to the brightst output. Incoming white level. ",
    92. int connectable = 0,
    93. string page = "2 : Color Clamp",
    94. ]],
    95. float MidTones = 1.0
    96. [[
    97. string help = "Adjusts the MidTones/Gamma.",float min = 0.01, float max = 9.99,
    98. int connectable = 0,
    99. string page = "2 : Color Clamp",
    100. ]],
    101. color OutputMin = 0.0
    102. [[
    103. string help = "The output black level.",
    104. int connectable = 0,
    105. string page = "2 : Color Clamp",
    106. ]],
    107. color OutputMax = 1.0
    108. [[
    109. string help = "The output white level.",
    110. int connectable = 0,
    111. string page = "2 : Color Clamp",
    112. ]],
    113. float windowAspect = 1
    114. [[
    115. int connectable = 0,
    116. string page = "3 : Window Shape",
    117. float min = 0,
    118. float max = 10
    119. ]],
    120. float roomDepth = 1
    121. [[
    122. float min = 0.1,
    123. float max = 100,
    124. int connectable = 0,
    125. string page = "3 : Window Shape",
    126. ]],
    127. float widthOverscan = 0
    128. [[
    129. float min = 0.0,
    130. float max = 0.9,
    131. int connectable = 0,
    132. string page = "3 : Window Shape",
    133. ]],
    134. float heightOverscan = 0
    135. [[
    136. float min = 0.0,
    137. float max = 0.9,
    138. int connectable = 0,
    139. string page = "3 : Window Shape",
    140. ]],
    141. int enableMidground = 0
    142. [[
    143. string widget = "checkBox",
    144. int connectable = 0,
    145. string page = "4 : Midground",
    146. ]],
    147. float midgroundDepth = 0.5
    148. [[
    149. float min = 0.1,
    150. float max = 100,
    151. int connectable = 0,
    152. string page = "4 : Midground"
    153. ]],
    154. float midgroundOffsetX = 0
    155. [[
    156. int connectable = 0,
    157. float min = -10.0,
    158. float max = 10.0,
    159. string page = "4 : Midground"
    160. ]],
    161. float midgroundOffsetY = 0
    162. [[
    163. int connectable = 0,
    164. float min = -10.0,
    165. float max = 10.0,
    166. string page = "4 : Midground"
    167. ]],
    168. int enableCurtains = 0
    169. [[
    170. string widget = "boolean",
    171. string page = "5 : Foreground",
    172. int connectable = 0,
    173. ]],
    174. int Clamp = 1
    175. [[
    176. string widget = "checkBox",
    177. string help = "Clamp the output to the 0-1 range for each component",
    178. string page = "6 : Extra",
    179. int connectable = 0,
    180. ]],
    181. int aces = 0
    182. [[ string widget = "checkBox",
    183. string page = "6 : Extra",
    184. string label = "ACES",
    185. int connectable = 0
    186. ]],
    187. output color outColor = 0.0
    188. )
    189. {
    190. // additional inits Mads Drøschler
    191. vector HSV = vector(Hue,Saturation,Value);
    192. //user controls remapping
    193. float roomDepthMult = clamp(roomDepth,0.1,100);
    194. float heightOverscanMult = 1 - clamp(heightOverscan,0,0.9);
    195. float widthOverscanMult = 1 - clamp(widthOverscan,0,0.9);
    196. float midgroundDepthMult = clamp(midgroundDepth,0.05,roomDepthMult-0.01);
    197. float midgroundOffY = midgroundOffsetY * (textureFlip*2-1) * 0.1;
    198. float midgroundOffX = midgroundOffsetX * (textureFlop*2-1) * 0.1;
    199. //global variables & remapping
    200. vector objI = transform("object", -I);
    201. if (zUpAxis > 0){
    202. objI = vector(-objI[0],-objI[1],-objI[2]) * color(widthOverscanMult*(1/windowAspect), heightOverscanMult, 1); //reorder to match UV for Y up axis
    203. } else {
    204. objI = vector(-objI[0],-objI[1],-objI[2]) * color(widthOverscanMult*(1/windowAspect), heightOverscanMult, 1); //reorder to match UV for Z up axis
    205. }
    206. color objPOrig = (color(u,v,0.5) * 2 - 1) * 0.5 + 0.5; //for curtains
    207. color objP = (color(u,v,0.5) * 2 - 1) * color(widthOverscanMult, heightOverscanMult, 1) * 0.5 + 0.5; //UV seems to be the better approach
    208. //bases for width/height/depth
    209. vector sections = step(0, objI);
    210. color baseDepth = (objP-sections)/(-objI * roomDepthMult);
    211. color mgDepth = (objP-sections)/(-objI * midgroundDepthMult);
    212. color baseBack = (objP-sections)/(-objI);
    213. color baseWidth = baseDepth * roomDepthMult;
    214. //depth and width ramps
    215. color baseDepthX = (baseDepth[1]*objI+objP + 1);
    216. color baseDepthY = (baseDepth[0]*objI+objP + 1);
    217. color baseWidthX = (baseWidth[1]*objI+objP + 1);
    218. color baseWidthY = (baseWidth[0]*objI+objP + 1);
    219. float horizU = baseDepthY[2] - 0.5;
    220. float vertU = baseWidthX[0] - 1;
    221. float horizV = baseWidthY[1] - 1;
    222. float vertV = baseDepthX[2] - 0.5;
    223. //convert ramps to UV/ST... WIP - not very efficient
    224. float sideWallsMask = step(0,horizU) * step(0,1-max(horizV, 1-horizV));
    225. color sideWallsUV = color(horizU, horizV, 0) / 3;
    226. color rWallUV = (sideWallsUV + color(2.0/3.0, 1.0/3.0, 0)) * sideWallsMask * sections[0];
    227. color lWallUV = (sideWallsUV + color(0.0, 1.0/3.0, 0)) * sideWallsMask * (1-sections[0]);
    228. lWallUV[0] = (1.0/3.0 - lWallUV[0]) * sideWallsMask * (1-sections[0]);
    229. float FloorCeilMask = step(0,vertV) * step(0,1-max(vertU, 1-vertU));
    230. color FloorCeilUV = color(vertU, vertV, 0) / 3;
    231. color ceilUV = (FloorCeilUV + color(1.0/3.0, 2.0/3.0, 0)) * FloorCeilMask * sections[1];
    232. color floorUV = (FloorCeilUV + color(1.0/3.0, 0, 0)) * FloorCeilMask * (1-sections[1]);
    233. floorUV[1] = (1.0/3.0 - floorUV[1]) * FloorCeilMask * (1-sections[1]);
    234. color backWallUV = ((baseBack[2]*objI + (objP/2)/(roomDepthMult)) * (roomDepthMult*2) / 3 + color(1.0/3.0, 1.0/3.0, 0) ) * (1 - max(step(0,horizU), step(0,vertV)));
    235. color midgroundUV = (1.0/3.0 - (baseBack[2]*objI + (objP)/(midgroundDepthMult*2)) * (midgroundDepthMult*2) / 3);
    236. float midgroundMask = step( 0, midgroundUV[1] * 3 * (1-midgroundUV[1]*3) ) * step( 0, midgroundUV[0] * (1.0/3.0-midgroundUV[0]) );
    237. midgroundUV = (color(midgroundOffX, midgroundOffY, 0) + midgroundUV);
    238. midgroundUV[1] = (1-midgroundUV[1]) - 2.0/3.0;
    239. color curtainsUV = objPOrig * color(1.0/3.0, 1.0/3.0, 1);
    240. curtainsUV[0] = 1.0/3.0 - curtainsUV[0];
    241. curtainsUV[1] = 2.0/3.0 + curtainsUV[1]; //VRay specific
    242. color finalUV = ceilUV + floorUV + rWallUV + lWallUV + backWallUV;
    243. //flipping ctrl
    244. if (textureFlop < 1){ //VRay specific
    245. midgroundUV[0] = 1.0/3.0 - midgroundUV[0];
    246. curtainsUV[0] = 1.0/3.0 - curtainsUV[0];
    247. }else
    248. finalUV[0] = 1-finalUV[0];
    249. if (textureFlip > 0){
    250. finalUV[1] = 1-finalUV[1];
    251. midgroundUV[1] = 1.0/3.0 - midgroundUV[1]; // VRay specific
    252. curtainsUV[1] = 1 - curtainsUV[1] + 2.0/3.0; // VRay specific
    253. }
    254. color roomRGB = texture(input_tex, finalUV[0], finalUV[1], "width" , 0);
    255. color finalRGB;
    256. //midground switch
    257. if (enableMidground > 0){
    258. float midgroundA;
    259. color midgroundRGB = texture(input_tex, midgroundUV[0], midgroundUV[1], "alpha", midgroundA, "width" , 0);
    260. finalRGB = mix(roomRGB,midgroundRGB,midgroundA * midgroundMask); //VRay specific
    261. }
    262. else{
    263. finalRGB = roomRGB;
    264. }
    265. //curtains switch
    266. if (enableCurtains > 0){
    267. float curtainsA;
    268. color curtainsRGB = texture(input_tex, curtainsUV[0], curtainsUV[1], "alpha", curtainsA, "width" , 0);
    269. finalRGB = mix(finalRGB,curtainsRGB,curtainsA);
    270. }
    271. // ACES sRGB Transform
    272. matrix aces_tm = matrix(
    273. 0.6131, 0.0701, 0.0206, 0,
    274. 0.3395, 0.9164, 0.1096, 0,
    275. 0.0474, 0.0135, 0.8698, 0,
    276. 0, 0, 0, 1);
    277. // Fetch the color grade function
    278. // Mads Drøschler
    279. finalRGB = Grader(finalRGB,InputMin,InputMax,HSV,OutputMax,OutputMin,MidTones,Clamp);
    280. color result = finalRGB;
    281. // Gamma & ACES
    282. {
    283. if (ManualGamma != 1.0)
    284. result = pow(finalRGB, ManualGamma);
    285. }
    286. float r = result[0], g = result[1], b = result[2];
    287. // ACES Output
    288. if (aces == 0)
    289. outColor = result;
    290. else
    291. {
    292. outColor = transform(aces_tm, vector(r,g,b));
    293. }
    294. }