如何在特定关键帧处停止动画

时间:2019-12-18 07:39:09

标签: three.js blender

我对Three.js和Blender还是很陌生,我试图获取从0到180旋转的机器人手臂的3D图像,反之亦然,我已经使用blender2.8.1进行了动画制作并导出了glb文件并在一个带有three.js模块的html文件中调用它,直到这里一切都很好,但现在我想在某种程度上停止它,假设我输入值30,那么机械手应该移动30,如果以前状态为60,则应加30并移至90。参考示例为https://threejs.org/examples/#webgl_animation_skinning_morph

有人可以帮忙吗? 先感谢您。 这是代码:

<!DOCTYPE html>
<html lang="en">
    <head>

        <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">
        <style>
            body {
                color: #222;
            }
            a {
                color: #2fa1d6;
            }
            p {
                max-width: 600px;
                margin-left: auto;
                margin-right: auto;
                padding: 0 2em;
            }

        </style>
    </head>

    <body>

        <script type="module">
            import * as THREE from '../build/three.module.js';

            import Stats from '../examples/jsm/libs/stats.module.js';
            import { GUI } from '../examples/jsm/libs/dat.gui.module.js';

            import { GLTFLoader } from '../examples/jsm/loaders/GLTFLoader.js';

            var container, stats, clock, gui, mixer, actions, activeAction, previousAction;
            var camera, scene, renderer, model, face;
            var api = { state: 'middle' };
            init();
            animate();
            function init() {
                container = document.createElement( 'div' );
                document.body.appendChild( container );
                camera = new THREE.PerspectiveCamera( 120, window.innerWidth / window.innerHeight, 0.25, 100 );
                camera.position.set( 50, 30, -70 );
                camera.lookAt( new THREE.Vector3( 0, 0, 0 ) );
                scene = new THREE.Scene();
                scene.background = new THREE.Color( 0xe0e0e0 );
                scene.fog = new THREE.Fog( 0xe0e0e0, 20, 100 );
                clock = new THREE.Clock();
                // lights
                var light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
                light.position.set( 0, 20, 0 );
                scene.add( light );
                light = new THREE.DirectionalLight( 0xffffff );
                light.position.set( 0, 20, 10 );
                scene.add( light );
                // ground
                var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
                mesh.rotation.x = - Math.PI / 2;
                scene.add( mesh );
                var grid = new THREE.GridHelper( 200, 40, 0x000000, 0x000000 );
                grid.material.opacity = 0.2;
                grid.material.transparent = true;
                scene.add( grid );
                // model
                var loader = new GLTFLoader();
                loader.load( 'robo.glb', function ( gltf ) {
                    model = gltf.scene;
                    scene.add( model );
                    createGUI( model, gltf.animations );
                }, undefined, function ( e ) {
                    console.error( e );
                } );
                renderer = new THREE.WebGLRenderer( { antialias: true } );
                renderer.setPixelRatio( window.devicePixelRatio );
                renderer.setSize( window.innerWidth, window.innerHeight );
                renderer.gammaOutput = true;
                renderer.gammaFactor = 2.2;
                container.appendChild( renderer.domElement );
                window.addEventListener( 'resize', onWindowResize, false );
                // stats
                stats = new Stats();
                //container.appendChild( stats.dom );
            }
            function createGUI( model, animations ) {
                var states = [ 'negative', 'positive'];

                gui = new GUI();
                mixer = new THREE.AnimationMixer( model );
                actions = {};
                for ( var i = 0; i < animations.length; i ++ ) {
                    var clip = animations[ i ];
                    var action = mixer.clipAction( clip );
                    actions[ clip.name ] = action;
                    if ( states.indexOf( clip.name ) >= 4 ) {
                        action.clampWhenFinished = true;
                        action.loop = THREE.LoopOnce;
                    }
                }
                // states
                var statesFolder = gui.addFolder( 'States' );
                var clipCtrl = statesFolder.add( api, 'state' ).options( states );
                clipCtrl.onChange( function () {
                    fadeToAction( api.state, 1 );
                } );
                statesFolder.close();
                // emotes
                //var emoteFolder = gui.addFolder( 'Emotes' );
                                }
                function restoreState() {
                    mixer.removeEventListener( 'finished', restoreState );
                    fadeToAction( api.state, 0 );
                }

                    face = model.getObjectByName( 'Head_2' );
                var expressions = Object.keys( face.morphTargetDictionary );
                var expressionFolder = gui.addFolder( 'Expressions' );
                for ( var i = 0; i < expressions.length; i ++ ) {
                    expressionFolder.add( face.morphTargetInfluences, i, 0, 1, 0.01 ).name( expressions[ i ] );
                }
                activeAction = actions[ 'middle' ];
                    activeAction.play();
                //expressionFolder.open();

            function fadeToAction( name, duration ) {
                previousAction = activeAction;
                activeAction = actions[ name ];
                if ( previousAction !== activeAction ) {
                    previousAction.fadeOut( duration );
                }
                activeAction
                    .reset()

                    .fadeIn( duration )
                                        .setDuration(10)
                    .play();
            }
            function onWindowResize() {
                camera.aspect = window.innerWidth / window.innerHeight;
                camera.updateProjectionMatrix();
                renderer.setSize( window.innerWidth, window.innerHeight );
            }
            //
            function animate() {
                var dt = clock.getDelta();
                if ( mixer ) mixer.update( dt );
                requestAnimationFrame( animate );
                renderer.render( scene, camera );
                stats.update();
            }

        </script>
    </body>


</html>

0 个答案:

没有答案