webgl_math_orientation_transform.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. let camera, scene, renderer, mesh, target;
  25. const spherical = new THREE.Spherical();
  26. const rotationMatrix = new THREE.Matrix4();
  27. const targetQuaternion = new THREE.Quaternion();
  28. const clock = new THREE.Clock();
  29. const speed = 2;
  30. init();
  31. animate();
  32. function init() {
  33. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
  34. camera.position.z = 5;
  35. scene = new THREE.Scene();
  36. const geometry = new THREE.ConeGeometry( 0.1, 0.5, 8 );
  37. geometry.rotateX( Math.PI * 0.5 );
  38. const material = new THREE.MeshNormalMaterial();
  39. mesh = new THREE.Mesh( geometry, material );
  40. scene.add( mesh );
  41. //
  42. const targetGeometry = new THREE.SphereGeometry( 0.05 );
  43. const targetMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  44. target = new THREE.Mesh( targetGeometry, targetMaterial );
  45. scene.add( target );
  46. //
  47. const sphereGeometry = new THREE.SphereGeometry( 2, 32, 32 );
  48. const sphereMaterial = new THREE.MeshBasicMaterial( { color: 0xcccccc, wireframe: true, transparent: true, opacity: 0.3 } );
  49. const sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
  50. scene.add( sphere );
  51. //
  52. renderer = new THREE.WebGLRenderer( { antialias: true } );
  53. renderer.setPixelRatio( window.devicePixelRatio );
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. document.body.appendChild( renderer.domElement );
  56. //
  57. window.addEventListener( 'resize', onWindowResize );
  58. //
  59. generateTarget();
  60. }
  61. function onWindowResize() {
  62. camera.aspect = window.innerWidth / window.innerHeight;
  63. camera.updateProjectionMatrix();
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. }
  66. function animate() {
  67. requestAnimationFrame( animate );
  68. const delta = clock.getDelta();
  69. if ( ! mesh.quaternion.equals( targetQuaternion ) ) {
  70. const step = speed * delta;
  71. mesh.quaternion.rotateTowards( targetQuaternion, step );
  72. }
  73. renderer.render( scene, camera );
  74. }
  75. function generateTarget() {
  76. // generate a random point on a sphere
  77. spherical.theta = Math.random() * Math.PI * 2;
  78. spherical.phi = Math.acos( ( 2 * Math.random() ) - 1 );
  79. spherical.radius = 2;
  80. target.position.setFromSpherical( spherical );
  81. // compute target rotation
  82. rotationMatrix.lookAt( target.position, mesh.position, mesh.up );
  83. targetQuaternion.setFromRotationMatrix( rotationMatrix );
  84. setTimeout( generateTarget, 2000 );
  85. }
  86. </script>
  87. </body>
  88. </html>