Mixing two Objects color, Creating the color of the two mixes

hello there unity devs.

i encountered a slight problem with how to do this Color mixing of two objects.

basicly i want Object A and Object B to Merge and the Two colors on the two objects would return a Mix of the two colors. like Red + Yellow = Orange.

is there some sort of easy way to do it? or would i have to go and make a shader for it?

if anyone can lead me in a direction or give me tips i would be greatful.

On first glance I would say:

  • Create 3 ints r, g and b.
  • Add together the corresponding value from each colour. int r = Colour1.r + Colour2.r;
  • Then divide the 3 ints by 2: int r /= 2;
  • Set it to a new colour: Color newColour = new Color(r, g, b);
  • Bish bash bosh, done
1 Like

Except that you should be using floats instead of ints, @TaleOf4Gamers_1 is correct. You can mix two colors by simply averaging each of the red, green, and blue components.

You can also do it with Color.Lerp. For example, Color.Lerp(color1, color2, 0.5f) would give you an even mixing, 50% of color1 plus 50% of color 2.

2 Likes

Note that a straight linear average may not be what you want for colors.
See How to average RGB colors together? | by Kevin Simper | Medium for example.

5 Likes

Woops, was thinking of Color32! Either way :wink:

so with that i should be able to slowly merge two objects colors? and make a third object appear slowly based on that? like if the two objects go from Left and Right and merge in the middle. sorta making a transparent object in the middle and only when the two objects start having contact on the very outer points of their collision then the 3rd box would slowly appear?.

is that possible

Yes, that’s possible.