three.js加载gltf文件过程以及遇到的问题

说明:在vue项目中使用的threejs;

刚开始,我是从网上下载的gltf文件,将.gltf 文件放在了src/assets/xxx.gltf   , 对gltf格式的文件并不了解,使用如下方式加载gltf文件时,

// 创建gltf加载器对象
const loader = new GLTFLoader();
let that = this;
  loader.load("/models/gltf/box.gltf", (gltf) => {
     const model = gltf.scene;
     that.scene.add(model);
});

提示  : Unexpected token '<', "<!DOCTYPE "... is not valid JSON

经过CSDN的查找,找到了解决办法:将  .gltf 文件放在了public下才行;

将文件路径更改后,报错了,提示:GLTFLoader.js:191 RangeError: Invalid typed array length: 75201

原来是 .gltf 文件中 引入了  .bin  格式的文件了,虽然 .gltf 和 .bin 两个文件在同一个路径下,在 .gltf 文件中就是找不到 .bin 格式的文件;

更改路径,暂时还不会,无奈只能在blender工具中自己做一个demo,然后导出 .gltf 格式的文件,然后在threejs中使用了;

导出设置如下:

如此设置,页面中终于可以显示效果了;效果如下:

 在代码中,我单独加了一个环境光;

主要代码如下:

<template>
  <div>
    <el-container>
      <el-header
        style="
          display: flex;
          align-items: center;
          justify-content: space-between;
        "
      >
        <el-page-header @back="goBack" :content="name + '详情页面'">
        </el-page-header>
      </el-header>
      <el-main>
        <div class="box-card-left">
          <div id="threejs" style=""></div>
          <div class="box-right">
            glTF是一种免版税的规范,用于引擎和应用程序高效传输和加载3D场景和模型。<br />
            glTF最小化了3D资产的大小,以及解包和使用它们所需的运行时处理。<br />
            glTF定义了一种可扩展的发布格式,通过在整个行业中实现3D内容的互操作使用,简化了创作工作流程和交互服务。<br />
            glTF2.0已作为ISO/IEC 12113:2022国际标准发布。<br />
          </div>
        </div>
      </el-main>
    </el-container>
  </div>
</template>
<script>
import Drawer from "@/components/Drawer.vue";
// 引入轨道控制器扩展库OrbitControls.js
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
import { onlyEnterMoney } from "@/utils/fn.js";
export default {
  components: { Drawer },
  data() {
    return {
      name: "",
      x: 0,
      y: 0,
      z: 0,
      cameraX: 200,
      cameraY: 200,
      cameraZ: 200,
      scene: null,
      camera: null,
      renderer: null,
      mesh: null,
      mesh_sun: null,
      geometry: null,
      group: null,
      axis: null,
      texture: null,
    };
  },
  created() {},
  mounted() {
    this.name = this.$route.query.name;
    this.init();
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    // 沿着自定义的方向移动。
    init() {
      // 1,创建场景对象
      this.scene = new this.$three.Scene();
      // 创建透视投影相机对象
      this.camera = new this.$three.PerspectiveCamera(60, 1, 0.01, 1000);
      this.camera.position.set(20, 20, 20);
      this.camera.lookAt(0, 0, 0); // 记得设置相机指向,
      // 创建辅助坐标轴对象
      const axesHelper = new this.$three.AxesHelper(200);
      this.scene.add(axesHelper);
      // 创建环境光对象
      const light = new this.$three.AmbientLight(0xffffff);
      this.scene.add(light);

      // 4,创建渲染器
      this.renderer = new this.$three.WebGLRenderer();
      this.renderer.setSize(1000, 800);
      this.renderer.render(this.scene, this.camera);
      document.getElementById("threejs").appendChild(this.renderer.domElement);
      // 创建gltf加载器对象
      const loader = new GLTFLoader();
      let that = this;
      loader.load("/models/gltf/box.gltf", (gltf) => {
        const model = gltf.scene;
        that.scene.add(model);
   
        this.renderFun();
        const controls = new OrbitControls(this.camera, this.renderer.domElement);
        controls.addEventListener('change', () => {
          this.renderer.render(this.scene, this.camera);
        })
      });
    },

    renderFun() {
      this.renderer.render(this.scene, this.camera);

    },
  },
};
</script>
<style lang="less" scoped>
.msg {
  padding: 20px;
  text-align: left;
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  .span {
    margin: 0 30px 30px 0;
    // white-space: nowrap;
  }
  .p {
    text-align: left;
  }
}
.box-card-left {
  display: flex;
  align-items: flex-start;
  flex-direction: row;
  width: 100%;
  .box-right {
    text-align: left;
    .xyz {
      width: 100px;
      margin-left: 20px;
    }
    .box-btn {
      margin-left: 20px;
    }
  }
}
</style>

附件放在了中国移动云盘上 了,坚决抵制垃圾的恶心的最2比百度云盘,限速,就是让你开通会员,开通会员后下载文件,下的也不一定完整;

附件地址:

链接:https://caiyun.139.com/m/i?185C7GGV8U40d
提取码:PZWy
复制内容打开移动云盘PC客户端,操作更方便哦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答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文件的基本步骤,当然还有更多的细节可以根据具体需求进行优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值