61 - three.js 笔记 - 用 morphTargets 制作变形动画

1、动画的两种方式

首先,了解一下变形动画骨骼动画

  • 变形动画
    通过变形目标morphTargets,或者说是关键位置,这些位置都会被存储下来,让所有顶点从一个位置变化到另一个位置,并重复该过程。
  • 骨骼动画
    骨骼即是网格的骨头,把所有顶点绑定到特定的骨头上,当移动骨头时所有的顶点会跟着移动,网格的变形基于骨头的位置,移动和缩放比例。
    以上两种方式three.js都支持。

2、变形动画示例

示例
http://ithanmang.com/threeJs/home/201809/20180904/02-morph-animation.html
效果
这里写图片描述

3、通过 morphTargets 制作变形动画

morphTargets 是制作动画的最直接的方法,但是这种方法有一个不足,就是对于大型的模型和大型动画,模型的文件会变得很大,因为每个变形都需要储存一下所有的顶点,示例是一匹马在奔跑。
首先,看一下模型文件horse.json
首先模型文件中需要包含变形目标morphTargets

3.1、模型文件
"morphTargets": [
    { "name": "horse_A_001", "vertices":...},
    { "name": "horse_A_002", "vertices":...},
    { "name": "horse_A_003", "vertices":...},
    { "name": "horse_A_004", "vertices":...},
    ......
]
3.2、实现步骤
3.2.1 引入json格式的模型文件

let loader = new THREE.JSONLoader();
loader.load('../../models/json/horse.json', function (geometry) {

     let material = new THREE.MeshLambertMaterial({

         morphTargets: true,
         vertexColors: THREE.FaceColors,

     });

     let horse = new THREE.Mesh(geometry, material);

     horse.scale.set(0.2, 0.2, 0.2);

     scene.add(horse);
}

注意
声明材质时候,需要把morphTargets设置为true,否则无法实现动画效果。

3.2.2 创建场景合成器THREE.AnimationMixer()

THREE.AnimationMixer()
是场景合成器,用来在特定的场景中的象实现动画的合成,当场景中有很多对象的时候,可以为每个对象都指定一个THREE.AnimationMixer()

mixer = new THREE.AnimationMixer(horse);
3.2.3 调用 THREE.AnimationClip的静态方法CreateFromMorphTargetSequence

THREE.AnimationClip()
是一组可以重复使用的关键帧,每一帧组合起来就是一个动画

let clip = THREE.AnimationClip.CreateFromMorphTargetSequence('run', horse.geometry.morphTargets, 30);

通过CreateFromMorphTargetSequence创建动画序列。

3.2.4 执行动画并设置 fps 值
let action = mixer.clipAction(clip);// 返回一个AnimationAction对象
action.setDuration(0.005).play();

setDuration()可以用来设置动画的速度,然后调用AnimationAction对象的play方法激活动画。

3.2.5 更新动画片段

完成以上步骤之后,还需要调用在循环函数中,来更新动画的位置。

mixer.update(clock.getDelta());

4、示例代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="icon" href="../../../three.png">
    <title>morph animation 变形动画</title>
    <style>
        body {
            margin: 0;
            overflow: hidden; /* 溢出隐藏 */
        }
        #loading {
            position: fixed;
            top: 50%;
            left: 50%;
            color: #FFFFFF;
            font-size: 20px;
            margin-top: -30px;
            margin-left: -40px;
        }
    </style>
    <script src="../../libs/build/three-r93.js"></script>
    <script src="../../libs/examples/js/Detector.js"></script>
    <script src="../../libs/examples/js/libs/dat.gui.min.js"></script>
    <script src="../../libs/examples/js/libs/stats.min.js"></script>
    <script src="../../libs/examples/js/controls/OrbitControls.js"></script>
</head>
<body>
<p id="loading">loading......</p>
<script>

    let scene, camera, renderer, controls, guiControls, mixer;
    let stats = initStats();

    /* 场景 */
    function initScene() {

        scene = new THREE.Scene();
        scene.background = new THREE.Color(0x050505);

    }

    /* 相机 */
    function initCamera() {

        camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000);
        camera.position.set(250, 100, 50);
        camera.lookAt(new THREE.Vector3(0, 0, 0));

    }

    /* 渲染器 */
    function initRender() {

        renderer = new THREE.WebGLRenderer({antialias: true});
        renderer.setSize(window.innerWidth, window.innerHeight);

        document.body.appendChild(renderer.domElement);

    }

    /* 灯光 */
    function initLight() {

        scene.add(new THREE.AmbientLight(0x0c0c0c));

        let spotLight = new THREE.SpotLight(0xffffff);
        spotLight.position.set(-400, -400, -400);

        let spotLight2 = new THREE.SpotLight(0xffffff);
        spotLight2.position.set(400, 800, 400);

        scene.add(spotLight);
        scene.add(spotLight2);

    }

    /* 控制器 */
    function initControls() {

        /* 地图控件 */
        controls = new THREE.OrbitControls(camera, renderer.domElement);

        /* 属性参数 */

    }

    /* 调试插件 */
    function initGui() {

        guiControls = new function () {

        };

        let gui = new dat.GUI();

    }

    /* 场景中的内容 */
    function initContent() {

        let loader = new THREE.JSONLoader();
        loader.load('../../models/json/horse.json', function (geometry) {

            let material = new THREE.MeshLambertMaterial({

                morphTargets: true,
                vertexColors: THREE.FaceColors,

            });

            let horse = new THREE.Mesh(geometry, material);

            horse.scale.set(0.2, 0.2, 0.2);

            scene.add(horse);

            document.getElementById('loading').style.display = 'none';

            mixer = new THREE.AnimationMixer(horse);

            let clip = THREE.AnimationClip.CreateFromMorphTargetSequence('run', horse.geometry.morphTargets, 30);

            let action = mixer.clipAction(clip);
            action.setDuration(1).play();


        });

    }

    /* 性能插件 */
    function initStats() {

        let stats = new Stats();

        document.body.appendChild(stats.domElement);

        return stats;

    }

    /* 窗口变动触发 */
    function onWindowResize() {

        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        renderer.setSize(window.innerWidth, window.innerHeight);

    }

    let clock = new THREE.Clock();

    /* 数据更新 */
    function update() {

        stats.update();

        controls.update();// 使用 OrbitControls 控件时 此处不需要加时间

        if (mixer) {

            mixer.update(clock.getDelta());

        }

    }

    /* 初始化 */
    function init() {

        initScene();
        initCamera();
        initRender();
        initLight();
        initControls();
        initContent();
        initGui();

        /* 监听事件 */
        window.addEventListener('resize', onWindowResize, false);

    }

    /* 循环渲染 */
    function animate() {

        requestAnimationFrame(animate);
        renderer.render(scene, camera);
        update();

    }

    /* 初始加载 */
    (function () {
        console.log("three init start...");

        init();
        animate();

        console.log("three init end...");
    })();

</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/ithanmang/article/details/82383567