MaterialFragmentFunctions.glsl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #pragma anki include "shaders/Pack.glsl"
  2. #define MAX_SHININESS 128.0
  3. //==============================================================================
  4. // getNormalFromTexture =
  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. vec3 getNormalFromTexture(in vec3 normal, in vec3 tangent, in float tangentW,
  12. in sampler2D map, in vec2 texCoords)
  13. {
  14. #if defined(COLOR_PASS)
  15. vec3 n = normalize(normal);
  16. vec3 t = normalize(tangent);
  17. vec3 b = cross(n, t) * tangentW;
  18. mat3 tbnMat = mat3(t, b, n);
  19. vec3 nAtTangentspace = (texture(map, texCoords).rgb - 0.5) * 2.0;
  20. return normalize(tbnMat * nAtTangentspace);
  21. #else
  22. return vec3(0.0);
  23. #endif
  24. }
  25. //==============================================================================
  26. // getNormalSimple =
  27. //==============================================================================
  28. /// Just normalize
  29. vec3 getNormalSimple(in vec3 normal)
  30. {
  31. #if defined(COLOR_PASS)
  32. return normalize(normal);
  33. #else
  34. return vec3(0.0);
  35. #endif
  36. }
  37. //==============================================================================
  38. // getEnvironmentColor =
  39. //==============================================================================
  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. vec3 getEnvironmentColor(in vec3 vertPosViewSpace, in vec3 normal,
  46. in sampler2D map)
  47. {
  48. #if defined(COLOR_PASS)
  49. // In case of normal mapping I could play with vertex's normal but this
  50. // gives better results and its allready computed
  51. vec3 u = normalize(vertPosViewSpace);
  52. vec3 r = reflect(u, normal);
  53. r.z += 1.0;
  54. float m = 2.0 * length(r);
  55. vec2 semTexCoords = r.xy / m + 0.5;
  56. vec3 semCol = texture(map, semTexCoords).rgb;
  57. return semCol;
  58. #else
  59. return vec3(0.0);
  60. #endif
  61. }
  62. //==============================================================================
  63. // getDiffuseColorAndDoAlphaTesting =
  64. //==============================================================================
  65. /// Using a 4-channel texture and a tolerance discard the fragment if the
  66. /// texture's alpha is less than the tolerance
  67. /// @param[in] map The diffuse map
  68. /// @param[in] tolerance Tolerance value
  69. /// @param[in] texCoords Texture coordinates
  70. /// @return The RGB channels of the map
  71. vec3 getDiffuseColorAndDoAlphaTesting(
  72. in sampler2D map,
  73. in vec2 texCoords,
  74. in float tolerance)
  75. {
  76. #if defined(COLOR_PASS)
  77. vec4 col = texture(map, texCoords);
  78. if(col.a < tolerance)
  79. {
  80. discard;
  81. }
  82. return col.rgb;
  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. }
  92. //==============================================================================
  93. // readRgbFromTexture =
  94. //==============================================================================
  95. /// Just read the RGB color from texture
  96. vec3 readRgbFromTexture(in sampler2D tex, in vec2 texCoords)
  97. {
  98. #if defined(COLOR_PASS)
  99. return texture(tex, texCoords).rgb;
  100. #else
  101. return vec3(0.0);
  102. #endif
  103. }
  104. //==============================================================================
  105. // add2Vec3 =
  106. //==============================================================================
  107. vec3 add2Vec3(in vec3 a, in vec3 b)
  108. {
  109. return a + b;
  110. }
  111. //==============================================================================
  112. // writeFais =
  113. //==============================================================================
  114. /// XXX
  115. void writeFais(
  116. in vec3 diffCol,
  117. in vec3 normal,
  118. in vec3 specularCol,
  119. in float shininess,
  120. in float blurring)
  121. {
  122. #if defined(COLOR_PASS)
  123. fMsNormalFai = vec3(packNormal(normal), blurring);
  124. fMsDiffuseFai = diffCol;
  125. fMsSpecularFai = vec4(specularCol, shininess / MAX_SHININESS);
  126. #endif
  127. }