transform.chunk 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. vec4 quaternionFromAxisAngle (float angle, vec3 axis){
  2. angle /= 2.;
  3. float s = sin(angle);
  4. vec4 res;
  5. res.xyz = s * axis;
  6. res.w = cos(angle);
  7. return res;
  8. }
  9. vec4 quaternionFromAxis (vec3 xAxis,vec3 yAxis,vec3 zAxis){
  10. mat3 m = mat3(xAxis,yAxis,zAxis);
  11. float trace = m[0][0] + m[1][1] + m[2][2];
  12. vec4 quat;
  13. if (trace > 0.) {
  14. float s = 0.5 / sqrt(trace + 1.0);
  15. quat.w = 0.25 / s;
  16. quat.x = (m[2][1] - m[1][2]) * s;
  17. quat.y = (m[0][2] - m[2][0]) * s;
  18. quat.z = (m[1][0] - m[0][1]) * s;
  19. } else if ((m[0][0] > m[1][1]) && (m[0][0] > m[2][2])) {
  20. float s = 2.0 * sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]);
  21. quat.w = (m[2][1] - m[1][2]) / s;
  22. quat.x = 0.25 * s;
  23. quat.y = (m[0][1] + m[1][0]) / s;
  24. quat.z = (m[0][2] + m[2][0]) / s;
  25. } else if (m[1][1] > m[2][2]) {
  26. float s = 2.0 * sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]);
  27. quat.w = (m[0][2] - m[2][0]) / s;
  28. quat.x = (m[0][1] + m[1][0]) / s;
  29. quat.y = 0.25 * s;
  30. quat.z = (m[1][2] + m[2][1]) / s;
  31. } else {
  32. float s = 2.0 * sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]);
  33. quat.w = (m[1][0] - m[0][1]) / s;
  34. quat.x = (m[0][2] + m[2][0]) / s;
  35. quat.y = (m[1][2] + m[2][1]) / s;
  36. quat.z = 0.25 * s;
  37. }
  38. float len = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;
  39. if (len > 0.) {
  40. len = 1. / sqrt(len);
  41. quat.x = quat.x * len;
  42. quat.y = quat.y * len;
  43. quat.z = quat.z * len;
  44. quat.w = quat.w * len;
  45. }
  46. return quat;
  47. }
  48. vec4 quaternionFromEuler (vec3 angle){
  49. float x = angle.x / 2.;
  50. float y = angle.y / 2.;
  51. float z = angle.z / 2.;
  52. float sx = sin(x);
  53. float cx = cos(x);
  54. float sy = sin(y);
  55. float cy = cos(y);
  56. float sz = sin(z);
  57. float cz = cos(z);
  58. vec4 quat = vec4(0);
  59. quat.x = sx * cy * cz + cx * sy * sz;
  60. quat.y = cx * sy * cz + sx * cy * sz;
  61. quat.z = cx * cy * sz - sx * sy * cz;
  62. quat.w = cx * cy * cz - sx * sy * sz;
  63. return quat;
  64. }
  65. mat4 matrixFromRT (vec4 q, vec3 p){
  66. float x2 = q.x + q.x;
  67. float y2 = q.y + q.y;
  68. float z2 = q.z + q.z;
  69. float xx = q.x * x2;
  70. float xy = q.x * y2;
  71. float xz = q.x * z2;
  72. float yy = q.y * y2;
  73. float yz = q.y * z2;
  74. float zz = q.z * z2;
  75. float wx = q.w * x2;
  76. float wy = q.w * y2;
  77. float wz = q.w * z2;
  78. return mat4(
  79. 1. - (yy + zz), xy + wz, xz - wy, 0,
  80. xy - wz, 1. - (xx + zz), yz + wx, 0,
  81. xz + wy, yz - wx, 1. - (xx + yy), 0,
  82. p.x, p.y, p.z, 1
  83. );
  84. }
  85. mat4 matFromRTS (vec4 q, vec3 t, vec3 s){
  86. float x = q.x, y = q.y, z = q.z, w = q.w;
  87. float x2 = x + x;
  88. float y2 = y + y;
  89. float z2 = z + z;
  90. float xx = x * x2;
  91. float xy = x * y2;
  92. float xz = x * z2;
  93. float yy = y * y2;
  94. float yz = y * z2;
  95. float zz = z * z2;
  96. float wx = w * x2;
  97. float wy = w * y2;
  98. float wz = w * z2;
  99. float sx = s.x;
  100. float sy = s.y;
  101. float sz = s.z;
  102. return mat4((1. - (yy + zz)) * sx, (xy + wz) * sx, (xz - wy) * sx, 0,
  103. (xy - wz) * sy, (1. - (xx + zz)) * sy, (yz + wx) * sy, 0,
  104. (xz + wy) * sz, (yz - wx) * sz, (1. - (xx + yy)) * sz, 0,
  105. t.x, t.y, t.z, 1);
  106. }
  107. void scaleMatrix (inout mat4 m, float s){
  108. m[0].xyz *= s;
  109. m[1].xyz *= s;
  110. m[2].xyz *= s;
  111. }
  112. vec4 quatMultiply (vec4 a, vec4 b){
  113. vec4 quat;
  114. quat.x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;
  115. quat.y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z;
  116. quat.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x;
  117. quat.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;
  118. return quat;
  119. }
  120. void rotateVecFromQuat (inout vec3 v, vec4 q){
  121. float ix = q.w * v.x + q.y * v.z - q.z * v.y;
  122. float iy = q.w * v.y + q.z * v.x - q.x * v.z;
  123. float iz = q.w * v.z + q.x * v.y - q.y * v.x;
  124. float iw = -q.x * v.x - q.y * v.y - q.z * v.z;
  125. // calculate result * inverse quat
  126. v.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;
  127. v.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;
  128. v.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;
  129. }
  130. vec3 rotateVecFromAxis (vec3 v, vec3 axis, float theta){
  131. return cos(theta) * v + sin(theta) * cross(v, axis) + (1. - cos(theta)) * dot(v, axis) * axis;
  132. }
  133. vec3 rotateInLocalSpace (vec3 pos, vec3 xAxis, vec3 yAxis, vec3 zAxis, vec4 q){
  134. vec4 viewQuat = quaternionFromAxis(xAxis, yAxis, zAxis);
  135. vec4 rotQuat = quatMultiply(viewQuat, q);
  136. rotateVecFromQuat(pos, rotQuat);
  137. return pos;
  138. }
  139. void rotateCorner (inout vec2 corner, float angle){
  140. float xOS = cos(angle) * corner.x - sin(angle) * corner.y;
  141. float yOS = sin(angle) * corner.x + cos(angle) * corner.y;
  142. corner.x = xOS;
  143. corner.y = yOS;
  144. }
  145. mat3 quatToMat3(vec4 q) {
  146. vec3 m0 = vec3(
  147. 1.0 - 2.0 * q.y * q.y - 2.0 * q.z * q.z,
  148. 2.0 * q.x * q.y + 2.0 * q.w * q.z,
  149. 2.0 * q.x * q.z - 2.0 * q.w * q.y);
  150. vec3 m1 = vec3(
  151. 2.0 * q.x * q.y - 2.0 * q.w * q.z,
  152. 1.0 - 2.0 * q.x * q.x - 2.0 * q.z * q.z,
  153. 2.0 * q.y * q.z + 2.0 * q.w * q.x);
  154. vec3 m2 = vec3(
  155. 2.0 * q.x * q.z + 2.0 * q.w * q.y,
  156. 2.0 * q.y * q.z - 2.0 * q.w * q.x,
  157. 1.0 - 2.0 * q.x * q.x - 2.0 * q.y * q.y);
  158. return mat3(m0, m1, m2);
  159. }
  160. mat4 quatToMat4(vec4 q) {
  161. mat3 m3 = quatToMat3(q);
  162. return mat4(vec4(m3[0], 0.0), vec4(m3[1], 0.0), vec4(m3[2], 0.0), vec4(0.0, 0.0, 0.0, 1.0));
  163. }
  164. vec4 mat3ToQuat(mat3 mat) {
  165. float tr = mat[0][0] + mat[1][1] + mat[2][2];
  166. float qw, qx, qy, qz;
  167. if (tr > 0.0) {
  168. float S = sqrt(tr + 1.0) * 2.0; // S=4*qw
  169. float invS = 1.0 / S;
  170. qw = 0.25 * S;
  171. qx = (mat[1][2] - mat[2][1]) * invS;
  172. qy = (mat[2][0] - mat[0][2]) * invS;
  173. qz = (mat[0][1] - mat[1][0]) * invS;
  174. } else if ((mat[0][0] > mat[1][1])&&(mat[0][0] > mat[2][2])) {
  175. float S = sqrt(1.0 + mat[0][0] - mat[1][1] - mat[2][2]) * 2.0; // S=4*qx
  176. float invS = 1.0 / S;
  177. qw = (mat[1][2] - mat[2][1]) * invS;
  178. qx = 0.25 * S;
  179. qy = (mat[1][0] + mat[0][1]) * invS;
  180. qz = (mat[2][0] + mat[0][2]) * invS;
  181. } else if (mat[1][1] > mat[2][2]) {
  182. float S = sqrt(1.0 + mat[1][1] - mat[0][0] - mat[2][2]) * 2.0; // S=4*qy
  183. float invS = 1.0 / S;
  184. qw = (mat[2][0] - mat[0][2]) * invS;
  185. qx = (mat[1][0] + mat[0][1]) * invS;
  186. qy = 0.25 * S;
  187. qz = (mat[2][1] + mat[1][2]) * invS;
  188. } else {
  189. float S = sqrt(1.0 + mat[2][2] - mat[0][0] - mat[1][1]) * 2.0; // S=4*qz
  190. float invS = 1.0 / S;
  191. qw = (mat[0][1] - mat[1][0]) * invS;
  192. qx = (mat[2][0] + mat[0][2]) * invS;
  193. qy = (mat[2][1] + mat[1][2]) * invS;
  194. qz = 0.25 * S;
  195. }
  196. return vec4(qx, qy, qz, qw);
  197. }
  198. vec4 eulerToQuat(vec3 euler) {
  199. vec3 er = euler * 0.5;
  200. float x = er.x, y = er.y, z = er.z;
  201. float sx = sin(x);
  202. float cx = cos(x);
  203. float sy = sin(y);
  204. float cy = cos(y);
  205. float sz = sin(z);
  206. float cz = cos(z);
  207. vec4 quat;
  208. quat.x = sx * cy * cz + cx * sy * sz;
  209. quat.y = cx * sy * cz + sx * cy * sz;
  210. quat.z = cx * cy * sz - sx * sy * cz;
  211. quat.w = cx * cy * cz - sx * sy * sz;
  212. return quat;
  213. }
  214. float atan2(float y, float x) {
  215. return x == 0.0 ? sign(y) * 3.1416 * 0.5 : atan(y, x);
  216. }
  217. vec3 quatToEuler(vec4 q) {
  218. float x = q.x, y = q.y, z = q.z, w = q.w;
  219. float bank = 0.0, heading = 0.0, attitude = 0.0;
  220. float test = x * y + z * w;
  221. if (test > 0.499999) {
  222. bank = 0.0; // default to zero
  223. heading = (2.0 * atan2(x, w));
  224. attitude = 3.1416 * 0.5;
  225. } else if (test < -0.499999) {
  226. bank = 0.0; // default to zero
  227. heading = -(2.0 * atan2(x, w));
  228. attitude = -3.1416 * 0.5;
  229. } else {
  230. float sqx = x * x;
  231. float sqy = y * y;
  232. float sqz = z * z;
  233. bank = (atan2(2.0 * x * w - 2.0 * y * z, 1.0 - 2.0 * sqx - 2.0 * sqz));
  234. heading = (atan2(2.0 * y * w - 2.0 * x * z, 1.0 - 2.0 * sqy - 2.0 * sqz));
  235. attitude = (asin(2.0 * test));
  236. }
  237. return vec3(bank, heading, attitude);
  238. }
  239. void departWorldMatrix(out vec3 translation, out vec3 scale, mat4 matWorld)
  240. {
  241. scale = vec3(length(matWorld[0].xyz), length(matWorld[1].xyz), length(matWorld[2].xyz));
  242. translation = vec3(matWorld[0].w, matWorld[1].w, matWorld[2].w);
  243. }
  244. void departWorldMatrix(out vec3 translation, out vec3 scale, out mat3 matRotation, mat4 matWorld)
  245. {
  246. departWorldMatrix(translation, scale, matWorld);
  247. matRotation[0] = matWorld[0].xyz / scale.x;
  248. matRotation[1] = matWorld[1].xyz / scale.y;
  249. matRotation[2] = matWorld[2].xyz / scale.z;
  250. }