Skip to content

Commit 6cde82d

Browse files
authored
Merge pull request #556 from adroitwhiz/unmultiply-extracted-drawables
Un-premultiply extracted drawables
2 parents d37c847 + a427461 commit 6cde82d

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/RenderWebGL.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,8 @@ class RenderWebGL extends EventEmitter {
11241124
gl.clear(gl.COLOR_BUFFER_BIT);
11251125
try {
11261126
gl.disable(gl.BLEND);
1127-
this._drawThese([drawableID], ShaderManager.DRAW_MODE.default, projection,
1127+
// ImageData objects store alpha un-premultiplied, so draw with the `straightAlpha` draw mode.
1128+
this._drawThese([drawableID], ShaderManager.DRAW_MODE.straightAlpha, projection,
11281129
{effectMask: ~ShaderManager.EFFECT_INFO.ghost.mask});
11291130
} finally {
11301131
gl.enable(gl.BLEND);

src/ShaderManager.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,15 @@ ShaderManager.EFFECTS = Object.keys(ShaderManager.EFFECT_INFO);
154154
*/
155155
ShaderManager.DRAW_MODE = {
156156
/**
157-
* Draw normally.
157+
* Draw normally. Its output will use premultiplied alpha.
158158
*/
159159
default: 'default',
160160

161+
/**
162+
* Draw with non-premultiplied alpha. Useful for reading pixels from GL into an ImageData object.
163+
*/
164+
straightAlpha: 'straightAlpha',
165+
161166
/**
162167
* Draw a silhouette using a solid color.
163168
*/

src/shaders/sprite.frag

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ void main()
210210
#endif // DRAW_MODE_colorMask
211211
#endif // DRAW_MODE_silhouette
212212

213+
#ifdef DRAW_MODE_straightAlpha
214+
// Un-premultiply alpha.
215+
gl_FragColor.rgb /= gl_FragColor.a + epsilon;
216+
#endif
217+
213218
#else // DRAW_MODE_lineSample
214219
gl_FragColor = u_lineColor * clamp(
215220
// Scale the capScale a little to have an aliased region.

0 commit comments

Comments
 (0)