This replaces the previous "curvature to roughness" method. Both are related and rely on the screen space variance of geometric normals but this new solution offers more control (the screen space variance and the clamping threshold can be controlled).
60 lines
2.2 KiB
GLSL
60 lines
2.2 KiB
GLSL
//------------------------------------------------------------------------------
|
|
// Material evaluation
|
|
//------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Computes global shading parameters used to apply lighting, such as the view
|
|
* vector in world space, the tangent frame at the shading point, etc.
|
|
*/
|
|
void computeShadingParams() {
|
|
#if defined(HAS_ATTRIBUTE_TANGENTS)
|
|
highp vec3 n = vertex_worldNormal;
|
|
|
|
#if defined(MATERIAL_HAS_DOUBLE_SIDED_CAPABILITY)
|
|
if (isDoubleSided()) {
|
|
n = gl_FrontFacing ? n : -n;
|
|
}
|
|
#endif
|
|
|
|
#if defined(MATERIAL_HAS_ANISOTROPY) || defined(MATERIAL_HAS_NORMAL) || defined(MATERIAL_HAS_CLEAR_COAT_NORMAL)
|
|
// Re-normalize post-interpolation values
|
|
shading_tangentToWorld = mat3(
|
|
normalize(vertex_worldTangent), normalize(vertex_worldBitangent), normalize(n));
|
|
#endif
|
|
// Leave the tangent and bitangent uninitialized, we won't use them
|
|
shading_tangentToWorld[2] = normalize(n);
|
|
#endif
|
|
|
|
shading_position = vertex_worldPosition;
|
|
shading_view = normalize(frameUniforms.cameraPosition - shading_position);
|
|
}
|
|
|
|
/**
|
|
* Computes global shading parameters that the material might need to access
|
|
* before lighting: N dot V, the reflected vector and the shading normal (before
|
|
* applying the normal map). These parameters can be useful to material authors
|
|
* to compute other material properties.
|
|
*
|
|
* This function must be invoked by the user's material code (guaranteed by
|
|
* the material compiler) after setting a value for MaterialInputs.normal.
|
|
*/
|
|
void prepareMaterial(const MaterialInputs material) {
|
|
#if defined(HAS_ATTRIBUTE_TANGENTS)
|
|
#if defined(MATERIAL_HAS_NORMAL)
|
|
shading_normal = normalize(shading_tangentToWorld * material.normal);
|
|
#else
|
|
shading_normal = getWorldGeometricNormalVector();
|
|
#endif
|
|
shading_NoV = clampNoV(dot(shading_normal, shading_view));
|
|
shading_reflected = reflect(-shading_view, shading_normal);
|
|
|
|
#if defined(MATERIAL_HAS_CLEAR_COAT)
|
|
#if defined(MATERIAL_HAS_CLEAR_COAT_NORMAL)
|
|
shading_clearCoatNormal = normalize(shading_tangentToWorld * material.clearCoatNormal);
|
|
#else
|
|
shading_clearCoatNormal = getWorldGeometricNormalVector();
|
|
#endif
|
|
#endif
|
|
#endif
|
|
}
|