Vue使用Three.js 渲染glb、gltf模型

网址(加载三维模型gltf):1. 建模软件绘制3D场景(Blender) | Three.js中文网

  • 引入
    <template>
      <div id="webgl"></div>
    </template>
    
    <script lang="ts" setup>
    import { onMounted, onUnmounted } from "vue";
    import * as THREE from "three";
    import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";//加载器
    import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";//鼠标控制器
    import { RoomEnvironment } from "three/examples/jsm/environments/RoomEnvironment";//材质
    </script>
    
  • 渲染
    let animateId: any = null;
    onMounted(() => {
      // 创建场景
      const scene = new THREE.Scene();
      // const url = "/preview.glb"; //本地,public下路径文件地址
      const url = `/api/preview/model/${directoryId}/${id}`;//获取glb文件接口地址
      scene.background = new THREE.Color(0xfffffff);//场景背景色
    
      // 创建相机
      const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 100);
      camera.position.set(2, 2, 2);
      camera.lookAt(scene.position);
    
    
      // 创建默认光源
      // const directionalLight = new THREE.DirectionalLight(0xbbbbbb, 1); // 平行光,参数1为强度
      // directionalLight.position.set(1, 1, 1); // 光源位置
      // scene.add(directionalLight); // 将光源添加到场景
      // 添加环境光
      // const ambientLight = new THREE.AmbientLight(0xffffff, 0.9);
      // scene.add(ambientLight);
    
      // 创建渲染器
      const renderer = new THREE.WebGLRenderer();
      renderer.setSize(700, 420);
      document.body.appendChild(renderer.domElement);
    
    
      // 控制器
      let control = new OrbitControls(camera, renderer.domElement);
      control.target.set(0, 0, 0);
    
      // 地表格
      // const grid = new THREE.GridHelper(500, 100, 0xffffff, 0xffffff);
      // grid.material.opacity = 0.5;
      // grid.material.depthWrite = false;
      // grid.material.transparent = true;
      // scene.add(grid);
    
      // 材质
      const environment = new RoomEnvironment();
      const pmremGenerator = new THREE.PMREMGenerator(renderer);
      scene.environment = pmremGenerator.fromScene(environment).texture;
    
      // 加载 glb 格式的 3D 模型
      const loader = new GLTFLoader();
      loader.load(
        url,
        (gltf: any) => {
          // 加载成功后的回调函数
          const model = gltf.scene;
          // model.scale.set(0.8, 0.8, 0.8); // 缩小模型
          const box = new THREE.Box3().setFromObject(model);
          const center = box.getCenter(new THREE.Vector3());
          model.position.sub(center); // 将模型位置移到原点处
          scene.add(model);
        },
        (xhr: any) => {
          // 加载过程中的回调函数
          console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
        },
        (error: any) => {
          // 加载失败的回调函数
          console.error("Failed to load model", error);
        }
      );
    
      // 渲染场景
      const animate = () => {
        //动画帧
        animateId = requestAnimationFrame(animate);
        renderer.render(scene, camera);
      };
      animate();
    });
  • 销毁,组件销毁清除渲染动画,减少不必要的调用
    // 销毁场景
    onUnmounted(() => {
      cancelAnimationFrame(animateId);
    });

  • 18
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于Vue中加载GLTF模型,重新设置旋转点的方法与上面的示例非常相似。以下是一个基本的代码示例: ```javascript <template> <div id="container"></div> </template> <script> import * as THREE from 'three'; import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'; export default { name: 'MyGLTFModel', data() { return { scene: null, camera: null, renderer: null, model: null, pivot: null } }, mounted() { // 初始化场景、相机和渲染器 this.initThree(); // 加载GLTF模型 const loader = new GLTFLoader(); loader.load('myModel.gltf', (gltf) => { // 获取模型 this.model = gltf.scene; // 创建空Object3D this.pivot = new THREE.Object3D(); // 将模型添加到Object3D中 this.pivot.add(this.model); // 将模型位置设置为相对于Object3D的位置 this.model.position.set(-10, 0, 0); // 将Object3D位置设置为旋转点的位置 this.pivot.position.set(10, 0, 0); // 将Object3D添加到场景中 this.scene.add(this.pivot); // 开始渲染循环 this.render(); }); }, methods: { initThree() { // 创建场景 this.scene = new THREE.Scene(); // 创建相机 this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); this.camera.position.set(0, 0, 20); // 创建渲染器 this.renderer = new THREE.WebGLRenderer({ antialias: true }); this.renderer.setSize(window.innerWidth, window.innerHeight); // 将渲染器添加到DOM元素中 const container = document.getElementById('container'); container.appendChild(this.renderer.domElement); }, render() { // 在渲染循环中旋转Object3D this.pivot.rotation.y += 0.01; // 渲染场景和相机 this.renderer.render(this.scene, this.camera); // 请求下一帧 requestAnimationFrame(this.render); } } } </script> ``` 在这个例子中,我们创建了一个空的Object3D对象,并将GLTF模型添加为其子元素。然后,我们将模型的位置设置为相对于空Object3D的位置,并将空Object3D的位置设置为旋转点的位置。在渲染循环中,我们旋转Object3D而不是模型,以便将旋转应用于模型。 需要注意的是,这个示例是一种基本的方法。在实际开发中,您可能需要更复杂的逻辑来管理多个模型和旋转点。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值