【问题标题】:Import image with threejs in VueJS project在VueJS项目中使用threejs导入图像
【发布时间】:2023-03-07 20:39:01
【问题描述】:

我按照 vuejs 项目中的 threejs 文档使用:

texture.load( "./clouds" )

此代码不起作用,我必须使用 require 导入图像:

texture.load( require( "./clouds.png" ) )

现在我想使用函数来实现成功或错误,所以感谢互联网我发现

texture.load( require( "./clouds.png" ), this.onSuccess, this.onProgress, this.onError )

问题出在成功函数中,我想创建一个有纹理的立方体,但什么也没发生。我也尝试过成功功能在材质中添加颜色,但没有成功。

onSuccess( image ) {

   this.material = new THREE.MeshLambertMaterial( { 
      color: 0xf3ffe2,
      map: image
   }

   this.generateCube()
}

generateCube() {

   let geometry = new THREE.BoxGeometry( 100, 100, 100 );

   this.forme = new THREE.Mesh( geometry, this.material );
   this.forme.position.z = -200
   this.forme.position.x = -100
   this.scene.add( this.forme );

},

【问题讨论】:

标签: vue.js three.js


【解决方案1】:

您的问题与 VueJS /ThreeJs 无关(再次 ^^),您应该学习如何在回调中使用 this,这是 E6 修复:

texture.load( require( "./clouds.png" ), t => this.onSuccess(t), e => this.onProgress(e), e => this.onError(e) )

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

【讨论】:

  • 它不起作用,使用console.log() 我知道我到达了每个函数但是this.material 我放了颜色但什么也没发生
  • 你确定你的网格是可见的吗?我的意思是这个位置看起来很奇怪
  • 我不明白为什么我的代码在函数中不起作用但在函数之外运行良好。我尝试删除位置,但什么也没发生
【解决方案2】:

您可以将您的图片放在“公共”文件夹中。

然后你可以通过

加载你的纹理
texture.load( "/clouds.png" )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-29
    • 2018-01-14
    • 2019-08-08
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多