Debian Codegraph @

shaders/src/shading_parameters.fs

raw
1 //------------------------------------------------------------------------------
2 // Material evaluation
3 //------------------------------------------------------------------------------
4
5 /**
6 * Computes global shading parameters used to apply lighting, such as the view
7 * vector in world space, the tangent frame at the shading point, etc.
8 */
9 void computeShadingParams() {
10 #if defined(HAS_ATTRIBUTE_TANGENTS)
11 highp vec3 n = vertex_worldNormal;
12 #if defined(MATERIAL_NEEDS_TBN)
13 highp vec3 t = vertex_worldTangent.xyz;
14 highp vec3 b = cross(n, t) * sign(vertex_worldTangent.w);
15 #endif
16
17 #if defined(MATERIAL_HAS_DOUBLE_SIDED_CAPABILITY)
18 if (isDoubleSided()) {
19 n = gl_FrontFacing ? n : -n;
20 #if defined(MATERIAL_NEEDS_TBN)
21 t = gl_FrontFacing ? t : -t;
22 b = gl_FrontFacing ? b : -b;
23 #endif
24 }
25 #endif
26
27 shading_geometricNormal = normalize(n);
28
29 #if defined(MATERIAL_NEEDS_TBN)
30 // We use unnormalized post-interpolation values, assuming mikktspace tangents
31 shading_tangentToWorld = mat3(t, b, n);
32 #endif
33 #endif
34
35 shading_position = vertex_worldPosition;
36 shading_view = normalize(frameUniforms.cameraPosition - shading_position);
37
38 // we do this so we avoid doing (matrix multiply), but we burn 4 varyings:
39 // p = clipFromWorldMatrix * shading_position;
40 // shading_normalizedViewportCoord = p.xy * 0.5 / p.w + 0.5
41 shading_normalizedViewportCoord = vertex_position.xy * (0.5 / vertex_position.w) + 0.5;
42 }
43
44 /**
45 * Computes global shading parameters that the material might need to access
46 * before lighting: N dot V, the reflected vector and the shading normal (before
47 * applying the normal map). These parameters can be useful to material authors
48 * to compute other material properties.
49 *
50 * This function must be invoked by the user's material code (guaranteed by
51 * the material compiler) after setting a value for MaterialInputs.normal.
52 */
53 void prepareMaterial(const MaterialInputs material) {
54 #if defined(HAS_ATTRIBUTE_TANGENTS)
55 #if defined(MATERIAL_HAS_NORMAL)
56 shading_normal = normalize(shading_tangentToWorld * material.normal);
57 #else
58 shading_normal = getWorldGeometricNormalVector();
59 #endif
60 shading_NoV = clampNoV(dot(shading_normal, shading_view));
61 shading_reflected = reflect(-shading_view, shading_normal);
62
63 #if defined(MATERIAL_HAS_BENT_NORMAL)
64 shading_bentNormal = normalize(shading_tangentToWorld * material.bentNormal);
65 #endif
66
67 #if defined(MATERIAL_HAS_CLEAR_COAT)
68 #if defined(MATERIAL_HAS_CLEAR_COAT_NORMAL)
69 shading_clearCoatNormal = normalize(shading_tangentToWorld * material.clearCoatNormal);
70 #else
71 shading_clearCoatNormal = getWorldGeometricNormalVector();
72 #endif
73 #endif
74 #endif
75 }