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路由跳轉(zhuǎn)與接收參數(shù)的實現(xiàn)方式
這篇文章主要介紹了Vue路由跳轉(zhuǎn)與接收參數(shù)的實現(xiàn)方式,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
深入理解Vue-cli搭建項目后的目錄結(jié)構(gòu)探秘
本篇文章主要介紹了深入理解Vue-cli搭建項目后的目錄結(jié)構(gòu)探秘,具有一定的參考價值,有興趣的可以了解一下2017-07-07
vue 實現(xiàn)在同一界面實現(xiàn)組件的動態(tài)添加和刪除功能
這篇文章主要介紹了vue 實現(xiàn)在同一界面實現(xiàn)組件的動態(tài)添加和刪除,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
Vue input控件通過value綁定動態(tài)屬性及修飾符的方法
這篇文章主要介紹了 Vue input控件通過value綁定動態(tài)屬性及修飾符的方法,需要的朋友可以參考下2017-05-05
Vue項目結(jié)合Vue-layer實現(xiàn)彈框式編輯功能(實例代碼)
這篇文章主要介紹了Vue項目中結(jié)合Vue-layer實現(xiàn)彈框式編輯功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
VUE3使用Element-Plus時如何修改ElMessage中的文字大小
在使用Element-plus的Elmessage時使用默認的size無法滿足我們的需求時,我們可以自定義字體的大小,但是直接重寫樣式后,并沒有起作用,甚至使用::v-deep深度選擇器也沒有效果,本文介紹VUE3使用Element-Plus時如何修改ElMessage中的文字大小,感興趣的朋友一起看看吧2023-09-09
一文讀懂vue動態(tài)屬性數(shù)據(jù)綁定(v-bind指令)
這篇文章主要介紹了vue動態(tài)屬性數(shù)據(jù)綁定(v-bind指令)的相關(guān)資料,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07

