説明なし

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #version 450
  2. layout (location = 0) in vec3 inWorldPos;
  3. layout (location = 1) in vec3 inNormal;
  4. layout (set = 0, binding = 0) uniform UBO
  5. {
  6. mat4 projection;
  7. mat4 model;
  8. mat4 view;
  9. vec3 camPos;
  10. } ubo;
  11. // Inline uniform block
  12. layout (set = 1, binding = 0) uniform UniformInline {
  13. float roughness;
  14. float metallic;
  15. float r;
  16. float g;
  17. float b;
  18. float ambient;
  19. } material;
  20. layout (location = 0) out vec4 outColor;
  21. const float PI = 3.14159265359;
  22. vec3 materialcolor()
  23. {
  24. return vec3(material.r, material.g, material.b);
  25. }
  26. // Normal Distribution function --------------------------------------
  27. float D_GGX(float dotNH, float roughness)
  28. {
  29. float alpha = roughness * roughness;
  30. float alpha2 = alpha * alpha;
  31. float denom = dotNH * dotNH * (alpha2 - 1.0) + 1.0;
  32. return (alpha2)/(PI * denom*denom);
  33. }
  34. // Geometric Shadowing function --------------------------------------
  35. float G_SchlicksmithGGX(float dotNL, float dotNV, float roughness)
  36. {
  37. float r = (roughness + 1.0);
  38. float k = (r*r) / 8.0;
  39. float GL = dotNL / (dotNL * (1.0 - k) + k);
  40. float GV = dotNV / (dotNV * (1.0 - k) + k);
  41. return GL * GV;
  42. }
  43. // Fresnel function ----------------------------------------------------
  44. vec3 F_Schlick(float cosTheta, float metallic)
  45. {
  46. vec3 F0 = mix(vec3(0.04), materialcolor(), metallic); // * material.specular
  47. vec3 F = F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
  48. return F;
  49. }
  50. // Specular BRDF composition --------------------------------------------
  51. vec3 BRDF(vec3 L, vec3 V, vec3 N, float metallic, float roughness)
  52. {
  53. // Precalculate vectors and dot products
  54. vec3 H = normalize (V + L);
  55. float dotNV = clamp(dot(N, V), 0.0, 1.0);
  56. float dotNL = clamp(dot(N, L), 0.0, 1.0);
  57. float dotLH = clamp(dot(L, H), 0.0, 1.0);
  58. float dotNH = clamp(dot(N, H), 0.0, 1.0);
  59. // Light color fixed
  60. vec3 lightColor = vec3(1.0);
  61. vec3 color = vec3(0.0);
  62. if (dotNL > 0.0)
  63. {
  64. float rroughness = max(0.05, roughness);
  65. // D = Normal distribution (Distribution of the microfacets)
  66. float D = D_GGX(dotNH, rroughness);
  67. // G = Geometric shadowing term (Microfacets shadowing)
  68. float G = G_SchlicksmithGGX(dotNL, dotNV, rroughness);
  69. // F = Fresnel factor (Reflectance depending on angle of incidence)
  70. vec3 F = F_Schlick(dotNV, metallic);
  71. vec3 spec = D * F * G / (4.0 * dotNL * dotNV);
  72. color += spec * dotNL * lightColor;
  73. }
  74. return color;
  75. }
  76. // ----------------------------------------------------------------------------
  77. void main()
  78. {
  79. vec3 N = normalize(inNormal);
  80. vec3 V = normalize(ubo.camPos - inWorldPos);
  81. float roughness = material.roughness;
  82. // Specular contribution
  83. vec3 lightPos = vec3(0.0f, 0.0f, 10.0f);
  84. vec3 Lo = vec3(0.0);
  85. vec3 L = normalize(lightPos.xyz - inWorldPos);
  86. Lo += BRDF(L, V, N, material.metallic, roughness);
  87. // Combine with ambient
  88. vec3 color = materialcolor() * material.ambient;
  89. color += Lo;
  90. // Gamma correct
  91. color = pow(color, vec3(0.4545));
  92. outColor = vec4(color, 1.0);
  93. }