opengl - Shader - Color blending -


i know how blend colors in specific way.

let's imagine have color (a) , other color (b).

i blend them in such way if choose white (b) output color (a) if have other color (b) outputs blending of (a) , (b).

  • i've tried addition, doesn't give expected result.
  • i've tried multiplicative blending it's quite (b) white value fail blue (b) , red (a) colors.

any idea how ?

with glsl, simplest approach use branch. if cola , colb 2 vectors (of type vec4) holding colors a , b:

if (any(lessthan(colb.xyz, vec3(1.0)))) {     outcolor = colb; } else {     outcolor = cola; } 

or, if want avoid branch, rely more on built-in functions. example, using observation if components in range [0.0, 1.0], dot product of vector 3.0 vector (1.0, 1.0, 1.0), , smaller other vectors:

outcolor = mix(colb, cola, step(3.0, dot(colb.xyz, colb.xyz))) 

you have benchmark find out of these faster.

there may concern floating point precision in comparisons both variations above. believe should fine, since 1.0 can represented float exactly. if run problems, may want allow imprecision changing constants colb compared against smaller values.