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