webgl_math_orientation_transform.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - gradually transform an orientation to a target orientation
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. var camera, scene, renderer, mesh, target;
  17. var spherical = new THREE.Spherical();
  18. var rotationMatrix = new THREE.Matrix4();
  19. var targetQuaternion = new THREE.Quaternion();
  20. var clock = new THREE.Clock();
  21. var speed = 2;
  22. init();
  23. animate();
  24. function init() {
  25. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
  26. camera.position.z = 5;
  27. scene = new THREE.Scene();
  28. var geometry = new THREE.ConeBufferGeometry( 0.1, 0.5, 8 );
  29. geometry.rotateX( Math.PI * 0.5 );
  30. var material = new THREE.MeshNormalMaterial();
  31. mesh = new THREE.Mesh( geometry, material );
  32. scene.add( mesh );
  33. //
  34. var targetGeometry = new THREE.SphereBufferGeometry( 0.05 );
  35. var targetMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  36. target = new THREE.Mesh( targetGeometry, targetMaterial );
  37. scene.add( target );
  38. //
  39. var sphereGeometry = new THREE.SphereBufferGeometry( 2, 32, 32 );
  40. var sphereMaterial = new THREE.MeshBasicMaterial( { color: 0xcccccc, wireframe: true, transparent: true, opacity: 0.3 } );
  41. var sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
  42. scene.add( sphere );
  43. //
  44. renderer = new THREE.WebGLRenderer( { antialias: true } );
  45. renderer.setPixelRatio( window.devicePixelRatio );
  46. renderer.setSize( window.innerWidth, window.innerHeight );
  47. document.body.appendChild( renderer.domElement );
  48. //
  49. window.addEventListener( 'resize', onResize, false );
  50. //
  51. generateTarget();
  52. }
  53. function onResize() {
  54. camera.aspect = window.innerWidth / window.innerHeight;
  55. camera.updateProjectionMatrix();
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. }
  58. function animate() {
  59. requestAnimationFrame( animate );
  60. var delta = clock.getDelta();
  61. if ( ! mesh.quaternion.equals( targetQuaternion ) ) {
  62. var step = speed * delta;
  63. mesh.quaternion.rotateTowards( targetQuaternion, step );
  64. }
  65. renderer.render( scene, camera );
  66. }
  67. function generateTarget() {
  68. // generate a random point on a sphere
  69. spherical.theta = Math.random() * Math.PI * 2;
  70. spherical.phi = Math.acos( ( 2 * Math.random() ) - 1 );
  71. spherical.radius = 2;
  72. target.position.setFromSpherical( spherical );
  73. // compute target rotation
  74. rotationMatrix.lookAt( target.position, mesh.position, mesh.up );
  75. targetQuaternion.setFromRotationMatrix( rotationMatrix );
  76. setTimeout( generateTarget, 2000 );
  77. }
  78. </script>
  79. </body>
  80. </html>