Vue+Three加載glb文件報錯問題及解決

報錯

加載glb的代碼
load3D() {
const loader = new GLTFLoader()
const dracoLoader = new DRACOLoader()
dracoLoader.setDecoderPath('/draco/')
dracoLoader.preload()
loader.setDRACOLoader(dracoLoader)
loader.load('/3D/city.glb', (gltf) => {
this.scene.add(gltf.scene)
this.renderer.render(this.scene, this.camera)
}, (xhr) => {
console.log((xhr.loaded / xhr.total) * 100 + '% loaded')
}, (error) => {
console.error(error)
})
}解決方式
1. glb模型文件請放到public文件下,否則會無法查找到(打包后其他文件都會加上一串編碼)
2. 前往node_modules文件下 找到three文件夾, 找到/examples/js/libs/draco/ 將draco整個文件夾復(fù)制下來
3. 將復(fù)制的draco文件夾復(fù)制到public文件夾內(nèi)
const dracoLoader = new DRACOLoader()
dracoLoader.setDecoderPath('/draco/')5. 大功告成
注意:
- 請先保證場景攝像機和光源都是正確的
- 3D/city.glb中的3D是我在public中創(chuàng)建的名為3D的文件夾
完整代碼
<template>
<section>
<section id="container"></section>
</section>
</template>
<script>
import * as Three from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader'
export default {
name: 'Index',
data() {
return {
camera: null,
scene: null,
renderer: null,
mesh: null
}
},
mounted() {
this.init()
this.animate()
},
methods: {
init() {
const container = document.getElementById('container')
this.camera = new Three.PerspectiveCamera(90, container.clientWidth / container.clientHeight, 0.1, 10000)
this.renderer = new Three.WebGLRenderer({ antialias: true })
this.camera.position.set(200, 200, 400)
this.scene = new Three.Scene()
this.renderer.setClearColor(new Three.Color(0xF7F2F1))
this.renderer.setSize(container.clientWidth, container.clientHeight)
this.renderer.shadowMap.enabled = true
container.appendChild(this.renderer.domElement)
this.controls = new OrbitControls(this.camera, this.renderer.domElement)
this.controls.target = new Three.Vector3(0, 0, 0)
this.loadLight()
this.load3D()
},
load3D() {
const loader = new GLTFLoader()
const dracoLoader = new DRACOLoader()
dracoLoader.setDecoderPath('/draco/')
dracoLoader.preload()
loader.setDRACOLoader(dracoLoader)
loader.load('/3D/city.glb', (gltf) => {
this.scene.add(gltf.scene)
this.renderer.render(this.scene, this.camera)
}, (xhr) => {
console.log((xhr.loaded / xhr.total) * 100 + '% loaded')
}, (error) => {
console.error(error)
})
},
loadLight() {
// 點光源
// const point = new Three.PointLight(0xffffff)
// point.position.set(4000, 4000, 4000) // 點光源位置
// this.scene.add(point) // 點光源添加到場景中
// 環(huán)境光
const ambient = new Three.AmbientLight(0xFFFFFF)
this.scene.add(ambient)
},
animate() {
requestAnimationFrame(this.animate)
this.renderer.render(this.scene, this.camera)
}
}
}
</script>
<style scoped>
#container {
width: 100%;
height: calc(100vh - 84px);
}
</style>總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
前端vue中實現(xiàn)嵌入代碼編輯器的詳細(xì)代碼
隨著Web技術(shù)的不斷發(fā)展,前端開發(fā)也正日新月異,下面這篇文章主要給大家介紹了關(guān)于前端vue中實現(xiàn)嵌入代碼編輯器的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
Vue組件庫ElementUI實現(xiàn)表格列表分頁效果
這篇文章主要為大家詳細(xì)介紹了Vue組件庫ElementUI實現(xiàn)表格列表分頁效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
Vue項目中實現(xiàn)描點跳轉(zhuǎn)scrollIntoView的案例
這篇文章主要介紹了Vue項目中實現(xiàn)描點跳轉(zhuǎn)scrollIntoView的案例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
整理項目中vue.config.js打包優(yōu)化配置方法
這篇文章主要介紹了整理項目中vue.config.js打包優(yōu)化,包括配置?webpack-bundle-analyzer?插件查看文件大小及配置compression-webpack-plugin?用gzip壓縮打包的文件大小,本文結(jié)合實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
Vue純前端實現(xiàn)導(dǎo)出excel中的圖片與文件超鏈接
這篇文章主要為大家詳細(xì)介紹了Vue如何純前端實現(xiàn)導(dǎo)出excel中的圖片與文件超鏈接,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2025-03-03

