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

Vue2+Three.js加载并展示一个三维模型(提供Gitee源码)

目录

一、案例截图

二、安装Three.js

三、代码实现

四、Gitee源码


一、案例截图

二、安装Three.js

npm install three

三、代码实现

模型资源我是放在public文件夹下面的:

完整代码: 

<template><div><div ref="container"></div></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 {name: "HomeView",data() {return {scene: null,camera: null,renderer: null,model: null,controls: null,width: window.innerWidth,height: window.innerHeight,};},mounted() {this.initThreeJs();this.loadModel();this.animate();},beforeDestroy() {if (this.renderer) {this.renderer.dispose();}},methods: {initThreeJs() {// 初始化场景this.scene = new THREE.Scene();// this.scene.background = new THREE.Color(0xffffff); // 白色背景// 设置相机this.camera = new THREE.PerspectiveCamera(75, this.width / this.height, 0.1, 5000);// 设置相机初始位置this.camera.position.set(0, 0.3, 1); // x = 0, y = 5, z = 10,可以根据需要调整这些值// 设置相机旋转角度(以弧度为单位)this.camera.rotation.x = THREE.MathUtils.degToRad(-30); // 绕x轴旋转 -30度this.camera.rotation.y = THREE.MathUtils.degToRad(0); // 绕y轴旋转 0度this.camera.rotation.z = THREE.MathUtils.degToRad(0); // 绕z轴旋转 0度// 创建渲染器this.renderer = new THREE.WebGLRenderer();this.renderer.setSize(this.width, this.height);this.$refs.container.appendChild(this.renderer.domElement);// 添加灯光const ambientLight = new THREE.AmbientLight(0xffffff, 1);this.scene.add(ambientLight);const pointLight = new THREE.PointLight(0xffffff, 1);pointLight.position.set(10, 10, 10);this.scene.add(pointLight);// 添加 OrbitControlsthis.controls = new OrbitControls(this.camera, this.renderer.domElement);this.controls.enableDamping = true; // 启用阻尼this.controls.dampingFactor = 0.25; // 阻尼因子,越大越慢this.controls.enableZoom = true; // 启用缩放},loadModel() {const loader = new GLTFLoader();loader.load('/obj.gltf', // 模型的路径,支持http请求(gltf) => {this.model = gltf.scene;this.scene.add(this.model);},undefined,(error) => {console.error('模型加载错误:', error);});},animate() {requestAnimationFrame(this.animate);if (this.model) {this.model.rotation.y += 0.01; // 让模型旋转}this.renderer.render(this.scene, this.camera);},}
};
</script><style scoped lang="scss"></style>

四、Gitee源码

码云地址:Vue2+Three.js加载并展示一个三维模型 

http://www.lryc.cn/news/543605.html

相关文章:

  • Linux红帽:RHCSA认证知识讲解(三)Linux基础指令与Vim编辑器的使用
  • python读取sqlite温度数据,并画出折线图
  • 《论企业集成平台的理解与应用》审题技巧 - 系统架构设计师
  • UE Python笔记
  • 使用django调用deepseek api,搭建ai网站
  • YOLOv12 ——基于卷积神经网络的快速推理速度与注意力机制带来的增强性能结合
  • 两台互通的服务器使用Docker部署一主两从MySQL8.0.35
  • Java23种设计模式案例
  • stm32hal库寻迹+蓝牙智能车(STM32F103C8T6)
  • JavaScript知识点4
  • 形式化数学编程在AI医疗中的探索路径分析
  • QT 引入Quazip和Zlib源码工程到项目中,无需编译成库,跨平台,加密压缩,带有压缩进度
  • Ubuntu 安装 Nginx并配置反向代理
  • GitHub SSH连接问题解决指南
  • C++ 跨平台的 GetCurrentThreadId() 获取当前线程ID实现
  • 钉钉MAKE AI生态大会思考
  • SQL笔记#复杂查询
  • 【Linux】基于UDP/TCP套接字编程与守护进程
  • springboot 引入前端
  • RTSP/Onvif安防平台EasyNVR接入EasyNVS显示服务缺失的原因与解决方案
  • 算法系列之回溯算法
  • Uniapp 小程序接口封装与使用
  • Harmony开发笔记(未完成)
  • 观成科技:海莲花“PerfSpyRAT”木马加密通信分析
  • Spring Boot @Async 注解深度指南
  • windows设置暂停更新时长
  • Orange 开源项目 - 集成百度智能云-千帆大模型
  • 特斯拉 FSD 算法深度剖析:软件层面全解读
  • 2025/2/17--2/23学习笔记(week1)_C语言
  • 数据结构:二叉树的数组结构以及堆的实现详解