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解決element-ui消息提示$message重疊問題
這篇文章主要為大家介紹了Vue解決element-ui消息提示$message重疊問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
vue3.x源碼剖析之數(shù)據(jù)響應(yīng)式的深入講解
這篇文章主要給大家介紹了關(guān)于vue3.x源碼剖析之數(shù)據(jù)響應(yīng)式的相關(guān)資料,在講解過程中,我們會對比Vue2.x的API特性,使用有哪些區(qū)別,需要的朋友可以參考下2022-01-01
Pycharm中開發(fā)vue?element項目時eslint的安裝和使用步驟
這篇文章主要介紹了Pycharm中開發(fā)vue?element項目時eslint的安裝和使用,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05
如何為vuex實現(xiàn)帶參數(shù)的 getter和state.commit
這篇文章主要介紹了如何為vuex實現(xiàn)帶參數(shù)的getter和state.commit,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
vue項目完成后如何實現(xiàn)項目優(yōu)化的示例
本文主要介紹了vue項目完成后如何實現(xiàn)項目優(yōu)化的示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12

