geometryPass_optimized.frag 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #version 430
  2. #pragma debug(on)
  3. //#extension GL_NV_shadow_samplers_cube : enable
  4. #extension GL_ARB_bindless_texture: require
  5. layout(location = 0) out ivec3 a_normal;
  6. layout(location = 1) out ivec4 a_textureDerivates;
  7. layout(location = 2) out vec3 a_posViewSpace;
  8. layout(location = 3) out int a_materialIndex;
  9. layout(location = 4) out vec4 a_textureUV;
  10. in vec3 v_normals;
  11. in vec2 v_texCoord;
  12. in vec3 v_positionViewSpace;
  13. uniform sampler2D u_normalSampler;
  14. //todo remove uniform if possible
  15. //todo add another uniform to do this computation on the cpu if(albedoSampler.x == 0 && albedoSampler.y == 0)
  16. uniform int u_materialIndex;
  17. struct MaterialStruct
  18. {
  19. vec4 kd;
  20. vec4 rma; //last component emmisive
  21. //float kdr; //= 1;
  22. //float kdg; //= 1;
  23. //float kdb; //= 1;
  24. //float roughness;
  25. //float metallic;
  26. //float ao; //one means full light
  27. uvec4 firstBIndlessSamplers; // xy albedoSampler, zw rmaSampler
  28. uvec2 secondBIndlessSamplers; // xy emmissiveSampler
  29. int rmaLoaded;
  30. int notUsed;
  31. };
  32. readonly layout(std140) buffer u_material
  33. {
  34. MaterialStruct mat[];
  35. };
  36. float PI = 3.14159265359;
  37. //https://gamedev.stackexchange.com/questions/22204/from-normal-to-rotation-matrix#:~:text=Therefore%2C%20if%20you%20want%20to,the%20first%20and%20second%20columns.
  38. mat3x3 NormalToRotation(in vec3 normal)
  39. {
  40. // Find a vector in the plane
  41. vec3 tangent0 = cross(normal, vec3(1, 0, 0));
  42. if (dot(tangent0, tangent0) < 0.001)
  43. tangent0 = cross(normal, vec3(0, 1, 0));
  44. tangent0 = normalize(tangent0);
  45. // Find another vector in the plane
  46. vec3 tangent1 = normalize(cross(normal, tangent0));
  47. // Construct a 3x3 matrix by storing three vectors in the columns of the matrix
  48. return mat3x3(tangent0,tangent1,normal);
  49. //return ColumnVectorsToMatrix(tangent0, tangent1, normal);
  50. }
  51. subroutine vec3 GetNormalMapFunc(vec3);
  52. subroutine (GetNormalMapFunc) vec3 normalMapped(vec3 v)
  53. {
  54. vec3 normal = texture2D(u_normalSampler, v_texCoord).rgb;
  55. normal = normalize(2*normal - 1.f);
  56. mat3 rotMat = NormalToRotation(v);
  57. normal = rotMat * normal;
  58. normal = normalize(normal);
  59. return normal;
  60. }
  61. subroutine (GetNormalMapFunc) vec3 noNormalMapped(vec3 v)
  62. {
  63. return v;
  64. }
  65. subroutine uniform GetNormalMapFunc getNormalMapFunc;
  66. int fromFloat2TouShort(float a)
  67. {
  68. //[-2 2] -> [0 4]
  69. a += 2.f;
  70. //[0 4] -> [0 1]
  71. a /= 4.f;
  72. //[0 1] -> [0 65536]
  73. a *= 65536;
  74. return int(a);
  75. }
  76. ivec3 fromFloatTouShort(vec3 a)
  77. {
  78. //[-1 1] -> [0 2]
  79. a += 1.f;
  80. //[0 2] -> [0 1]
  81. a /= 2.f;
  82. //[0 1] -> [0 65536]
  83. a *= 65536;
  84. return ivec3(a);
  85. }
  86. void main()
  87. {
  88. //vec4 color = u_getAlbedo(); //texture color
  89. //if(color.a < 0.1)discard;
  90. uvec2 albedoSampler = mat[u_materialIndex].firstBIndlessSamplers.xy;
  91. if(albedoSampler.x == 0 && albedoSampler.y == 0)
  92. {
  93. //no albedo data
  94. }else
  95. {
  96. float alphaData = texture2D(sampler2D(albedoSampler), v_texCoord).a;
  97. if(alphaData*255 < 1)
  98. discard;
  99. }
  100. vec3 noMappedNorals = normalize(v_normals);
  101. vec3 normal = getNormalMapFunc(noMappedNorals);
  102. //normal = noMappedNorals; //(option) remove normal mapping
  103. normal = normalize(normal);
  104. a_normal = fromFloatTouShort(normal);
  105. a_posViewSpace = v_positionViewSpace;
  106. a_materialIndex = u_materialIndex+1;
  107. a_textureUV.xy = v_texCoord.xy;
  108. a_textureDerivates.x = fromFloat2TouShort(dFdx(v_texCoord.x));
  109. a_textureDerivates.y = fromFloat2TouShort(dFdy(v_texCoord.x));
  110. a_textureDerivates.z = fromFloat2TouShort(dFdx(v_texCoord.y));
  111. a_textureDerivates.w = fromFloat2TouShort(dFdy(v_texCoord.y));
  112. //a_emmisive = a_outColor.rgb * mat[u_materialIndex].rma.a;
  113. //a_emmisive = texture2D(u_emissiveTexture, v_texCoord).rgb;
  114. }