Advertisement
Guest User

Untitled

a guest
Mar 16th, 2022
48
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None
  1. #define NORMAL_PARALLAX
  2. // #define STEEP_PARALLAX
  3.  
  4. mat3 GetTBN();
  5. vec3 GetBumpedNormal(mat3 tbn, vec2 texcoord);
  6. vec2 ParallaxMap(mat3 tbn);
  7.  
  8. Material ProcessMaterial()
  9. {
  10. mat3 tbn = GetTBN();
  11. vec2 texCoord = ParallaxMap(tbn);
  12.  
  13. Material material;
  14. material.Base = getTexel(texCoord);
  15. material.Normal = GetBumpedNormal(tbn, texCoord);
  16. #if defined(SPECULAR)
  17. material.Specular = texture(speculartexture, texCoord).rgb;
  18. material.Glossiness = uSpecularMaterial.x;
  19. material.SpecularLevel = uSpecularMaterial.y;
  20. #endif
  21. #if defined(PBR)
  22. material.Metallic = texture(metallictexture, texCoord).r;
  23. material.Roughness = texture(roughnesstexture, texCoord).r;
  24. material.AO = texture(aotexture, texCoord).r;
  25. #endif
  26. #if defined(BRIGHTMAP)
  27. material.Bright = texture(brighttexture, texCoord);
  28. #endif
  29. return material;
  30. }
  31.  
  32. // Tangent/bitangent/normal space to world space transform matrix
  33. mat3 GetTBN()
  34. {
  35. vec3 n = normalize(vWorldNormal.xyz);
  36. vec3 p = pixelpos.xyz;
  37. vec2 uv = vTexCoord.st;
  38.  
  39. // get edge vectors of the pixel triangle
  40. vec3 dp1 = dFdx(p);
  41. vec3 dp2 = dFdy(p);
  42. vec2 duv1 = dFdx(uv);
  43. vec2 duv2 = dFdy(uv);
  44.  
  45. // solve the linear system
  46. vec3 dp2perp = cross(n, dp2); // cross(dp2, n);
  47. vec3 dp1perp = cross(dp1, n); // cross(n, dp1);
  48. vec3 t = dp2perp * duv1.x + dp1perp * duv2.x;
  49. vec3 b = dp2perp * duv1.y + dp1perp * duv2.y;
  50.  
  51. // construct a scale-invariant frame
  52. float invmax = inversesqrt(max(dot(t,t), dot(b,b)));
  53. return mat3(t * invmax, b * invmax, n);
  54. }
  55.  
  56. vec3 GetBumpedNormal(mat3 tbn, vec2 texcoord)
  57. {
  58. #if defined(NORMALMAP)
  59. vec3 map = texture(normaltexture, texcoord).xyz;
  60. map = map * 255./127. - 128./127.; // Math so "odd" because 0.5 cannot be precisely described in an unsigned format
  61. map.xy *= vec2(0.5, -0.5); // Make normal map less strong and flip Y
  62. return normalize(tbn * map);
  63. #else
  64. return normalize(vWorldNormal.xyz);
  65. #endif
  66. }
  67.  
  68. float GetDisplacementAt(vec2 currentTexCoords)
  69. {
  70. return 0.5 - texture(displacement, currentTexCoords).r;
  71. }
  72.  
  73. #if defined(NORMAL_PARALLAX)
  74. vec2 ParallaxMap(mat3 tbn)
  75. {
  76. const float parallaxScale = 0.15;
  77.  
  78. // Calculate fragment view direction in tangent space
  79. mat3 invTBN = transpose(tbn);
  80. vec3 V = normalize(invTBN * (uCameraPos.xyz - pixelpos.xyz));
  81.  
  82. vec2 texCoords = vTexCoord.st;
  83. vec2 p = V.xy / abs(V.z) * GetDisplacementAt(texCoords) * parallaxScale;
  84. return texCoords - p;
  85. }
  86. #elif defined(STEEP_PARALLAX)
  87. vec2 ParallaxMap(mat3 tbn)
  88. {
  89. const float parallaxScale = 0.2;
  90. const float minLayers = 8.0;
  91. const float maxLayers = 16.0;
  92.  
  93. // Calculate fragment view direction in tangent space
  94. mat3 invTBN = transpose(tbn);
  95. vec3 V = normalize(invTBN * (uCameraPos.xyz - pixelpos.xyz));
  96. vec2 T = vTexCoord.st;
  97.  
  98. float numLayers = mix(maxLayers, minLayers, clamp(abs(V.z), 0.0, 1.0)); // clamp is required due to precision loss
  99.  
  100. // calculate the size of each layer
  101. float layerDepth = 1.0 / numLayers;
  102.  
  103. // depth of current layer
  104. float currentLayerDepth = 0.0;
  105.  
  106. // the amount to shift the texture coordinates per layer (from vector P)
  107. vec2 P = V.xy * parallaxScale;
  108. vec2 deltaTexCoords = P / numLayers;
  109. vec2 currentTexCoords = T;
  110. float currentDepthMapValue = GetDisplacementAt(currentTexCoords);
  111.  
  112. while (currentLayerDepth < currentDepthMapValue)
  113. {
  114. // shift texture coordinates along direction of P
  115. currentTexCoords -= deltaTexCoords;
  116.  
  117. // get depthmap value at current texture coordinates
  118. currentDepthMapValue = GetDisplacementAt(currentTexCoords);
  119.  
  120. // get depth of next layer
  121. currentLayerDepth += layerDepth;
  122. }
  123.  
  124. // get texture coordinates before collision (reverse operations)
  125. vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
  126.  
  127. // get depth after and before collision for linear interpolation
  128. float afterDepth = currentDepthMapValue - currentLayerDepth;
  129. float beforeDepth = GetDisplacementAt(prevTexCoords) - currentLayerDepth + layerDepth;
  130.  
  131. // interpolation of texture coordinates
  132. float weight = afterDepth / (afterDepth - beforeDepth);
  133. vec2 finalTexCoords = prevTexCoords * weight + currentTexCoords * (1.0 - weight);
  134.  
  135. return finalTexCoords;
  136. }
  137. #endif
Advertisement
RAW Paste Data Copied
Advertisement