123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - math - orientation transform</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - gradually transform an orientation to a target orientation
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- var camera, scene, renderer, mesh, target;
- var spherical = new THREE.Spherical();
- var rotationMatrix = new THREE.Matrix4();
- var targetQuaternion = new THREE.Quaternion();
- var clock = new THREE.Clock();
- var speed = 2;
- init();
- animate();
- function init() {
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
- camera.position.z = 5;
- scene = new THREE.Scene();
- var geometry = new THREE.ConeBufferGeometry( 0.1, 0.5, 8 );
- geometry.rotateX( Math.PI * 0.5 );
- var material = new THREE.MeshNormalMaterial();
- mesh = new THREE.Mesh( geometry, material );
- scene.add( mesh );
- //
- var targetGeometry = new THREE.SphereBufferGeometry( 0.05 );
- var targetMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
- target = new THREE.Mesh( targetGeometry, targetMaterial );
- scene.add( target );
- //
- var sphereGeometry = new THREE.SphereBufferGeometry( 2, 32, 32 );
- var sphereMaterial = new THREE.MeshBasicMaterial( { color: 0xcccccc, wireframe: true, transparent: true, opacity: 0.3 } );
- var sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
- scene.add( sphere );
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- //
- window.addEventListener( 'resize', onResize, false );
- //
- generateTarget();
- }
- function onResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- var delta = clock.getDelta();
- if ( ! mesh.quaternion.equals( targetQuaternion ) ) {
- var step = speed * delta;
- mesh.quaternion.rotateTowards( targetQuaternion, step );
- }
- renderer.render( scene, camera );
- }
- function generateTarget() {
- // generate a random point on a sphere
- spherical.theta = Math.random() * Math.PI * 2;
- spherical.phi = Math.acos( ( 2 * Math.random() ) - 1 );
- spherical.radius = 2;
- target.position.setFromSpherical( spherical );
- // compute target rotation
- rotationMatrix.lookAt( target.position, mesh.position, mesh.up );
- targetQuaternion.setFromRotationMatrix( rotationMatrix );
- setTimeout( generateTarget, 2000 );
- }
- </script>
- </body>
- </html>
|