No Description

parallax.frag 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #version 450
  2. layout (binding = 1) uniform sampler2D sColorMap;
  3. layout (binding = 2) uniform sampler2D sNormalHeightMap;
  4. layout (binding = 3) uniform UBO
  5. {
  6. float heightScale;
  7. float parallaxBias;
  8. float numLayers;
  9. int mappingMode;
  10. } ubo;
  11. layout (location = 0) in vec2 inUV;
  12. layout (location = 1) in vec3 inTangentLightPos;
  13. layout (location = 2) in vec3 inTangentViewPos;
  14. layout (location = 3) in vec3 inTangentFragPos;
  15. layout (location = 0) out vec4 outColor;
  16. vec2 parallax_uv(vec2 uv, vec3 view_dir, int type)
  17. {
  18. if (type == 2) {
  19. // Parallax mapping
  20. float depth = 1.0 - textureLod(sNormalHeightMap, uv, 0.0).a;
  21. vec2 p = view_dir.xy * (depth * (ubo.heightScale * 0.5) + ubo.parallaxBias) / view_dir.z;
  22. return uv - p;
  23. } else {
  24. float layer_depth = 1.0 / ubo.numLayers;
  25. float cur_layer_depth = 0.0;
  26. vec2 delta_uv = view_dir.xy * ubo.heightScale / (view_dir.z * ubo.numLayers);
  27. vec2 cur_uv = uv;
  28. float depth_from_tex = 1.0 - textureLod(sNormalHeightMap, cur_uv, 0.0).a;
  29. for (int i = 0; i < 32; i++) {
  30. cur_layer_depth += layer_depth;
  31. cur_uv -= delta_uv;
  32. depth_from_tex = 1.0 - textureLod(sNormalHeightMap, cur_uv, 0.0).a;
  33. if (depth_from_tex < cur_layer_depth) {
  34. break;
  35. }
  36. }
  37. if (type == 3) {
  38. // Steep parallax mapping
  39. return cur_uv;
  40. } else {
  41. // Parallax occlusion mapping
  42. vec2 prev_uv = cur_uv + delta_uv;
  43. float next = depth_from_tex - cur_layer_depth;
  44. float prev = 1.0 - textureLod(sNormalHeightMap, prev_uv, 0.0).a - cur_layer_depth + layer_depth;
  45. float weight = next / (next - prev);
  46. return mix(cur_uv, prev_uv, weight);
  47. }
  48. }
  49. }
  50. vec2 parallaxMapping(vec2 uv, vec3 viewDir)
  51. {
  52. float height = 1.0 - textureLod(sNormalHeightMap, uv, 0.0).a;
  53. vec2 p = viewDir.xy * (height * (ubo.heightScale * 0.5) + ubo.parallaxBias) / viewDir.z;
  54. return uv - p;
  55. }
  56. vec2 steepParallaxMapping(vec2 uv, vec3 viewDir)
  57. {
  58. float layerDepth = 1.0 / ubo.numLayers;
  59. float currLayerDepth = 0.0;
  60. vec2 deltaUV = viewDir.xy * ubo.heightScale / (viewDir.z * ubo.numLayers);
  61. vec2 currUV = uv;
  62. float height = 1.0 - textureLod(sNormalHeightMap, currUV, 0.0).a;
  63. for (int i = 0; i < ubo.numLayers; i++) {
  64. currLayerDepth += layerDepth;
  65. currUV -= deltaUV;
  66. height = 1.0 - textureLod(sNormalHeightMap, currUV, 0.0).a;
  67. if (height < currLayerDepth) {
  68. break;
  69. }
  70. }
  71. return currUV;
  72. }
  73. vec2 parallaxOcclusionMapping(vec2 uv, vec3 viewDir)
  74. {
  75. float layerDepth = 1.0 / ubo.numLayers;
  76. float currLayerDepth = 0.0;
  77. vec2 deltaUV = viewDir.xy * ubo.heightScale / (viewDir.z * ubo.numLayers);
  78. vec2 currUV = uv;
  79. float height = 1.0 - textureLod(sNormalHeightMap, currUV, 0.0).a;
  80. for (int i = 0; i < ubo.numLayers; i++) {
  81. currLayerDepth += layerDepth;
  82. currUV -= deltaUV;
  83. height = 1.0 - textureLod(sNormalHeightMap, currUV, 0.0).a;
  84. if (height < currLayerDepth) {
  85. break;
  86. }
  87. }
  88. vec2 prevUV = currUV + deltaUV;
  89. float nextDepth = height - currLayerDepth;
  90. float prevDepth = 1.0 - textureLod(sNormalHeightMap, prevUV, 0.0).a - currLayerDepth + layerDepth;
  91. return mix(currUV, prevUV, nextDepth / (nextDepth - prevDepth));
  92. }
  93. void main(void)
  94. {
  95. vec3 V = normalize(inTangentViewPos - inTangentFragPos);
  96. vec2 uv = inUV;
  97. if (ubo.mappingMode == 0) {
  98. // Color only
  99. outColor = texture(sColorMap, inUV);
  100. } else {
  101. switch(ubo.mappingMode) {
  102. case 2:
  103. uv = parallaxMapping(inUV, V);
  104. break;
  105. case 3:
  106. uv = steepParallaxMapping(inUV, V);
  107. break;
  108. case 4:
  109. uv = parallaxOcclusionMapping(inUV, V);
  110. break;
  111. }
  112. // Discard fragments at texture border
  113. if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) {
  114. discard;
  115. }
  116. vec3 N = normalize(textureLod(sNormalHeightMap, uv, 0.0).rgb * 2.0 - 1.0);
  117. vec3 L = normalize(inTangentLightPos - inTangentFragPos);
  118. vec3 R = reflect(-L, N);
  119. vec3 H = normalize(L + V);
  120. vec3 color = texture(sColorMap, uv).rgb;
  121. vec3 ambient = 0.2 * color;
  122. vec3 diffuse = max(dot(L, N), 0.0) * color;
  123. vec3 specular = vec3(0.15) * pow(max(dot(N, H), 0.0), 32.0);
  124. outColor = vec4(ambient + diffuse + specular, 1.0f);
  125. }
  126. }