deck.gl icon indicating copy to clipboard operation
deck.gl copied to clipboard

IconLayer (png) render artifacts

Open j13y opened this issue 8 years ago • 25 comments

I use IconLayer with png sprite. Icons render with a small glitch, something like a gray border or shadow but originally they have only a white border.

Preview

j13y avatar Aug 07 '18 12:08 j13y

can you add your original png and the final rendering effects here?

jianhuang01 avatar Oct 25 '18 21:10 jianhuang01

hey @jianhuang01,

We've come across this issue as well, so hopefully this will help:

const iconMapping = {
  default: {
    x: 0,
    y: 0,
    width: 80,
    height: 80,
  },
}

const iconLayer = new IconLayer({
  id: `icon-layer`,
  pickable: true,
  iconMapping,
  iconAtlas: 'locations-spritesheet-white.png',
  getIcon: () => 'default',
  getSize: 80,
  sizeScale: 5,
  data: layersData,
  opacity: 1,
});

White sprite-sheet, only using the first sprite: locations-spritesheet-white

Rendered output in chrome: Screen Shot 2019-04-17 at 3 41 09 pm

When we add in the second sprite for onHover, the result looks like this: Screen Shot 2019-04-17 at 3 43 56 pm

Any ideas?

wooly avatar Apr 17 '19 05:04 wooly

You can also see it happening on the IconLayer deck.gl example here: https://deck.gl/#/examples/core-layers/icon-layer

It's noticeable when the markers overlap: Screen Shot 2019-04-17 at 4 19 55 pm Screen Shot 2019-04-17 at 4 20 53 pm

Maybe something to do with alpha blending?

wooly avatar Apr 17 '19 06:04 wooly

@wooly Are those sprites using the alpha channel to fade off at the edges? @jianhuang01 Does the icon layer use the alpha channel from the image it's sampling?

tsherif avatar Apr 17 '19 16:04 tsherif

@tsherif they're using the alpha channel to anti-alias the circle edges, yep.

wooly avatar Apr 17 '19 22:04 wooly

It looks like the icon layer does sample the texture alpha. The issue might be that deck.gl doesn't premultiply its output alphas (some discussion here of why this is an issue).

tsherif avatar Apr 22 '19 16:04 tsherif

@wooly Can you try adding the following to your icon layer constructor props:

parameters: {
    depthMask: false
}

tsherif avatar Apr 22 '19 19:04 tsherif

@tsherif didn't make any discernible difference, unfortunately.

wooly avatar Apr 22 '19 23:04 wooly

Happens also to me. @tsherif , any workarounds?

leeran88 avatar May 14 '19 05:05 leeran88

Anything that can be done here? Happens for me as well with the latest version:

image image

    "@deck.gl/react": "^8.2.2",
    "@deck.gl/layers": "^8.2.2",
    "react-map-gl": "^5.2.7",

My original sprite: (looks like Github removes the transparency) image

BowlingX avatar Jul 13 '20 10:07 BowlingX

If I set premultipliedAlpha: false, the border get's a bit nicer

image

BowlingX avatar Jul 13 '20 10:07 BowlingX

I found the issue in my case. I upgraded to the latest deck.gl version (from 8.1 to 8.2). After reading the changelog I adjusted my IconLayer loadOptions back to the previous default. (see https://deck.gl/docs/upgrade-guide#upgrading-from-deckgl-v81-to-v82).

Example:

new IconLayer({ ...yourProps, loadOptions: {
          image: {
            type: 'image'
          }
        }})

BowlingX avatar Jul 13 '20 11:07 BowlingX

@BowlingX Good that you were able to pinpoint this. It is unexpected. ImageBitmap is the recommended format for loading images on modern browsers, but based on your findings it looks like there is potential issue with transparency when converted to texture.

I googled a bit but could not find more information on this.

For completeness, can you provide browser information?

If you have time, you could also try the following and see if it makes a difference:

new IconLayer({ ...yourProps, loadOptions: {
          imagebitmap: {
            premultiplyAlpha: 'premultiply'
          }
        }})

For information on that, see createImageBitmap

ibgreen avatar Jul 14 '20 01:07 ibgreen

@ibgreen I tried this option already and it did not fix the issue, unfortunately. The only thing that helped was to change the type to image. Browser is Chrome 83.0.4103.116. But the same issue was also present on Firefox 77.0.1 and 78.0.2. Operating system is Windows 10 with NVIDIA 2080 TI.

BowlingX avatar Jul 14 '20 08:07 BowlingX

@ibgreen I think we want premultiplyAlpha: 'none' to remove the border. I created a demo here: https://codepen.io/Pessimistress/pen/xxZQGMM

The option doesn't seem to have any effect either way in Chrome, but works in FireFox.

Pessimistress avatar Jul 17 '20 18:07 Pessimistress

That sounds good. We could perhaps set it as a default option in the image loader?

https://github.com/visgl/loaders.gl/blob/master/modules/images/src/image-loader.js#L36

ibgreen avatar Jul 17 '20 20:07 ibgreen

@ibgreen ImageBitmap's options do not work in Chrome. Setting a default won't solve this issue.

Pessimistress avatar Jul 17 '20 20:07 Pessimistress

I've just run into this as well and I can confirm what @Pessimistress says about loadOptions for ImageBitmap not working on Chrome :(

When looking at what Chrome renders with renderdoc I can see that the texture holding the icons is indeed premultiplied (e.g. fully transparent pixels have value RGBA: 0x00000000). However, the blend function set when drawing is: GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; I believe it should be GL_ONE, GL_ONE_MINUS_SRC_ALPHA for premultiplied texture data?

I think icon-layer.js#L208 should call draw with custom parameters, e.g. something like:

    this.state.model
        .setUniforms(
          Object.assign({}, uniforms, {
            iconsTexture,
            iconsTextureDim: [iconsTexture.width, iconsTexture.height],
            sizeScale: sizeScale * (sizeUnits === 'pixels' ? viewport.metersPerPixel : 1),
            sizeMinPixels,
            sizeMaxPixels,
            billboard,
            alphaCutoff
          })
        )
        .draw({
            parameters: {
                blendFunc: [GL.ONE, GL.ONE_MINUS_SRC_ALPHA]
          }
        });

cneumann avatar Oct 06 '20 18:10 cneumann

Thanks @cneumann for the insight!

Can confirm that this solves the problem:

  new IconLayer({
    ...
    parameters: {
        blendFunc: [1, 771] // GL.ONE, GL.ONE_MINUS_SRC_ALPHA
    },
    loadOptions: {
        imagebitmap: {
          premultiplyAlpha: 'none'
        }
    }
})

We can change IconLayer's default blending parameters, though that could be a breaking change for users who are customizing global blending. @ibgreen

Pessimistress avatar Oct 06 '20 18:10 Pessimistress

Just to be sure: if you change the blend function in this way you do want premultiplied alpha, so the option should be imagebitmap.premultiplyAlpha = 'premultiply' - of course that only matters when/if the options actually get passed to the browser ;)

Also, great to know I can pass parameters this way to the layer, I had actually cloned IconLayer and hacked it's draw call to include the alternate blend function :)

cneumann avatar Oct 06 '20 18:10 cneumann