EnvironmentGradient - 图1

    1. // environment_gradient - Various ways to drive an environment gradient ramp
    2. // Michael Abrahams, 3/23/2021
    3. // MIT license
    4. shader env_gradient
    5. [[ string help = "Spherical Gradient for Redshift Environment Mapping",
    6. string label = "Environment Gradient" ]]
    7. (
    8. int gradient_type = 0
    9. [[ string label= "Gradient type",
    10. string widget = "mapper",
    11. string options = "World (Linear):0|World (Polar Angle):1|Camera Space:2" ]],
    12. int out_dimension = 0
    13. [[ string label= "Output coordinate",
    14. string widget = "mapper",
    15. string options = "V:0|U:1" ]],
    16. int zUpAxis = 0 [[ string widget = "checkBox", string label = "Z is Up" ]],
    17. output float out = 0.0,
    18. output vector outUV = 0.0
    19. )
    20. {
    21. float outU;
    22. float outV;
    23. float incline;
    24. float azimuth;
    25. vector shadingI = I;
    26. if (zUpAxis) {
    27. shadingI = vector(I.x,I.z,I.y);
    28. }
    29. // World (linear/cylindrical)
    30. if (gradient_type == 0) {
    31. outUV.y = shadingI.y / 2.0 + 0.5;
    32. azimuth = atan(shadingI.z / shadingI.x);
    33. outUV.x = azimuth / M_PI + 0.5;
    34. }
    35. // World (Polar angle)
    36. if (gradient_type == 1) {
    37. incline = acos(shadingI.y);
    38. outUV.y = 1.0 - incline / M_PI;
    39. azimuth = atan(shadingI.z / shadingI.x);
    40. outUV.x = azimuth / M_PI + 0.5;
    41. }
    42. // Screen space
    43. if (gradient_type == 2) {
    44. vector shadingP = transform("NDC", P);
    45. outUV.x = shadingP.x;
    46. outUV.y = shadingP.y;
    47. }
    48. if (out_dimension == 0) {
    49. out = outUV.y;
    50. }
    51. else {
    52. out = outUV.x;
    53. }
    54. }