MaterialFragmentFunctions.glsl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /// @file
  2. /// The file contains common functions for fragment operations
  3. #pragma anki include "shaders/Pack.glsl"
  4. /// Generic add
  5. #define add_DEFINED
  6. #define add(a, b) (a + b)
  7. /// Generic mul
  8. #define mul_DEFINED
  9. #define mul(a, b) (a * b)
  10. /// @param[in] normal The fragment's normal in view space
  11. /// @param[in] tangent The tangent
  12. /// @param[in] tangent Extra stuff for the tangent
  13. /// @param[in] map The map
  14. /// @param[in] texCoords Texture coordinates
  15. #if defined(PASS_COLOR)
  16. # define getNormalFromTexture_DEFINED
  17. vec3 getNormalFromTexture(in vec3 normal, in vec3 tangent, in float tangentW,
  18. in sampler2D map, in vec2 texCoords)
  19. {
  20. # if LOD > 0
  21. return normalize(normal);
  22. # else
  23. vec3 n = normalize(normal);
  24. vec3 t = normalize(tangent);
  25. vec3 b = cross(n, t) * tangentW;
  26. mat3 tbnMat = mat3(t, b, n);
  27. vec3 nAtTangentspace = (texture(map, texCoords).rgb - 0.5) * 2.0;
  28. return normalize(tbnMat * nAtTangentspace);
  29. # endif
  30. }
  31. #endif
  32. /// Just normalize
  33. #if defined(PASS_COLOR)
  34. # define getNormalSimple_DEFINED
  35. vec3 getNormalSimple(in vec3 normal)
  36. {
  37. return normalize(normal);
  38. }
  39. #endif
  40. /// Environment mapping calculations
  41. /// @param[in] vertPosViewSpace Fragment position in view space
  42. /// @param[in] normal Fragment's normal in view space as well
  43. /// @param[in] map The env map
  44. /// @return The color
  45. #if defined(PASS_COLOR)
  46. # define getEnvironmentColor_DEFINED
  47. vec3 getEnvironmentColor(in vec3 vertPosViewSpace, in vec3 normal,
  48. in sampler2D map)
  49. {
  50. // In case of normal mapping I could play with vertex's normal but this
  51. // gives better results and its allready computed
  52. vec3 u = normalize(vertPosViewSpace);
  53. vec3 r = reflect(u, normal);
  54. r.z += 1.0;
  55. float m = 2.0 * length(r);
  56. vec2 semTexCoords = r.xy / m + 0.5;
  57. vec3 semCol = texture(map, semTexCoords).rgb;
  58. return semCol;
  59. }
  60. #endif
  61. /// Using a 4-channel texture and a tolerance discard the fragment if the
  62. /// texture's alpha is less than the tolerance
  63. /// @param[in] map The diffuse map
  64. /// @param[in] tolerance Tolerance value
  65. /// @param[in] texCoords Texture coordinates
  66. /// @return The RGB channels of the map
  67. #define getDiffuseColorAndDoAlphaTesting_DEFINED
  68. vec3 getDiffuseColorAndDoAlphaTesting(
  69. in sampler2D map,
  70. in vec2 texCoords,
  71. in float tolerance)
  72. {
  73. #if defined(PASS_COLOR)
  74. vec4 col = texture(map, texCoords);
  75. if(col.a < tolerance)
  76. {
  77. discard;
  78. }
  79. return col.rgb;
  80. #else // Depth
  81. # if LOD > 0
  82. return vec3(0.0);
  83. # else
  84. float a = texture(map, texCoords).a;
  85. if(a < tolerance)
  86. {
  87. discard;
  88. }
  89. return vec3(0.0);
  90. # endif
  91. #endif
  92. }
  93. /// Just read the RGB color from texture
  94. #if defined(PASS_COLOR)
  95. # define readRgbFromTexture_DEFINED
  96. vec3 readRgbFromTexture(in sampler2D tex, in vec2 texCoords)
  97. {
  98. return texture(tex, texCoords).rgb;
  99. }
  100. #endif
  101. /// Write the data to FAIs
  102. #if defined(PASS_COLOR)
  103. # define writeFais_DEFINED
  104. void writeFais(
  105. in vec3 diffCol, // Normalized
  106. in vec3 normal,
  107. in float specularComponent, // Streangth and shininess
  108. in float blurring)
  109. {
  110. // Diffuse color and specular
  111. fMsFai0[0] = packUnorm4x8(vec4(diffCol, specularComponent));
  112. // Normal
  113. fMsFai0[1] = packHalf2x16(packNormal(normal));
  114. }
  115. #endif
  116. /// Write the data to FAIs
  117. #if defined(PASS_COLOR)
  118. # define writeFaisPackSpecular_DEFINED
  119. void writeFaisPackSpecular(
  120. in vec3 diffCol, // Normalized
  121. in vec3 normal,
  122. in vec2 specular, // Streangth and shininess
  123. in float blurring)
  124. {
  125. writeFais(diffCol, normal, packSpecular(specular), blurring);
  126. }
  127. #endif