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>
相关推荐
FogLetter6 分钟前
深入理解Flex布局:grow、shrink和basis的计算艺术
前端·css
remember_me7 分钟前
前端打印实现-全网最简单实现方法
前端·javascript·react.js
前端小巷子9 分钟前
IndexedDB:浏览器端的强大数据库
前端·javascript·面试
JYeontu14 分钟前
浏览器书签还能一键下载B站视频封面?
前端·javascript
Xy91018 分钟前
从代码角度拆解Apptrace的一键拉起
javascript·数据库
Sun_light23 分钟前
深入理解JavaScript中的「this」:从概念到实战
前端·javascript
小桥风满袖24 分钟前
Three.js-硬要自学系列33之专项学习基础材质
前端·css·three.js
水冗水孚1 小时前
🚀四种方案解决浏览器地址栏预览txt文本乱码问题🚀Content-Type: text/plain;没有charset=utf-8
javascript·nginx·node.js
绅士玖1 小时前
JavaScript 中的 arguments、柯里化和展开运算符详解
前端·javascript·ecmascript 6
每天都想着怎么摸鱼的前端菜鸟1 小时前
【uniapp】uniapp热更新WGT资源,简单的多环境WGT打包脚本
javascript·uni-app