无法在Vue2中使用GLTFLoader加载模型
我使用 GLTFLoader
将模型加载到我的 Vue2 应用程序。我使用常见的 Three.js 模板。 GLTFLoader
直接取自 Three.js 文档。我的示例非常简单,但即使在这种情况下我也无法加载模型。我不知道我的代码有什么问题,我看不到任何错误:
<template>
<div id="viewport" ref="container"></div>
</template>
<script>
import * as THREE from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
export default {
name: 'Scene',
data() {
return {
camera: null,
scene: null,
renderer: null,
mesh: null
}
},
methods: {
init() {
this.camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10);
this.camera.position.z = 1;
this.scene = new THREE.Scene();
this.renderer = new THREE.WebGLRenderer({antialias: true});
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.$refs.container.appendChild(this.renderer.domElement);
const loader = new GLTFLoader();
loader.load("Avocado.glb", (glb) => {
console.log(glb)
this.scene.add(glb.scene);
},
function (xhr) {
console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
},
function (error) {
console.log("An error happened");
}
);
},
animate() {
requestAnimationFrame(this.animate);
this.renderer.render(this.scene, this.camera);
}
},
mounted() {
this.init();
this.animate();
}
}
</script>
这是一个可重现的示例: https://codesandbox.io/s/vue-2-playground-forked-q6lchp?file=/src/components/Model.vue
I use GLTFLoader
to load model to my Vue2 app. I use common Three.js template. GLTFLoader
is taken directly from Three.js docs. My example is very easy but even in this case I can't load the model. I have no idea what can be wrong with my code, I can't see any errors:
<template>
<div id="viewport" ref="container"></div>
</template>
<script>
import * as THREE from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
export default {
name: 'Scene',
data() {
return {
camera: null,
scene: null,
renderer: null,
mesh: null
}
},
methods: {
init() {
this.camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10);
this.camera.position.z = 1;
this.scene = new THREE.Scene();
this.renderer = new THREE.WebGLRenderer({antialias: true});
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.$refs.container.appendChild(this.renderer.domElement);
const loader = new GLTFLoader();
loader.load("Avocado.glb", (glb) => {
console.log(glb)
this.scene.add(glb.scene);
},
function (xhr) {
console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
},
function (error) {
console.log("An error happened");
}
);
},
animate() {
requestAnimationFrame(this.animate);
this.renderer.render(this.scene, this.camera);
}
},
mounted() {
this.init();
this.animate();
}
}
</script>
Here is a reproducible example: https://codesandbox.io/s/vue-2-playground-forked-q6lchp?file=/src/components/Model.vue
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我已经修复了codesandbox中当前和后续的问题。请参阅此处: https://codesandbox.io/s/vue-2-playground -forked-tlpdi9。
已解决的问题:
App
组件中的任何位置都没有Home
组件。Three 而不是
Three-js
上面链接的codesandbox中有更多内容,例如增加相机的远距离值并添加照明以便您可以看到鳄梨
so I've fixed the current and subsequent issues in the codesandbox. See here: https://codesandbox.io/s/vue-2-playground-forked-tlpdi9.
The problems resolved:
Home
component anywhere in yourApp
component.three
and notthree-js
There's more in the codesandbox linked above, like increasing the far value for your camera and adding lighting so that you can see the avocado