webgl_math_orientation_transform.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  25. let camera, scene, renderer, mesh, target;
  26. const spherical = new THREE.Spherical();
  27. const rotationMatrix = new THREE.Matrix4();
  28. const targetQuaternion = new THREE.Quaternion();
  29. const clock = new THREE.Clock();
  30. const speed = Math.PI / 2;
  31. const params = {
  32. useLookAt: false,
  33. };
  34. init();
  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.setAnimationLoop( animate );
  59. document.body.appendChild( renderer.domElement );
  60. //
  61. window.addEventListener( 'resize', onWindowResize );
  62. //
  63. const gui = new GUI();
  64. gui.add( params, 'useLookAt' );
  65. gui.open();
  66. //
  67. generateTarget();
  68. }
  69. function onWindowResize() {
  70. camera.aspect = window.innerWidth / window.innerHeight;
  71. camera.updateProjectionMatrix();
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. }
  74. function animate() {
  75. const delta = clock.getDelta();
  76. if ( mesh.quaternion.equals( targetQuaternion ) === false ) {
  77. if ( params.useLookAt === true ) {
  78. // using lookAt() will make the mesh instantly look at the target
  79. mesh.lookAt( target.position );
  80. } else {
  81. // using rotateTowards() will gradually rotate the mesh towards the target
  82. // the "speed" variable represents the rotation speed in radians per seconds
  83. const step = speed * delta;
  84. mesh.quaternion.rotateTowards( targetQuaternion, step );
  85. }
  86. }
  87. renderer.render( scene, camera );
  88. }
  89. function generateTarget() {
  90. // generate a random point on a sphere
  91. spherical.theta = Math.random() * Math.PI * 2;
  92. spherical.phi = Math.acos( ( 2 * Math.random() ) - 1 );
  93. spherical.radius = 2;
  94. target.position.setFromSpherical( spherical );
  95. // compute target rotation
  96. rotationMatrix.lookAt( target.position, mesh.position, mesh.up );
  97. targetQuaternion.setFromRotationMatrix( rotationMatrix );
  98. setTimeout( generateTarget, 2000 );
  99. }
  100. </script>
  101. </body>
  102. </html>
粤ICP备19079148号