three.js在Vue项目中加载fbx模型文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | <template> <div> <div id= "threeContained" ></div> </div> </template> <script> import * as THREE from "three" ; //引入Threejs import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader" ; import { OrbitControls } from "three/examples/jsm/controls/OrbitControls" ; import Stats from "three/examples/jsm/libs/stats.module" ; // const OrbitControls = require("three-orbit-controls")(THREE); export default { name: "vue-three" , data() { return { scene: "" , light: "" , camera: "" , controls: "" , renderer: "" , load: '' , clock: '' , mixer: '' }; }, methods: { init() { var that = this ; let container = document.getElementById( "threeContained" ); // 创建场景 that.scene = new THREE.Scene(); that.scene.background = new THREE.Color(0x8cc7de); // 创建相机 that.camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 2000 ); // that.camera.position.set( -70, 25, 90 ); // 定位相机,并且指向场景中心 that.camera.position.x = 30; that.camera.position.y = 30; that.camera.position.z = 170; that.camera.lookAt(that.scene.position) /// 显示三维坐标系 var axes = new THREE.AxesHelper(100) // 添加坐标系到场景中 that.scene.add(axes); // 创建地面 // const geometry = new THREE.BoxGeometry(); // const material = new THREE.MeshPhongMaterial({ color: 0xffffff }); // const cube = new THREE.Mesh(geometry, material); // that.scene.add(cube); // 创建地面的几何体 var planeGeometry = new THREE.PlaneGeometry(100, 60); // 给地面物体上色 var planeMaterial = new THREE.MeshStandardMaterial({ color: 0xcccccc }); // 创建地面 var plane = new THREE.Mesh(planeGeometry, planeMaterial); plane.material.opacity = 0.6; plane.material.transparent = true ; plane.rotation.x = -0.5 * Math.PI; plane.position.x = 0; plane.position.y = 0; plane.position.z = 0; plane.castShadow = true ; // 接收阴影 plane.receiveShadow = true ; that.scene.add(plane); const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444); hemiLight.position.set(0, 1, 0); that.scene.add(hemiLight); const directionalLight1 = new THREE.DirectionalLight(0xffeeff, 0.8); directionalLight1.position.set(1, 1, 1); that.scene.add(directionalLight1); const directionalLight2 = new THREE.DirectionalLight(0xffffff, 0.8); directionalLight2.position.set(-1, 0.5, -1); that.scene.add(directionalLight2); const ambientLight = new THREE.AmbientLight(0xffffee, 0.25); that.scene.add(ambientLight); // stats that.stats = new Stats(); container.appendChild(that.stats.dom); // 材质 const normal = new THREE.TextureLoader().load( 'models/shanghai/textures/shanghai.jpg' ); // model that.loader = new FBXLoader(); that.loader.load( "/models/shanghai/model/shanghai.fbx" , function (object) { // that.mixer = new THREE.AnimationMixer( object ); // const action = that.mixer.clipAction( object.animations[ 0 ] ); // action.play(); // object.traverse( function ( child ) { // if ( child.isMesh ) { // child.castShadow = true; // child.receiveShadow = true; // } // }); object.traverse( function ( child ) { if ( child.isMesh ) { child.material.specular.setScalar( 0.1 ); child.material.normalMap = normal; } } ); console.log(object); object.scale.set(0.1,0.1,0.1) // object.scale.set(1,1,1) that.scene.add(object); that.animate(); }); // 创建渲染器 that.renderer = new THREE.WebGLRenderer(); that.renderer.setPixelRatio(window.devicePixelRatio); // 设置渲染器的初始颜色 that.renderer.setClearColor( new THREE.Color(0xeeeeee)); // 设置输出canvas画面的大小 that.renderer.setSize(window.innerWidth, window.innerHeight); container.appendChild(that.renderer.domElement); const controls = new OrbitControls(that.camera, that.renderer.domElement); controls.target.set(0, 12, 0); controls.update(); // debugger // window.addEventListener("onpointerdown", that.selectObject()); window.addEventListener( "resize" , that.onWindowResize); }, onWindowResize() { this .camera.aspect = window.innerWidth / window.innerHeight; this .camera.updateProjectionMatrix(); this .renderer.setSize(window.innerWidth, window.innerHeight); }, animate() { // requestAnimationFrame(this.animate); // this.renderer.render(this.scene, this.camera); // this.stats.update(); requestAnimationFrame( this .animate ); const delta = this .clock.getDelta(); if ( this .mixer ) this .mixer.update( delta ); this .renderer.render( this .scene, this .camera ); this .stats.update(); }, selectObject(event) { debugger if (event.button != 0) return ; const mouse = new THREE.Vector2(); mouse.x = (event.clientX / window.innerWidth) * 2 - 1; mouse.y = -(event.clientY / window.innerHeight) * 2 + 1; const raycaster = new THREE.Raycaster(); raycaster.setFromCamera(mouse, this .camera); const intersected = raycaster.intersectObjects( this .scene.children, false ); console.log(intersected) if (intersected.length) { const found = intersected[0]; const faceIndex = found.faceIndex; const geometry = found.object.geometry; console.log( this .load) // const id = ifcLoader.ifcManager.getExpressId(geometry, faceIndex); const modelID = found.object.modelID; // ifcLoader.ifcManager.createSubset( { modelID, ids: [ id ], scene, removePrevious: true, material: highlightMaterial } ); // const props = ifcLoader.ifcManager.getItemProperties(modelID, id, true); // console.log(props); // this.renderer.render( this.scene, this.camera ); } }, }, mounted() { this .clock = new THREE.Clock(); this .init(); this .animate(); window.onpointerdown = this .selectObject; }, }; </script> <style scoped> html, body { height: 100%; } /* #container { width: 100%; margin: 0 auto; height: 1000px; overflow: hidden; } */ </style> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
2018-11-28 三十七、小程序页面跳转传参参数值为url时参数丢失