webgl_math_orientation_transform.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. document.body.appendChild( renderer.domElement );
  59. //
  60. window.addEventListener( 'resize', onWindowResize );
  61. //
  62. generateTarget();
  63. }
  64. function onWindowResize() {
  65. camera.aspect = window.innerWidth / window.innerHeight;
  66. camera.updateProjectionMatrix();
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. }
  69. function animate() {
  70. requestAnimationFrame( animate );
  71. const delta = clock.getDelta();
  72. if ( ! mesh.quaternion.equals( targetQuaternion ) ) {
  73. const step = speed * delta;
  74. mesh.quaternion.rotateTowards( targetQuaternion, step );
  75. }
  76. renderer.render( scene, camera );
  77. }
  78. function generateTarget() {
  79. // generate a random point on a sphere
  80. spherical.theta = Math.random() * Math.PI * 2;
  81. spherical.phi = Math.acos( ( 2 * Math.random() ) - 1 );
  82. spherical.radius = 2;
  83. target.position.setFromSpherical( spherical );
  84. // compute target rotation
  85. rotationMatrix.lookAt( target.position, mesh.position, mesh.up );
  86. targetQuaternion.setFromRotationMatrix( rotationMatrix );
  87. setTimeout( generateTarget, 2000 );
  88. }
  89. </script>
  90. </body>
  91. </html>