webgl_math_orientation_transform.html 3.2 KB

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