webgl_math_orientation_transform.html 3.5 KB

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