利用three.js在Vue项目中展示重构的stl模型文件

devtools/2025/3/1 18:05:08/

一、目的

   为了在前端页面展示3d打印机打印过程

二、前期准备

完整模型的stl文件和模型切割成的n个stl文件

models文件夹下的文件就是切割后的stl文件

三、代码

<template><div ref="threeContainer" class="three-container"></div></template><script>import * as THREE from "three";import { STLLoader } from "three/examples/jsm/loaders/STLLoader.js";import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";export default {name: "CastleDemo",mounted() {this.initThree();},methods: {initThree() {const scene = new THREE.Scene();scene.background = new THREE.Color(0xeeeeee);const camera = new THREE.PerspectiveCamera(75,window.innerWidth / window.innerHeight,0.1,1000);camera.position.set(0, 20, 50); // 调整相机位置,使其离模型更远camera.lookAt(scene.position);const renderer = new THREE.WebGLRenderer({ antialias: true });renderer.setSize(window.innerWidth, window.innerHeight);this.$refs.threeContainer.appendChild(renderer.domElement);const ambientLight = new THREE.AmbientLight(0x404040, 1);const pointLight = new THREE.PointLight(0xffffff, 1, 1000);pointLight.position.set(0, 50, 50);scene.add(ambientLight, pointLight);const loader = new STLLoader();const models = this.generateModels(); // 生成47个模型的配置// 状态变量:控制是否开始旋转let allModelsLoaded = false;// 逐层加载模型let currentModelIndex = 0;const loadNextModel = () => {if (currentModelIndex < models.length) {const model = models[currentModelIndex];loader.load(model.url, (geometry) => {geometry.center();const material = new THREE.MeshStandardMaterial({color: model.color,transparent: true, // 启用透明度opacity: 0.8, // 设置透明度值});const mesh = new THREE.Mesh(geometry, material);mesh.position.set(...model.position);mesh.scale.set(model.scale, model.scale, model.scale);scene.add(mesh);// 动态更新进度currentModelIndex++;loadNextModel();});} else {// 所有模型加载完成allModelsLoaded = true;}};loadNextModel(); // 开始加载第一个模型// 添加轨道控制器const controls = new OrbitControls(camera, renderer.domElement);controls.enableDamping = true; // 启用阻尼效果controls.dampingFactor = 0.25; // 阻尼系数controls.enableZoom = true; // 允许缩放controls.enablePan = true; // 允许平移// 添加旋转逻辑let rotationSpeed = 0.01; // 旋转速度function animate() {requestAnimationFrame(animate);// 只有在所有模型加载完成后才开始旋转if (allModelsLoaded) {scene.traverse((object) => {if (object.isMesh) {object.rotation.y += rotationSpeed; // 绕Y轴旋转object.rotation.x += rotationSpeed * 0.5; // 绕X轴旋转}});}controls.update(); // 更新轨道控制器renderer.render(scene, camera);}animate();},// 生成47个模型的配置generateModels() {const models = [];const basePosition = [0, -36.5, 0]; // 基础位置,从底部开始const spacing = 0.5; // 模型之间的间距for (let i = 0; i < 72; i++) { const position = [basePosition[0], // X轴位置basePosition[1] + i * spacing, // Y轴方向排列,从低到高basePosition[2],];const color = this.getColorByIndex(i); // 根据索引计算颜色models.push({url: `/3Dmodels/castledemo/models/part_${String(i).padStart(6, "0")}.stl`, // 文件名格式为 part_000000.stl 到 part_000046.stlposition,scale: 0.3,color,});}return models;},// 根据索引计算颜色getColorByIndex(index) {const startColor = 0xffff00; // 起始颜色为黄色const endColor = 0x00ffff; // 结束颜色为青色const colorRange = endColor - startColor;const ratio = index / (47 - 1); // 计算颜色比例const color = startColor + Math.floor(colorRange * ratio);return color;},},};</script><style scoped>.three-container {background-color: #ffffff;}</style>

四、最终效果

五、问题

模型重构出来会存在走位的情况,可能需要根据每个模型文件的实际大小进行调整。


http://www.ppmy.cn/devtools/163687.html

相关文章

DeepSeek-V3:AI语言模型的高效训练与推理之路

参考&#xff1a;【论文学习】DeepSeek-V3 全文翻译 在人工智能领域&#xff0c;语言模型的发展日新月异。从早期的简单模型到如今拥有数千亿参数的巨无霸模型&#xff0c;技术的进步令人瞩目。然而&#xff0c;随着模型规模的不断扩大&#xff0c;训练成本和推理效率成为了摆在…

WordPress多语言插件GTranslate

GTranslate是一个免费的WordPress多语言插件&#xff0c;它允许您将网站内容翻译成多种语言。这个插件提供了一个简单易用的界面&#xff0c;让您可以在WordPress后台直接进行翻译操作。以下是GTranslate插件的一些主要特点&#xff1a; 免费使用&#xff1a;GTranslate插件完…

2.部署kafka:9092

官方文档&#xff1a;http://kafka.apache.org/documentation.html (虽然kafka中集成了zookeeper,但还是建议使用独立的zk集群) Kafka3台集群搭建环境&#xff1a; 操作系统: centos7 防火墙&#xff1a;全关 3台zookeeper集群内的机器&#xff0c;1台logstash 软件版本: …

【MySQL】表空间丢失处理(Tablespace is missing for table 错误处理)

问题背景 最近&#xff0c;我在运行一个基于Python爬虫的项目时&#xff0c;爬虫需要频繁与MySQL数据库交互。不幸的是&#xff0c;在数据爬取过程中&#xff0c;Windows系统突然强制更新并重启。这次意外中断导致MySQL数据库的三个表格&#xff08;2022年、2023年和2024年的数…

使用 Docker 管理 Alpine 镜像的完整指南

在这篇博客中&#xff0c;我们将深入探讨如何使用 Docker 命令来拉取、保存和加载 Docker 镜像。我们将以 alpine 镜像为例&#xff0c;展示每个步骤的详细操作和输出示例。【因特殊原因可以借助外网下载镜像&#xff0c;然后导入到本地的服务器】 1. 拉取镜像 (docker pull) …

Brave 132 编译指南 Android 篇 - 获取源代码 (四)

1. 引言 在之前的章节中&#xff0c;我们详细探讨了 Brave 132 浏览器的独特优势、Android 项目的精妙结构&#xff0c;以及编译所需的系统软硬件环境和推荐工具。至此&#xff0c;我们已经为编译 Brave 132 Android 版做好了充分的前期准备。现在&#xff0c;我们将迈出编译之…

清华—北大DeepSeek教学资料合集—从入门到精通【免费下载】

资料下载链接: https://pan.quark.cn/s/55b250244ca3 普通人如何快速高效学习AI&#xff1f; 如果说清华大学发布的《DeepSeek:从入门到精通》是新手小白初识AI最好的学习宝典&#xff0c;那么北京大学发布的DeepSeek学习资料&#xff0c;则是最完美的配套教程。普通人入门A…

IO 和NIO有什么区别?

IO 与 NIO 的区别详解 Java 中的 IO&#xff08;Input/Output&#xff09; 和 NIO&#xff08;New IO 或 Non-blocking IO&#xff09; 是两种不同的输入输出处理机制&#xff0c;主要区别体现在设计模型、性能优化和应用场景上。以下是详细对比&#xff1a; 1. 阻塞与非阻塞模…