MaterialFragmentFunctions.glsl 3.6 KB

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