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>
相关推荐
flashlight_hi1 天前
LeetCode 分类刷题:404. 左叶子之和
javascript·算法·leetcode
木易 士心1 天前
th-table 中 基于双字段计算的表格列展示方案
前端·javascript·angular.js
toooooop81 天前
uniapp多个页面监听?全局监听uni.$emit/$on
前端·javascript·uni-app
星火飞码iFlyCode1 天前
iFlyCode+SpecKit应用:照片等比智能压缩功能实现
前端·javascript
GISer_Jing1 天前
3DThreeJS渲染核心架构深度解析
javascript·3d·架构·webgl
拉不动的猪1 天前
文件下载:后端配置、前端方式与进度监控
前端·javascript·浏览器
特级业务专家1 天前
续集:Vite 字体插件重构之路 —— 从“能用”到“生产级稳定”
javascript·vue.js·vite
Never_Satisfied1 天前
在JavaScript / 微信小程序中,动态修改页面元素的方法
开发语言·javascript·微信小程序
王大宇_1 天前
虚拟列表从入门到出门
前端·javascript
淡淡蓝蓝1 天前
uni.uploadFile使用PUT方法上传图片
开发语言·前端·javascript