Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. mat3 GetTBN();
  2. vec3 GetBumpedNormal(mat3 tbn, vec2 texcoord);
  3.  
  4. Material ProcessMaterial()
  5. {
  6. mat3 tbn = GetTBN();
  7. vec2 texCoord = vTexCoord.st;
  8.  
  9. Material material;
  10. vec4 addEnv = texture(displacement, (normalize(transpose(tbn) * (uCameraPos.xyz - pixelpos.xyz)).xy) + (texture(normaltexture, texCoord).xy * 0.2));
  11.  
  12. addEnv *= texture(envmask, texCoord);
  13.  
  14. material.Base = getTexel(texCoord) + addEnv;
  15.  
  16. material.Normal = GetBumpedNormal(tbn, texCoord);
  17.  
  18.  
  19. #if defined(SPECULAR)
  20. material.Specular = texture(speculartexture, texCoord).rgb;
  21. material.Glossiness = uSpecularMaterial.x;
  22. material.SpecularLevel = uSpecularMaterial.y;
  23. #endif
  24. #if defined(PBR)
  25. material.Metallic = texture(metallictexture, texCoord).r;
  26. material.Roughness = texture(roughnesstexture, texCoord).r;
  27. material.AO = texture(aotexture, texCoord).r;
  28. #endif
  29. #if defined(BRIGHTMAP)
  30. material.Bright = texture(brighttexture, texCoord);
  31. #endif
  32. return material;
  33. }
  34.  
  35. // Tangent/bitangent/normal space to world space transform matrix
  36. mat3 GetTBN()
  37. {
  38. vec3 n = normalize(vWorldNormal.xyz);
  39. vec3 p = pixelpos.xyz;
  40. vec2 uv = vTexCoord.st;
  41.  
  42. // get edge vectors of the pixel triangle
  43. vec3 dp1 = dFdx(p);
  44. vec3 dp2 = dFdy(p);
  45. vec2 duv1 = dFdx(uv);
  46. vec2 duv2 = dFdy(uv);
  47.  
  48. // solve the linear system
  49. vec3 dp2perp = cross(n, dp2); // cross(dp2, n);
  50. vec3 dp1perp = cross(dp1, n); // cross(n, dp1);
  51. vec3 t = dp2perp * duv1.x + dp1perp * duv2.x;
  52. vec3 b = dp2perp * duv1.y + dp1perp * duv2.y;
  53.  
  54. // construct a scale-invariant frame
  55. float invmax = inversesqrt(max(dot(t,t), dot(b,b)));
  56. return mat3(t * invmax, b * invmax, n);
  57. }
  58.  
  59. vec3 GetBumpedNormal(mat3 tbn, vec2 texcoord)
  60. {
  61. #if defined(NORMALMAP)
  62. vec3 map = texture(normaltexture, texcoord).xyz;
  63. map = map * 255./127. - 128./127.; // Math so "odd" because 0.5 cannot be precisely described in an unsigned format
  64. map.xy *= vec2(0.5, -0.5); // Make normal map less strong and flip Y
  65. return normalize(tbn * map);
  66. #else
  67. return normalize(vWorldNormal.xyz);
  68. #endif
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement