简体   繁体   English

Three.js:THREE.WebGLRenderer:图像不是2的幂(1600x900)。 调整为1024x512

[英]Three.js: THREE.WebGLRenderer: image is not power of two (1600x900). Resized to 1024x512

I'm currently learning three.js. 我目前正在学习three.js。 I wanna display a 16x9 photo in my scene. 我想在场景中显示一张16x9的照片。

This is the code for adding an Array of images to my scene: 这是用于将images数组添加到场景中的代码:

  const material = new MeshBasicMaterial({
    map: loader.load(images[i]),
    transparent: true,
    opacity: 1,
  });
  const plane = new Mesh(new PlaneGeometry(imageWidth, 45), material);
  plane.overdraw = true;
  plane.position.x = i * (imageWidth + imageOffset);
  plane.position.y = 0;
  this.introImages.push({
    obj: plane,
    round: 1,
  });
  this.introImagesGroup.add(this.introImages[i].obj);

Now I'm getting the console warning: 现在,我收到控制台警告:

THREE.WebGLRenderer: image is not power of two (1600x900). THREE.WebGLRenderer:图像不是2的幂(1600x900)。 Resized to 1024x512 调整为1024x512

I have read that texture dimensions should be the power of two so it can be put into memory in an optimized way which makes me think if the way I'm putting my images in the scene is the correct way or if there is another way of putting images that don't follow this in three.js? 我已经读过,纹理尺寸应该是2 的幂,因此可以以优化的方式将其存储到内存中,这使我思考将图像放置在场景中的方式是否正确或者是否存在另一种方式。将不遵循此要求的图像放在Three.js中?

You probably don't want THREE.js to scale down your image, because you'd be losing resolution, so you want to scale it up to the next ^2. 您可能不希望THREE.js 缩小图像,因为您将失去分辨率,因此您希望将其放大到下一个^ 2。 You have two options: 您有两种选择:

  1. You could scale up your image and export it at a ^2 resolution 2048 x 1024 in your favorite photo editor. 您可以按比例放大图像,并在您喜欢的照片编辑器中以^ 2分辨率2048 x 1024导出图像。

  2. You could dynamically generate a 2048 x 1024 canvas, draw the image onto it scaled up, and use that canvas as your texture source: 您可以动态生成2048 x 1024画布,将图像按比例绘制到其上,然后将该画布用作纹理源:

     var imgURL = "path/to/whatever.jpg"; // Create image element const image = document.createElement('img'); image.src = imgURL; // Once image is loaded image.onload = () => { // Create canvas, size it to ^2 dimensions var canvas = document.createElement('canvas'); canvas.width = 2048; canvas.height = 1024; // Draw image on canvas, scaled to fit var ctx = canvas.getContext('2d'); var ctx.drawImage(image, 0, 0, 2048, 1024); // Create texture var texture = new THREE.Texture(canvas); }; 

Then you can assign that texture variable to whatever material you want, and Three.js won't complain. 然后,您可以将该texture变量分配给所需的任何材质,Three.js不会抱怨。

Edit: 编辑:

If you want to avoid texture re-sizing, you could just change your texture.minFilter to THREE.NearestFilter or THREE.LinearFilter , and the engine won't give you warnings. 如果要避免调整纹理大小,只需将texture.minFilter更改为THREE.NearestFilterTHREE.LinearFilter ,引擎就不会发出警告。 The problem with doing this is that your textures could look grainy or aliased when scaled down, since they won't be Mipmapped 这样做的问题是您的纹理在按比例缩小时可能看起来粒状或混叠,因为它们不会被Mipmapped

This could be the result you like, or it could look bad, depending on your project. 根据您的项目,这可能是您喜欢的结果,也可能看起来很糟糕。 You could see the effects of using NearestFilter in this example: https://threejs.org/examples/?q=filter#webgl_materials_texture_filters 您可以在此示例中看到使用NearestFilter的效果: https : NearestFilter = NearestFilter

I have read that texture dimensions should be the power of two so it can be put into memory in an optimized way which makes me think if the way I'm putting my images in the scene is the correct way or if there is another way of putting images that don't follow this in three.js? 我已经读过,纹理尺寸应该是2的幂,因此可以以优化的方式将其存储到内存中,这使我思考将图像放置在场景中的方式是否正确或者是否存在另一种方式。将不遵循此要求的图像放在Three.js中?

In WebGL 1 you need POT textures for mipmapping. 在WebGL 1中,您需要用于贴图的POT纹理。 The mentioned warning disappears if you set the .minFilter property of your canvas texture to THREE.LinearFilter . 如果你设置的警告提到消失.minFilter您的帆布质地的财产THREE.LinearFilter Keep in mind that using mipmaps is not necessary for all scenarios. 请记住,并非在所有情况下都必须使用mipmap。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM