webgl_math_orientation_transform.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. <meta property="og:title" content="three.js webgl - math - orientation transform">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_math_orientation_transform.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_math_orientation_transform.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - gradually transform an orientation to a target orientation
  17. </div>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. let camera, scene, renderer, mesh, target;
  30. const spherical = new THREE.Spherical();
  31. const rotationMatrix = new THREE.Matrix4();
  32. const targetQuaternion = new THREE.Quaternion();
  33. const timer = new THREE.Timer();
  34. timer.connect( document );
  35. const speed = Math.PI / 2;
  36. const params = {
  37. useLookAt: false,
  38. };
  39. init();
  40. function init() {
  41. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
  42. camera.position.z = 5;
  43. scene = new THREE.Scene();
  44. const geometry = new THREE.ConeGeometry( 0.1, 0.5, 8 );
  45. geometry.rotateX( Math.PI * 0.5 );
  46. const material = new THREE.MeshNormalMaterial();
  47. mesh = new THREE.Mesh( geometry, material );
  48. scene.add( mesh );
  49. //
  50. const targetGeometry = new THREE.SphereGeometry( 0.05 );
  51. const targetMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  52. target = new THREE.Mesh( targetGeometry, targetMaterial );
  53. scene.add( target );
  54. //
  55. const sphereGeometry = new THREE.SphereGeometry( 2, 32, 32 );
  56. const sphereMaterial = new THREE.MeshBasicMaterial( { color: 0xcccccc, wireframe: true, transparent: true, opacity: 0.3 } );
  57. const sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
  58. scene.add( sphere );
  59. //
  60. renderer = new THREE.WebGLRenderer( { antialias: true } );
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. renderer.setAnimationLoop( animate );
  64. document.body.appendChild( renderer.domElement );
  65. //
  66. window.addEventListener( 'resize', onWindowResize );
  67. //
  68. const gui = new GUI();
  69. gui.add( params, 'useLookAt' );
  70. gui.open();
  71. //
  72. generateTarget();
  73. }
  74. function onWindowResize() {
  75. camera.aspect = window.innerWidth / window.innerHeight;
  76. camera.updateProjectionMatrix();
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. }
  79. function animate() {
  80. timer.update();
  81. const delta = timer.getDelta();
  82. if ( mesh.quaternion.equals( targetQuaternion ) === false ) {
  83. if ( params.useLookAt === true ) {
  84. // using lookAt() will make the mesh instantly look at the target
  85. mesh.lookAt( target.position );
  86. } else {
  87. // using rotateTowards() will gradually rotate the mesh towards the target
  88. // the "speed" variable represents the rotation speed in radians per seconds
  89. const step = speed * delta;
  90. mesh.quaternion.rotateTowards( targetQuaternion, step );
  91. }
  92. }
  93. renderer.render( scene, camera );
  94. }
  95. function generateTarget() {
  96. // generate a random point on a sphere
  97. spherical.theta = Math.random() * Math.PI * 2;
  98. spherical.phi = Math.acos( ( 2 * Math.random() ) - 1 );
  99. spherical.radius = 2;
  100. target.position.setFromSpherical( spherical );
  101. // compute target rotation
  102. rotationMatrix.lookAt( target.position, mesh.position, mesh.up );
  103. targetQuaternion.setFromRotationMatrix( rotationMatrix );
  104. setTimeout( generateTarget, 2000 );
  105. }
  106. </script>
  107. </body>
  108. </html>
粤ICP备19079148号