当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Three.js导入FBX格式骨骼绑定模型(代码实例)

Three.js导入FBX格式骨骼绑定模型(代码实例)

2018年12月10日  | 移动技术网IT编程  | 我要评论

简介

上一节,深入了解了一下skinnedmesh模型对象的创建。这一节,我们导入外部骨骼绑定的模型,来实现动画显示。由于three.js支持的三维格式非常多,由于导入模式大同小异,我们就选择两种格式作为案例作为参考。其余的按照相同的套路相信大家也能够导入进来。

带骨骼模型和普通模型导入模式是一样的,不同的是three.js返回的带骨骼模型的对象里面多了几项与动画相关的配置。

案例实现

这里写图片描述

案例的模型使用的官网上的模型,大家也可以去官网上去获取模型。

- 首先,我们需要引入相应的loader插件,fbx格式需要引入一个解析二进制格式的插件,和fbxloader:

<script src="/lib/js/libs/inflate.min.js"></script> //解析二进制文件
<script src="/lib/js/loaders/fbxloader.js"></script> //loader

然后导入文件,在回调里面获取文件对象,并把对象添加到场景中:

loader.load("/lib/models/fbx/samba dancing.fbx", function (mesh) {
    scene.add(mesh);
});

现在,已经实现了模型显示,我们还需要实现当前动画的执行,按照前几节的skinnedmesh模型的动画实现方式:

//animationmixer是场景中特定对象的动画播放器。当场景中的多个对象独立动画时,可以为每个对象使用一个animationmixer
mixer = mesh.mixer = new three.animationmixer(mesh);

//mixer.clipaction 返回一个可以控制动画的animationaction对象  参数需要一个animationclip 对象
//animationaction.setduration 设置一个循环所需要的时间,当前设置了一秒
//告诉animationaction启动该动作
action = mixer.clipaction(mesh.animations[0]);
action.play();

最后,还要在每一帧里面调用帧对象:

var time = clock.getdelta();
if (mixer) {
    mixer.update(time);
}

案例代码

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <style type="text/css">
        html, body {
            margin: 0;
            height: 100%;
        }

        canvas {
            display: block;
        }
    </style>
</head>

<body onload="draw();">
</body>
<script src="https://cdn.bootcss.com/three.js/91/three.min.js"></script>
<script src="/lib/js/libs/inflate.min.js"></script>
<script src="/lib/js/loaders/colladaloader.js"></script>
<script src="/lib/js/controls/orbitcontrols.js"></script>
<script src="https://cdn.bootcss.com/stats.js/r17/stats.min.js"></script>
<script src="https://cdn.bootcss.com/dat-gui/0.7.1/dat.gui.min.js"></script>
<script src="/lib/js/detector.js"></script>

<script>
    var renderer, camera, scene, gui, light, stats, controls, meshhelper, mixer, action;
    var clock = new three.clock();

    function initrender() {
        renderer = new three.webglrenderer({antialias: true});
        renderer.setpixelratio(window.devicepixelratio);
        renderer.setsize(window.innerwidth, window.innerheight);
        renderer.setclearcolor(0xeeeeee);
        renderer.shadowmap.enabled = true;
        //告诉渲染器需要阴影效果
        document.body.appendchild(renderer.domelement);
    }

    function initcamera() {
        camera = new three.perspectivecamera(45, window.innerwidth / window.innerheight, 0.1, 200);
        camera.position.set(5, 10, 15 );
    }

    function initscene() {
        scene = new three.scene();
        scene.background = new three.color( 0xa0a0a0 );
        scene.fog = new three.fog( 0xa0a0a0, 20, 100 );
    }

    //初始化dat.gui简化试验流程
    function initgui() {
        //声明一个保存需求修改的相关数据的对象
        gui = {
            animation: true,
            helper:true //模型辅助线
        };
        var datgui = new dat.gui();
        //将设置属性添加到gui当中,gui.add(对象,属性,最小值,最大值)
        datgui.add(gui, "animation").onchange(function (e) {
            if (e) {
                action.play();
            }
            else {
                action.stop();
            }
        });

        datgui.add(gui, "helper").onchange(function (e) {
            meshhelper.visible = e;
        })
    }

    function initlight() {
        scene.add(new three.ambientlight(0x444444));

        light = new three.directionallight(0xffffff);
        light.position.set(0, 20, 10 );

        light.castshadow = true;
        light.shadow.camera.top = 10;
        light.shadow.camera.bottom = -10;
        light.shadow.camera.left = -10;
        light.shadow.camera.right = 10;

        //告诉平行光需要开启阴影投射
        light.castshadow = true;

        scene.add(light);
    }

    function initmodel() {

        //辅助工具
        var helper = new three.axeshelper(50);
        scene.add(helper);

        // 地板
        var mesh = new three.mesh( new three.planebuffergeometry( 200, 200 ), new three.meshphongmaterial( { color: 0xffffff, depthwrite: false } ) );
        mesh.rotation.x = - math.pi / 2;
        mesh.receiveshadow = true;
        scene.add( mesh );

        //添加地板割线
        var grid = new three.gridhelper( 200, 50, 0x000000, 0x000000 );
        grid.material.opacity = 0.2;
        grid.material.transparent = true;
        scene.add( grid );

        //加载模型
        var loader = new three.colladaloader();
        loader.load("/lib/models/collada/stormtrooper/stormtrooper.dae", function (mesh) {

            console.log(mesh);

            var obj = mesh.scene; //获取到模型对象

            //添加骨骼辅助
            meshhelper = new three.skeletonhelper(obj);
            scene.add(meshhelper);

            //设置模型的每个部位都可以投影
            obj.traverse( function ( child ) {
                if ( child.ismesh ) {
                    child.castshadow = true;
                    child.receiveshadow = true;
                }
            } );

            //animationmixer是场景中特定对象的动画播放器。当场景中的多个对象独立动画时,可以为每个对象使用一个animationmixer
            mixer = obj.mixer = new three.animationmixer(obj);

            //mixer.clipaction 返回一个可以控制动画的animationaction对象  参数需要一个animationclip 对象
            //animationaction.setduration 设置一个循环所需要的时间,当前设置了一秒
            //告诉animationaction启动该动作
            action = mixer.clipaction(mesh.animations[0]);
            action.play();

            obj.rotation.z += math.pi;

            scene.add(obj);
        });
    }

    //初始化性能插件
    function initstats() {
        stats = new stats();
        document.body.appendchild(stats.dom);
    }

    function initcontrols() {

        controls = new three.orbitcontrols(camera, renderer.domelement);
        //设置控制器的中心点
        //controls.target.set( 0, 100, 0 );
        // 如果使用animate方法时,将此函数删除
        //controls.addeventlistener( 'change', render );
        // 使动画循环使用时阻尼或自转 意思是否有惯性
        controls.enabledamping = true;
        //动态阻尼系数 就是鼠标拖拽旋转灵敏度
        //controls.dampingfactor = 0.25;
        //是否可以缩放
        controls.enablezoom = true;
        //是否自动旋转
        controls.autorotate = false;
        controls.autorotatespeed = 0.5;
        //设置相机距离原点的最远距离
        controls.mindistance = 1;
        //设置相机距离原点的最远距离
        controls.maxdistance = 2000;
        //是否开启右键拖拽
        controls.enablepan = true;
    }

    function render() {

        var time = clock.getdelta();
        if (mixer) {
            mixer.update(time);
        }

        controls.update();
    }

    //窗口变动触发的函数
    function onwindowresize() {

        camera.aspect = window.innerwidth / window.innerheight;
        camera.updateprojectionmatrix();
        renderer.setsize(window.innerwidth, window.innerheight);

    }

    function animate() {
        //更新控制器
        render();

        //更新性能插件
        stats.update();

        renderer.render(scene, camera);

        requestanimationframe(animate);
    }

    function draw() {
        //兼容性判断
        if (!detector.webgl) detector.addgetwebglmessage();

        initgui();
        initrender();
        initscene();
        initcamera();
        initlight();
        initmodel();
        initcontrols();
        initstats();

        animate();
        window.onresize = onwindowresize;
    }


</script>
</html>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网