无法在Vue2中使用GLTFLoader加载模型

发布于 2025-01-11 07:30:45 字数 1799 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽清风入怀 2025-01-18 07:30:45

所以我已经修复了codesandbox中当前和后续的问题。请参阅此处: https://codesandbox.io/s/vue-2-playground -forked-tlpdi9

已解决的问题:

  1. 您的 App 组件中的任何位置都没有 Home 组件。
  2. 您应该使用 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:

  1. You don't have the Home component anywhere in your App component.
  2. You should be using three and not three-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

preview

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文