gimbal_lock.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - gimbal lock</title>
  5. <meta charset="utf-8" />
  6. <meta
  7. name="viewport"
  8. content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"
  9. />
  10. <style>
  11. body {
  12. color: #000;
  13. font-family: Monospace;
  14. font-size: 13px;
  15. text-align: center;
  16. font-weight: bold;
  17. background-color: #fff;
  18. margin: 0px;
  19. overflow: hidden;
  20. }
  21. #info {
  22. color: #000;
  23. position: absolute;
  24. top: 0px;
  25. width: 100%;
  26. padding: 5px;
  27. }
  28. a {
  29. color: red;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div id="container"></div>
  35. <div id="info">
  36. <a href="http://threejs.org" target="_blank">three.js</a> - gimbal lock
  37. example
  38. <p>
  39. The object slowly rotates back and forth; one of its Euler angle
  40. components is close to pi/2.
  41. </p>
  42. <p>
  43. Every frame, with probability 0.5, the orientation is transformed into a
  44. quaternion then back into Euler angles.
  45. </p>
  46. <p>
  47. This causes an observable glitch when the calculation is imprecise due
  48. to gimbal lock.
  49. </p>
  50. </div>
  51. <script src="../build/three.js"></script>
  52. <script src="js/controls/OrbitControls.js"></script>
  53. <script src="js/libs/stats.min.js"></script>
  54. <script>
  55. var stats;
  56. var camera, controls, scene, renderer, mesh;
  57. init();
  58. render(); // remove when using next line for animation loop (requestAnimationFrame)
  59. animate();
  60. function init() {
  61. scene = new THREE.Scene();
  62. scene.fog = new THREE.FogExp2(0xcccccc, 0.002);
  63. renderer = new THREE.WebGLRenderer();
  64. renderer.setClearColor(scene.fog.color);
  65. renderer.setPixelRatio(window.devicePixelRatio);
  66. renderer.setSize(window.innerWidth, window.innerHeight);
  67. var container = document.getElementById('container');
  68. container.appendChild(renderer.domElement);
  69. camera = new THREE.PerspectiveCamera(
  70. 60,
  71. window.innerWidth / window.innerHeight,
  72. 1,
  73. 1000
  74. );
  75. camera.position.copy(new THREE.Vector3(-10, -22, 17));
  76. camera.rotation.copy(new THREE.Euler(0.9, -0.3, 0.4, 'XYZ'));
  77. controls = new THREE.OrbitControls(camera, renderer.domElement);
  78. controls.addEventListener('change', render); // remove when using animation loop
  79. // enable animation loop when using damping or autorotation
  80. //controls.enableDamping = true;
  81. //controls.dampingFactor = 0.25;
  82. controls.enableZoom = false;
  83. // world
  84. var geometry = new THREE.CylinderGeometry(0, 10, 30, 16, 10);
  85. var material = new THREE.MeshPhongMaterial({
  86. color: 0xffffff,
  87. shading: THREE.FlatShading,
  88. });
  89. mesh = new THREE.Mesh(geometry, material);
  90. scene.add(mesh);
  91. // lights
  92. light = new THREE.DirectionalLight(0xffffff);
  93. light.position.set(1, 1, 1);
  94. scene.add(light);
  95. light = new THREE.DirectionalLight(0x002288);
  96. light.position.set(-1, -1, -1);
  97. scene.add(light);
  98. light = new THREE.AmbientLight(0x222222);
  99. scene.add(light);
  100. //
  101. stats = new Stats();
  102. container.appendChild(stats.dom);
  103. //
  104. window.addEventListener('resize', onWindowResize, false);
  105. }
  106. function onWindowResize() {
  107. camera.aspect = window.innerWidth / window.innerHeight;
  108. camera.updateProjectionMatrix();
  109. renderer.setSize(window.innerWidth, window.innerHeight);
  110. }
  111. function animate() {
  112. requestAnimationFrame(animate);
  113. controls.update(); // required if controls.enableDamping = true, or if controls.autoRotate = true
  114. stats.update();
  115. render();
  116. }
  117. var startTime = Date.now();
  118. function render() {
  119. var timer = (Date.now() - startTime) * 0.00025;
  120. var theta = Math.PI / 2 + Math.sin(timer) * 0.1;
  121. // var theta = 1.575;
  122. var euler = new THREE.Euler(0.7, theta, 0.5, 'ZYX');
  123. if (Math.random() < 0.5) {
  124. let quat = new THREE.Quaternion().setFromEuler(euler);
  125. let matrix = new THREE.Matrix4().makeRotationFromQuaternion(quat);
  126. var newEuler = new THREE.Euler().setFromRotationMatrix(matrix, 'ZYX');
  127. euler = newEuler;
  128. }
  129. mesh.setRotationFromEuler(euler);
  130. renderer.render(scene, camera);
  131. }
  132. </script>
  133. </body>
  134. </html>
粤ICP备19079148号