简体   繁体   English

从 DOM 图像加载 WebGL 纹理时,如何判断它是否具有 Alpha 通道?

[英]When you load a WebGL texture from a DOM image, how do you tell if it has an alpha channel?

When you load a WebGL texture directly from a DOM image, how do you tell if the image has an alpha channel or not?当您直接从 DOM 图像加载 WebGL 纹理时,如何判断图像是否具有 Alpha 通道? Is there anyway except to guess based on the filename (eg "contains.PNG may be RGBA otherwise RGB").除了根据文件名猜测之外是否还有其他方法(例如“contains.PNG 可能是 RGBA,否则为 RGB”)。 There is a width and height in the DOM image, but nothing I can see that says what format it is. DOM 图像中有一个宽度和高度,但我看不到它是什么格式。 ie: IE:

    const img = await loadDOMImage(url);
    const format = gl.RGBA; //Does this always need to be RGBA? I'm wasting space in most cases where its only RGB
    const internalFormat = gl.RGBA;
    const type = gl.UNSIGNED_BYTE; //This is guaranteed to be correct, right?  No HDR formats supported by the DOM?
    gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, format, type, img); 

My load function looks like this FWIW:我的负载 function 看起来像这样 FWIW:

  async loadDOMImage(url) {
         return new Promise(
           (resolve, reject)=>{
             const img = new Image(); 
             img.crossOrigin = 'anonymous';
             img.addEventListener('load', function() {
               resolve(img);
             }, false);
             img.addEventListener('error', function(err) {
               reject(err);
             }, false);

             img.src = uri;

           }
         );
       }

how do you tell if the image has an alpha channel or not?您如何判断图像是否具有 Alpha 通道?

You can't.你不能。 You can only guess.你只能猜测。

  1. you could see the URL ends in.png and assume it has alpha.您可以看到 URL 以 .png 结尾并假设它具有 alpha。 You might be wrong你可能错了

  2. you could draw image into a 2D canvas then call getImageData, read all the alpha pixels and see if any of them are not 255您可以将图像绘制成 2D canvas 然后调用 getImageData,读取所有 alpha 像素,看看它们是否不是 255

 const format = gl.RGBA;

Does this always need to be RGBA?这总是需要RGBA吗? I'm wasting space in most cases where its only RGB在大多数情况下,我只是在浪费空间,只有 RGB

It's unlikely to waste space.不太可能浪费空间。 Most GPUs work best with RGBA values so even if you choose RGB it's unlikely to save space.大多数 GPU 最适合使用 RGBA 值,因此即使您选择 RGB,也不太可能节省空间。

 const type = gl.UNSIGNED_BYTE;

This is guaranteed to be correct, right?这保证是正确的,对吧?

texImage2D takes the image you pass in and converts it to type and format . texImage2D获取您传入的图像并将其转换为typeformat It then passes that converted data to the GPU.然后它将转换后的数据传递给 GPU。

No HDR formats supported by the DOM? DOM 不支持 HDR 格式吗?

That is undefined and browser specific.这是未定义的和特定于浏览器的。 I know of no HDR image formats supported by any browsers.我知道任何浏览器都不支持 HDR 图像格式。 What image formats a browser supports is up to the browser.浏览器支持什么图像格式取决于浏览器。 For example Firefox and Chrome support animated webp but Safari does not.例如 Firefox 和 Chrome 支持动画 webp,但 Safari 不支持。


A common question some developers have is they want to know if they should turn on blending / transparency for a particular texture.一些开发人员的一个常见问题是他们想知道是否应该为特定纹理打开混合/透明度。 Some mistakenly believe if the texture has alpha they should, otherwise they should not.有些人错误地认为纹理是否应该有 alpha,否则就不应该。 This is false.这是错误的。 Whether blending / transparency should be used is entirely separate from the format of the image and needs to be stored separately.是否应该使用混合/透明度与图像的格式完全分开,需要单独存储。

The same is true with other things related to images.与图像相关的其他事物也是如此。 What format to upload the image into the GPU is application and usage specific and has no relation to the format of the image file itself.将图像上传到 GPU 的格式是特定于应用程序和用途的,与图像文件本身的格式无关。

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

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