GBufferCommonVert.glsl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (C) 2009-2018, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_SHADERS_GBUFFER_COMMON_VERT_GLSL
  6. #define ANKI_SHADERS_GBUFFER_COMMON_VERT_GLSL
  7. #include "shaders/Common.glsl"
  8. //
  9. // Uniforms
  10. //
  11. #if BONES
  12. layout(ANKI_SS_BINDING(0, 0), row_major) readonly buffer ss00_
  13. {
  14. mat4 u_boneTransforms[];
  15. };
  16. #endif
  17. //
  18. // Input
  19. //
  20. layout(location = POSITION_LOCATION) in highp vec3 in_position;
  21. #if PASS == PASS_GB_FS
  22. layout(location = TEXTURE_COORDINATE_LOCATION) in highp vec2 in_uv;
  23. layout(location = NORMAL_LOCATION) in mediump vec3 in_normal;
  24. layout(location = TANGENT_LOCATION) in mediump vec4 in_tangent;
  25. #endif
  26. #if BONES
  27. layout(location = BONE_WEIGHTS_LOCATION) in mediump vec4 in_boneWeights;
  28. layout(location = BONE_INDICES_LOCATION) in uvec4 in_boneIndices;
  29. #endif
  30. //
  31. // Output
  32. //
  33. out gl_PerVertex
  34. {
  35. vec4 gl_Position;
  36. };
  37. #if PASS == PASS_GB_FS
  38. layout(location = 0) out highp vec2 out_uv;
  39. layout(location = 1) out mediump vec3 out_normal;
  40. layout(location = 2) out mediump vec4 out_tangent;
  41. # if CALC_BITANGENT_IN_VERT
  42. layout(location = 3) out mediump vec3 out_bitangent;
  43. # endif
  44. layout(location = 4) out mediump float out_distFromTheCamera; // Parallax
  45. layout(location = 5) out mediump vec3 out_eyeTangentSpace; // Parallax
  46. layout(location = 6) out mediump vec3 out_normalTangentSpace; // Parallax
  47. #endif
  48. //
  49. // Globals
  50. //
  51. vec3 g_position = in_position;
  52. #if PASS == PASS_GB_FS
  53. highp vec2 g_uv = in_uv;
  54. mediump vec3 g_normal = in_normal;
  55. mediump vec4 g_tangent = in_tangent;
  56. #endif
  57. //
  58. // Functions
  59. //
  60. // Common store function
  61. #if PASS == PASS_GB_FS
  62. void positionUvNormalTangent(mat4 mvp, mat3 rotationMat)
  63. {
  64. out_uv = g_uv;
  65. gl_Position = mvp * vec4(g_position, 1.0);
  66. out_normal = rotationMat * g_normal.xyz;
  67. out_tangent.xyz = rotationMat * g_tangent.xyz;
  68. out_tangent.w = g_tangent.w;
  69. # if CALC_BITANGENT_IN_VERT
  70. out_bitangent = cross(out_normal, out_tangent.xyz) * out_tangent.w;
  71. # endif
  72. }
  73. #endif // PASS == PASS_GB_FS
  74. // Store stuff for parallax mapping
  75. #if PASS == PASS_GB_FS
  76. void parallax(mat4 modelViewMat)
  77. {
  78. vec3 n = in_normal;
  79. vec3 t = in_tangent.xyz;
  80. vec3 b = cross(n, t) * in_tangent.w;
  81. mat3 normalMat = mat3(modelViewMat);
  82. mat3 invTbn = transpose(normalMat * mat3(t, b, n));
  83. vec3 viewPos = (modelViewMat * vec4(g_position, 1.0)).xyz;
  84. out_distFromTheCamera = viewPos.z;
  85. out_eyeTangentSpace = invTbn * viewPos;
  86. out_normalTangentSpace = invTbn * n;
  87. }
  88. #endif // PASS == PASS_GB_FS
  89. /// Will compute new position, normal and tangent
  90. #if BONES
  91. void skinning()
  92. {
  93. vec3 position = vec3(0.0);
  94. vec3 normal = vec3(0.0);
  95. vec3 tangent = vec3(0.0);
  96. for(uint i = 0; i < 4; ++i)
  97. {
  98. uint boneIdx = in_boneIndices[i];
  99. if(boneIdx < 0xFFFF)
  100. {
  101. float boneWeight = in_boneWeights[i];
  102. position += (u_boneTransforms[boneIdx] * vec4(g_position * boneWeight, 1.0)).xyz;
  103. # if PASS == PASS_GB_FS
  104. normal += (u_boneTransforms[boneIdx] * vec4(g_normal * boneWeight, 0.0)).xyz;
  105. tangent += (u_boneTransforms[boneIdx] * vec4(g_tangent.xyz * boneWeight, 0.0)).xyz;
  106. # endif
  107. }
  108. }
  109. g_position = position;
  110. # if PASS == PASS_GB_FS
  111. g_tangent.xyz = tangent;
  112. g_normal = normal;
  113. # endif
  114. }
  115. #endif
  116. #endif