Advertisement
Guest User

lit_surface_vert.glsl

a guest
Oct 14th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.93 KB | None | 0 0
  1.  
  2. #ifndef HAIR_SHADER
  3. in vec3 pos;
  4. in vec3 nor;
  5. #endif
  6.  
  7. out vec3 worldPosition;
  8. out vec3 viewPosition;
  9.  
  10. out vec3 worldNormal;
  11. out vec3 viewNormal;
  12.  
  13. #ifdef HAIR_SHADER
  14. out vec3 hairTangent;
  15. out float hairThickTime;
  16. out float hairThickness;
  17. out float hairTime;
  18. flat out int hairStrandID;
  19. #endif
  20.  
  21. void main()
  22. {
  23. #ifdef GPU_INTEL
  24.   /* Due to some shader compiler bug, we somewhat
  25.    * need to access gl_VertexID to make it work. even
  26.    * if it's actually dead code. */
  27.   gl_Position.x = float(gl_VertexID);
  28. #endif
  29.  
  30. #ifdef HAIR_SHADER
  31.   hairStrandID = hair_get_strand_id();
  32.   vec3 pos, binor;
  33.   hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0),
  34.                               ModelMatrixInverse,
  35.                               ViewMatrixInverse[3].xyz,
  36.                               ViewMatrixInverse[2].xyz,
  37.                               pos,
  38.                               hairTangent,
  39.                               binor,
  40.                               hairTime,
  41.                               hairThickness,
  42.                               hairThickTime);
  43.   worldNormal = cross(hairTangent, binor);
  44.   worldPosition = pos;
  45. #elif defined(SKELETON)
  46.  
  47.  
  48.   #define NO_JOINT      0xff
  49.  
  50.  
  51.   // Transformation
  52.   uniform mat4 uModelViewProjMatrix;
  53.   uniform mat3 uNormalMatrix = mat3(1.0f); //
  54.  
  55.   // Skinning
  56.   layout(binding=0) uniform samplerBuffer uSkinningDatas;
  57.   //uniform isamplerBuffer uSkinningIndices;
  58.   //uniform  samplerBuffer uSkinningWeights;
  59.  
  60.   // Blend Shape
  61.                     uniform           uint uBS_count;   ///< number of blendshape in data
  62.                     uniform           uint uBS_used;    ///< number of used blendshape
  63.   layout(binding=1) uniform  samplerBuffer uBS_weights; ///< user specified weights
  64.   layout(binding=2) uniform isamplerBuffer uBS_indices; ///< user specified indices
  65.   layout(binding=3) uniform  samplerBuffer uBS_data;    ///< mesh's blendshapes
  66.   layout(binding=4) uniform isamplerBuffer uBS_LUT;     ///< VertexID to blendshape ID LUT
  67.  
  68.   // Input attributes
  69.   layout(location = 0) in  vec3 inPosition;
  70.   layout(location = 1) in  vec3 inNormal;
  71.   layout(location = 2) in  vec2 inTexCoord;
  72.   layout(location = 3) in ivec4 inJointIndices; //
  73.   layout(location = 4) in  vec3 inJointWeight;  //
  74.  
  75.   // Output attributes
  76.   out VDataBlock {
  77.     vec3 normal;
  78.     vec2 texCoord;
  79.   } OUT;
  80.  
  81.  
  82.  
  83.   ///--------------------------------------------------------------------
  84.   /// SKINNING
  85.   ///--------------------------------------------------------------------
  86.   subroutine void skinning_subroutine(in vec4 weights, inout vec3 v, inout vec3 n);
  87.   subroutine uniform skinning_subroutine uSkinning;
  88.  
  89.   void calculate_skinning(inout vec3 position, inout vec3 normal) {
  90.   if (!(inJointWeight.x > 0.0f)) {
  91.     return;
  92.   }
  93.  
  94.     vec4 weights   = vec4(inJointWeight.xyz, 0.0f);
  95.     weights.w = 1.0f - (weights.x + weights.y + weights.z);
  96.  
  97.   uSkinning(weights, position, normal);
  98.   }
  99.  
  100. ///--------------------------------------------------------------------
  101. /// SKINNING : DUAL QUATERNION BLENDING
  102. ///--------------------------------------------------------------------
  103.  
  104.   void getDualQuaternions(out mat4 Ma, out mat4 Mb) {
  105.     ivec4 indices = 2 * inJointIndices;
  106.  
  107.     /// Retrieve the real (Ma) and dual (Mb) part of the dual-quaternions
  108.     Ma[0] = texelFetch(uSkinningDatas, indices.x+0);
  109.     Mb[0] = texelFetch(uSkinningDatas, indices.x+1);
  110.  
  111.     Ma[1] = texelFetch(uSkinningDatas, indices.y+0);
  112.     Mb[1] = texelFetch(uSkinningDatas, indices.y+1);
  113.  
  114.     Ma[2] = texelFetch(uSkinningDatas, indices.z+0);
  115.     Mb[2] = texelFetch(uSkinningDatas, indices.z+1);
  116.  
  117.     Ma[3] = texelFetch(uSkinningDatas, indices.w+0);
  118.     Mb[3] = texelFetch(uSkinningDatas, indices.w+1);
  119.   }
  120.  
  121.   subroutine(skinning_subroutine)
  122.     void skinning_DQBS(in vec4 weights, inout vec3 v, inout vec3 n) {
  123.     /// Paper :
  124.     ///   "Geometric Skinning with Approximate Dual Quaternion Blending"
  125.     ///   - Kavan et al 2008
  126.  
  127.     // Retrieve the dual quaternions
  128.     mat4 Ma, Mb;
  129.     getDualQuaternions(Ma, Mb);
  130.  
  131.     // Handles antipodality by sticking joints in the same neighbourhood
  132.     weights.xyz *= sign(Ma[3] * mat3x4(Ma));
  133.  
  134.     // Apply weights
  135.     vec4 A = Ma * weights;  // real part
  136.     vec4 B = Mb * weights;  // dual part
  137.  
  138.     // Normalize
  139.     float invNormA = 1.0f / length(A);
  140.     A *= invNormA;
  141.     B *= invNormA;
  142.  
  143.     // Position
  144.     v += 2.0f * cross(A.xyz, cross(A.xyz, v) + A.w*v);              // rotate
  145.     v += 2.0f * (A.w * B.xyz - B.w * A.xyz + cross(A.xyz, B.xyz));  // translate
  146.  
  147.     // Normal
  148.     n += 2.0f * cross(A.xyz, cross(A.xyz, n) + A.w*n);
  149.   }
  150.  
  151.  
  152. ///--------------------------------------------------------------------
  153. /// SKINNING : LINEAR BLENDING
  154. ///--------------------------------------------------------------------
  155.  
  156.   void getSkinningMatrix(in int jointId, out mat3x4 skMatrix) {
  157.     if (jointId == NO_JOINT) {
  158.       skMatrix = mat3x4(1.0f);
  159.       return;
  160.     }
  161.  
  162.     const int matrixId = 3*jointId;
  163.     skMatrix[0] = texelFetch(uSkinningDatas, matrixId+0);
  164.     skMatrix[1] = texelFetch(uSkinningDatas, matrixId+1);
  165.     skMatrix[2] = texelFetch(uSkinningDatas, matrixId+2);
  166.   }
  167.  
  168.   subroutine(skinning_subroutine)
  169.     void skinning_LBS(in vec4 weights, inout vec3 v, inout vec3 n) {
  170.       /// To allow less texture fetching, and thus better memory bandwith, skinning
  171.       /// matrices are setup as 3x4 on the host (ie. transposed and last column removed)
  172.       /// Hence, matrix / vector multiplications are "reversed".
  173.  
  174.       // Retrieve skinning matrices
  175.       mat3x4 jointMatrix[4];
  176.       getSkinningMatrix(inJointIndices.x, jointMatrix[0]);
  177.       getSkinningMatrix(inJointIndices.y, jointMatrix[1]);
  178.       getSkinningMatrix(inJointIndices.z, jointMatrix[2]);
  179.       getSkinningMatrix(inJointIndices.w, jointMatrix[3]);
  180.  
  181.       mat4x3 M;
  182.       vec4 x_;
  183.  
  184.     // Position
  185.     x_ = vec4(v, 1.0f);
  186.     M[0] = x_ * jointMatrix[0];
  187.     M[1] = x_ * jointMatrix[1];
  188.     M[2] = x_ * jointMatrix[2];
  189.     M[3] = x_ * jointMatrix[3];
  190.     v = M * weights;
  191.  
  192.     // Normal
  193.     x_ = vec4(n, 0.0f);
  194.     M[0] = x_ * jointMatrix[0];
  195.     M[1] = x_ * jointMatrix[1];
  196.     M[2] = x_ * jointMatrix[2];
  197.     M[3] = x_ * jointMatrix[3];
  198.     n = M * weights;
  199.   }
  200.  
  201.   ///--------------------------------------------------------------------
  202.   /// BLEND SHAPE
  203.   ///--------------------------------------------------------------------
  204.  
  205.   void calculate_blendshape(inout vec3 v, inout vec3 n) {
  206.     for (int i = 0; i < int(uBS_used); ++i) {
  207.       float   weight = texelFetch(uBS_weights, i).r;
  208.       int      index = texelFetch(uBS_indices, i).r;
  209.  
  210.       int     lut_id = gl_VertexID * int(uBS_count) + index;
  211.       int  target_id = texelFetch(uBS_LUT, lut_id).r;
  212.  
  213.       // Position
  214.       v += weight * texelFetch(uBS_data, target_id).xyz;
  215.       // Normal [todo]
  216.       //n += weight * texelFetch(uBS_data, 2*target_id + 1);
  217.     }
  218.   }
  219.  
  220.   ///--------------------------------------------------------------------
  221.   //// MAIN
  222.   ///--------------------------------------------------------------------
  223.  
  224.   void main() {
  225.     // Input
  226.     vec3 position = inPosition;
  227.     vec3 normal   = inNormal;
  228.  
  229.     // Processing
  230.     calculate_skinning(position, normal);
  231.     calculate_blendshape(position, normal);
  232.  
  233.     // Output
  234.     gl_Position   = uModelViewProjMatrix * vec4(position, 1.0f);
  235.     OUT.normal    = normalize(uNormalMatrix * normal);
  236.     OUT.texCoord  = inTexCoord;
  237.  }
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250. #else
  251.   worldPosition = point_object_to_world(pos);
  252.   worldNormal = normalize(normal_object_to_world(nor));
  253. #endif
  254.  
  255.   /* No need to normalize since this is just a rotation. */
  256.   viewNormal = normal_world_to_view(worldNormal);
  257.  
  258.   viewPosition = point_world_to_view(worldPosition);
  259.   gl_Position = point_world_to_ndc(worldPosition);
  260.  
  261.   /* Used for planar reflections */
  262.   gl_ClipDistance[0] = dot(vec4(worldPosition, 1.0), clipPlanes[0]);
  263.  
  264. #ifdef USE_ATTR
  265.   pass_attr(pos);
  266. #endif
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement