Skip to content

Instantly share code, notes, and snippets.

@nothke
Created August 18, 2022 11:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nothke/8c1eb962368bd09ad905b3072bbf1777 to your computer and use it in GitHub Desktop.
Save nothke/8c1eb962368bd09ad905b3072bbf1777 to your computer and use it in GitHub Desktop.
// Pixel (nearest neighbor) shader by nothke
//- Substance 3D Painter Metal/Rough PBR shader
//- ====================================
//-
//- Import from libraries.
import lib-pbr.glsl
import lib-bent-normal.glsl
import lib-emissive.glsl
import lib-pom.glsl
import lib-sss.glsl
import lib-utils.glsl
//- Declare the iray mdl material to use with this shader.
//: metadata {
//: "mdl":"mdl::alg::materials::skin_metallic_roughness::skin_metallic_roughness"
//: }
//- Channels needed for metal/rough workflow are bound here.
//: param auto channel_basecolor
uniform SamplerSparse basecolor_tex;
//: param auto channel_roughness
uniform SamplerSparse roughness_tex;
//: param auto channel_metallic
uniform SamplerSparse metallic_tex;
//: param auto channel_specularlevel
uniform SamplerSparse specularlevel_tex;
vec2 rnd(vec2 v, vec2 size)
{
return (0.5 + floor(v * size)) / size;
}
//- Shader entry point.
void shade(V2F inputs)
{
inputs.sparse_coord.tex_coord = rnd(inputs.sparse_coord.tex_coord, basecolor_tex.size.xy);
//inputs.sparse_coord.dfdx = vec2(0);
//inputs.sparse_coord.dfdy = vec2(0);
// Apply parallax occlusion mapping if possible
vec3 viewTS = worldSpaceToTangentSpace(getEyeVec(inputs.position), inputs);
applyParallaxOffset(inputs, viewTS);
// Fetch material parameters, and conversion to the specular/roughness model
float roughness = getRoughness(roughness_tex, inputs.sparse_coord);
vec3 baseColor = getBaseColor(basecolor_tex, inputs.sparse_coord);
float metallic = getMetallic(metallic_tex, inputs.sparse_coord);
float specularLevel = getSpecularLevel(specularlevel_tex, inputs.sparse_coord);
vec3 diffColor = generateDiffuseColor(baseColor, metallic);
vec3 specColor = generateSpecularColor(specularLevel, baseColor, metallic);
// Get detail (ambient occlusion) and global (shadow) occlusion factors
// separately in order to blend the bent normals properly
float shadowFactor = getShadowFactor();
float occlusion = getAO(inputs.sparse_coord, true, use_bent_normal);
float specOcclusion = specularOcclusionCorrection(
use_bent_normal ? shadowFactor : occlusion * shadowFactor,
metallic,
roughness);
LocalVectors vectors = computeLocalFrame(inputs);
computeBentNormal(vectors,inputs);
// Feed parameters for a physically based BRDF integration
emissiveColorOutput(pbrComputeEmissive(emissive_tex, inputs.sparse_coord));
albedoOutput(diffColor);
diffuseShadingOutput(occlusion * shadowFactor * envIrradiance(getDiffuseBentNormal(vectors)));
specularShadingOutput(specOcclusion * pbrComputeSpecular(vectors, specColor, roughness, occlusion, getBentNormalSpecularAmount()));
sssCoefficientsOutput(getSSSCoefficients(inputs.sparse_coord));
sssColorOutput(getSSSColor(inputs.sparse_coord));
}
@levelonesucks
Copy link

thank you for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment