Three.js学习之旅--加载3D模型

今天继续学习Three.js,完成一个小案例,目标:学会导入GLTF(3D模型)

废话不多说,几行代码,现在开始!

初始化环境

新建html文件,并添加相机、场景、轨道控制器、渲染器,这个不会添加的,可以看看我前面的文章,很简单的。

往期回顾

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>案例-小火车</title>
    <script type="importmap">
        {
            "imports":{
                "three":"./node_modules/three/build/three.module.js",
                "three/addons/":"./node_modules/three/examples/jsm/"
            }
        }
    </script>
</head>

<body>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
    </style>

    <script type="module">
        import * as THREE from 'three';
        // 引入轨道控制器
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
        import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
        import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';


        // 创建场景
        const scene = new THREE.Scene();
        scene.background = new THREE.Color(0xbfe3dd);



        // 相机
        const camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 100);
        // 相机位置  
        camera.position.set(5, 2, 8);


        // 创建渲染器  
        const renderer = new THREE.WebGLRenderer({ antialias: true });  //抗锯齿
        // 设置渲染画布大小
        renderer.setSize(window.innerWidth, window.innerHeight);
        // 将渲染画布插入到body中
        document.body.appendChild(renderer.domElement); //renderer.domElement为渲染画布元素

        // 创建轨道控制器
        const controls = new OrbitControls(camera, renderer.domElement);
        controls.target.set(0, 0.5, 0);
        controls.update();
        // 添加阻尼效果(惯性)
        controls.enableDamping = true;


        // 渲染
        function animate() {
            renderer.render(scene, camera);
            controls.update();

            requestAnimationFrame(animate)
        }

        animate();

    </script>

</body>

</html>

添加3D模型

html 复制代码
const loader = new GLTFLoader();
const dracoLoader = new DRACOLoader()
dracoLoader.setDecoderPath('./node_modules/three/examples/jsm/libs/draco/')
loader.setDRACOLoader(dracoLoader)
loader.load('./static/img/LittlestTokyo.glb', function (gltf) {
    const model = gltf.scene;
    model.position.set(1, 1, 0);
    model.scale.set(0.01, 0.01, 0.01);
    scene.add(model);
    animate();
});

此时的效果:

为什么会是一坨黑色的呢,因为我们没有给场景加上光源,就像是日常没有太阳,我们加上一个环境光:

html 复制代码
//创建光源
// 环境光
const light = new THREE.AmbientLight(0xffffff);
scene.add(light);

此时,这个3D模型已经可以被看见了:

使用 AnimationMixer动画混合器

使用 AnimationMixer动画混合器,让3D模型的小火车动起来,这里小火车运动,是模型上已经有的,所以说three.js很大部分,还是要靠建模的!建模太耗费精力,还是让专业的建模师去做吧。

model是3D模型,gltf.animations[0]是模型的组件(小火车),每个模型都可以由很多的组件。

html 复制代码
let mixer;
const clock = new THREE.Clock();


loader.load('./static/img/LittlestTokyo.glb', function (gltf) {
    ...
    mixer = new THREE.AnimationMixer(model);
    mixer.clipAction(gltf.animations[0]).play();
    ...
});


// 渲染
function animate() {
    ... 
    const delta = clock.getDelta();
    mixer.update(delta);
    ...
    
}

效果:

其实除了场景、相机外,也就几行代码。

完整代码

xml 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>案例-小火车</title>
    <script type="importmap">
        {
            "imports":{
                "three":"./node_modules/three/build/three.module.js",
                "three/addons/":"./node_modules/three/examples/jsm/"
            }
        }
    </script>
</head>

<body>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
    </style>

    <script type="module">
        import * as THREE from 'three';
        // 引入轨道控制器
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
        import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
        import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';


        // 创建场景
        const scene = new THREE.Scene();
        scene.background = new THREE.Color(0xbfe3dd);


        //创建光源
        // 环境光
        const light = new THREE.AmbientLight(0xffffff);
        scene.add(light);

        // 相机
        const camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 100);
        // 相机位置  
        camera.position.set(5, 2, 8);


        // 创建渲染器  
        const renderer = new THREE.WebGLRenderer({ antialias: true });  //抗锯齿
        // 设置渲染画布大小
        renderer.setSize(window.innerWidth, window.innerHeight);
        // 将渲染画布插入到body中
        document.body.appendChild(renderer.domElement); //renderer.domElement为渲染画布元素

        // 创建轨道控制器
        const controls = new OrbitControls(camera, renderer.domElement);
        controls.target.set(0, 0.5, 0);
        controls.update();
        // 添加阻尼效果(惯性)
        controls.enableDamping = true;


        // GLTFLoader 加载GLB模型
        const loader = new GLTFLoader();
        const dracoLoader = new DRACOLoader()
        dracoLoader.setDecoderPath('./node_modules/three/examples/jsm/libs/draco/')
        loader.setDRACOLoader(dracoLoader)
        loader.load('./static/img/LittlestTokyo.glb', function (gltf) {
            const model = gltf.scene;
            model.position.set(1, 1, 0);
            model.scale.set(0.01, 0.01, 0.01);
            scene.add(model);
            mixer = new THREE.AnimationMixer(model);
            mixer.clipAction(gltf.animations[0]).play();
            animate();
        });

        // 让小火车动起来
        let mixer;

        const clock = new THREE.Clock();


        // 渲染
        function animate() {
            renderer.render(scene, camera);
            controls.update();

            const delta = clock.getDelta();
            mixer.update(delta);
            
            requestAnimationFrame(animate)
        }

        // animate();

    </script>

</body>

</html>
相关推荐
hlyling22 分钟前
一键高效处理,批量缩放PNG图片,按比例轻松调整,高效工作从此开始!
javascript·python·c#·objective-c·batch·symfony
不如吃茶去2 小时前
一文搞懂React Hooks闭包问题
前端·javascript·react.js
tomla2 小时前
使用clip-path polygon()画一个多边形
css
来之梦3 小时前
uniapp中 uni.previewImage用法
前端·javascript·uni-app
野猪佩奇0073 小时前
uni-app使用ucharts地图,自定义Tooltip鼠标悬浮显示内容并且根据@getIndex点击事件获取点击的地区下标和地区名
前端·javascript·vue.js·uni-app·echarts·ucharts
生活、追梦者3 小时前
html+css+JavaScript 实现两个输入框的反转动画
javascript·css·html
星辰中的维纳斯4 小时前
vue新手入门教程(项目创建+组件导入+VueRouter)
前端·javascript·vue.js
ksuper&4 小时前
ts和js的关系
javascript
嫣嫣细语4 小时前
css实现鼠标禁用(鼠标滑过显示红色禁止符号)
前端·css
NaRciCiSSuS4 小时前
第一章-JavaScript简介
开发语言·javascript·ecmascript