2
$\begingroup$

I'm trying to write a GLSL shader that is supposed to do what the cycles nodes do with texture color mixing and stuff, but I'm not sure about the math involved behind the MixRGB node. I'm interested in the multiply mode of the MixRGB node. I suspect that the node is simply multiplying the input colors by default.

output = color1.rgba * color2.rgba

But what happens when I'm setting the frac as well? Is it multiplied along with the others as well like:

output = color1.rgba * color2.rgba * frac
$\endgroup$
1
$\begingroup$

I think the formula is:

output = (1-fac) * color1.rgba + fac * color1.rgba * color2.rgba

So with fac you interpolate between the first color and the product of two colors. You can see below how this works.

Fac = 0.0:

enter image description here

Fac = 0.5:

enter image description here

Fac = 1.0:

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ This looks like it but isn't it (1-fac) * color1? $\endgroup$ – Greg K. Sep 21 '16 at 18:40
  • $\begingroup$ @GregK. oops, sorry, corrected $\endgroup$ – Noidea Sep 21 '16 at 18:45
2
$\begingroup$

color1 *= ((1.0 - fac) + fac * color2)

/* r_col = current value, col = new value, (fac == 0) is no change */
void ramp_blend(int type, float r_col[3], const float fac, const float col[3])
{
    float tmp, facm = 1.0f - fac;

    switch (type) {
        case MA_RAMP_BLEND:
            r_col[0] = facm * (r_col[0]) + fac * col[0];
            r_col[1] = facm * (r_col[1]) + fac * col[1];
            r_col[2] = facm * (r_col[2]) + fac * col[2];
            break;
        case MA_RAMP_ADD:
            r_col[0] += fac * col[0];
            r_col[1] += fac * col[1];
            r_col[2] += fac * col[2];
            break;
        case MA_RAMP_MULT:
            r_col[0] *= (facm + fac * col[0]);
            r_col[1] *= (facm + fac * col[1]);
            r_col[2] *= (facm + fac * col[2]);
            break;

From GitLab: https://gitlab.com/ideasman42/blender/-/blob/dyntopo_knife/source/blender/blenkernel/intern/material.c

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.