three.js入门应用-展示3d模型
前言:
最近公司计划做一个关于计测方面的案子,希望可以以3d的方式去直观的展示测量目标本体以及测量工位等信息。先后决定从达索,华天sview,HOOPS等web3d方案解决商去选取,经过调研之后发现这三款库价格且比较昂贵,并且有的开发文档全英文,有的不详细,反正就是开发上困难不小,加上开始案子都不重视,导致后面赶进度,发现一堆问题。之后技术总监建议去研究使用three.js。于是被迫去扩充自己的技能包。。。 哈哈
准备:
开始去看threejs的官网以及api,发现真的繁多,于是转而去百度,看别人是怎么用的,去借鉴模仿【搬砖】。总结出一套完整的流程为:
1.引入three.js,
【不使用vue等框架,直接以script 方式引入<script src="js/three.js"></script>】 【vue: npm i three => import * as THREE from 'three' 】
2.自己画模型还是引入模型,如果是引入外部模型,就需要去找对应的加载器
以导入外部.mtl模型为例, 就需要用到 ·MTLLoader·
【<script src="js/MTLLoader.js"></script> 】
【import { MTLLoader } from 'three/examples/jsm/loaders/MTLLoader.js', npm安装后,直接引入即可】
3.初始化操作,包括创建场景,创建光源,创建相机,创建渲染器,创建控件对象,加载模型【如果引入外部模型的话】,最后渲染-render
总结如上。
下面是结合vue使用的demo【demo来自 点燃火柴, 导入的STL模型,MTL原理和STL一样通用】:
【不知道是不是 three.js 版本更新的缘故,我直接使用会报错, 百度查阅 需要将场景和scene全局话,不能直接放到 data属性中,这里需要注意!!!】
<script> import * as THREE from 'three' import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js' import { STLLoader } from 'three/examples/jsm/loaders/STLLoader.js' let scene, mesh // 全局化 scene 和 mesh export default { data() { return { camera: null, scene: null, renderer: null, controls: null } }, mounted() { this.init() // console.log(this.mesh) }, methods: { // 初始化 init() { this.createScene() // 创建场景 this.loadSTL() // 加载STL模型 this.createLight() // 创建光源 this.createCamera() // 创建相机 this.createRender() // 创建渲染器 this.createControls() // 创建控件对象 this.render() // 渲染 }, // 创建场景 createScene() { this.scene = new THREE.Scene() }, // 加载STL模型 loadSTL() { const THIS = this const loader = new STLLoader() loader.load( `/model/Back_door.stl`, geometry => { // 创建材质 const material = new THREE.MeshLambertMaterial({ color: 0x7777ff }) this.mesh = new THREE.Mesh(geometry, material) this.mesh.rotation.x = -0.5 * Math.PI this.mesh.scale.set(0.6, 0.6, 0.6) this.scene.add(this.mesh) } ) }, // 创建光源 createLight() { // 环境光 const ambientLight = new THREE.AmbientLight(0xffffff, 0.1) // 创建环境光 this.scene.add(ambientLight) // 将环境光添加到场景 const spotLight = new THREE.SpotLight(0xffffff) // 创建聚光灯 spotLight.position.set(150, 150, 150) spotLight.castShadow = true this.scene.add(spotLight) }, // 创建相机 createCamera() { const element = document.getElementById('container') const width = element.clientWidth // 窗口宽度 const height = element.clientHeight // 窗口高度 const k = width / height // 窗口宽高比 // PerspectiveCamera( fov, aspect, near, far ) this.camera = new THREE.PerspectiveCamera(35, k, 0.1, 1000) this.camera.position.set(250, 250, 150) // 设置相机位置 this.camera.lookAt(new THREE.Vector3(10, 40, 0)) // 设置相机方向 this.scene.add(this.camera) }, // 创建渲染器 createRender() { const element = document.getElementById('container') this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }) this.renderer.setSize(element.clientWidth, element.clientHeight) // 设置渲染区域尺寸 this.renderer.shadowMap.enabled = true // 显示阴影 this.renderer.shadowMap.type = THREE.PCFSoftShadowMap this.renderer.setClearColor(0x3f3f3f, 1) // 设置背景颜色 element.appendChild(this.renderer.domElement) }, render() { if (this.mesh) { // this.mesh.rotation.z += 0.006 } this.renderer.render(this.scene, this.camera) requestAnimationFrame(this.render) }, // 创建控件对象 createControls() { this.controls = new OrbitControls(this.camera, this.renderer.domElement) } } } </script> <template> <div class="main" id="container"></div> </template> <style scoped lang="scss"> .main { width: 100%; height: 100%; position: absolute; } </style>
附上demo展示图:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了