javascript ThreeJS 旋转动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9094971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:34:45  来源:igfitidea点击:

ThreeJS Rotation Animation

javascriptthree.js

提问by Catherine Hwang

I have a cube in ThreeJS and I would like to rotate it 90 degrees clockwise every time I press a button. I think I have the basic gist of it: create a Three.Animation instance, bind it to the cube, and then have the animation begin every time I press the correct button. However, I'm having a difficult time understanding ThreeJS's API, because it doesn't seem to contain any examples for its methods.

我在 ThreeJS 中有一个立方体,每次按下按钮时我都想将它顺时针旋转 90 度。我想我有它的基本要点:创建一个 Three.Animation 实例,将它绑定到立方体,然后每次按下正确的按钮时动画开始。但是,我很难理解 ThreeJS 的 API,因为它似乎没有包含任何方法示例。

This is THREE.js's Animation constructor: ( root, data, interpolationType, JITCompile ) I don't understand what goes into the fields. I'm guessing root would be where I put my cube, but what about the rest?

这是 THREE.js 的 Animation 构造函数:(根、数据、插值类型、JITCompile)我不明白字段中的内容。我猜根将是我放置立方体的地方,但其余的呢?

Also can I just call animation.play()to cause the animation whenever I want? And how does the animationHandler work?

我也可以随时打电话animation.play()来引发动画吗?animationHandler 是如何工作的?

回答by George Profenza

I think for for rotating an object 90 degrees clockwise, using the TWEEN class will do. I think the Animation class is handy for heavier stuff (like bones/skin morphs/etc.)

我认为要将对象顺时针旋转 90 度,使用 TWEEN 类就可以了。我认为 Animation 类对于较重的东西(如骨骼/皮肤变形等)很方便。

To use the tween class there are 3 basic steps:

要使用 tween 类,有 3 个基本步骤:

  1. include the class in your file (<script src="js/Tween.js"></script>)
  2. add your tween for the event you need (new TWEEN.Tween( cube.rotation ).to( { y:Math.random()}, 1000 ).easing( TWEEN.Easing.Quadratic.EaseOut).start();)
  3. update the tween in your render loop (TWEEN.update();)
  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();)

You can have a have a look at the cubes tween examplefor a start.

您可以先看看多维数据集补间示例

I've modified the default cube example to have the tween in:

我修改了默认的多维数据集示例以在其中添加补间:

three.js cube tween

三.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>