簡體   English   中英

ThreeJS旋轉動畫

[英]ThreeJS Rotation Animation

我在ThreeJS中有一個立方體,每次按下按鈕時我都想順時​​針旋轉90度。 我想我有它的基本要點:創建一個Three.Animation實例,將它綁定到立方體,然后每次按下正確的按鈕時都會開始動畫。 但是,我很難理解ThreeJS的API,因為它似乎沒有包含任何其方法的示例。

這是THREE.js的動畫構造函數:( root,data,interpolationType,JITCompile)我不明白這些字段是什么。 我猜根將是我放置立方體的地方,但剩下的呢?

我也可以隨時調用animation.play()來制作動畫嗎? animationHandler是如何工作的?

我認為對於順時針旋轉物體90度,使用TWEEN類會做。 我認為動畫類對於較重的東西(比如骨骼/皮膚變形等)很方便。

要使用補間類,有3個基本步驟:

  1. 在您的文件中包含該類( <script src="js/Tween.js"></script>
  2. 為您需要的事件添加補間( new TWEEN.Tween( cube.rotation ).to( { y:Math.random()}, 1000 ).easing( TWEEN.Easing.Quadratic.EaseOut).start();
  3. 更新渲染循環中的補間( TWEEN.update();

您可以查看多維數據集補間示例的開頭。

我修改了默認的多維數據集示例以使補間:

three.js立方體補間

<!doctype html>
<html lang="en">
    <head>
        <title>three.js canvas - geometry - cube</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <style>
            body {
                font-family: Monospace;
                background-color: #f0f0f0;
                margin: 0px;
                overflow: hidden;
            }
        </style>
    </head>
    <body>

        <script src="../build/Three.js"></script>
        <script src="js/Tween.js"></script>
        <script src="js/RequestAnimationFrame.js"></script>
        <script src="js/Stats.js"></script>

        <script>

            var container, stats;

            var camera, scene, renderer;

            var cube, plane;

            var windowHalfX = window.innerWidth / 2;
            var windowHalfY = window.innerHeight / 2;

            var rad90 = Math.PI * .5;

            init();
            animate();

            function init() {

                container = document.createElement( 'div' );
                document.body.appendChild( container );

                var info = document.createElement( 'div' );
                info.style.position = 'absolute';
                info.style.top = '10px';
                info.style.width = '100%';
                info.style.textAlign = 'center';
                info.innerHTML = 'click to tween';
                container.appendChild( info );

                camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
                camera.position.y = 150;
                camera.position.z = 500;

                scene = new THREE.Scene();

                // Cube

                var materials = [];

                for ( var i = 0; i < 6; i ++ ) {

                    materials.push( [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } ) ] );

                }

                cube = new THREE.Mesh( new THREE.CubeGeometry( 200, 200, 200, 1, 1, 1, materials ), new THREE.MeshFaceMaterial() );
                cube.position.y = 150;
                cube.overdraw = true;
                scene.add( cube );

                // Plane

                plane = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
                plane.rotation.x = - 90 * ( Math.PI / 180 );
                plane.overdraw = true;
                scene.add( plane );

                renderer = new THREE.CanvasRenderer();
                renderer.setSize( window.innerWidth, window.innerHeight );

                container.appendChild( renderer.domElement );

                stats = new Stats();
                stats.domElement.style.position = 'absolute';
                stats.domElement.style.top = '0px';
                container.appendChild( stats.domElement );

                document.addEventListener( 'mousedown', onDocumentMouseDown, false );
            }

            //

            function onDocumentMouseDown( event ) {

                event.preventDefault();
                new TWEEN.Tween( cube.rotation ).to( {  y:  cube.rotation.y + rad90}, 1000 ).easing( TWEEN.Easing.Quadratic.EaseOut).start();
                new TWEEN.Tween( plane.rotation ).to( { z:  plane.rotation.z + rad90}, 1000 ).easing( TWEEN.Easing.Quadratic.EaseOut).start();

                console.log("click");
            }

            //

            function animate() {

                requestAnimationFrame( animate );

                render();
                stats.update();

            }

            function render() {
                TWEEN.update();
                renderer.render( scene, camera );

            }

        </script>

    </body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM