skin.glslv 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #ifndef SKIN_GLSLV
  2. #define SKIN_GLSLV
  3. // #import u_frame_factor u_quatsb u_quatsa u_transb u_transa a_influence
  4. // #import u_arm_rel_trans u_arm_rel_quat
  5. /*==============================================================================
  6. VARS
  7. ==============================================================================*/
  8. #var DISABLE_TANGENT_SKINNING 0
  9. #var SKINNED 0
  10. #var FRAMES_BLENDING 0
  11. /*============================================================================*/
  12. #if SKINNED
  13. #include <math.glslv>
  14. #define SKIN_SLERP 0
  15. # if SKIN_SLERP
  16. /*
  17. * Ported from gl-matrix
  18. */
  19. vec4 quat4_slerp(in vec4 quat, in vec4 quat2, in float slerp)
  20. {
  21. float cosHalfTheta = quat[0] * quat2[0] + quat[1] * quat2[1] +
  22. quat[2] * quat2[2] + quat[3] * quat2[3];
  23. if (cosHalfTheta < 0.0) {
  24. quat2 *= -1.0;
  25. cosHalfTheta = -cosHalfTheta;
  26. }
  27. if (abs(cosHalfTheta) >= 1.0)
  28. return quat;
  29. float halfTheta = acos(cosHalfTheta);
  30. float sinHalfTheta = sqrt(1.0 - cosHalfTheta * cosHalfTheta);
  31. if (abs(sinHalfTheta) < 0.001)
  32. return vec4(quat * 0.5 + quat2 * 0.5);
  33. float ratioA = sin((1.0 - slerp) * halfTheta) / sinHalfTheta;
  34. float ratioB = sin(slerp * halfTheta) / sinHalfTheta;
  35. return quat * ratioA + quat2 * ratioB;
  36. }
  37. /*
  38. vec4 quat4_normalize(vec4 quat) {
  39. vec4 dest;
  40. float x = quat[0], y = quat[1], z = quat[2], w = quat[3];
  41. float len = sqrt(x * x + y * y + z * z + w * w);
  42. if (len == 0.0) {
  43. dest[0] = 0.0;
  44. dest[1] = 0.0;
  45. dest[2] = 0.0;
  46. dest[3] = 0.0;
  47. return dest;
  48. }
  49. len = 1.0 / len;
  50. dest[0] = x * len;
  51. dest[1] = y * len;
  52. dest[2] = z * len;
  53. dest[3] = w * len;
  54. return dest;
  55. }
  56. */
  57. /*
  58. vec4 quat4_slerp(in vec4 quat, in vec4 quat2, in float slerp) {
  59. // v0 and v1 should be unit length or else
  60. // something broken will happen.
  61. // Compute the cosine of the angle between the two vectors.
  62. float quat_dot = quat.x*quat2.x + quat.y*quat2.y + quat.z*quat2.z + quat.w*quat2.w;
  63. quat = quat4_normalize(quat);
  64. quat2 = quat4_normalize(quat2);
  65. if (quat_dot < -0.0) {
  66. quat2 *= -1.0;
  67. quat_dot *= -1.0;
  68. }
  69. const float DOT_THRESHOLD = 0.9995;
  70. if (quat_dot > DOT_THRESHOLD) {
  71. // If the inputs are too close for comfort, linearly interpolate
  72. // and normalize the result.
  73. vec4 result = quat + slerp*(quat2 - quat);
  74. quat4_normalize(result);
  75. return result;
  76. }
  77. clamp(quat_dot, -1.0, 1.0); // Robustness: Stay within domain of acos()
  78. float theta_0 = acos(quat_dot); // theta_0 = angle between input vectors
  79. float theta = theta_0*slerp; // theta = angle between quat and result
  80. vec4 v2 = quat2 - quat*quat_dot;
  81. quat4_normalize(v2); // { quat, v2 } is now an orthonormal basis
  82. return quat*cos(theta) + v2*sin(theta);
  83. }
  84. */
  85. # endif // SKIN_SLERP
  86. # if FRAMES_BLENDING
  87. vec3 skin_point(in vec3 position,
  88. in vec4 quatb,
  89. in vec4 quata,
  90. in vec4 tranb,
  91. in vec4 trana,
  92. in float frame_factor)
  93. {
  94. vec3 pos_armobj_space = tsr_transform(u_arm_rel_trans, u_arm_rel_quat, position);
  95. # if SKIN_SLERP
  96. vec4 quat = quat4_slerp(quatb, quata, frame_factor);
  97. vec4 tran = mix(tranb, trana, frame_factor);
  98. vec3 pos_rot = qrot(quat, pos_armobj_space);
  99. vec3 pos_tran_rot = pos_rot * tran.w + tran.xyz;
  100. # else
  101. vec3 pos_rot_before = qrot(quatb, pos_armobj_space);
  102. vec3 pos_rot_after = qrot(quata, pos_armobj_space);
  103. // uniform scale in w, translation in xyz
  104. vec3 pos_tran_rot_before = pos_rot_before * tranb.w + tranb.xyz;
  105. vec3 pos_tran_rot_after = pos_rot_after * trana.w + trana.xyz;
  106. // blending performed AFTER quat transforms
  107. // to avoid distortions on sharp angles (knees, elbows etc)
  108. vec3 pos_tran_rot = mix(pos_tran_rot_before, pos_tran_rot_after,
  109. frame_factor);
  110. # endif
  111. return tsr_transform_inv(u_arm_rel_trans, u_arm_rel_quat, pos_tran_rot);
  112. }
  113. vec3 skin_vector(in vec3 vector,
  114. in vec4 quatb,
  115. in vec4 quata,
  116. in float frame_factor)
  117. {
  118. vec3 vec_armobj_space = tsr_transform_dir(u_arm_rel_trans, u_arm_rel_quat, vector);
  119. # if SKIN_SLERP
  120. vec4 quat = quat4_slerp(quatb, quata, frame_factor);
  121. vec3 vector_rot = qrot(quat, vec_armobj_space);
  122. # else
  123. vec3 vector_rot_before = qrot(quatb, vec_armobj_space);
  124. vec3 vector_rot_after = qrot(quata, vec_armobj_space);
  125. vec3 vector_rot = mix(vector_rot_before, vector_rot_after, frame_factor);
  126. # endif
  127. return tsr_transform_inv_dir(u_arm_rel_trans, u_arm_rel_quat, vector_rot);
  128. }
  129. # else // FRAMES_BLENDING
  130. vec3 skin_point(in vec3 position, in vec4 quatb, in vec4 tranb)
  131. {
  132. vec3 pos_armobj_space = tsr_transform(u_arm_rel_trans, u_arm_rel_quat, position);
  133. vec3 pos_rot = qrot(quatb, pos_armobj_space);
  134. // uniform scale in w, translation in xyz
  135. vec3 pos_tran_rot = pos_rot * tranb.w + tranb.xyz;
  136. return tsr_transform_inv(u_arm_rel_trans, u_arm_rel_quat, pos_tran_rot);
  137. }
  138. vec3 skin_vector(in vec3 vector, in vec4 quatb)
  139. {
  140. vec3 vec_armobj_space = tsr_transform_dir(u_arm_rel_trans, u_arm_rel_quat, vector);
  141. vec3 vector_rot = qrot(quatb, vec_armobj_space);
  142. return tsr_transform_inv_dir(u_arm_rel_trans, u_arm_rel_quat, vector_rot);
  143. }
  144. # endif // FRAMES_BLENDING
  145. void skin(inout vec3 position, inout vec3 tangent, inout vec3 binormal, inout vec3 normal)
  146. {
  147. # if FRAMES_BLENDING
  148. float ff = u_frame_factor;
  149. # endif
  150. // bone index is an integer part and weight is a fractional part
  151. if (a_influence.y > 0.0) { // sorted in descending order so no need to check others
  152. vec3 spos = vec3(0.0, 0.0, 0.0);
  153. vec3 stng = vec3(0.0, 0.0, 0.0);
  154. vec3 sbnr = vec3(0.0, 0.0, 0.0);
  155. vec3 snrm = vec3(0.0, 0.0, 0.0);
  156. // NOTE: Copy attributes to prevent bugs on some Qualcomm GPUs
  157. vec4 influece = a_influence;
  158. for (int i = 0; i < 4; i++) {
  159. int ind = int(influece[i]);
  160. float wght = fract(influece[i]);
  161. # if FRAMES_BLENDING
  162. // NOTE: skin_point loop glitches on some mobiles
  163. // spos += wght * skin_point(position, u_quatsb[ind], u_quatsa[ind],
  164. // u_transb[ind], u_transa[ind], ff);
  165. stng += wght * skin_vector(tangent, u_quatsb[ind], u_quatsa[ind], ff);
  166. sbnr += wght * skin_vector(binormal, u_quatsb[ind], u_quatsa[ind], ff);
  167. snrm += wght * skin_vector(normal, u_quatsb[ind], u_quatsa[ind], ff);
  168. # else
  169. spos += wght * skin_point(position, u_quatsb[ind], u_transb[ind]);
  170. snrm += wght * skin_vector(normal, u_quatsb[ind]);
  171. # if !DISABLE_TANGENT_SKINNING
  172. stng += wght * skin_vector(tangent, u_quatsb[ind]);
  173. sbnr += wght * skin_vector(binormal, u_quatsb[ind]);
  174. # endif
  175. # endif
  176. }
  177. // NOTE: hack for Yotaphone 2 (Adreno 330, Android 5.0)
  178. // preventing flickering
  179. # if FRAMES_BLENDING
  180. int ind = int(influece[0]);
  181. float wght = fract(influece[0]);
  182. spos += wght * skin_point(position, u_quatsb[ind], u_quatsa[ind],
  183. u_transb[ind], u_transa[ind], ff);
  184. ind = int(influece[1]);
  185. wght = fract(influece[1]);
  186. spos += wght * skin_point(position, u_quatsb[ind], u_quatsa[ind],
  187. u_transb[ind], u_transa[ind], ff);
  188. ind = int(influece[2]);
  189. wght = fract(influece[2]);
  190. spos += wght * skin_point(position, u_quatsb[ind], u_quatsa[ind],
  191. u_transb[ind], u_transa[ind], ff);
  192. ind = int(influece[3]);
  193. wght = fract(influece[3]);
  194. spos += wght * skin_point(position, u_quatsb[ind], u_quatsa[ind],
  195. u_transb[ind], u_transa[ind], ff);
  196. # endif
  197. position = spos;
  198. normal = snrm;
  199. # if !DISABLE_TANGENT_SKINNING
  200. tangent = stng;
  201. binormal = sbnr;
  202. # endif
  203. }
  204. if (!(a_influence.y > 0.0)) { // sorted in descending order so no need to check others
  205. // if only one bone then weight is 1.0
  206. int index = int(a_influence[0] - 1.0); // subtract 1.0 weight
  207. if (index > -1) { // distinguish from default zero values
  208. # if FRAMES_BLENDING
  209. position = skin_point(position, u_quatsb[index], u_quatsa[index],
  210. u_transb[index], u_transa[index], ff);
  211. tangent = skin_vector(tangent, u_quatsb[index], u_quatsa[index], ff);
  212. binormal = skin_vector(binormal, u_quatsb[index], u_quatsa[index], ff);
  213. normal = skin_vector(normal, u_quatsb[index], u_quatsa[index], ff);
  214. # else
  215. position = skin_point(position, u_quatsb[index], u_transb[index]);
  216. normal = skin_vector(normal, u_quatsb[index]);
  217. # if !DISABLE_TANGENT_SKINNING
  218. tangent = skin_vector(tangent, u_quatsb[index]);
  219. binormal = skin_vector(binormal, u_quatsb[index]);
  220. # endif
  221. # endif
  222. }
  223. }
  224. }
  225. #endif // SKINNED
  226. #endif