three.js 加载gltf格式文件入坑总结(Blender导出gltf、gltf-pipeline压缩转Draco.gltf)

1 篇文章 0 订阅
1 篇文章 0 订阅

总结three.js加载gltf格式文件开发过程中遇到的坑

项目介绍

此项目主要是做一个三维运维机房,笔者刚接触three一个月,属于初学者。项目起始,加载模型用的FBX格式,直接引用的3D格式文件,后期发现在性能较差的笔记本上加载模型时,耗时太长(50M大小 3~5min),体验极差。后面通过查阅资料,发现three官方是推荐gltf/glb格式文件的,后面就开始研究如何载入gltf格式文件。开发过程中遇到很多问题,最后经过不懈努力终于得到解决。此次three开发经历值得记录一下。本文适合初学者阅读,对于你比较熟悉three的同学可做交流,欢迎提出宝贵意见。
下面对开发过程入的坑做一总结:

  1. 如何使用blender导出gltf格式文件;
  2. GLTFLoader如何载入gltf格式文件;
  3. gltf格式文件如何压缩;

如何使用blender导出gltf格式文件

如何使用blender导出GLTF—参考:https://blog.csdn.net/ithanmang/article/details/82147686
提示最新版blender(version2.9.0)已经内置gltf格式导出,无需手动下载安装插件包了。

VUE项目中 GLTFLoader如何载入gltf格式文件

import * as THREE from 'three';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
 
const loader = new GLTFLoader(), path =  'three/model/jifang.gltf' ;
loader.load(
    path ,
    ( gltf ) => {
        // called when the resource is loaded
        scene.add( gltf.scene );
    },
    ( xhr ) => {
        // called while loading is progressing
        console.log( `${( xhr.loaded / xhr.total * 100 )}% loaded` );
    },
    ( error ) => {
        // called when loading has errors
        console.error( 'An error happened', error );
    },
);

注意:gltf文件——必须、必须、必须放在根目录public文件夹下(vue-li2构建项目对应static静态资源文件夹下面),路径为: ‘public/three/model/jifang.gltf’。gltf文件放入其他目录下 比如‘src/assets’,会导致模型load失败,至于原因后面学到会及时更新。

gltf格式文件如何压缩

项目进行到上面阶段时,模型已经能够载进dom了。但是一般的三维模型文件体积较大,动辄几十上百M,模型载入时间过长(几十秒甚至几分钟),严重影响体验。这时候就需要压缩模型文件了,一般方法有两种:减少模型细节 物理压缩体积(减少了纹理细节 不推荐)、通过gltf-pipeline插件压缩gltf文件(推荐)。

如何gltf-pipeline压缩GLTF—参考:https://blog.csdn.net/rexfow/article/details/107378041

注意:需要先cmd控制台全局安装gltf-pipeline:npm install -g gltf-pipeline,再cd进入gltf文件目录下执行:gltf-pipeline -i jifang.gltf -o jifangDraco.gltf -d,指定导出名为 XXDraco.gltf的文件。指令结束后 就可以在‘public/three/model/’目录下(根据自己的项目路径)看到XXDraco.gltf文件了。

此刻,你以为XXDraco.gltf文件就可以直接通过GLTFLoader方法载入使用了?错!

的确,想当然的以为既然压缩后同样是gltf格式文件,直接GLTFLoader载入为啥还会报错呢。通过参考官方demo找到了答案。
学习官方demo_loader_gltf_extensions—参考:http://www.yanhuangxueyuan.com/threejs/examples/?q=loader#webgl_loader_gltf_extensions

import * as THREE from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
import { DRACOLoader  } from 'three/examples/jsm/loaders/DRACOLoader'

const loader = new GLTFLoader(), path =  'three/model/jifangDraco.gltf' ;
var dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath( 'three/js/libs/draco/gltf/' );
loader.setDRACOLoader( dracoLoader );
loader.load(
    path ,
    ( gltf ) => {
        // called when the resource is loaded
        scene.add( gltf.scene );
    },
    ( xhr ) => {
        // called while loading is progressing
        console.log( `${( xhr.loaded / xhr.total * 100 )}% loaded` );
    },
    ( error ) => {
        // called when loading has errors
        console.error( 'An error happened', error );
    },
);

执行代码会发下以下错误:DRACOLoader实例对象下setDecoderPath方法未定义。
在这里插入图片描述
此刻 是不是有点奇怪,明明和官方demo写法一致为什么会报错,查阅了很多文档终于在一篇文章中找到了答案(参考文章:https://blog.csdn.net/yue1241630499/article/details/105322176),代码做以下改动:

import * as THREE from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
import { DRACOLoader  } from 'three/examples/jsm/loaders/DRACOLoader'

const loader = new GLTFLoader(), path =  'three/model/jifangDraco.gltf' ;
DRACOLoader.setDecoderPath( 'three/js/libs/draco/gltf/' );//设置解压库文件路径
var dracoLoader = new DRACOLoader();
modelLoader.setDRACOLoader( dracoLoader );
loader.load(
    path ,
    ( gltf ) => {
        // called when the resource is loaded
        scene.add( gltf.scene );
    },
    ( xhr ) => {
        // called while loading is progressing
        console.log( `${( xhr.loaded / xhr.total * 100 )}% loaded` );
    },
    ( error ) => {
        // called when loading has errors
        console.error( 'An error happened', error );
    },
);

发现区别了嘛?太奇怪了,暂时还未发现这么写的原因,有知晓者欢迎评论区指点迷津。

对于DRACOLoader.setDecoderPath( 'three/js/libs/draco/gltf/' )设置解压库文件路径的补充

是不是对这个方法的路径参数很懵?我也很懵。。。项目中没有这些文件怎么办。。。那就无奈引入吧,先实现效果再说。去官网工具资源下载官方Three.js-master包,里面有three.js所需各种插件,解压缩后将Three.js-master\R102版本\three.js-master\three.js-r102\examples\路径下js文件夹存入public/three路径下面即可。

总结:到此gltf格式文件终于可以成功导入了,模型加载速度提升不少。此篇文章对于three.js初学者大有帮助,欢迎批评指正,评论区留言。

  • 13
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: Vue.js 是一个流行的 JavaScript 框架,用于构建现代化的 web 应用程序。Three.js 是一个使用 WebGL 技术来创建 3D 图形的 JavaScript 库。gltf 文件格式是一种开放、可扩展的文件格式,用于在 3D 应用程序之间传输和共享 3D 模型和场景。 要在 Vue.js 应用程序中加载 gltf 文件,需要使用 Three.js 库来加载和呈现 3D 模型。首先,需要在 Vue.js 应用程序中安装 Three.js 库,可以使用 npm 进行安装。在 Vue 组件中引入 Three.js 库后,可以通过以下步骤加载 gltf 文件: 1. 创建一个 Three.js 场景和相机对象。 2. 创建一个 Three.jsGLTFLoader 对象,用于加载 gltf 文件。 3. 使用 GLTFLoader 对象的 load() 方法来加载 gltf 文件,并为其指定回调函数。 4. 在回调函数中将加载的 3D 模型添加到场景中,并渲染相机。 以下是一个简单的示例代码: ``` <template> <div id="container"></div> </template> <script> import * as THREE from 'three'; import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'; export default { data() { return { scene: null, camera: null, renderer: null, mesh: null, } }, mounted() { // 创建场景和相机 this.scene = new THREE.Scene(); this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); this.camera.position.z = 5; // 创建渲染器并添加到页面 this.renderer = new THREE.WebGLRenderer({ antialias: true }); this.renderer.setSize(window.innerWidth, window.innerHeight); this.renderer.setClearColor(0x000000, 1); document.getElementById('container').appendChild(this.renderer.domElement); // 创建 GLTFLoader 对象并加载 gltf 文件 const loader = new GLTFLoader(); loader.load('/path/to/model.gltf', (gltf) => { // 将加载的 3D 模型添加到场景中 this.mesh = gltf.scene; this.scene.add(this.mesh); // 开始渲染场景和相机 this.animate(); }); }, methods: { animate() { requestAnimationFrame(this.animate); // 控制模型的旋角度 if (this.mesh) { this.mesh.rotation.x += 0.01; this.mesh.rotation.y += 0.01; } // 渲染场景和相机 this.renderer.render(this.scene, this.camera); }, }, }; </script> ``` 在这个示例中,首先创建了一个场景和相机对象,然后创建了一个 GLTFLoader 对象并加载 gltf 文件。在回调函数中将加载的 3D 模型添加到场景中,并开始渲染场景和相机。在 animate() 方法中使用 requestAnimationFrame() 方法来更新场景中模型的旋角度,并最终渲染场景和相机。 ### 回答2: Vue和three.js结合使用可以实现非常炫酷的3D互动效果。其中,加载gltf文件是实现这种效果的常用手段。gltf是一种基于JSON的3D文件格式,它可以携带模型、材质贴图、纹理、动画等信息,非常适合在三维场景中使用。 在Vue的项目中,我们可以使用Vue CLI脚手架工具快速搭建一个基于Vue的Web应用。然后,在Vue组件中引入three.js库和GLTFLoader.js加载器。 接着,在Vue组件中通过three.js提供的Scene、Camera、Renderer等对象,创建一个三维场景。然后,使用GLTFLoader.js加载加载gltf文件,并在回调函数中将模型添加至场景中。 最后,为了让模型动起来,我们可以通过three.js提供的AnimationMixer和AnimationClip等对象,给模型添加动画效果。 除了以上的基本步骤,加载gltf文件还需要注意一些细节,比如模型坐标系的问题、贴图和纹理的路径配置、模型大小适配等等,这需要开发者仔细排查。 总之,通过Vue和three.js的结合使用,加载gltf文件可以让我们在Web应用中实现更加丰富的3D视觉体验。 ### 回答3: Vue是一款流行的JavaScript框架,而Three.js是一个强大的3D渲染引擎,它能够在网页上显示出3D模型和动画效果。而gltf文件格式则是现代的3D模型和场景的标准格式之一。 要在Vue中加载gltf文件,我们可以使用Three.jsGLTFLoader。首先,我们需要在Vue项目中安装Three.jsGLTFLoader,可以使用以下命令进行安装: ``` npm install three gltf-loader ``` 接下来,在Vue的组件中创建一个canvas元素和一个Three.js场景。然后,使用GLTFLoader从服务器加载gltf文件。 ``` <template> <div> <canvas ref="canvas"></canvas> </div> </template> <script> import * as THREE from 'three'; import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'; export default { mounted() { // 获取canvas元素 const canvasEl = this.$refs.canvas; // 创建Three.js场景 const scene = new THREE.Scene(); // 创建Three.js相机 const camera = new THREE.PerspectiveCamera(75, canvasEl.clientWidth / canvasEl.clientHeight, 0.1, 1000); camera.position.z = 5; // 创建Three.js渲染器 const renderer = new THREE.WebGLRenderer({ canvas: canvasEl }); renderer.setSize(canvasEl.clientWidth, canvasEl.clientHeight); // 加载gltf文件 const loader = new GLTFLoader(); loader.load('/path/to/model.gltf', function (gltf) { // 将模型添加到场景中 scene.add(gltf.scene); // 渲染场景 renderer.render(scene, camera); }); } }; </script> ``` 在这段代码中,我们首先创建了一个canvas元素作为Three.js渲染器的渲染目标。然后,我们创建了一个场景和相机,并使用GLTFLoader从服务器加载了一个gltf文件。在加载完成后,我们将获取到的3D模型添加到场景中,并使用渲染器进行渲染。 以上就是在Vue中加载gltf文件的基本步骤,当然还有更多的细节可以根据具体需求进行优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值