Advertisement
Guest User

Untitled

a guest
Aug 10th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.86 KB | None | 0 0
  1. float sqr(float x) { return x*x; }
  2. // Apply a gamma to a color
  3. color MaxColorGamma(color c, float gamma)
  4. {
  5.    // Reject madness
  6.    if (gamma <= 0.0 || gamma == 1.0)
  7.       return c;
  8.    // Apply a sign-safe power function
  9.    // TODO is OSL pow sign safe ?
  10.    return pow(c, gamma);
  11. }
  12. shader standard_surface(float base = .8,
  13.                         color base_color = color(1),
  14.                         float diffuse_roughness = 0,
  15.                         float specular = 1,
  16.                         color specular_color = color(1),
  17.                         float specular_roughness = .1,
  18.                         float specular_IOR = 1.52,
  19.                         float specular_anisotropy = 0,
  20.                         float specular_rotation = 0,
  21.                         float metalness = 0,
  22.                         float transmission = 0,
  23.                         color transmission_color = color(1),
  24.                         float transmission_depth = 0,
  25.                         float transmission_scatter = 0,
  26.                         float transmission_scatter_anisotropy = 0,
  27.                         float transmission_dispersion = 0,
  28.                         float transmission_extra_roughness = 0,
  29.                         int transmit_aovs = 0
  30.                             [[ string widget = "boolean"]],
  31.                         float subsurface = 0,
  32.                         color subsurface_color = color(1),
  33.                         color subsurface_radius = color(1),
  34.                         float subsurface_scale = 1,
  35.                         float subsurface_anisotropy = 0,
  36.                         int subsurface_type = 0
  37.                             [[ string widget = "mapper",
  38.                               string options = "diffusion:0|randomwalk:1" ]],
  39.                         float sheen = 0,
  40.                         color sheen_color = color(1),
  41.                         float sheen_roughness = 0.3,
  42.                         int thin_walled = 0
  43.                             [[ string widget = "boolean"]],
  44.                         normal input_normal = N,
  45.                         vector tangent = vector(0),
  46.                         float coat = 1,
  47.                         color coat_color = color(1),
  48.                         float coat_roughness = .1,
  49.                         float coat_IOR = 1.5,
  50.                         normal coat_normal = N,
  51.                         float coat_affect_color = 0,
  52.                         float coat_affect_roughness = 0,
  53.                         float thin_film_thickness = 0,
  54.                         float thin_film_IOR = 1.5,
  55.                         float emission_w = 0,
  56.                         color emission_color = color(0),
  57.                         color opacity = color(1),
  58.                         int caustics = 0
  59.                             [[ string widget = "boolean"]],
  60.                         int internal_reflections = 1
  61.                             [[ string widget = "boolean"]],
  62.                         int exit_to_background = 0
  63.                             [[ string widget = "boolean"]],
  64.                         float indirect_diffuse = 1,
  65.                         float indirect_specular = 1,
  66.                         output closure color result = 0)
  67. {
  68.    // Compute common parameters
  69.    float metalness_clamped = clamp(metalness, 0.0, 1.0);
  70.    float coat_clamped = clamp(coat, 0.0, 1.0);
  71.    // Roughness
  72.    float coat_roughness_sqr = sqr(coat_roughness);
  73.    float rx;
  74.    float ry;
  75.    vector U = vector(0);
  76.    float coat_affect = coat_clamped * coat_affect_roughness;
  77.    float specular_roughness_sqr = 1 - (1 - sqr(specular_roughness)) * (1 - coat_affect * coat_roughness);
  78.    if (specular_anisotropy != 0)
  79.    {
  80.       float aspect = sqrt(1 - clamp(specular_anisotropy, 0, 0.98));
  81.       rx = min(specular_roughness_sqr / aspect, 1);
  82.       ry = specular_roughness_sqr * aspect;
  83.       // TODO: build U
  84.    }
  85.    else
  86.    {
  87.       // TODO: build U
  88.       rx = ry = specular_roughness_sqr;
  89.    }
  90.    // Create closures
  91.    closure color closures;
  92.    // Factor to tweak color under the coat
  93.    float coat_gamma = clamp(coat, 0.0, 1.0) * coat_affect_color + 1;
  94.    //
  95.    // Diffuse reflection
  96.    //
  97.    closures += MaxColorGamma(base * base_color, coat_gamma) * oren_nayar(input_normal, diffuse_roughness);
  98.    //
  99.    // Diffuse transmission
  100.    //
  101.    closures *= 1 - clamp(subsurface, 0, 1);
  102.    if (thin_walled)
  103.    {
  104.       closures += clamp(subsurface, 0, 1) * MaxColorGamma(subsurface_color, coat_gamma) * translucent(input_normal, diffuse_roughness);
  105.    }
  106.    else
  107.    {
  108.       // Compute the final RGB mean free path
  109.       vector subsurface_mfp = vector(subsurface_scale * subsurface_radius);
  110.       if (subsurface_type == 0)
  111.          closures += clamp(subsurface, 0, 1) * empirical_bssrdf(subsurface_mfp, MaxColorGamma(subsurface_color, coat_gamma));
  112.       else
  113.          closures += clamp(subsurface, 0, 1) * randomwalk_bssrdf(subsurface_mfp, MaxColorGamma(subsurface_color, coat_gamma), subsurface_anisotropy);
  114.    }
  115.    //
  116.    // Sheen
  117.    //
  118.    // TODO : add Sheen closure
  119.    //
  120.    // Specular transmission
  121.    //
  122.    float transmission_IOR = (thin_walled) ? 1 : max(specular_IOR, 1.e-04);
  123.    // TODO : handle dispersion
  124.    // TODO : handle absorption and interior closures
  125.    closures *= 1 - clamp(transmission, 0, 1);
  126.    // TODO : handle specular transmission thin film
  127.    closures += clamp(transmission, 0, 1) * transmission_color * microfacet("ggx", input_normal, U,
  128.                                                               min(rx + sqr(transmission_extra_roughness), 1),
  129.                                                               min(ry + sqr(transmission_extra_roughness), 1),
  130.                                                               transmission_IOR, 1);
  131.    //
  132.    // Specular reflection
  133.    //
  134.    float Kr, Kt;
  135.    fresnel(I, input_normal, 1 / specular_IOR, Kr, Kt);
  136.    closures *= 1 - specular * specular_color * Kr;
  137.    // TODO : handle handle specular reflection thin film
  138.    closures += specular * specular_color  * microfacet("ggx", input_normal, U, rx, ry, specular_IOR, 0);
  139.    //
  140.    // Metal
  141.    //
  142.    closures *= 1 - metalness;
  143.    // TODO : handle metal closure with complex fresnel, and thin film
  144.    closures += metalness * base_color * microfacet("ggx", input_normal, U, rx, ry, 0, 0);
  145.    //
  146.    // Emission
  147.    //
  148.    closures += emission_w * emission_color * emission();
  149.    //
  150.    // Coat
  151.    //
  152.    // mix the coating bsdf on top of the rest based on average fresnel, tinting all
  153.    // the BSDFs so far with the coat color
  154.    float coat_Kr, coat_Kt;
  155.    fresnel(I, coat_normal, 1 / coat_IOR, coat_Kr, coat_Kt);
  156.    closures *= mix(coat, color(1), clamp(1 - Kr, 0, 1) * coat_color);
  157.    closures += coat * microfacet("ggx", coat_normal, U, coat_roughness, coat_roughness, coat_IOR, 0);
  158.    //
  159.    // Opacity
  160.    //
  161.    closures *= opacity;
  162.    closures += (color(1) - opacity) * transparent();
  163.    result += closures;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement