Skip to content

Instantly share code, notes, and snippets.

@WhileRomeBurns
Created May 29, 2015 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WhileRomeBurns/38edb7ac5476f52ae3fd to your computer and use it in GitHub Desktop.
Save WhileRomeBurns/38edb7ac5476f52ae3fd to your computer and use it in GitHub Desktop.
////////////////////////////////////////////////////////////////
// UI Parameters
////////////////////////////////////////////////////////////////
#pragma label diff_cd "Diffuse Color"
#pragma hint diff_cd color
#pragma label diff_int "Diffuse Intensity"
#pragma range diff_int 0! 1!
#pragma label diff_rough "Diffuse Roughness"
#pragma range diff_rough 0! 1!
#pragma label diff_tex_p "Diffuse Texture Path"
#pragma hint diff_tex_p image
#pragma group "Diffuse" diff_cd diff_int diff_rough diff_tex_p
#pragma label refl_b_cd "Reflect Base Color"
#pragma hint refl_b_cd color
#pragma label refl_b_int "Reflect Base Intensity"
#pragma range refl_b_int 0! 1!
#pragma label refl_b_rough "Reflect Base Roughness"
#pragma range refl_b_rough 0! 1!
#pragma label refl_b_tex_p "Reflect Base Texture Path"
#pragma hint refl_b_tex_p image
#pragma group "Refl Base" refl_b_cd refl_b_int refl_b_rough refl_b_tex_p
#pragma label refl_c_cd "Reflect Coat Color"
#pragma hint refl_c_cd color
#pragma label refl_c_int "Reflect Coat Intensity"
#pragma range refl_c_int 0! 1!
#pragma label refl_c_rough "Reflect Coat Roughness"
#pragma range refl_c_rough 0! 1!
#pragma label refl_c_tex_p "Reflect Coat Texture Path"
#pragma hint refl_c_tex_p image
#pragma group "Refl Coat" refl_c_cd refl_c_int refl_c_rough refl_c_tex_p
#pragma label emit_cd "Emission Color"
#pragma hint emit_cd color
#pragma label emit_int "Emission Intensity"
#pragma range emit_int 0 10
#pragma group "Emission" emit_cd emit_int
#pragma label opac_cd "Opacity Color"
#pragma hint opac_cd color
#pragma label opac_int "Opacity Intensity"
#pragma range opac_int 0! 1!
#pragma group "Opacity" opac_cd opac_int
////////////////////////////////////////////////////////////////
// Attibutes In
////////////////////////////////////////////////////////////////
#pragma hint uv hidden
////////////////////////////////////////////////////////////////
// Attibutes Out
////////////////////////////////////////////////////////////////
#pragma hint Ce hidden
#define DEFAULT_MAP "Mandril.pic"
surface
vih_mantra_material_c2(vector diff_cd = {0.2, 0.2, 0.2};
float diff_int = 0.5;
float diff_rough = 0.3;
string diff_tex_p = DEFAULT_MAP;
vector refl_b_cd = {1.0, 1.0, 1.0};
float refl_b_int = 0.5;
float refl_b_rough = 0.3;
string refl_b_tex_p = DEFAULT_MAP;
vector refl_c_cd = {1.0, 1.0, 1.0};
float refl_c_int = 0.2;
float refl_c_rough = 0.05;
string refl_c_tex_p = DEFAULT_MAP;
vector emit_cd = {1.0, 1.0, 1.0};
float emit_int = 0.0;
vector opac_cd = {1.0, 1.0, 1.0};
float opac_int = 1.0;
vector uv = {0.0, 0.0, 0.0};
export vector Ce = {0.0, 0.0, 0.0};
) {
vector nN = normalize(N);
////////////////////////////////////////////////////////////////
// Diffuse (oren-nayer)
////////////////////////////////////////////////////////////////
bsdf f_diff = cvex_bsdf("diffuse_eval", "diffuse_sample",
"label", "diffuse",
"N", nN,
"Ng", Ng,
"sigma", diff_rough);
// keeping things simple by hard coding options for texture transforms, wrapping, colorspace
// we're also ignoring alpha for simplicity. for udim support, see the vex function:
// string expand_udim(float u, v; string path)
// to sample from Disney ptex maps:
// vector ptexture(string map, int face_id, ...)
vector diff_tex = texture(diff_tex_p, uv.x, uv.y,
"wrap", "repeat",
"filter", "box",
"width", 1,
"border", { 0, 0, 0, 1 },
"lerp", 0,
"blur", 0,
"pixelblur", 0,
"srccolorspace", "auto");
////////////////////////////////////////////////////////////////
// Reflection Base (ggx)
////////////////////////////////////////////////////////////////
// roughness squared is visually more linear
float alpha_base = max(0.001, refl_b_rough * refl_b_rough);
// ggx distribution gives a nice sharp peak with a long tail
// this is the simpler isotropic version so alphax = alphay = alphaG
bsdf f_ggx_b = cvex_bsdf("ggx_eval", "ggx_sample",
"label", "reflect",
"aniso", 0,
"ng", nN,
"alphax", alpha_base,
"alphay", alpha_base,
"alphaG", alpha_base,
"masking", 1);
// we're handed an un-normalized distribution so we have to correct that.
// we also cannot simply write f /= lum(albedo()) as division isn't defined on bsdfs
f_ggx_b *= 1.0 / luminance(albedo(f_ggx_b));
vector refl_b_tex = texture(refl_b_tex_p, uv.x, uv.y,
"wrap", "repeat",
"filter", "box",
"width", 1,
"border", { 0, 0, 0, 1 },
"lerp", 0,
"blur", 0,
"pixelblur", 0,
"srccolorspace", "auto");
////////////////////////////////////////////////////////////////
// Reflection Coat (ggx)
////////////////////////////////////////////////////////////////
float alpha_coat = max(0.001, refl_c_rough * refl_c_rough);
bsdf f_ggx_c = cvex_bsdf("ggx_eval", "ggx_sample",
"label", "coat",
"aniso", 0,
"ng", nN,
"alphax", alpha_coat,
"alphay", alpha_coat,
"alphaG", alpha_coat,
"masking", 1);
f_ggx_c *= 1.0 / luminance(albedo(f_ggx_c));
vector refl_c_tex = texture(refl_c_tex_p, uv.x, uv.y,
"wrap", "repeat",
"filter", "box",
"width", 1,
"border", { 0, 0, 0, 1 },
"lerp", 0,
"blur", 0,
"pixelblur", 0,
"srccolorspace", "auto");
////////////////////////////////////////////////////////////////
// Complete BSDF
////////////////////////////////////////////////////////////////
// (diffuse bsdf) + (reflection base bsdf) + (reflection coat bsdf)
bsdf f = (f_diff * diff_cd * diff_tex * diff_int * 2.0) +
(f_ggx_b * refl_b_cd * refl_b_tex * refl_b_int) +
(f_ggx_c * refl_c_cd * refl_c_tex * refl_c_int);
// export the final bsdf with energy conservation
float scale = max(luminance(albedo(f)), 0.000005);
scale = min(1.0 / scale, 1.0);
F = f * scale;
////////////////////////////////////////////////////////////////
// Opacity and Emission
////////////////////////////////////////////////////////////////
// emission is handled outside the bsdf with opacity
Ce = emit_cd * emit_int;
Of = opac_cd * opac_int;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment