1
0

webgl_math_orientation_transform.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 timer = new THREE.Timer();
  30. timer.connect( document );
  31. const speed = Math.PI / 2;
  32. const params = {
  33. useLookAt: false,
  34. };
  35. init();
  36. function init() {
  37. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
  38. camera.position.z = 5;
  39. scene = new THREE.Scene();
  40. const geometry = new THREE.ConeGeometry( 0.1, 0.5, 8 );
  41. geometry.rotateX( Math.PI * 0.5 );
  42. const material = new THREE.MeshNormalMaterial();
  43. mesh = new THREE.Mesh( geometry, material );
  44. scene.add( mesh );
  45. //
  46. const targetGeometry = new THREE.SphereGeometry( 0.05 );
  47. const targetMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  48. target = new THREE.Mesh( targetGeometry, targetMaterial );
  49. scene.add( target );
  50. //
  51. const sphereGeometry = new THREE.SphereGeometry( 2, 32, 32 );
  52. const sphereMaterial = new THREE.MeshBasicMaterial( { color: 0xcccccc, wireframe: true, transparent: true, opacity: 0.3 } );
  53. const sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
  54. scene.add( sphere );
  55. //
  56. renderer = new THREE.WebGLRenderer( { antialias: true } );
  57. renderer.setPixelRatio( window.devicePixelRatio );
  58. renderer.setSize( window.innerWidth, window.innerHeight );
  59. renderer.setAnimationLoop( animate );
  60. document.body.appendChild( renderer.domElement );
  61. //
  62. window.addEventListener( 'resize', onWindowResize );
  63. //
  64. const gui = new GUI();
  65. gui.add( params, 'useLookAt' );
  66. gui.open();
  67. //
  68. generateTarget();
  69. }
  70. function onWindowResize() {
  71. camera.aspect = window.innerWidth / window.innerHeight;
  72. camera.updateProjectionMatrix();
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. }
  75. function animate() {
  76. timer.update();
  77. const delta = timer.getDelta();
  78. if ( mesh.quaternion.equals( targetQuaternion ) === false ) {
  79. if ( params.useLookAt === true ) {
  80. // using lookAt() will make the mesh instantly look at the target
  81. mesh.lookAt( target.position );
  82. } else {
  83. // using rotateTowards() will gradually rotate the mesh towards the target
  84. // the "speed" variable represents the rotation speed in radians per seconds
  85. const step = speed * delta;
  86. mesh.quaternion.rotateTowards( targetQuaternion, step );
  87. }
  88. }
  89. renderer.render( scene, camera );
  90. }
  91. function generateTarget() {
  92. // generate a random point on a sphere
  93. spherical.theta = Math.random() * Math.PI * 2;
  94. spherical.phi = Math.acos( ( 2 * Math.random() ) - 1 );
  95. spherical.radius = 2;
  96. target.position.setFromSpherical( spherical );
  97. // compute target rotation
  98. rotationMatrix.lookAt( target.position, mesh.position, mesh.up );
  99. targetQuaternion.setFromRotationMatrix( rotationMatrix );
  100. setTimeout( generateTarget, 2000 );
  101. }
  102. </script>
  103. </body>
  104. </html>
粤ICP备19079148号