Difference between revisions of "BlendMode Formulas"

(Added 'screen' blend mode)
m (readability)
Line 3: Line 3:
 
LÖVE currently uses OpenGL and exposes some of the blend equations and functions of it.
 
LÖVE currently uses OpenGL and exposes some of the blend equations and functions of it.
  
Here's a list where the numbers are in the interval [0,1]; dst is the existing color in the buffer; src is the global color, texture color, or both of them mixed together (depending on the color mode); and res is the resulting color.
+
Here's a list where the numbers are in the interval [0,1].
 +
 
 +
Description:
 +
 +
*'''dst''' - existing color in the buffer.
 +
 
 +
*'''src''' - global color, texture color, or both of them mixed together (depending on the color mode).
 +
 
 +
*'''res''' - resulting color.
  
 
== alpha ==
 
== alpha ==

Revision as of 16:02, 28 August 2015

Copied from: Forum Post

LÖVE currently uses OpenGL and exposes some of the blend equations and functions of it.

Here's a list where the numbers are in the interval [0,1].

Description:

  • dst - existing color in the buffer.
  • src - global color, texture color, or both of them mixed together (depending on the color mode).
  • res - resulting color.

alpha

0.9.0 and newer:

   res.r = dst.r * (1 - src.a) + src.r * src.a
   res.g = dst.g * (1 - src.a) + src.g * src.a
   res.b = dst.b * (1 - src.a) + src.b * src.a
   res.a = dst.a * (1 - src.a) + src.a

0.8.0 and older:

   res.r = dst.r * (1 - src.a) + src.r * src.a
   res.g = dst.g * (1 - src.a) + src.g * src.a
   res.b = dst.b * (1 - src.a) + src.b * src.a
   res.a = dst.a * (1 - src.a) + src.a * src.a

premultiplied

   res.r = dst.r * (1 - src.a) + src.r
   res.g = dst.g * (1 - src.a) + src.g
   res.b = dst.b * (1 - src.a) + src.b
   res.a = dst.a * (1 - src.a) + src.a

screen

   res.r = dst.r * (1 - src.r) + src.r
   res.g = dst.g * (1 - src.g) + src.g
   res.b = dst.b * (1 - src.b) + src.b
   res.a = dst.a * (1 - src.a) + src.a

additive

   res.r = dst.r + (src.r * src.a)
   res.g = dst.g + (src.g * src.a)
   res.b = dst.b + (src.b * src.a)
   res.a = dst.a + (src.a * src.a)

subtractive

   res.r = dst.r - src.r * src.a
   res.g = dst.g - src.g * src.a
   res.b = dst.b - src.b * src.a
   res.a = dst.a - src.a * src.a

multiplicative

0.9.0 and newer:

   res.r = src.r * dst.r
   res.g = src.g * dst.g
   res.b = src.b * dst.b
   res.a = src.a * dst.a

0.8.0 and older:

   res.r = dst.r * (1 - src.a) + src.r * dst.r
   res.g = dst.g * (1 - src.a) + src.g * dst.g
   res.b = dst.b * (1 - src.a) + src.b * dst.b
   res.a = dst.a * (1 - src.a) + src.a * dst.a

replace

   res.r = src.r
   res.g = src.g
   res.b = src.b
   res.a = src.a

Notes

OpenGL clamps to [0,1] (except when rendering to HDR Canvases) in case you're wondering if subtractive could have had negative values.

See Also