MsCommonFrag.glsl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_SHADERS_MS_COMMON_FRAG_GLSL
  6. #define ANKI_SHADERS_MS_COMMON_FRAG_GLSL
  7. #include "shaders/Pack.glsl"
  8. #include "shaders/MsFsCommon.glsl"
  9. //
  10. // Input
  11. //
  12. layout(location = 0) in highp vec2 in_uv;
  13. layout(location = 1) in mediump vec3 in_normal;
  14. layout(location = 2) in mediump vec4 in_tangent;
  15. #if CALC_BITANGENT_IN_VERT
  16. layout(location = 3) in mediump vec3 in_bitangent;
  17. #endif
  18. layout(location = 4) in mediump vec3 in_vertPosViewSpace;
  19. layout(location = 5) in mediump vec3 in_eyeTangentSpace; // Parallax
  20. layout(location = 6) in mediump vec3 in_normalTangentSpace; // Parallax
  21. //
  22. // Output
  23. //
  24. #if PASS == 0
  25. layout(location = 0) out vec4 out_msRt0;
  26. layout(location = 1) out vec4 out_msRt1;
  27. layout(location = 2) out vec4 out_msRt2;
  28. #endif
  29. // Do normal mapping
  30. vec3 readNormalFromTexture(sampler2D map, highp vec2 texCoords)
  31. {
  32. // First read the texture
  33. vec3 nAtTangentspace = normalize((texture(map, texCoords).rgb - 0.5) * 2.0);
  34. vec3 n = normalize(in_normal);
  35. vec3 t = normalize(in_tangent.xyz);
  36. #if CALC_BITANGENT_IN_VERT
  37. vec3 b = normalize(in_bitangent.xyz);
  38. #else
  39. vec3 b = cross(n, t) * in_tangent.w;
  40. #endif
  41. mat3 tbnMat = mat3(t, b, n);
  42. return tbnMat * nAtTangentspace;
  43. }
  44. // Using a 4-channel texture and a tolerance discard the fragment if the texture's alpha is less than the tolerance
  45. vec3 readTextureRgbAlphaTesting(sampler2D map, in highp vec2 texCoords, float tolerance)
  46. {
  47. vec4 col = vec4(texture(map, texCoords));
  48. if(col.a < tolerance)
  49. {
  50. discard;
  51. }
  52. return col.rgb;
  53. }
  54. vec2 computeTextureCoordParallax(in sampler2D heightMap, in vec2 uv, in float heightMapScale)
  55. {
  56. const uint MAX_SAMPLES = 25;
  57. const uint MIN_SAMPLES = 1;
  58. const float MAX_EFFECTIVE_DISTANCE = 32.0;
  59. // Get that because we are sampling inside a loop
  60. vec2 dPdx = dFdx(uv);
  61. vec2 dPdy = dFdy(uv);
  62. vec3 eyeTangentSpace = in_eyeTangentSpace;
  63. vec3 normTangentSpace = in_normalTangentSpace;
  64. float parallaxLimit = -length(eyeTangentSpace.xy) / eyeTangentSpace.z;
  65. parallaxLimit *= heightMapScale;
  66. vec2 offsetDir = normalize(eyeTangentSpace.xy);
  67. vec2 maxOffset = offsetDir * parallaxLimit;
  68. vec3 E = normalize(eyeTangentSpace);
  69. float factor0 = -dot(E, normTangentSpace);
  70. float factor1 = in_vertPosViewSpace.z / -MAX_EFFECTIVE_DISTANCE;
  71. float factor = (1.0 - factor0) * (1.0 - factor1);
  72. float sampleCountf = mix(float(MIN_SAMPLES), float(MAX_SAMPLES), factor);
  73. float stepSize = 1.0 / sampleCountf;
  74. float crntRayHeight = 1.0;
  75. vec2 crntOffset = vec2(0.0);
  76. vec2 lastOffset = vec2(0.0);
  77. float lastSampledHeight = 1.0;
  78. float crntSampledHeight = 1.0;
  79. uint crntSample = 0;
  80. uint sampleCount = uint(sampleCountf);
  81. while(crntSample < sampleCount)
  82. {
  83. crntSampledHeight = textureGrad(heightMap, uv + crntOffset, dPdx, dPdy).r;
  84. if(crntSampledHeight > crntRayHeight)
  85. {
  86. float delta1 = crntSampledHeight - crntRayHeight;
  87. float delta2 = (crntRayHeight + stepSize) - lastSampledHeight;
  88. float ratio = delta1 / (delta1 + delta2);
  89. crntOffset = mix(crntOffset, lastOffset, ratio);
  90. crntSample = sampleCount + 1;
  91. }
  92. else
  93. {
  94. crntSample++;
  95. crntRayHeight -= stepSize;
  96. lastOffset = crntOffset;
  97. crntOffset += stepSize * maxOffset;
  98. lastSampledHeight = crntSampledHeight;
  99. }
  100. }
  101. return uv + crntOffset;
  102. }
  103. // Write the data to FAIs
  104. #if PASS == 0
  105. void writeRts(in vec3 diffColor, // from 0 to 1
  106. in vec3 normal,
  107. in vec3 specularColor,
  108. in float roughness,
  109. in float subsurface,
  110. in vec3 emission,
  111. in float metallic)
  112. {
  113. GbufferInfo g;
  114. g.diffuse = diffColor;
  115. g.normal = normal;
  116. g.specular = specularColor;
  117. g.roughness = roughness;
  118. g.subsurface = subsurface;
  119. g.emission = (emission.r + emission.g + emission.b) / 3.0;
  120. g.metallic = metallic;
  121. writeGBuffer(g, out_msRt0, out_msRt1, out_msRt2);
  122. }
  123. #endif
  124. #endif