webgl_math_orientation_transform.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - math - orientation transform</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. background:#777;
  10. padding:0;
  11. margin:0;
  12. font-weight: bold;
  13. overflow:hidden;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 0px;
  18. width: 100%;
  19. color: #ffffff;
  20. padding: 5px;
  21. font-family:Monospace;
  22. font-size:13px;
  23. text-align:center;
  24. }
  25. a {
  26. color: #ffffff;
  27. }
  28. </style>
  29. <script src="../build/three.js"></script>
  30. <script src="js/controls/OrbitControls.js"></script>
  31. <script src="js/Detector.js"></script>
  32. </head>
  33. <body>
  34. <div id="container"></div>
  35. <div id="info">
  36. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - gradually transform an orientation to a target orientation
  37. </div>
  38. <script>
  39. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  40. var camera, scene, renderer, mesh, target;
  41. var spherical = new THREE.Spherical();
  42. var rotationMatrix = new THREE.Matrix4();
  43. var targetRotation = new THREE.Quaternion();
  44. var clock = new THREE.Clock();
  45. var speed = 2;
  46. init();
  47. animate();
  48. function init() {
  49. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
  50. camera.position.z = 5;
  51. scene = new THREE.Scene();
  52. var geometry = new THREE.ConeBufferGeometry( 0.2, 1, 8 );
  53. geometry.rotateX( Math.PI * 0.5 );
  54. var material = new THREE.MeshNormalMaterial();
  55. mesh = new THREE.Mesh( geometry, material );
  56. scene.add( mesh );
  57. //
  58. var targetGeometry = new THREE.SphereBufferGeometry( 0.05 );
  59. var targetMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  60. target = new THREE.Mesh( targetGeometry, targetMaterial );
  61. scene.add( target );
  62. //
  63. var sphereGeometry = new THREE.SphereBufferGeometry( 2, 32, 32 );
  64. var sphereMaterial = new THREE.MeshBasicMaterial( { color: 0xcccccc, wireframe: true, transparent: true, opacity: 0.3 } );
  65. var sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
  66. scene.add( sphere );
  67. //
  68. renderer = new THREE.WebGLRenderer( { antialias: true } );
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. document.body.appendChild( renderer.domElement );
  72. //
  73. window.addEventListener( 'resize', onResize, false );
  74. //
  75. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  76. //
  77. generateTarget();
  78. }
  79. function onResize() {
  80. camera.aspect = window.innerWidth / window.innerHeight;
  81. camera.updateProjectionMatrix();
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. }
  84. function animate() {
  85. requestAnimationFrame( animate );
  86. var delta = clock.getDelta();
  87. if ( ! mesh.quaternion.equals( targetRotation ) ) {
  88. var step = speed * delta;
  89. mesh.quaternion.rotateTowards( targetRotation, step );
  90. }
  91. renderer.render( scene, camera );
  92. }
  93. function generateTarget() {
  94. // generate a random point on a sphere
  95. spherical.theta = Math.random() * Math.PI * 2;
  96. spherical.phi = Math.acos( ( 2 * Math.random() ) - 1 );
  97. spherical.radius = 2;
  98. target.position.setFromSpherical( spherical );
  99. // compute target rotation
  100. rotationMatrix.lookAt( target.position, mesh.position, mesh.up );
  101. targetRotation.setFromRotationMatrix( rotationMatrix );
  102. setTimeout( generateTarget, 2000 );
  103. }
  104. </script>
  105. </body>
  106. </html>