| 1 |
//------------------------------------------------------------------------------ |
| 2 |
// Lighting |
| 3 |
//------------------------------------------------------------------------------ |
| 4 |
|
| 5 |
float computeDiffuseAlpha(float a) { |
| 6 |
#if defined(BLEND_MODE_TRANSPARENT) || defined(BLEND_MODE_FADE) || defined(BLEND_MODE_MASKED) |
| 7 |
return a; |
| 8 |
#else |
| 9 |
return 1.0; |
| 10 |
#endif |
| 11 |
} |
| 12 |
|
| 13 |
#if defined(BLEND_MODE_MASKED) |
| 14 |
float computeMaskedAlpha(float a) { |
| 15 |
// Use derivatives to smooth alpha tested edges |
| 16 |
return (a - getMaskThreshold()) / max(fwidth(a), 1e-3) + 0.5; |
| 17 |
} |
| 18 |
#endif |
| 19 |
|
| 20 |
void applyAlphaMask(inout vec4 baseColor) { |
| 21 |
#if defined(BLEND_MODE_MASKED) |
| 22 |
baseColor.a = computeMaskedAlpha(baseColor.a); |
| 23 |
if (baseColor.a <= 0.0) { |
| 24 |
discard; |
| 25 |
} |
| 26 |
#endif |
| 27 |
} |
| 28 |
|
| 29 |
#if defined(GEOMETRIC_SPECULAR_AA) |
| 30 |
float normalFiltering(float perceptualRoughness, const vec3 worldNormal) { |
| 31 |
// Kaplanyan 2016, "Stable specular highlights" |
| 32 |
// Tokuyoshi 2017, "Error Reduction and Simplification for Shading Anti-Aliasing" |
| 33 |
// Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing" |
| 34 |
|
| 35 |
// This implementation is meant for deferred rendering in the original paper but |
| 36 |
// we use it in forward rendering as well (as discussed in Tokuyoshi and Kaplanyan |
| 37 |
// 2019). The main reason is that the forward version requires an expensive transform |
| 38 |
// of the half vector by the tangent frame for every light. This is therefore an |
| 39 |
// approximation but it works well enough for our needs and provides an improvement |
| 40 |
// over our original implementation based on Vlachos 2015, "Advanced VR Rendering". |
| 41 |
|
| 42 |
vec3 du = dFdx(worldNormal); |
| 43 |
vec3 dv = dFdy(worldNormal); |
| 44 |
|
| 45 |
float variance = materialParams._specularAntiAliasingVariance * (dot(du, du) + dot(dv, dv)); |
| 46 |
|
| 47 |
float roughness = perceptualRoughnessToRoughness(perceptualRoughness); |
| 48 |
float kernelRoughness = min(2.0 * variance, materialParams._specularAntiAliasingThreshold); |
| 49 |
float squareRoughness = saturate(roughness * roughness + kernelRoughness); |
| 50 |
|
| 51 |
return roughnessToPerceptualRoughness(sqrt(squareRoughness)); |
| 52 |
} |
| 53 |
#endif |
| 54 |
|
| 55 |
void getCommonPixelParams(const MaterialInputs material, inout PixelParams pixel) { |
| 56 |
vec4 baseColor = material.baseColor; |
| 57 |
applyAlphaMask(baseColor); |
| 58 |
|
| 59 |
#if defined(BLEND_MODE_FADE) && !defined(SHADING_MODEL_UNLIT) |
| 60 |
// Since we work in premultiplied alpha mode, we need to un-premultiply |
| 61 |
// in fade mode so we can apply alpha to both the specular and diffuse |
| 62 |
// components at the end |
| 63 |
unpremultiply(baseColor); |
| 64 |
#endif |
| 65 |
|
| 66 |
#if defined(SHADING_MODEL_SPECULAR_GLOSSINESS) |
| 67 |
// This is from KHR_materials_pbrSpecularGlossiness. |
| 68 |
vec3 specularColor = material.specularColor; |
| 69 |
float metallic = computeMetallicFromSpecularColor(specularColor); |
| 70 |
|
| 71 |
pixel.diffuseColor = computeDiffuseColor(baseColor, metallic); |
| 72 |
pixel.f0 = specularColor; |
| 73 |
#elif !defined(SHADING_MODEL_CLOTH) |
| 74 |
#if defined(HAS_REFRACTION) && (!defined(MATERIAL_HAS_REFLECTANCE) && defined(MATERIAL_HAS_IOR)) |
| 75 |
pixel.diffuseColor = baseColor.rgb; |
| 76 |
// If refraction is enabled, and reflectance is not set in the material, but ior is, |
| 77 |
// then use it -- othterwise proceed as usual. |
| 78 |
pixel.f0 = vec3(iorToF0(material.ior, 1.0)); |
| 79 |
#else |
| 80 |
pixel.diffuseColor = computeDiffuseColor(baseColor, material.metallic); |
| 81 |
// Assumes an interface from air to an IOR of 1.5 for dielectrics |
| 82 |
float reflectance = computeDielectricF0(material.reflectance); |
| 83 |
pixel.f0 = computeF0(baseColor, material.metallic, reflectance); |
| 84 |
#endif |
| 85 |
|
| 86 |
#else |
| 87 |
pixel.diffuseColor = baseColor.rgb; |
| 88 |
pixel.f0 = material.sheenColor; |
| 89 |
#if defined(MATERIAL_HAS_SUBSURFACE_COLOR) |
| 90 |
pixel.subsurfaceColor = material.subsurfaceColor; |
| 91 |
#endif |
| 92 |
#endif |
| 93 |
|
| 94 |
#if defined(HAS_REFRACTION) |
| 95 |
// Air's Index of refraction is 1.000277 at STP but everybody uses 1.0 |
| 96 |
const float airIor = 1.0; |
| 97 |
#if !defined(MATERIAL_HAS_IOR) |
| 98 |
// [common case] ior is not set in the material, deduce it from F0 |
| 99 |
float materialor = f0ToIor(pixel.f0.g); |
| 100 |
#else |
| 101 |
// if ior is set in the material, use it (can lead to unrealistic materials) |
| 102 |
float materialor = max(1.0, material.ior); |
| 103 |
#endif |
| 104 |
pixel.etaIR = airIor / materialor; // air -> material |
| 105 |
pixel.etaRI = materialor / airIor; // material -> air |
| 106 |
#if defined(MATERIAL_HAS_TRANSMISSION) |
| 107 |
pixel.transmission = saturate(material.transmission); |
| 108 |
#else |
| 109 |
pixel.transmission = 1.0; |
| 110 |
#endif |
| 111 |
#if defined(MATERIAL_HAS_ABSORPTION) |
| 112 |
#if defined(MATERIAL_HAS_THICKNESS) || defined(MATERIAL_HAS_MICRO_THICKNESS) |
| 113 |
pixel.absorption = max(vec3(0.0), material.absorption); |
| 114 |
#else |
| 115 |
pixel.absorption = saturate(material.absorption); |
| 116 |
#endif |
| 117 |
#else |
| 118 |
pixel.absorption = vec3(0.0); |
| 119 |
#endif |
| 120 |
#if defined(MATERIAL_HAS_THICKNESS) |
| 121 |
pixel.thickness = max(0.0, material.thickness); |
| 122 |
#endif |
| 123 |
#if defined(MATERIAL_HAS_MICRO_THICKNESS) && (REFRACTION_TYPE == REFRACTION_TYPE_THIN) |
| 124 |
pixel.uThickness = max(0.0, material.microThickness); |
| 125 |
#else |
| 126 |
pixel.uThickness = 0.0; |
| 127 |
#endif |
| 128 |
#endif |
| 129 |
} |
| 130 |
|
| 131 |
void getSheenPixelParams(const MaterialInputs material, inout PixelParams pixel) { |
| 132 |
#if defined(MATERIAL_HAS_SHEEN_COLOR) && !defined(SHADING_MODEL_CLOTH) && !defined(SHADING_MODEL_SUBSURFACE) |
| 133 |
pixel.sheenColor = material.sheenColor; |
| 134 |
|
| 135 |
float sheenPerceptualRoughness = material.sheenRoughness; |
| 136 |
sheenPerceptualRoughness = clamp(sheenPerceptualRoughness, MIN_PERCEPTUAL_ROUGHNESS, 1.0); |
| 137 |
|
| 138 |
#if defined(GEOMETRIC_SPECULAR_AA) |
| 139 |
sheenPerceptualRoughness = |
| 140 |
normalFiltering(sheenPerceptualRoughness, getWorldGeometricNormalVector()); |
| 141 |
#endif |
| 142 |
|
| 143 |
pixel.sheenPerceptualRoughness = sheenPerceptualRoughness; |
| 144 |
pixel.sheenRoughness = perceptualRoughnessToRoughness(sheenPerceptualRoughness); |
| 145 |
#endif |
| 146 |
} |
| 147 |
|
| 148 |
void getClearCoatPixelParams(const MaterialInputs material, inout PixelParams pixel) { |
| 149 |
#if defined(MATERIAL_HAS_CLEAR_COAT) |
| 150 |
pixel.clearCoat = material.clearCoat; |
| 151 |
|
| 152 |
// Clamp the clear coat roughness to avoid divisions by 0 |
| 153 |
float clearCoatPerceptualRoughness = material.clearCoatRoughness; |
| 154 |
clearCoatPerceptualRoughness = |
| 155 |
clamp(clearCoatPerceptualRoughness, MIN_PERCEPTUAL_ROUGHNESS, 1.0); |
| 156 |
|
| 157 |
#if defined(GEOMETRIC_SPECULAR_AA) |
| 158 |
clearCoatPerceptualRoughness = |
| 159 |
normalFiltering(clearCoatPerceptualRoughness, getWorldGeometricNormalVector()); |
| 160 |
#endif |
| 161 |
|
| 162 |
pixel.clearCoatPerceptualRoughness = clearCoatPerceptualRoughness; |
| 163 |
pixel.clearCoatRoughness = perceptualRoughnessToRoughness(clearCoatPerceptualRoughness); |
| 164 |
|
| 165 |
#if defined(CLEAR_COAT_IOR_CHANGE) |
| 166 |
// The base layer's f0 is computed assuming an interface from air to an IOR |
| 167 |
// of 1.5, but the clear coat layer forms an interface from IOR 1.5 to IOR |
| 168 |
// 1.5. We recompute f0 by first computing its IOR, then reconverting to f0 |
| 169 |
// by using the correct interface |
| 170 |
pixel.f0 = mix(pixel.f0, f0ClearCoatToSurface(pixel.f0), pixel.clearCoat); |
| 171 |
#endif |
| 172 |
#endif |
| 173 |
} |
| 174 |
|
| 175 |
void getRoughnessPixelParams(const MaterialInputs material, inout PixelParams pixel) { |
| 176 |
#if defined(SHADING_MODEL_SPECULAR_GLOSSINESS) |
| 177 |
float perceptualRoughness = computeRoughnessFromGlossiness(material.glossiness); |
| 178 |
#else |
| 179 |
float perceptualRoughness = material.roughness; |
| 180 |
#endif |
| 181 |
|
| 182 |
// This is used by the refraction code and must be saved before we apply specular AA |
| 183 |
pixel.perceptualRoughnessUnclamped = perceptualRoughness; |
| 184 |
|
| 185 |
#if defined(GEOMETRIC_SPECULAR_AA) |
| 186 |
perceptualRoughness = normalFiltering(perceptualRoughness, getWorldGeometricNormalVector()); |
| 187 |
#endif |
| 188 |
|
| 189 |
#if defined(MATERIAL_HAS_CLEAR_COAT) && defined(MATERIAL_HAS_CLEAR_COAT_ROUGHNESS) |
| 190 |
// This is a hack but it will do: the base layer must be at least as rough |
| 191 |
// as the clear coat layer to take into account possible diffusion by the |
| 192 |
// top layer |
| 193 |
float basePerceptualRoughness = max(perceptualRoughness, pixel.clearCoatPerceptualRoughness); |
| 194 |
perceptualRoughness = mix(perceptualRoughness, basePerceptualRoughness, pixel.clearCoat); |
| 195 |
#endif |
| 196 |
|
| 197 |
// Clamp the roughness to a minimum value to avoid divisions by 0 during lighting |
| 198 |
pixel.perceptualRoughness = clamp(perceptualRoughness, MIN_PERCEPTUAL_ROUGHNESS, 1.0); |
| 199 |
// Remaps the roughness to a perceptually linear roughness (roughness^2) |
| 200 |
pixel.roughness = perceptualRoughnessToRoughness(pixel.perceptualRoughness); |
| 201 |
} |
| 202 |
|
| 203 |
void getSubsurfacePixelParams(const MaterialInputs material, inout PixelParams pixel) { |
| 204 |
#if defined(SHADING_MODEL_SUBSURFACE) |
| 205 |
pixel.subsurfacePower = material.subsurfacePower; |
| 206 |
pixel.subsurfaceColor = material.subsurfaceColor; |
| 207 |
pixel.thickness = saturate(material.thickness); |
| 208 |
#endif |
| 209 |
} |
| 210 |
|
| 211 |
void getAnisotropyPixelParams(const MaterialInputs material, inout PixelParams pixel) { |
| 212 |
#if defined(MATERIAL_HAS_ANISOTROPY) |
| 213 |
vec3 direction = material.anisotropyDirection; |
| 214 |
pixel.anisotropy = material.anisotropy; |
| 215 |
pixel.anisotropicT = normalize(shading_tangentToWorld * direction); |
| 216 |
pixel.anisotropicB = normalize(cross(getWorldGeometricNormalVector(), pixel.anisotropicT)); |
| 217 |
#endif |
| 218 |
} |
| 219 |
|
| 220 |
void getEnergyCompensationPixelParams(inout PixelParams pixel) { |
| 221 |
// Pre-filtered DFG term used for image-based lighting |
| 222 |
pixel.dfg = prefilteredDFG(pixel.perceptualRoughness, shading_NoV); |
| 223 |
|
| 224 |
#if !defined(SHADING_MODEL_CLOTH) |
| 225 |
// Energy compensation for multiple scattering in a microfacet model |
| 226 |
// See "Multiple-Scattering Microfacet BSDFs with the Smith Model" |
| 227 |
pixel.energyCompensation = 1.0 + pixel.f0 * (1.0 / pixel.dfg.y - 1.0); |
| 228 |
#else |
| 229 |
pixel.energyCompensation = vec3(1.0); |
| 230 |
#endif |
| 231 |
|
| 232 |
#if !defined(SHADING_MODEL_CLOTH) |
| 233 |
#if defined(MATERIAL_HAS_SHEEN_COLOR) |
| 234 |
pixel.sheenDFG = prefilteredDFG(pixel.sheenPerceptualRoughness, shading_NoV).z; |
| 235 |
pixel.sheenScaling = 1.0 - max3(pixel.sheenColor) * pixel.sheenDFG; |
| 236 |
#endif |
| 237 |
#endif |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Computes all the parameters required to shade the current pixel/fragment. |
| 242 |
* These parameters are derived from the MaterialInputs structure computed |
| 243 |
* by the user's material code. |
| 244 |
* |
| 245 |
* This function is also responsible for discarding the fragment when alpha |
| 246 |
* testing fails. |
| 247 |
*/ |
| 248 |
void getPixelParams(const MaterialInputs material, out PixelParams pixel) { |
| 249 |
getCommonPixelParams(material, pixel); |
| 250 |
getSheenPixelParams(material, pixel); |
| 251 |
getClearCoatPixelParams(material, pixel); |
| 252 |
getRoughnessPixelParams(material, pixel); |
| 253 |
getSubsurfacePixelParams(material, pixel); |
| 254 |
getAnisotropyPixelParams(material, pixel); |
| 255 |
getEnergyCompensationPixelParams(pixel); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* This function evaluates all lights one by one: |
| 260 |
* - Image based lights (IBL) |
| 261 |
* - Directional lights |
| 262 |
* - Punctual lights |
| 263 |
* |
| 264 |
* Area lights are currently not supported. |
| 265 |
* |
| 266 |
* Returns a pre-exposed HDR RGBA color in linear space. |
| 267 |
*/ |
| 268 |
vec4 evaluateLights(const MaterialInputs material) { |
| 269 |
PixelParams pixel; |
| 270 |
getPixelParams(material, pixel); |
| 271 |
|
| 272 |
// Ideally we would keep the diffuse and specular components separate |
| 273 |
// until the very end but it costs more ALUs on mobile. The gains are |
| 274 |
// currently not worth the extra operations |
| 275 |
vec3 color = vec3(0.0); |
| 276 |
|
| 277 |
// We always evaluate the IBL as not having one is going to be uncommon, |
| 278 |
// it also saves 1 shader variant |
| 279 |
evaluateIBL(material, pixel, color); |
| 280 |
|
| 281 |
#if defined(HAS_DIRECTIONAL_LIGHTING) |
| 282 |
evaluateDirectionalLight(material, pixel, color); |
| 283 |
#endif |
| 284 |
|
| 285 |
#if defined(HAS_DYNAMIC_LIGHTING) |
| 286 |
evaluatePunctualLights(pixel, color); |
| 287 |
#endif |
| 288 |
|
| 289 |
#if defined(BLEND_MODE_FADE) && !defined(SHADING_MODEL_UNLIT) |
| 290 |
// In fade mode we un-premultiply baseColor early on, so we need to |
| 291 |
// premultiply again at the end (affects diffuse and specular lighting) |
| 292 |
color *= material.baseColor.a; |
| 293 |
#endif |
| 294 |
|
| 295 |
return vec4(color, computeDiffuseAlpha(material.baseColor.a)); |
| 296 |
} |
| 297 |
|
| 298 |
void addEmissive(const MaterialInputs material, inout vec4 color) { |
| 299 |
#if defined(MATERIAL_HAS_EMISSIVE) |
| 300 |
highp vec4 emissive = material.emissive; |
| 301 |
highp float attenuation = mix(1.0, frameUniforms.exposure, emissive.w); |
| 302 |
color.rgb += emissive.rgb * (attenuation * color.a); |
| 303 |
#endif |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Evaluate lit materials. The actual shading model used to do so is defined |
| 308 |
* by the function surfaceShading() found in shading_model_*.fs. |
| 309 |
* |
| 310 |
* Returns a pre-exposed HDR RGBA color in linear space. |
| 311 |
*/ |
| 312 |
vec4 evaluateMaterial(const MaterialInputs material) { |
| 313 |
vec4 color = evaluateLights(material); |
| 314 |
addEmissive(material, color); |
| 315 |
return color; |
| 316 |
} |