lib-sampler.glsl
Public Functions:
getAO
getShadowFactor
getGlossiness
getRoughness
getMetallic
getAnisotropyLevel
getAnisotropyAngle
getOpacity
getHeight
getDisplacement
getSpecularLevel
getBaseColor
getDiffuse
getSpecularColor
getTranslucency
getAbsorptionColor
getScatteringPerComponent
getScatteringColor
getSpecularEdgeColor
getSpecularTint
getSheenOpacity
getSheenColor
getSheenRoughness
getCoatOpacity
getCoatColor
getCoatRoughness
getCoatSpecularLevel
generateAnisotropicRoughness
generateDiffuseColor
generateSpecularColor
Import from library
Copied to your clipboardimport lib-defines.glslimport lib-sparse.glsl
Default background colors when there is no data in channel (alpha is 0)
Copied to your clipboardconst vec3 DEFAULT_BASE_COLOR = vec3(0.5);const float DEFAULT_ROUGHNESS = 0.3;const float DEFAULT_METALLIC = 0.0;const float DEFAULT_ANISOTROPY_LEVEL = 0.0;const float DEFAULT_ANISOTROPY_ANGLE = 0.0;const float DEFAULT_OPACITY = 1.0;const float DEFAULT_AO = 1.0;const float DEFAULT_SPECULAR_LEVEL = 0.5;const float DEFAULT_HEIGHT = 0.0;const float DEFAULT_DISPLACEMENT = 0.0;const float DEFAULT_TRANSMISSIVE = 0.0;const float DEFAULT_TRANSLUCENCY = 0.0;const vec3 DEFAULT_ABSORPTION_COLOR = vec3(1.0);const float DEFAULT_SCATTERING = 0.0;const vec3 DEFAULT_SCATTERINGCOLOR = vec3(1.0);const vec3 DEFAULT_SPECULAR_TINT = vec3(1.0);const vec3 DEFAULT_SPEC_EDGE_COLOR = vec3(1.0);const float DEFAULT_SHEEN_OPACITY = 0.0;const vec3 DEFAULT_SHEEN_COLOR = vec3(1.0);const float DEFAULT_SHEEN_ROUGHNESS = 0.5;const float DEFAULT_COAT_OPACITY = 0.0;const vec3 DEFAULT_COAT_COLOR = vec3(1.0);const float DEFAULT_COAT_ROUGHNESS = 0.0;const float DEFAULT_COAT_SPEC_LEVEL = 0.5;
AO map.
Copied to your clipboard//: param auto ao_blending_modeuniform int ao_blending_mode;//: param auto texture_aouniform SamplerSparse base_ao_tex;//: param auto channel_aouniform SamplerSparse ao_tex;
A value used to tweak the Ambient Occlusion intensity.
Copied to your clipboard//: param custom {//: "default": 0.75,//: "label": "AO Intensity",//: "min": 0.00,//: "max": 1.0,//: "group": "Base Surface",//: "description": "<html><head/><body><p>Ambience occlusion (AO) represents shadows from cavities and creases preventing light from hitting the surface. Uses the baked mesh map <b>Ambient occlusion</b> and/or the channel <b>Ambient occlusion</b>.</p></body></html>"//: }uniform float ao_intensity;
Shadowmask.
Copied to your clipboard//: param auto shadow_mask_enableuniform bool sm_enable;//: param auto shadow_mask_opacityuniform float sm_opacity;//: param auto shadow_maskuniform sampler2D sm_tex;//: param auto screen_sizeuniform vec4 screen_size;
Return sampled glossiness or a default value
Copied to your clipboardfloat getGlossiness(vec4 sampledValue){return sampledValue.r + (1.0 - DEFAULT_ROUGHNESS) * (1.0 - sampledValue.g);}float getGlossiness(SamplerSparse smp, SparseCoord coord){return getGlossiness(textureSparse(smp, coord));}
Return sampled roughness or a default value
Copied to your clipboardfloat getRoughness(vec4 sampledValue){return sampledValue.r + DEFAULT_ROUGHNESS * (1.0 - sampledValue.g);}float getRoughness(SamplerSparse smp, SparseCoord coord){return getRoughness(textureSparse(smp, coord));}
Return sampled metallic or a default value
Copied to your clipboardfloat getMetallic(vec4 sampledValue){return sampledValue.r + DEFAULT_METALLIC * (1.0 - sampledValue.g);}float getMetallic(SamplerSparse smp, SparseCoord coord){return getMetallic(textureSparse(smp, coord));}
Return sampled anisotropy level or a default value
Copied to your clipboardfloat getAnisotropyLevel(vec4 sampledValue){return sampledValue.r + DEFAULT_ANISOTROPY_LEVEL * (1.0 - sampledValue.g);}float getAnisotropyLevel(SamplerSparse smp, SparseCoord coord){return getAnisotropyLevel(textureSparse(smp, coord));}
Return sampled anisotropy angle or a default value
Copied to your clipboardfloat getAnisotropyAngle(vec4 sampledValue){return M_2PI * (sampledValue.r + DEFAULT_ANISOTROPY_ANGLE * (1.0 - sampledValue.g));}float getAnisotropyAngle(SamplerSparse smp, SparseCoord coord){// Manual trilinear filteringfloat level = max(0.0, textureSparseQueryLod(smp, coord) + uvtile_lod_bias);int level0 = int(level);int level1 = level0 + 1;ivec2 texSize0 = ivec2(smp.size.xy) >> level0;ivec2 texSize1 = texSize0 >> 1;ivec2 itex_coord0 = ivec2(coord.tex_coord * vec2(texSize0));ivec2 itex_coord1 = ivec2(coord.tex_coord * vec2(texSize1));// Assuming tex sizes are pow of 2, we can do the fast moduloivec2 texSizeMask0 = texSize0 - ivec2(1);ivec2 texSizeMask1 = texSize1 - ivec2(1);// Fetch the 8 samples neededfloat a000 = getAnisotropyAngle(texelFetch(smp.tex, itex_coord0 & texSizeMask0, level0));float a001 = getAnisotropyAngle(texelFetch(smp.tex, (itex_coord0 + ivec2(1, 0)) & texSizeMask0, level0)) - a000;float a010 = getAnisotropyAngle(texelFetch(smp.tex, (itex_coord0 + ivec2(0, 1)) & texSizeMask0, level0)) - a000;float a011 = getAnisotropyAngle(texelFetch(smp.tex, (itex_coord0 + ivec2(1, 1)) & texSizeMask0, level0)) - a000;float a100 = getAnisotropyAngle(texelFetch(smp.tex, itex_coord1 & texSizeMask1, level1)) - a000;float a101 = getAnisotropyAngle(texelFetch(smp.tex, (itex_coord1 + ivec2(1, 0)) & texSizeMask1, level1)) - a000;float a110 = getAnisotropyAngle(texelFetch(smp.tex, (itex_coord1 + ivec2(0, 1)) & texSizeMask1, level1)) - a000;float a111 = getAnisotropyAngle(texelFetch(smp.tex, (itex_coord1 + ivec2(1, 1)) & texSizeMask1, level1)) - a000;// Detect if the angle warps inside the filtering footprint, and fix ita001 += abs(a001) > M_PI ? sign(a001) * -M_2PI + a000 : a000;a010 += abs(a010) > M_PI ? sign(a010) * -M_2PI + a000 : a000;a011 += abs(a011) > M_PI ? sign(a011) * -M_2PI + a000 : a000;a100 += abs(a100) > M_PI ? sign(a100) * -M_2PI + a000 : a000;a101 += abs(a101) > M_PI ? sign(a101) * -M_2PI + a000 : a000;a110 += abs(a110) > M_PI ? sign(a110) * -M_2PI + a000 : a000;a111 += abs(a111) > M_PI ? sign(a111) * -M_2PI + a000 : a000;// Trilinear blending of the samplesvec2 t0 = coord.tex_coord * vec2(texSize0) - vec2(itex_coord0);vec2 t1 = coord.tex_coord * vec2(texSize1) - vec2(itex_coord1);return mix(mix(mix(a000, a001, t0.x), mix(a010, a011, t0.x), t0.y),mix(mix(a100, a101, t1.x), mix(a110, a111, t1.x), t1.y),level - float(level0));}
Return sampled opacity or a default value
Copied to your clipboardfloat getOpacity(vec4 sampledValue){return sampledValue.r + DEFAULT_OPACITY * (1.0 - sampledValue.g);}float getOpacity(SamplerSparse smp, SparseCoord coord){return getOpacity(textureSparse(smp, coord));}
Return sampled height or a default value
Copied to your clipboardfloat getHeight(vec4 sampledValue){return sampledValue.r + DEFAULT_HEIGHT * (1.0 - sampledValue.g);}float getHeight(SamplerSparse smp, SparseCoord coord){return getHeight(textureSparse(smp, coord));}
Return sampled displacement or a default value
Copied to your clipboardfloat getDisplacement(vec4 sampledValue){return sampledValue.r + DEFAULT_DISPLACEMENT * (1.0 - sampledValue.g);}float getDisplacement(SamplerSparse smp, SparseCoord coord){return getDisplacement(textureSparse(smp, coord));}
Return ambient occlusion
Copied to your clipboardfloat getAO(SparseCoord coord, bool is_premult, bool is_full_strength){vec2 ao_lookup = textureSparse(base_ao_tex, coord).ra;float ao = ao_lookup.x + DEFAULT_AO * (1.0 - ao_lookup.y);if (ao_tex.is_set) {ao_lookup = textureSparse(ao_tex, coord).rg;if (!is_premult) ao_lookup.x *= ao_lookup.y;float channel_ao = ao_lookup.x + DEFAULT_AO * (1.0 - ao_lookup.y);if (ao_blending_mode == BlendingMode_Replace) {ao = channel_ao;} else if (ao_blending_mode == BlendingMode_Multiply) {ao *= channel_ao;}}// skip mix if full strength// Otherwise modulate AO value by AO_intensityreturn is_full_strength ? ao : mix(1.0, ao, ao_intensity);}float getAO(SparseCoord coord, bool is_premult){return getAO(coord, is_premult, false);}float getAO(SparseCoord coord){return getAO(coord, true, false);}
Return specular level
Copied to your clipboardfloat getSpecularLevel(vec4 sampledValue){return sampledValue.r + DEFAULT_SPECULAR_LEVEL * (1.0 - sampledValue.g);}float getSpecularLevel(SamplerSparse smp, SparseCoord coord){return getSpecularLevel(textureSparse(smp, coord));}
Fetch the shadowing factor (screen-space)
Copied to your clipboardfloat getShadowFactor(){float shadowFactor = 1.0;if (sm_enable) {vec2 screenCoord = (gl_FragCoord.xy * vec2(screen_size.z, screen_size.w));vec2 shadowSample = texture(sm_tex, screenCoord).xy;// shadowSample.x / shadowSample.y is the normalized shadow factor.// shadowSample.x may already be normalized, shadowSample.y contains 0.0 in this case.shadowFactor = shadowSample.y == 0.0 ? shadowSample.x : shadowSample.x / shadowSample.y;}return mix(1.0, shadowFactor, sm_opacity);}
Return sampled base color or a default value
Copied to your clipboardvec3 getBaseColor(vec4 sampledValue){return sampledValue.rgb + DEFAULT_BASE_COLOR * (1.0 - sampledValue.a);}vec3 getBaseColor(SamplerSparse smp, SparseCoord coord){return getBaseColor(textureSparse(smp, coord));}
Return sampled diffuse color or a default value
Copied to your clipboardvec3 getDiffuse(vec4 sampledValue){return getBaseColor(sampledValue);}vec3 getDiffuse(SamplerSparse smp, SparseCoord coord){return getDiffuse(textureSparse(smp, coord));}
Return sampled specular color or a default value Specular/Glossiness PBR workflow only
Copied to your clipboardvec3 getSpecularColor(vec4 sampledValue){vec3 specColor = sampledValue.rgb + DEFAULT_BASE_COLOR * (1.0 - sampledValue.a);vec3 defaultF0 = mix(vec3(0.04), specColor, DEFAULT_METALLIC);return mix(specColor, defaultF0, (1.0 - sampledValue.a));}vec3 getSpecularColor(SamplerSparse smp, SparseCoord coord){return getSpecularColor(textureSparse(smp, coord));}
Generate anisotropic roughness from roughness and anisotropy level
Copied to your clipboardvec2 generateAnisotropicRoughness(float roughness, float anisoLevel){return vec2(roughness, roughness / sqrt(max(1e-8, 1.0 - anisoLevel)));}
Generate anisotropic roughness from roughness and anisotropy level, for the ASM model.
Copied to your clipboardvec2 generateAnisotropicRoughnessASM(float roughness, float anisoLevel){float alphaU = min(1., roughness*roughness + pow(anisoLevel , 4.0));return vec2(sqrt(alphaU), roughness);}
Generate diffuse color from base color and metallic factor
Copied to your clipboardvec3 generateDiffuseColor(vec3 baseColor, float metallic){return baseColor * (1.0 - metallic);}
Generate specular color from dielectric specular level, base color and metallic factor
Copied to your clipboardvec3 generateSpecularColor(float specularLevel, vec3 baseColor, float metallic){return mix(vec3(0.08 * specularLevel), baseColor, metallic);}
Generate specular color from base color and metallic factor, using default specular level (0.04) for dielectrics
Copied to your clipboardvec3 generateSpecularColor(vec3 baseColor, float metallic){return mix(vec3(0.04), baseColor, metallic);}
Return sampled transmissive value or a default value
Copied to your clipboardfloat getTranslucency(vec4 sampledValue){return sampledValue.r + DEFAULT_TRANSLUCENCY * (1.0 - sampledValue.g);}float getTranslucency(SamplerSparse smp, SparseCoord coord){return getTranslucency(textureSparse(smp, coord));}vec3 getAbsorptionColor(vec4 sampledValue){return sampledValue.rgb + DEFAULT_ABSORPTION_COLOR * (1.0 - sampledValue.a);}vec3 getAbsorptionColor(SamplerSparse smp, SparseCoord coord){return getAbsorptionColor(textureSparse(smp, coord));}
Return sampled scattering value per component or a default value Handle grayscale (same radius for R, G \& B) or color texture
Copied to your clipboardvec3 getScatteringPerComponent(SamplerSparse smp, SparseCoord coord){vec4 sampledValue = textureSparse(smp, coord);return smp.is_color ?sampledValue.rgb + vec3(DEFAULT_SCATTERING) * (1.0 - sampledValue.a) :vec3(sampledValue.r + DEFAULT_SCATTERING * (1.0 - sampledValue.g));}
Return sampled scatter color or a default value
Copied to your clipboardvec3 getScatteringColor(vec4 sampledValue){return sampledValue.rgb + DEFAULT_SCATTERINGCOLOR * (1.0 - sampledValue.a);}vec3 getScatteringColor(SamplerSparse smp, SparseCoord coord){return getScatteringColor(textureSparse(smp, coord));}
Return sampled specular tint value or a default value
Copied to your clipboardvec3 getSpecularTint(vec4 sampledValue){return sampledValue.rgb + DEFAULT_SPECULAR_TINT * (1.0 - sampledValue.a);}vec3 getSpecularTint(SamplerSparse smp, SparseCoord coord){return getSpecularTint(textureSparse(smp, coord));}vec3 getSpecularEdgeColor(vec4 sampledValue){return sampledValue.rgb + DEFAULT_SPEC_EDGE_COLOR * (1.0 - sampledValue.a);}vec3 getSpecularEdgeColor(SamplerSparse smp, SparseCoord coord){return getSpecularEdgeColor(textureSparse(smp, coord));}
Return sampled sheen opacity (sheen weight) value or a default value
Copied to your clipboardfloat getSheenOpacity(vec4 sampledValue){return sampledValue.r + DEFAULT_SHEEN_OPACITY * (1.0 - sampledValue.g);}float getSheenOpacity(SamplerSparse smp, SparseCoord coord){return getSheenOpacity(textureSparse(smp, coord));}
Return sampled sheen color value or a default value
Copied to your clipboardvec3 getSheenColor(vec4 sampledValue){return sampledValue.rgb + DEFAULT_SHEEN_COLOR * (1.0 - sampledValue.a);}vec3 getSheenColor(SamplerSparse smp, SparseCoord coord){return getSheenColor(textureSparse(smp, coord));}
Return sampled sheen roughness value or a default value
Copied to your clipboardfloat getSheenRoughness(vec4 sampledValue){return sampledValue.r + DEFAULT_SHEEN_ROUGHNESS * (1.0 - sampledValue.g);}float getSheenRoughness(SamplerSparse smp, SparseCoord coord){return getSheenRoughness(textureSparse(smp, coord));}
Return sampled coat opacity (coat weight) value or a default value
Copied to your clipboardfloat getCoatOpacity(vec4 sampledValue){return sampledValue.r + DEFAULT_COAT_OPACITY * (1.0 - sampledValue.g);}float getCoatOpacity(SamplerSparse smp, SparseCoord coord){return getCoatOpacity(textureSparse(smp, coord));}
Return sampled coat color value or a default value
Copied to your clipboardvec3 getCoatColor(vec4 sampledValue){return sampledValue.rgb + DEFAULT_COAT_COLOR * (1.0 - sampledValue.a);}vec3 getCoatColor(SamplerSparse smp, SparseCoord coord){return getCoatColor(textureSparse(smp, coord));}
Return sampled coat roughness value or a default value
Copied to your clipboardfloat getCoatRoughness(vec4 sampledValue){return sampledValue.r + DEFAULT_COAT_ROUGHNESS * (1.0 - sampledValue.g);}float getCoatRoughness(SamplerSparse smp, SparseCoord coord){return getCoatRoughness(textureSparse(smp, coord));}
Return sampled coat specular level value or a default value
Copied to your clipboardfloat getCoatSpecularLevel(vec4 sampledValue){return sampledValue.r + DEFAULT_COAT_SPEC_LEVEL * (1.0 - sampledValue.g);}float getCoatSpecularLevel(SamplerSparse smp, SparseCoord coord){return getCoatSpecularLevel(textureSparse(smp, coord));}