pixijs / pixi-compressed-textures

Compressed textures and retina support for pixijs. Loader can choose textures depends on platform and rendering mode.
MIT License
93 stars 25 forks source link

premultipliedAlpha not work #2

Closed shawn0326 closed 7 years ago

shawn0326 commented 8 years ago

gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true); not work for gl.compressedTexImage2D(),is there something we can do? or we can only use premultipliedAlpha texture make by texturetools?

ivanpopelyshev commented 8 years ago

According to https://github.com/pixijs/pixi-compressed-textures/blob/master/src/CompressedTextureManager.js#L57

when you compressed textures, you can specify "false" there.

shawn0326 commented 8 years ago

I mean, when I use the method of compressedTexImage2D upload picture data, set premultipy-Alpha to true is invalid, even if I set to true, it doesn't work. However, if I texImage2D way to upload the picture data. It is effective

ivanpopelyshev commented 8 years ago

I hope that you use different sources for texImage2D and compressedTexImage2D. Give me your textures and I'll see whats wrong with them.

I mean, that you can use ONLY textures made with special tools: .dds, .pvr, .etc, depending on platform. You can't pass PNG to compressedTexImage2D

shawn0326 commented 8 years ago

texImage2D for png and compressTexImage2D for pvr 4bpp,i can give you my textures later

ivanpopelyshev commented 8 years ago

https://developer.mozilla.org/ru/docs/Web/API/WEBGL_compressed_texture_pvrtc

can you make sure that correct constant is passed there?

shawn0326 commented 8 years ago

i sent my demo to your email, thanks for your help

mems commented 8 years ago

WebGL use compressed textures as pre multiplied by default. If you use compressed textures (DXT, PVRTC, etc.) with non pre-multiplied alpha you need a to use a blend mode for that with factors SRC_ALPHA (as source factor) and ONE_MINUS_SRC_ALPHA (as destination factor)

With PIXI, the solution I found:

// Create a specific blend mode
PIXI.BLEND_MODES.X_MULTIPLY_ALPHA = 9999;// see core/const ("X_" prefix for forward compatibility)
// Register source and destination factors
if(renderer.type == PIXI.RENDERER_TYPE.WEBGL){
    renderer.state.blendModes[PIXI.BLEND_MODES.X_MULTIPLY_ALPHA] = [renderer.gl.SRC_ALPHA, renderer.gl.ONE_MINUS_SRC_ALPHA];// see core/renderers/webgl/utils/mapWebGLBlendModesToPixi
}
// then later...
if(renderer.type == PIXI.RENDERER_TYPE.WEBGL && sprite.texture.baseTexture.source.isCompressedImage){
    sprite.blendMode = PIXI.BLEND_MODES.X_MULTIPLY_ALPHA;// but what about movieclips ?
}

If you want pre multiplied textures, you can use ImageMagick:

convert RGBA.png \( +clone -alpha Extract \) -channel RGB -compose Multiply -composite RGBA_premultiplied.png

Or create direclty a DDS file:

convert RGBA.png \( +clone -alpha Extract \) -channel RGB -compose Multiply -composite -format dds -define dds:compression=dxt5 -define dds:cluster-fit=true -define dds:mipmaps=0 DXT5_premultiplied.dds

Edit: But I don't found how to fix alpha on sprites using compressed textures

andrewstart commented 7 years ago

I've made a tool to assist in making DDS and CRN textures for Pixi. By default, it turns your PNGs into premultiplied alpha before converting them, so you should be able to use them without stting any special blend modes.

ivanpopelyshev commented 7 years ago

So yeah, its not possible to use compressed textures with premultiply alpha. Workaround:

  1. You have to use "extrude" features of texturepacker, that way LINEAR interpolation wont give you strange black pixels on edges.

  2. Yes, pixi blendModes are for premultiplied stuff. Either you hack PIXI WebGLRenderer BlendModes and replace gl.ONE to gl.SRC_ALPHA, either that: https://limnu.com/webgl-blending-youre-probably-wrong/, we need

gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);

That's correct blendmode for not-premultiplied textures.

May be add new BlendMode, call it NORMAL_NPM and set blendMode of sprites that use compressed textures? @andrewstart we have to introduce that blendMode in pixi!

ivanpopelyshev commented 7 years ago

So it begins: https://github.com/pixijs/pixi.js/pull/4033

ivanpopelyshev commented 7 years ago

It was fixed completely.