You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
10 KiB

  1. #version 330
  2. #define MAX_REFLECTION_LOD 4.0
  3. #define MAX_DEPTH_LAYER 20
  4. #define MIN_DEPTH_LAYER 10
  5. #define MAX_LIGHTS 4
  6. #define LIGHT_DIRECTIONAL 0
  7. #define LIGHT_POINT 1
  8. struct MaterialProperty {
  9. vec3 color;
  10. int useSampler;
  11. sampler2D sampler;
  12. };
  13. struct Light {
  14. int enabled;
  15. int type;
  16. vec3 position;
  17. vec3 target;
  18. vec4 color;
  19. };
  20. // Input vertex attributes (from vertex shader)
  21. in vec3 fragPosition;
  22. in vec2 fragTexCoord;
  23. in vec3 fragNormal;
  24. in vec3 fragTangent;
  25. in vec3 fragBinormal;
  26. // Input material values
  27. uniform MaterialProperty albedo;
  28. uniform MaterialProperty normals;
  29. uniform MaterialProperty metalness;
  30. uniform MaterialProperty roughness;
  31. uniform MaterialProperty occlusion;
  32. uniform MaterialProperty emission;
  33. uniform MaterialProperty height;
  34. // Input uniform values
  35. uniform samplerCube irradianceMap;
  36. uniform samplerCube prefilterMap;
  37. uniform sampler2D brdfLUT;
  38. // Input lighting values
  39. uniform Light lights[MAX_LIGHTS];
  40. // Other uniform values
  41. uniform int renderMode;
  42. uniform vec3 viewPos;
  43. vec2 texCoord;
  44. // Constant values
  45. const float PI = 3.14159265359;
  46. // Output fragment color
  47. out vec4 finalColor;
  48. vec3 ComputeMaterialProperty(MaterialProperty property);
  49. float DistributionGGX(vec3 N, vec3 H, float roughness);
  50. float GeometrySchlickGGX(float NdotV, float roughness);
  51. float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness);
  52. vec3 fresnelSchlick(float cosTheta, vec3 F0);
  53. vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness);
  54. vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir);
  55. // WARNING: There is some weird behaviour with this function, always returns black!
  56. // Yes, I even tried: return texture(property.sampler, texCoord).rgb;
  57. vec3 ComputeMaterialProperty(MaterialProperty property)
  58. {
  59. vec3 result = vec3(0.0, 0.0, 0.0);
  60. if (property.useSampler == 1) result = texture(property.sampler, texCoord).rgb;
  61. else result = property.color;
  62. return result;
  63. }
  64. float DistributionGGX(vec3 N, vec3 H, float roughness)
  65. {
  66. float a = roughness*roughness;
  67. float a2 = a*a;
  68. float NdotH = max(dot(N, H), 0.0);
  69. float NdotH2 = NdotH*NdotH;
  70. float nom = a2;
  71. float denom = (NdotH2*(a2 - 1.0) + 1.0);
  72. denom = PI*denom*denom;
  73. return nom/denom;
  74. }
  75. float GeometrySchlickGGX(float NdotV, float roughness)
  76. {
  77. float r = (roughness + 1.0);
  78. float k = r*r/8.0;
  79. float nom = NdotV;
  80. float denom = NdotV*(1.0 - k) + k;
  81. return nom/denom;
  82. }
  83. float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
  84. {
  85. float NdotV = max(dot(N, V), 0.0);
  86. float NdotL = max(dot(N, L), 0.0);
  87. float ggx2 = GeometrySchlickGGX(NdotV, roughness);
  88. float ggx1 = GeometrySchlickGGX(NdotL, roughness);
  89. return ggx1*ggx2;
  90. }
  91. vec3 fresnelSchlick(float cosTheta, vec3 F0)
  92. {
  93. return F0 + (1.0 - F0)*pow(1.0 - cosTheta, 5.0);
  94. }
  95. vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness)
  96. {
  97. return F0 + (max(vec3(1.0 - roughness), F0) - F0)*pow(1.0 - cosTheta, 5.0);
  98. }
  99. vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir)
  100. {
  101. // Calculate the number of depth layers and calculate the size of each layer
  102. float numLayers = mix(MAX_DEPTH_LAYER, MIN_DEPTH_LAYER, abs(dot(vec3(0.0, 0.0, 1.0), viewDir)));
  103. float layerDepth = 1.0/numLayers;
  104. // Calculate depth of current layer
  105. float currentLayerDepth = 0.0;
  106. // Calculate the amount to shift the texture coordinates per layer (from vector P)
  107. // Note: height amount is stored in height material attribute color R channel (sampler use is independent)
  108. vec2 P = viewDir.xy*height.color.r;
  109. vec2 deltaTexCoords = P/numLayers;
  110. // Store initial texture coordinates and depth values
  111. vec2 currentTexCoords = texCoords;
  112. float currentDepthMapValue = texture(height.sampler, currentTexCoords).r;
  113. while (currentLayerDepth < currentDepthMapValue)
  114. {
  115. // Shift texture coordinates along direction of P
  116. currentTexCoords -= deltaTexCoords;
  117. // Get depth map value at current texture coordinates
  118. currentDepthMapValue = texture(height.sampler, currentTexCoords).r;
  119. // Get depth of next layer
  120. currentLayerDepth += layerDepth;
  121. }
  122. // Get texture coordinates before collision (reverse operations)
  123. vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
  124. // Get depth after and before collision for linear interpolation
  125. float afterDepth = currentDepthMapValue - currentLayerDepth;
  126. float beforeDepth = texture(height.sampler, prevTexCoords).r - currentLayerDepth + layerDepth;
  127. // Interpolation of texture coordinates
  128. float weight = afterDepth/(afterDepth - beforeDepth);
  129. vec2 finalTexCoords = prevTexCoords*weight + currentTexCoords*(1.0 - weight);
  130. return finalTexCoords;
  131. }
  132. void main()
  133. {
  134. // Calculate TBN and RM matrices
  135. mat3 TBN = transpose(mat3(fragTangent, fragBinormal, fragNormal));
  136. // Calculate lighting required attributes
  137. vec3 normal = normalize(fragNormal);
  138. vec3 view = normalize(viewPos - fragPosition);
  139. vec3 refl = reflect(-view, normal);
  140. // Check if parallax mapping is enabled and calculate texture coordinates to use based on height map
  141. // NOTE: remember that 'texCoord' variable must be assigned before calling any ComputeMaterialProperty() function
  142. if (height.useSampler == 1) texCoord = ParallaxMapping(fragTexCoord, view);
  143. else texCoord = fragTexCoord; // Use default texture coordinates
  144. // Fetch material values from texture sampler or color attributes
  145. vec3 color = texture(albedo.sampler, texCoord).rgb; //ComputeMaterialProperty(albedo);
  146. vec3 metal = texture(metalness.sampler, texCoord).rgb; //ComputeMaterialProperty(metalness);
  147. vec3 rough = texture(roughness.sampler, texCoord).rgb; //ComputeMaterialProperty(roughness);
  148. vec3 emiss = texture(emission.sampler, texCoord).rgb; //ComputeMaterialProperty(emission);
  149. vec3 ao = texture(occlusion.sampler, texCoord).rgb; //ComputeMaterialProperty(occlusion);
  150. // Check if normal mapping is enabled
  151. if (normals.useSampler == 1)
  152. {
  153. // Fetch normal map color and transform lighting values to tangent space
  154. normal = texture(normals.sampler, texCoord).rgb; //ComputeMaterialProperty(normals);
  155. normal = normalize(normal*2.0 - 1.0);
  156. normal = normalize(normal*TBN);
  157. // Convert tangent space normal to world space due to cubemap reflection calculations
  158. refl = normalize(reflect(-view, normal));
  159. }
  160. // Calculate reflectance at normal incidence
  161. vec3 F0 = vec3(0.04);
  162. F0 = mix(F0, color, metal.r);
  163. // Calculate lighting for all lights
  164. vec3 Lo = vec3(0.0);
  165. vec3 lightDot = vec3(0.0);
  166. for (int i = 0; i < MAX_LIGHTS; i++)
  167. {
  168. if (lights[i].enabled == 1)
  169. {
  170. // Calculate per-light radiance
  171. vec3 light = vec3(0.0);
  172. vec3 radiance = lights[i].color.rgb;
  173. if (lights[i].type == LIGHT_DIRECTIONAL) light = -normalize(lights[i].target - lights[i].position);
  174. else if (lights[i].type == LIGHT_POINT)
  175. {
  176. light = normalize(lights[i].position - fragPosition);
  177. float distance = length(lights[i].position - fragPosition);
  178. float attenuation = 1.0/(distance*distance);
  179. radiance *= attenuation;
  180. }
  181. // Cook-torrance BRDF
  182. vec3 high = normalize(view + light);
  183. float NDF = DistributionGGX(normal, high, rough.r);
  184. float G = GeometrySmith(normal, view, light, rough.r);
  185. vec3 F = fresnelSchlick(max(dot(high, view), 0.0), F0);
  186. vec3 nominator = NDF*G*F;
  187. float denominator = 4*max(dot(normal, view), 0.0)*max(dot(normal, light), 0.0) + 0.001;
  188. vec3 brdf = nominator/denominator;
  189. // Store to kS the fresnel value and calculate energy conservation
  190. vec3 kS = F;
  191. vec3 kD = vec3(1.0) - kS;
  192. // Multiply kD by the inverse metalness such that only non-metals have diffuse lighting
  193. kD *= 1.0 - metal.r;
  194. // Scale light by dot product between normal and light direction
  195. float NdotL = max(dot(normal, light), 0.0);
  196. // Add to outgoing radiance Lo
  197. // Note: BRDF is already multiplied by the Fresnel so it doesn't need to be multiplied again
  198. Lo += (kD*color/PI + brdf)*radiance*NdotL*lights[i].color.a;
  199. lightDot += radiance*NdotL + brdf*lights[i].color.a;
  200. }
  201. }
  202. // Calculate ambient lighting using IBL
  203. vec3 F = fresnelSchlickRoughness(max(dot(normal, view), 0.0), F0, rough.r);
  204. vec3 kS = F;
  205. vec3 kD = 1.0 - kS;
  206. kD *= 1.0 - metal.r;
  207. // Calculate indirect diffuse
  208. vec3 irradiance = texture(irradianceMap, fragNormal).rgb;
  209. vec3 diffuse = color*irradiance;
  210. // Sample both the prefilter map and the BRDF lut and combine them together as per the Split-Sum approximation
  211. vec3 prefilterColor = textureLod(prefilterMap, refl, rough.r*MAX_REFLECTION_LOD).rgb;
  212. vec2 brdf = texture(brdfLUT, vec2(max(dot(normal, view), 0.0), rough.r)).rg;
  213. vec3 reflection = prefilterColor*(F*brdf.x + brdf.y);
  214. // Calculate final lighting
  215. vec3 ambient = (kD*diffuse + reflection)*ao;
  216. // Calculate fragment color based on render mode
  217. vec3 fragmentColor = ambient + Lo + emiss; // Physically Based Rendering
  218. if (renderMode == 1) fragmentColor = color; // Albedo
  219. else if (renderMode == 2) fragmentColor = normal; // Normals
  220. else if (renderMode == 3) fragmentColor = metal; // Metalness
  221. else if (renderMode == 4) fragmentColor = rough; // Roughness
  222. else if (renderMode == 5) fragmentColor = ao; // Ambient Occlusion
  223. else if (renderMode == 6) fragmentColor = emiss; // Emission
  224. else if (renderMode == 7) fragmentColor = lightDot; // Lighting
  225. else if (renderMode == 8) fragmentColor = kS; // Fresnel
  226. else if (renderMode == 9) fragmentColor = irradiance; // Irradiance
  227. else if (renderMode == 10) fragmentColor = reflection; // Reflection
  228. // Apply HDR tonemapping
  229. fragmentColor = fragmentColor/(fragmentColor + vec3(1.0));
  230. // Apply gamma correction
  231. fragmentColor = pow(fragmentColor, vec3(1.0/2.2));
  232. // Calculate final fragment color
  233. finalColor = vec4(fragmentColor, 1.0);
  234. }