| 1 |
void addEmissive(const MaterialInputs material, inout vec4 color) { |
| 2 |
#if defined(MATERIAL_HAS_EMISSIVE) |
| 3 |
highp vec4 emissive = material.emissive; |
| 4 |
highp float attenuation = mix(1.0, frameUniforms.exposure, emissive.w); |
| 5 |
color.rgb += emissive.rgb * (attenuation * color.a); |
| 6 |
#endif |
| 7 |
} |
| 8 |
|
| 9 |
#if defined(BLEND_MODE_MASKED) |
| 10 |
float computeMaskedAlpha(float a) { |
| 11 |
// Use derivatives to smooth alpha tested edges |
| 12 |
return (a - getMaskThreshold()) / max(fwidth(a), 1e-3) + 0.5; |
| 13 |
} |
| 14 |
#endif |
| 15 |
|
| 16 |
/** |
| 17 |
* Evaluates unlit materials. In this lighting model, only the base color and |
| 18 |
* emissive properties are taken into account: |
| 19 |
* |
| 20 |
* finalColor = baseColor + emissive |
| 21 |
* |
| 22 |
* The emissive factor is only applied if the fragment passes the optional |
| 23 |
* alpha test. |
| 24 |
* |
| 25 |
* When the shadowMultiplier property is enabled on the material, the final |
| 26 |
* color is multiplied by the inverse light visibility to apply a shadow. |
| 27 |
* This is mostly useful in AR to cast shadows on unlit transparent shadow |
| 28 |
* receiving planes. |
| 29 |
*/ |
| 30 |
vec4 evaluateMaterial(const MaterialInputs material) { |
| 31 |
vec4 color = material.baseColor; |
| 32 |
|
| 33 |
#if defined(BLEND_MODE_MASKED) |
| 34 |
color.a = computeMaskedAlpha(color.a); |
| 35 |
if (color.a <= 0.0) { |
| 36 |
discard; |
| 37 |
} |
| 38 |
#endif |
| 39 |
|
| 40 |
addEmissive(material, color); |
| 41 |
|
| 42 |
#if defined(HAS_DIRECTIONAL_LIGHTING) |
| 43 |
#if defined(HAS_SHADOWING) |
| 44 |
float visibility = 1.0; |
| 45 |
if ((frameUniforms.directionalShadows & 1u) != 0u) { |
| 46 |
uint cascade = getShadowCascade(); |
| 47 |
uint layer = cascade; |
| 48 |
#if defined(HAS_VSM) |
| 49 |
visibility = shadowVsm(light_shadowMap, layer, getCascadeLightSpacePosition(cascade)); |
| 50 |
#else |
| 51 |
visibility = shadow(light_shadowMap, layer, getCascadeLightSpacePosition(cascade)); |
| 52 |
#endif |
| 53 |
} |
| 54 |
if ((frameUniforms.directionalShadows & 0x2u) != 0u && visibility > 0.0) { |
| 55 |
if (objectUniforms.screenSpaceContactShadows != 0u) { |
| 56 |
visibility *= (1.0 - screenSpaceContactShadow(frameUniforms.lightDirection)); |
| 57 |
} |
| 58 |
} |
| 59 |
color *= 1.0 - visibility; |
| 60 |
#else |
| 61 |
color = vec4(0.0); |
| 62 |
#endif |
| 63 |
#elif defined(HAS_SHADOW_MULTIPLIER) |
| 64 |
color = vec4(0.0); |
| 65 |
#endif |
| 66 |
|
| 67 |
return color; |
| 68 |
} |