【问题标题】:How to use ThreeJS with VueJS如何在 VueJS 中使用 ThreeJS
【发布时间】:2019-08-08 22:46:20
【问题描述】:

我想使用threeJS,所以我使用npm i three --save 安装它。我按照threeJS文档中的基本教程进行操作,但出现错误。

  mounted () {
    this.initThree();
  },
  methods: {
    initThree() {
      this.scene = new THREE.Scene();
      this.camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

      this.renderer = new THREE.WebGLRenderer( { canvas: document.getElementById( "background" ), alpha: true, antialias: true } );
      this.renderer.setSize( window.innerWidth, window.innerHeight );

      let geometry = new THREE.BoxGeometry( 1, 1, 1 );
      let material = new THREE.MeshBasicMaterial( { color: 0x0000ff } );
      this.cube = new THREE.Mesh( geometry, material );
      this.scene.add( this.cube );

      this.camera.position.z = 5;
      this.animate()

    },

    animate() {

      this.cube.rotation.x += 0.01;
      this.renderer.render( this.scene, this.camera );
      requestAnimationFrame( this.animate() );

    }
  }
}

【问题讨论】:

  • 我认为您需要在 main.js 中初始化三个。不在组件中。或许可以考虑github.com/fritx/vue-threejs
  • 我初始化threejs没有问题,但是当我使用递归函数时。你的链接也许可以解决我的问题,我会试试的,谢谢
  • 您在哪里以及如何导入/声明您的三个模块?

标签: vue.js three.js


【解决方案1】:

您的问题与 VueJS /ThreeJs 无关,您应该简单地修复这一行:

requestAnimationFrame( this.animate() );

应该是:

requestAnimationFrame(this.animate)

请阅读有关回调的更多信息:https://developer.mozilla.org/en-US/docs/Glossary/Callback_function

编辑:您可以尝试 TroisJS,轻松将 ThreeJS 与 VueJS 集成:https://github.com/troisjs/trois

【讨论】:

猜你喜欢
  • 2023-03-07
  • 1970-01-01
  • 2016-12-16
  • 1970-01-01
  • 2017-10-25
  • 2017-07-21
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
相关资源
最近更新 更多