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

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.lryc.cn/news/489373.html

相关文章:

  • 部落商城App开发笔记 2024.11.21 实现进入app就是短视频
  • 解决.DS_Store 在项目一致无法排除,.gitignore里也不生效
  • MySQL-关键字执行顺序
  • 极客时间《Redis核心技术与实战》开篇词 知识点总结
  • TCP并发服务器
  • Debug-031-近期功能实现小结
  • Consumer Group
  • .NET架构师学习大纲
  • 【代码随想录】贪心
  • Harmony鸿蒙类似与Android中broadcast广播的api使用及释义
  • openGauss 6.0.0主备部署(企业版)
  • 【机器学习】聚类算法原理详解
  • Ubuntu20.04从零安装IsaacSim/IsaacLab
  • 基于Java Springboot大学校园旧物捐赠网站
  • 【Java 集合】Collections 空列表细节处理
  • 大数据实验4-HBase
  • deepin系统下载pnpm cnpm等报错
  • #Js篇:JSON.stringify 和 JSON.parse用法和传参
  • c#通过网上AI大模型实现对话功能
  • pymysql模块
  • WPF-模板和样式
  • 网络编程 day1.2~day2——TCP和UDP的通信基础(TCP)
  • element ui table 每行不同状态
  • 力扣--LRC 142.训练计划IV
  • windows下,用CMake编译qt项目,出现错误By not providing “FindQt5.cmake“...
  • 【element-tiptap】Tiptap编辑器核心概念----结构篇
  • 半导体工艺与制造篇3 离子注入
  • 利用开源的低代码表单设计器FcDesigner高效管理和渲染复杂表单结构
  • 淘宝 NPM 镜像源
  • i春秋-GetFlag(md5加密,字符串比较绕过)