Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. const rotation = Quat.fromAxisAngle(new Vec3([0, 1, 0]), t / 10000)
  2.  
  3. function fromAxisAngle(axis: Vec3, angle: number, dest = new Quat()): Quat {
  4. angle *= 0.5
  5. const sin = Math.sin(angle)
  6.  
  7. dest.x = axis.x * sin
  8. dest.y = axis.y * sin
  9. dest.z = axis.z * sin
  10. dest.w = Math.cos(angle)
  11.  
  12. return dest
  13. }
  14.  
  15. toMat4(dest = new Mat4()): Mat4 {
  16. const { x, y, z, w } = this
  17.  
  18. const x2 = x + x
  19. const y2 = y + y
  20. const z2 = z + z
  21.  
  22. const xx = x * x2
  23. const xy = x * y2
  24. const xz = x * z2
  25. const yy = y * y2
  26. const yz = y * z2
  27. const zz = z * z2
  28. const wx = w * x2
  29. const wy = w * y2
  30. const wz = w * z2
  31.  
  32. dest.init([
  33. 1 - (yy + zz), xy - wz, xz + wy, 0,
  34. xy + wz, 1 - (xx + zz), yz - wx, 0,
  35. xz - wy, yz + wx, 1 - (xx + yy), 0,
  36. 0, 0, 0, 1,
  37. ])
  38.  
  39. return dest
  40. }
  41.  
  42. precision mediump float;
  43.  
  44. attribute vec3 aVertexPosition;
  45. attribute vec2 aVertexUV;
  46.  
  47. uniform mat4 uModelMatrix;
  48. uniform mat4 uViewMatrix;
  49. uniform mat4 uProjectionMatrix;
  50.  
  51. varying vec2 vUV;
  52.  
  53. void main(void) {
  54. vUV = aVertexUV;
  55.  
  56. gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * vec4(aVertexPosition.xyz, 1.0);
  57. }
  58.  
  59. precision mediump float;
  60.  
  61. attribute vec3 aVertexPosition;
  62. attribute vec2 aVertexUV;
  63.  
  64. struct Transform {
  65. float scale;
  66. vec3 translation;
  67. vec4 rotation;
  68. };
  69.  
  70. uniform Transform uModel;
  71. uniform Transform uView;
  72. uniform mat4 uProjection;
  73.  
  74. varying vec2 vUV;
  75.  
  76. vec3 rotateVector(vec4 quat, vec3 vec) {
  77. return vec + 2.0 * cross(cross(vec, quat.xyz) + quat.w * vec, quat.xyz);
  78. }
  79.  
  80. void main(void) {
  81. vUV = aVertexUV;
  82.  
  83. vec3 world = rotateVector(uModel.rotation, aVertexPosition * uModel.scale) + uModel.translation;
  84. vec3 view = rotateVector(uView.rotation, world * uView.scale) + uView.translation;
  85.  
  86. gl_Position = uProjection * vec4(view, 1.0);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement