webgl_math_orientation_transform.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. <style>
  8. body {
  9. background:#777;
  10. padding:0;
  11. margin:0;
  12. font-weight: bold;
  13. overflow:hidden;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 0px;
  18. width: 100%;
  19. color: #ffffff;
  20. padding: 5px;
  21. font-family:Monospace;
  22. font-size:13px;
  23. text-align:center;
  24. }
  25. a {
  26. color: #ffffff;
  27. }
  28. </style>
  29. <script src="../build/three.js"></script>
  30. <script src="js/Detector.js"></script>
  31. </head>
  32. <body>
  33. <div id="container"></div>
  34. <div id="info">
  35. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - gradually transform an orientation to a target orientation
  36. </div>
  37. <script>
  38. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  39. var camera, scene, renderer, mesh, target;
  40. var spherical = new THREE.Spherical();
  41. var rotationMatrix = new THREE.Matrix4();
  42. var targetRotation = new THREE.Quaternion();
  43. var clock = new THREE.Clock();
  44. var speed = 2;
  45. init();
  46. animate();
  47. function init() {
  48. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
  49. camera.position.z = 5;
  50. scene = new THREE.Scene();
  51. var geometry = new THREE.ConeBufferGeometry( 0.1, 0.5, 8 );
  52. geometry.rotateX( Math.PI * 0.5 );
  53. var material = new THREE.MeshNormalMaterial();
  54. mesh = new THREE.Mesh( geometry, material );
  55. scene.add( mesh );
  56. //
  57. var targetGeometry = new THREE.SphereBufferGeometry( 0.05 );
  58. var targetMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  59. target = new THREE.Mesh( targetGeometry, targetMaterial );
  60. scene.add( target );
  61. //
  62. var sphereGeometry = new THREE.SphereBufferGeometry( 2, 32, 32 );
  63. var sphereMaterial = new THREE.MeshBasicMaterial( { color: 0xcccccc, wireframe: true, transparent: true, opacity: 0.3 } );
  64. var sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
  65. scene.add( sphere );
  66. //
  67. renderer = new THREE.WebGLRenderer( { antialias: true } );
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. document.body.appendChild( renderer.domElement );
  71. //
  72. window.addEventListener( 'resize', onResize, false );
  73. //
  74. generateTarget();
  75. }
  76. function onResize() {
  77. camera.aspect = window.innerWidth / window.innerHeight;
  78. camera.updateProjectionMatrix();
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. }
  81. function animate() {
  82. requestAnimationFrame( animate );
  83. var delta = clock.getDelta();
  84. if ( ! mesh.quaternion.equals( targetRotation ) ) {
  85. var step = speed * delta;
  86. mesh.quaternion.rotateTowards( targetRotation, step );
  87. }
  88. renderer.render( scene, camera );
  89. }
  90. function generateTarget() {
  91. // generate a random point on a sphere
  92. spherical.theta = Math.random() * Math.PI * 2;
  93. spherical.phi = Math.acos( ( 2 * Math.random() ) - 1 );
  94. spherical.radius = 2;
  95. target.position.setFromSpherical( spherical );
  96. // compute target rotation
  97. rotationMatrix.lookAt( target.position, mesh.position, mesh.up );
  98. targetRotation.setFromRotationMatrix( rotationMatrix );
  99. setTimeout( generateTarget, 2000 );
  100. }
  101. </script>
  102. </body>
  103. </html>