webgl_math_orientation_transform.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. function init() {
  32. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
  33. camera.position.z = 5;
  34. scene = new THREE.Scene();
  35. const geometry = new THREE.ConeGeometry( 0.1, 0.5, 8 );
  36. geometry.rotateX( Math.PI * 0.5 );
  37. const material = new THREE.MeshNormalMaterial();
  38. mesh = new THREE.Mesh( geometry, material );
  39. scene.add( mesh );
  40. //
  41. const targetGeometry = new THREE.SphereGeometry( 0.05 );
  42. const targetMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  43. target = new THREE.Mesh( targetGeometry, targetMaterial );
  44. scene.add( target );
  45. //
  46. const sphereGeometry = new THREE.SphereGeometry( 2, 32, 32 );
  47. const sphereMaterial = new THREE.MeshBasicMaterial( { color: 0xcccccc, wireframe: true, transparent: true, opacity: 0.3 } );
  48. const sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
  49. scene.add( sphere );
  50. //
  51. renderer = new THREE.WebGLRenderer( { antialias: true } );
  52. renderer.setPixelRatio( window.devicePixelRatio );
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. renderer.setAnimationLoop( animate );
  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. const delta = clock.getDelta();
  68. if ( ! mesh.quaternion.equals( targetQuaternion ) ) {
  69. const step = speed * delta;
  70. mesh.quaternion.rotateTowards( targetQuaternion, step );
  71. }
  72. renderer.render( scene, camera );
  73. }
  74. function generateTarget() {
  75. // generate a random point on a sphere
  76. spherical.theta = Math.random() * Math.PI * 2;
  77. spherical.phi = Math.acos( ( 2 * Math.random() ) - 1 );
  78. spherical.radius = 2;
  79. target.position.setFromSpherical( spherical );
  80. // compute target rotation
  81. rotationMatrix.lookAt( target.position, mesh.position, mesh.up );
  82. targetQuaternion.setFromRotationMatrix( rotationMatrix );
  83. setTimeout( generateTarget, 2000 );
  84. }
  85. </script>
  86. </body>
  87. </html>