vue Three.js加载.gltf格式的3d文件

一、实现效果,选中3d模型,可拖动、旋转、放大和缩小

二、前提

把.gltf文件放到public/static文件夹路径下,如下图

Tips:如果想下载.gltf的格式文件,可以该网站下载:

Sketchfabhttps://sketchfab.com/search?q=gltf&type=models

三、导入three.js

npm install three

四、引入three.js,由于GLTFLoader和OrbitControls属于three.js,按照路径引入就可以了,不用关心引入路径,因为npm已经对应上了。

import * as THREE from "three";

import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";

import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";

五、定义div,id为container

<div id="container" style="width: 100%; height: 100%"></div>

六、完整实现代码如下

属性可以参照教程查看:Three.js教程http://www.webgl3d.cn/Three.js/

<template>
  <!-- 3d -->
  <div id="container" style="width: 1000px; height: 500px"></div>
</template>

<script>
import * as THREE from "three";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";

export default {
  data() {
    return {
      publicPath: process.env.BASE_URL,
      mesh: null,
      camera: null,
      scene: null,
      renderer: null,
      controls: null,
    };
  },
  created() {},
  mounted() {
    this.init();
  },

  methods: {
    // 初始化
    init() {
      this.createScene(); // 创建场景
      this.loadGLTF(); // 加载GLTF模型
      this.createLight(); // 创建光源
      this.createCamera(); // 创建相机
      this.createRender(); // 创建渲染器
      this.createControls(); // 创建控件对象
      this.render(); // 渲染
    },
    // 创建场景
    createScene() {
      this.scene = new THREE.Scene();
    },
    // 加载GLTF模型
    loadGLTF() {
      const THIS = this;
      const loader = new GLTFLoader();
      loader.load(
        //this.publicPath + "static/gltf/ha/scene.gltf",
        this.publicPath + "static/gltf/devicegltf/scene.gltf",
        (model) => {
          model.scene.children[0].scale.set(50, 50, 50);
          this.scene.add(model.scene.children[0]);
        }
      );
    },

    // 创建光源
    createLight() {
      // 环境光
      const ambientLight = new THREE.AmbientLight(0xffffff, 0.1); // 创建环境光
      this.scene.add(ambientLight); // 将环境光添加到场景

      const spotLight = new THREE.SpotLight(0xffffff); // 创建聚光灯
      spotLight.position.set(150, 150, 150);
      spotLight.castShadow = true;
      this.scene.add(spotLight);
    },
    // 创建相机
    createCamera() {
      const element = document.getElementById("container");
      const width = element.clientWidth; // 窗口宽度
      const height = element.clientHeight; // 窗口高度
      const k = width / height; // 窗口宽高比
      // PerspectiveCamera( fov, aspect, near, far )
      this.camera = new THREE.PerspectiveCamera(35, k, 0.1, 1000);
      this.camera.position.set(150, 150, 150); // 设置相机位置

      this.camera.lookAt(new THREE.Vector3(10, 40, 0)); // 设置相机方向
      this.scene.add(this.camera);
    },
    // 创建渲染器
    createRender() {
      const element = document.getElementById("container");
      this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
      this.renderer.setSize(element.clientWidth, element.clientHeight); // 设置渲染区域尺寸
      this.renderer.shadowMap.enabled = true; // 显示阴影
      this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
      this.renderer.setClearColor(0x3f3f3f, 1); // 设置背景颜色
      //this.renderer.setClearColor(0xf5f5f5, 1); // 设置背景颜色
      element.appendChild(this.renderer.domElement);
    },

    render() {
      if (this.mesh) {
        this.mesh.rotation.z += 0.006;
      }
      this.renderer.render(this.scene, this.camera);
      requestAnimationFrame(this.render);
    },
    // 创建控件对象
    createControls() {
      this.controls = new OrbitControls(this.camera, this.renderer.domElement);
    },
  },
};
</script>

参考地址:

Vue中使用Three.js加载glTF模型 - 百度文库https://wenku.baidu.com/view/b19b491bba0d6c85ec3a87c24028915f804d84e5.html

  • 6
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答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: Vuethree.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文件还需要注意一些细节,比如模型坐标系的问题、贴图和纹理的路径配置、模型大小适配等等,这需要开发者仔细排查。 总之,通过Vuethree.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文件的基本步骤,当然还有更多的细节可以根据具体需求进行优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值