|
| 1 | +# Exercise |
| 2 | + |
| 3 | +# Blending |
| 4 | + |
| 5 | +In WebGL, blending combines the contents of the currently rendered fragment with the color value stored in the drawing buffer to compute a new color value. |
| 6 | + |
| 7 | +By convention, the fragment being drawn is called the *source* and the fragment in the drawing buffer is called the *destination*. |
| 8 | + |
| 9 | +By default, the new fragment color is just the source color, however if we turn on blending: |
| 10 | + |
| 11 | +```javascript |
| 12 | +gl.enable(gl.BLEND) |
| 13 | +``` |
| 14 | + |
| 15 | +Then the color of the resulting fragment is computed as a weighted combination of the source and destination fragments. To specify these weights, we can use the following command: |
| 16 | + |
| 17 | +#### `gl.blendFuncSeparate(sourceRGB, sourceAlpha, destRGB, destAlpha)` |
| 18 | + |
| 19 | +Each of the parameter `sourceRGB`, `sourceAlpha`, `destRGB`, `destAlpha` describes the weight which is applied to either the source or destination fragment. The possible values for these weights are as follows: |
| 20 | + |
| 21 | +* `gl.ZERO`: (0,0,0,0) |
| 22 | +* `gl.ONE`: (1,1,1,1) |
| 23 | +* `gl.SRC_COLOR`: (Rs, Gs, Bs, As) |
| 24 | +* `gl.ONE_MINUS_SRC_COLOR`: (1,1,1,1)−(Rs,Gs,Bs,As) |
| 25 | +* `gl.DST_COLOR`: (Rd, Gd, Bd, Ad) |
| 26 | +* `gl.ONE_MINUS_DST_COLOR`: (1,1,1,1)−(Rd,Gd,Bd,Ad) |
| 27 | +* `gl.SRC_ALPHA`: (As, As, As, As) |
| 28 | +* `gl.ONE_MINUS_SRC_ALPHA`: (1,1,1,1)−(As,As,As,As) |
| 29 | +* `gl.DST_ALPHA`: (Ad, Ad, Ad, Ad) |
| 30 | +* `gl.ONE_MINUS_DST_ALPHA`: (1,1,1,1)−(Ad,Ad,Ad,Ad) |
| 31 | + |
| 32 | +By convention, (Rs, Gs, Bs, As) are the components of the source color and (Rd, Gd, Bd, Ad) are the components of the destination color. |
| 33 | + |
| 34 | +There are also `gl.CONSTANT_COLOR, gl.ONE_MINUS_CONSTANT_COLOR, gl.CONSTANT_ALPHA, gl.ONE_MINUS_CONSTANT_ALPHA` and `gl.SRC_ALPHA_SATURATE`, but don't worry about them for now. |
| 35 | + |
| 36 | +In addition to the above blend functions, you can also change how the |
| 37 | + |
| 38 | +#### `gl.blendEquationSeparate(modeRGB, modeAlpha)` |
| 39 | + |
| 40 | +`modeRGB` and `modeAlpha` control the two different ways of |
| 41 | + |
| 42 | +* `gl.FUNC_ADD` |
| 43 | +* `gl.FUNC_SUBTRACT` |
| 44 | +* `gl.FUNC_REVERSE_SUBTRACT` |
| 45 | + |
0 commit comments