Debian Codegraph @

src/scene/glsl/source/lighting_model_physical/shading_phong.fs

raw
1 /*
2 Copyright 2020-2020 Michalis Kamburelis and glTF-Sample-Viewer authors.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14
15 Calculate Physical (PBR) lighting model, in Phong shading. */
16
17 uniform vec4 castle_MaterialBaseAlpha;
18 uniform vec3 castle_MaterialEmissive;
19 uniform float castle_MaterialMetallic;
20 uniform float castle_MaterialRoughness;
21
22 /* ---------------------------------------------------------------------------
23 Code below is adapted from glTF-Sample-Viewer ( Apache License 2.0 )
24 https://github.com/KhronosGroup/glTF-Sample-Viewer/
25 In particular src/shaders/metallic-roughness.frag .
26 References there:
27
28 // This fragment shader defines a reference implementation for Physically Based Shading of
29 // a microfacet surface material defined by a glTF model.
30 //
31 // References:
32 // [1] Real Shading in Unreal Engine 4
33 // http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
34 // [2] Physically Based Shading at Disney
35 // http://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_notes_v3.pdf
36 // [3] README.md - Environment Maps
37 // https://github.com/KhronosGroup/glTF-WebGL-PBR/#environment-maps
38 // [4] "An Inexpensive BRDF Model for Physically based Rendering" by Christophe Schlick
39 // https://www.cs.virginia.edu/~jdl/bib/appearance/analytic%20models/schlick94b.pdf
40
41 */
42
43 const float M_PI = 3.141592653589793;
44
45 // Calculate AngularInfo structure
46 // pointToLight is assumed to be already normalized.
47 AngularInfo getAngularInfo(const in vec3 pointToLight, const in vec3 normal, const in vec3 view)
48 {
49 // Standard one-letter names
50 vec3 n = normalize(normal); // Outward direction of surface point
51 vec3 v = normalize(view); // Direction from surface point to view
52 vec3 l = /*normalize*/ (pointToLight); // Direction from surface point to light
53 vec3 h = normalize(l + v); // Direction of the vector between l and v
54
55 float NdotL = clamp(dot(n, l), 0.0, 1.0);
56 float NdotV = clamp(dot(n, v), 0.0, 1.0);
57 float NdotH = clamp(dot(n, h), 0.0, 1.0);
58 float LdotH = clamp(dot(l, h), 0.0, 1.0);
59 float VdotH = clamp(dot(v, h), 0.0, 1.0);
60
61 return AngularInfo(
62 NdotL,
63 NdotV,
64 NdotH,
65 LdotH,
66 VdotH,
67 vec3(0, 0, 0)
68 );
69 }
70
71 // Lambert lighting
72 // see https://seblagarde.wordpress.com/2012/01/08/pi-or-not-to-pi-in-game-lighting-equation/
73 vec3 diffuse(const in MaterialInfo materialInfo)
74 {
75 return materialInfo.diffuseColor / M_PI;
76 }
77
78 // The following equation models the Fresnel reflectance term of the spec equation (aka F())
79 // Implementation of fresnel from [4], Equation 15
80 vec3 specularReflection(const in MaterialInfo materialInfo, const in AngularInfo angularInfo)
81 {
82 return materialInfo.reflectance0 + (materialInfo.reflectance90 - materialInfo.reflectance0) * pow(clamp(1.0 - angularInfo.VdotH, 0.0, 1.0), 5.0);
83 }
84
85 // Smith Joint GGX
86 // Note: Vis = G / (4 * NdotL * NdotV)
87 // see Eric Heitz. 2014. Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs. Journal of Computer Graphics Techniques, 3
88 // see Real-Time Rendering. Page 331 to 336.
89 // see https://google.github.io/filament/Filament.md.html#materialsystem/specularbrdf/geometricshadowing(specularg)
90 float visibilityOcclusion(const in MaterialInfo materialInfo, const in AngularInfo angularInfo)
91 {
92 float NdotL = angularInfo.NdotL;
93 float NdotV = angularInfo.NdotV;
94 float alphaRoughnessSq = materialInfo.alphaRoughness * materialInfo.alphaRoughness;
95
96 float GGXV = NdotL * sqrt(NdotV * NdotV * (1.0 - alphaRoughnessSq) + alphaRoughnessSq);
97 float GGXL = NdotV * sqrt(NdotL * NdotL * (1.0 - alphaRoughnessSq) + alphaRoughnessSq);
98
99 float GGX = GGXV + GGXL;
100 if (GGX > 0.0)
101 {
102 return 0.5 / GGX;
103 }
104 return 0.0;
105 }
106
107 // The following equation(s) model the distribution of microfacet normals across the area being drawn (aka D())
108 // Implementation from "Average Irregularity Representation of a Roughened Surface for Ray Reflection" by T. S. Trowbridge, and K. P. Reitz
109 // Follows the distribution function recommended in the SIGGRAPH 2013 course notes from EPIC Games [1], Equation 3.
110 float microfacetDistribution(const in MaterialInfo materialInfo, const in AngularInfo angularInfo)
111 {
112 float alphaRoughnessSq = materialInfo.alphaRoughness * materialInfo.alphaRoughness;
113 float f = (angularInfo.NdotH * alphaRoughnessSq - angularInfo.NdotH) * angularInfo.NdotH + 1.0;
114 return alphaRoughnessSq / (M_PI * f * f);
115 }
116
117 /* Get PhysicalMaterial properies.
118 Matches glTF logic for metallic-roughness model.
119 */
120 MaterialInfo getPhysicalMaterialInfo(const in vec4 material_base_alpha)
121 {
122 // Metallic and Roughness material properties are packed together
123 // In glTF, these factors can be specified by fixed scalar values
124 // or from a metallic-roughness map
125 float perceptualRoughness = castle_MaterialRoughness;
126 float metallic = castle_MaterialMetallic;
127 vec3 diffuseColor = vec3(0.0);
128 vec3 specularColor = vec3(0.0);
129 vec3 f0 = vec3(0.04);
130
131 /* PLUG: material_metallic_roughness (metallic, perceptualRoughness) */
132
133 diffuseColor = material_base_alpha.rgb * (vec3(1.0) - f0) * (1.0 - metallic);
134
135 specularColor = mix(f0, material_base_alpha.rgb, metallic);
136
137 perceptualRoughness = clamp(perceptualRoughness, 0.0, 1.0);
138 metallic = clamp(metallic, 0.0, 1.0);
139
140 // Roughness is authored as perceptual roughness; as is convention,
141 // convert to material roughness by squaring the perceptual roughness [2].
142 float alphaRoughness = perceptualRoughness * perceptualRoughness;
143
144 // Compute reflectance.
145 float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);
146
147 vec3 specularEnvironmentR0 = specularColor.rgb;
148 // Anything less than 2% is physically impossible and is instead considered to be shadowing. Compare to "Real-Time-Rendering" 4th editon on page 325.
149 vec3 specularEnvironmentR90 = vec3(clamp(reflectance * 50.0, 0.0, 1.0));
150
151 return MaterialInfo(
152 perceptualRoughness,
153 specularEnvironmentR0,
154 alphaRoughness,
155 diffuseColor,
156 specularEnvironmentR90,
157 specularColor
158 );
159 }
160
161 /* ---------------------------------------------------------------------------
162 End of code adapted from glTF-Sample-Viewer ( Apache License 2.0 )
163 */
164
165 void calculate_lighting(out vec4 result, const in vec4 vertex_eye, const in vec3 normal_eye)
166 {
167 /* Calculated color from
168 Material.baseColor/transparency (or ColorRGBA node) * base texture.
169 Contains complete "base/transparency" information that is independent of light source.
170 In case of Gouraud shading it is not multiplied by the base texture
171 (because it cannot be, as we're on vertex shader). */
172 vec4 material_base_alpha;
173
174 material_base_alpha = castle_apply_color_per_vertex(castle_MaterialBaseAlpha);
175
176 main_texture_apply(material_base_alpha, normal_eye);
177
178 result = vec4(0.0, 0.0, 0.0, material_base_alpha.a);
179
180 vec3 view = normalize(-vec3(vertex_eye));
181 MaterialInfo material_info = getPhysicalMaterialInfo(material_base_alpha);
182 /* PLUG: add_light (result, vertex_eye, normal_eye, material_info, view) */
183
184 /* PLUG: material_occlusion (result) */
185
186 vec3 emissive = castle_MaterialEmissive;
187 /* PLUG: material_emissive (emissive) */
188 result.rgb += emissive;
189
190 // TODO: No need for this in PBR?
191 // TODO: No need for this in Phong lighting with Phong shading, too?
192 // result.rgb = min(result.rgb, 1.0);
193 }