webgl_math_orientation_transform.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/WebGL.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 ( WEBGL.isWebGLAvailable() === false ) {
  39. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  40. }
  41. var camera, scene, renderer, mesh, target;
  42. var spherical = new THREE.Spherical();
  43. var rotationMatrix = new THREE.Matrix4();
  44. var targetRotation = new THREE.Quaternion();
  45. var clock = new THREE.Clock();
  46. var speed = 2;
  47. init();
  48. animate();
  49. function init() {
  50. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
  51. camera.position.z = 5;
  52. scene = new THREE.Scene();
  53. var geometry = new THREE.ConeBufferGeometry( 0.1, 0.5, 8 );
  54. geometry.rotateX( Math.PI * 0.5 );
  55. var material = new THREE.MeshNormalMaterial();
  56. mesh = new THREE.Mesh( geometry, material );
  57. scene.add( mesh );
  58. //
  59. var targetGeometry = new THREE.SphereBufferGeometry( 0.05 );
  60. var targetMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  61. target = new THREE.Mesh( targetGeometry, targetMaterial );
  62. scene.add( target );
  63. //
  64. var sphereGeometry = new THREE.SphereBufferGeometry( 2, 32, 32 );
  65. var sphereMaterial = new THREE.MeshBasicMaterial( { color: 0xcccccc, wireframe: true, transparent: true, opacity: 0.3 } );
  66. var sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
  67. scene.add( sphere );
  68. //
  69. renderer = new THREE.WebGLRenderer( { antialias: true } );
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. document.body.appendChild( renderer.domElement );
  73. //
  74. window.addEventListener( 'resize', onResize, false );
  75. //
  76. generateTarget();
  77. }
  78. function onResize() {
  79. camera.aspect = window.innerWidth / window.innerHeight;
  80. camera.updateProjectionMatrix();
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. }
  83. function animate() {
  84. requestAnimationFrame( animate );
  85. var delta = clock.getDelta();
  86. if ( ! mesh.quaternion.equals( targetRotation ) ) {
  87. var step = speed * delta;
  88. mesh.quaternion.rotateTowards( targetRotation, step );
  89. }
  90. renderer.render( scene, camera );
  91. }
  92. function generateTarget() {
  93. // generate a random point on a sphere
  94. spherical.theta = Math.random() * Math.PI * 2;
  95. spherical.phi = Math.acos( ( 2 * Math.random() ) - 1 );
  96. spherical.radius = 2;
  97. target.position.setFromSpherical( spherical );
  98. // compute target rotation
  99. rotationMatrix.lookAt( target.position, mesh.position, mesh.up );
  100. targetRotation.setFromRotationMatrix( rotationMatrix );
  101. setTimeout( generateTarget, 2000 );
  102. }
  103. </script>
  104. </body>
  105. </html>