当前位置: 首页 > article >正文

vue2中使用three.js步骤

1.使用npm 下载依赖这里以0.158.0版本为例

npm install three@0.158.0 --save

2.

<template>
  <div id="container"></div>
</template>

<script>
import * as THREE from 'three';
import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader.js';
import { MTLLoader } from 'three/examples/jsm/loaders/MTLLoader.js';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
import { FBXLoader } from 'three/examples/jsm/loaders/FBXLoader.js';
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js';

export default {
  name: 'ThreeJsModelLoader',
  mounted() {
    const container = document.getElementById('container');
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(45, container.offsetWidth / container.offsetHeight, 0.1, 1000);
    camera.position.set(7, 7, 7)

    const renderer = new THREE.WebGLRenderer({
      alpha: true,//渲染器透明
      antialias: true,//抗锯齿
      precision: 'highp',//着色器开启高精度
    }
    );
    //开启HiDPI设置
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(container.offsetWidth, container.offsetHeight);

    container.appendChild(renderer.domElement);
    renderer.setClearColor(0xffffff);
    // // 光源强度
    const ambientLight = new THREE.AmbientLight(0xffffff,2);
    // pointLight.position.set(0, 0, 0);
    scene.add(ambientLight);
    // 平行光源
    const directionalLight = new THREE.DirectionalLight(0xffffff,2);
    scene.add(directionalLight);

    // 创建控件
    const controls = new OrbitControls(camera, renderer.domElement);
    const objLoader = new OBJLoader();
    const mtlLoader = new MTLLoader();
    const loader = new GLTFLoader();
    const fbxloader = new FBXLoader();
    const dracoLoader = new DRACOLoader();//压缩模型
    // 加载材质和OBJ模型
    // mtlLoader.load('/static/model/objvs.mtl', (materials) => {
    //   materials.preload();
    //   objLoader.setMaterials(materials).load('/static/model/dvg.obj', (object) => {
    //     scene.add(object);
    //     animate();
    //   });
    // });

    // 加载材质和glb模型
    // loader.load('/static/model/glbmodel.glb', function (gltf) {
    //   // 获取模型的尺寸
    //   const box = new THREE.Box3().setFromObject(gltf.scene);
    //   const size = box.getSize(new THREE.Vector3()).length();

    //   // 设置模型到单位球体大小
    //   const scaleFactor = 20 / size;
    //   gltf.scene.scale.set(scaleFactor, scaleFactor, scaleFactor);

    //   scene.add(gltf.scene);
    //   animate();
    // }, undefined, function (err) {
    //   console.error('失败了啊', err);
    // });

    // 配置Draco解码路径
    dracoLoader.setDecoderPath('three/examples/jsm/libs/draco/');
    dracoLoader.setDecoderConfig({ type: 'js' });
    loader.setDRACOLoader(dracoLoader);
    //加载fbx模型
    fbxloader.load('/static/model/family.fbx', (object) => {
      //  获取模型的尺寸
      const box = new THREE.Box3().setFromObject(object);
      const size = box.getSize(new THREE.Vector3()).length();
      // 设置模型到单位球体大小
      const scaleFactor = 10 / size;
      object.scale.set(scaleFactor, scaleFactor, scaleFactor);
      scene.add(object); // 将模型添加到场景中
      animate();
    });
    function animate() {
      requestAnimationFrame(animate);
      renderer.render(scene, camera);
      controls.update(); // 更新控件状态
    }
  }
}
</script>

<style>
#container {
  width: 100vw;
  height: 100vh;
}
</style>


http://www.kler.cn/a/401681.html

相关文章:

  • 微服务网关聚合swagger(knife4j版本)
  • 【环境配置】ubuntu-jetson上的定时任务
  • STM32设计井下瓦斯检测联网WIFI加Zigbee多路节点协调器传输-分享
  • 【Qt】使用QString的toLocal8Bit()导致的问题
  • 【Linux学习】【Ubuntu入门】1-6 ubuntu文件系统结构
  • 使用 Elasticsearch 构建食谱搜索(二)
  • 微信小程序申请getlocation权限
  • 【视频讲解】Python深度神经网络DNNs-K-Means(K-均值)聚类方法在MNIST等数据可视化对比分析...
  • 同向双指针
  • Excel如何把两列数据合并成一列,4种方法
  • 微信小程序 https://thirdwx.qlogo.cn 不在以下 downloadFile 合法域名列表中
  • 自动化仪表故障排除法
  • Ubuntu 系统下,如何清空 swap 分区
  • Swift 宏(Macro)入门趣谈(四)
  • 数据结构(一)链表
  • 【Unity基础】对比Unity中两种粒子系统
  • ubuntu系统中使用docker-compose安装部署docker集群(本地)
  • 聚焦 NLP 和生成式 AI 的创新与未来 基础前置知识点
  • 多目标优化算法:多目标鳗鱼和石斑鱼优化算法(MOEGO)求解ZDT1、ZDT2、ZDT3、ZDT4、ZDT6,提供完整MATLAB代码
  • vue2+a-table——实现框选选中功能——js技能提升