# Local shape color blending

I am trying to implement this in Unity 4 Pro.

But I am stuck in the blending part. I don't understand how you could blend multiples textures/colors using multiples volumes on an object. How could you access those volumes in the shader and check for "collision"?

It's seems to be very similar to vertex/pixel lighting. But maybe I am wrong.

Here is the simple effect I am trying to create.

Essentially this looks like something where you'd need a custom blending function where the result will depend on the distance to the outlines of the actual shape.

• For each shapve covering a pixel determine how close that point is to the outline of the shape.
• For a circle this could be something as simple as 1 - (distance_to_center / radius).
• For a rectangle it would be similar, determining judging the shortest distance to any border.
• Then compare these values of all shapes involved - the ratio will tell you how to mix colors:
• If both have the same value, you mix the colors 50:50.
• If one value is 1, the other 0.5 you'd mix the colors 67:33.

I can't tell you how to do that in a shader, but I can at least explain the math.

Basically, all your objects have two textures: an RGB color texture (which in your example is just a solid color for each object) and a weight texture (which in your example is just a radial gradient). In practice, you could conveniently store the weight as the alpha component of a 4-component RGBA texture (although you won't be using it for normal alpha blending). Alternatively, for simple shapes and gradients like these, you can just calculate the weight for each object procedurally in your shader.

Now, to calculate the color of each pixel, you take the weighted average of the colors of the objects that overlap that pixel. That is, if object k has weight Wk and color Ck = (Rk, Gk, Bk), then you'd calculate:

Csum = (W1 * C1) + (W2 * C2) + (W3 * C3) + ...

and:

Wsum = W1 + W2 + W3 + ...

and let the color of the pixel be Cavg = Csum / Wsum.

Note that you can avoid the multiplications in calculating Csum if you store the color channels premultiplied by the weight. Also note that it's possible for both Csum and Wsum to be zero, e.g. if no objects overlap a given pixel. In that case, you need to decide what color to use; your example picture appears to be using black for that case.