Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text and other transparent images have darkened sub-pixel borders #290

Open
BenjaminDRichards opened this issue Oct 13, 2017 · 2 comments
Open
Labels

Comments

@BenjaminDRichards
Copy link
Contributor

I think this is due to linear texture interpolation, but it's proving difficult to pin down. I've just replaced every RGB field in the RGBA ImageData of a Text plugin rendering canvas with a bright colour; and the borders are still darkened. WebGL may be doing something nefarious. Worth investigating.

@BenjaminDRichards
Copy link
Contributor Author

Investigation complete.

The following code is the problem:

Kiwi.Renderers.GLTextureWrapper.prototype.uploadTexture:

gl.pixelStorei( gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0 );

Kiwi.Renderers.GLBlendMode constructor:

this._srcRGB = gl.SRC_ALPHA;

Kiwi.Shaders.TextureAtlasShader.fragSource:

gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.x, vTextureCoord.y));
gl_FragColor.a *= vAlpha;

This treats textures as unpremultiplied, leading to the sub-pixel border flaw.

The correct code is as follows:

Kiwi.Renderers.GLTextureWrapper.prototype.uploadTexture:

gl.pixelStorei( gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1 );

Kiwi.Renderers.GLBlendMode constructor:

this._srcRGB = gl.ONE;

Kiwi.Shaders.TextureAtlasShader.fragSource:

gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.x, vTextureCoord.y)) * vAlpha;

I also like that it reduces the main of the standard fragment shader down to one line. It might not be more efficient, but it certainly isn't less.

Note that this will change the way unpremultiplied textures are processed. However, considering that all our assets seem to work as desired at the end of our pipeline, I anticipate most users will simply benefit from the more accurate rendering system.

@BenjaminDRichards
Copy link
Contributor Author

Further implementation notes: some other renderers need the old blend mode pipeline. In particular, Primitives require srcRGB set back to gl.SRC_ALPHA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant