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整个文件夹复制下来
3. 将复制的draco文件夹复制到public文件夹内
const dracoLoader = new DRACOLoader()
dracoLoader.setDecoderPath('/draco/')
5. 大功告成
注意: 请先保证场景摄像机和光源都是正确的, 3D/city.glb中的3D是我在public中创建的名为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) // 点光源添加到场景中
// 环境光
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>
报错:加载glb的代码:load3D() { const loader = new GLTFLoader() const dracoLoader = new DRACOLoader() dracoLoader.setDecoderPath('/draco/') dracoLoader.preload() loader.setDRACOLoader(dracoLoader) loader.load('/3D/city...
Three.js第2篇,Three.js加载glb / gltf模型,Vue加载glb / gltf模型(如何在vue中使用three.js,vue使用threejs加载glb模型)
(function(){
//const loader = new GLTFLoader().setPath( './models/gltf/DamagedHelmet/glTF/' );
//loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
const loader = new GLTFLoader().setPath( '.
import * as THREE from "three"; //引入Threejs
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
首先,创建初始化方法:
init() {
this.scene = n