欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Three.js的基础动画实现案例

程序员文章站 2024-02-18 00:01:34
简介 模型导入到上一节也就先告一段落了,接下来,我将学习动画方面的知识。在这之前,我先复习了一下基础变化的相关知识。 基础动画就是缩放、位置和旋转,也就是配置模型的scale、position和ro...

简介

模型导入到上一节也就先告一段落了,接下来,我将学习动画方面的知识。在这之前,我先复习了一下基础变化的相关知识。

基础动画就是缩放、位置和旋转,也就是配置模型的scale、position和rotation。我直接写了一个案例,来实现三种动画。

实现案例

- 首先,我们创建了三个添加动画的模型:立方体、球和圆柱。

sphere = new three.mesh(spheregeometry, spherematerial);
cube = new three.mesh(cubegeometry, cubematerial);
cylinder = new three.mesh(cylindergeometry, cylindermaterial);

然后在render里面分别修改它们的scale、position和rotation。

//处理位置变化的球的移动
positionstep += gui.positionstep;
sphere.position.x = 10*math.cos(positionstep) - 10;
sphere.position.y = 5 + math.abs(math.sin(positionstep))*10;

//处理立方体的旋转
cube.rotation.x += gui.rotationstep;
cube.rotation.y += gui.rotationstep;
cube.rotation.z += gui.rotationstep;

//处理圆柱体的缩放
scalestep += gui.scalestep;
cylinder.scale.x = math.abs(math.sin(scalestep));
cylinder.scale.y = math.abs(math.cos(scalestep));
cylinder.scale.z = math.abs(math.sin(scalestep));

案例代码

<!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="/lib/three.js"></script>
<script src="/lib/js/controls/orbitcontrols.js"></script>
<script src="/lib/js/libs/stats.min.js"></script>
<script src="/lib/js/libs/dat.gui.min.js"></script>

<script>
    var renderer;
    function initrender() {
        renderer = new three.webglrenderer({antialias:true});
        renderer.setsize(window.innerwidth, window.innerheight);
        //告诉渲染器需要阴影效果
        renderer.shadowmap.enabled = true;
        renderer.shadowmap.type = three.pcfsoftshadowmap; // 默认的是,没有设置的这个清晰 three.pcfshadowmap
        document.body.appendchild(renderer.domelement);
    }

    var camera;
    function initcamera() {
        camera = new three.perspectivecamera(45, window.innerwidth/window.innerheight, 0.1, 1000);
        camera.position.set(0, 40, 50);
        camera.lookat(new three.vector3(0,0,0));
    }

    var scene;
    function initscene() {
        scene = new three.scene();
    }

    //初始化dat.gui简化试验流程
    var gui;
    function initgui() {
        //声明一个保存需求修改的相关数据的对象
        gui = {
            scalestep:0.01,
            rotationstep:0.03,
            positionstep:0.05
        };
        var datgui = new dat.gui();
        //将设置属性添加到gui当中,gui.add(对象,属性,最小值,最大值)
        datgui.add(gui, "scalestep", 0, 1);
        datgui.add(gui, "rotationstep", 0, 1);
        datgui.add(gui, "positionstep", 0, 1);
    }

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

        light = new three.pointlight(0xffffff);
        light.position.set(15,50,10);

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

        scene.add(light);
    }

    var sphere,cube,cylinder;
    function initmodel() {

        //球
        var spheregeometry = new three.spheregeometry(5,200,200);
        var spherematerial = new three.meshlambertmaterial({color:0xaaaaaa});

        sphere = new three.mesh(spheregeometry, spherematerial);
        sphere.position.x = -10;
        sphere.position.y = 5;
        sphere.position.z = 10;

        //告诉球需要投射阴影
        sphere.castshadow = true;

        scene.add(sphere);

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

        //立方体
        var cubegeometry = new three.cubegeometry(10,10,8);
        var cubematerial = new three.meshlambertmaterial({color:0x00ffff});

        cube = new three.mesh(cubegeometry, cubematerial);
        cube.position.x = 15;
        cube.position.y = 5;
        cube.position.z = -5;

        //告诉立方体需要投射阴影
        cube.castshadow = true;

        scene.add(cube);

        //创建圆柱体
        var cylindergeometry = new three.cylindergeometry(5, 5, 10, 40, 20);
        var cylindermaterial = new three.meshphongmaterial({color:0xff5f4d});
        cylinder = new three.mesh(cylindergeometry, cylindermaterial);
        cylinder.position.set(-15, 5, -10);
        cylinder.castshadow = true;

        scene.add(cylinder);

        //底部平面
        var planegeometry = new three.planegeometry(100,100);
        var planematerial = new three.meshstandardmaterial({color:0xaaaaaa});

        var plane = new three.mesh(planegeometry, planematerial);
        plane.rotation.x = - 0.5 * math.pi;
        plane.position.y = 0;

        //告诉底部平面需要接收阴影
        plane.receiveshadow = true;

        scene.add(plane);

    }

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

    //用户交互插件 鼠标左键按住旋转,右键按住平移,滚轮缩放
    var controls;
    function initcontrols() {

        controls = new three.orbitcontrols( camera, renderer.domelement );

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

    var positionstep = 0,scalestep = 0; //模型旋转的速度
    function render() {

        //处理简单动画

        //处理位置变化的球的移动
        positionstep += gui.positionstep;
        sphere.position.x = 10*math.cos(positionstep) - 10;
        sphere.position.y = 5 + math.abs(math.sin(positionstep))*10;

        //处理立方体的旋转
        cube.rotation.x += gui.rotationstep;
        cube.rotation.y += gui.rotationstep;
        cube.rotation.z += gui.rotationstep;

        //处理圆柱体的缩放
        scalestep += gui.scalestep;
        cylinder.scale.x = math.abs(math.sin(scalestep));
        cylinder.scale.y = math.abs(math.cos(scalestep));
        cylinder.scale.z = math.abs(math.sin(scalestep));

    }

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

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

    }

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

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

        controls.update();

        renderer.render( scene, camera );

        requestanimationframe(animate);
    }

    function draw() {
        initgui();
        initrender();
        initscene();
        initcamera();
        initlight();
        initmodel();
        initcontrols();
        initstats();

        animate();
        window.onresize = onwindowresize;
    }
</script>
</html>