2
$\begingroup$

I have an image on a plane, is it possible to limit the number of colors on that image, sort of like the posterize node in the compositor but within the material shader?

Compositor Posterize Node

$\endgroup$

1 Answer 1

6
$\begingroup$

//EDIT: Okay, I actually didn't think of a much easier solution. The answer below is completely valid and working, but for the vector maths there is a shortcut: instead of upscaling the colors by the step size, then flooring them and downscaling by the step size reciprocal, you can simply use the step size reciprocal in a Vector Math node set to Snap (for the long explanation read the answer below):

posterize with snap

Yes, it is actually not too complicated if you know what's going on in the Posterize node.

When you set the steps to something like 8, it means the complete range from 0 to 1 (or 0 to 255 in RGB values) will be reduced to only 8 colors per channel or 512 in total, because it's the number of steps to the power of 3, in this example $$8^3=512.$$

With the colors in the 0 to 1 range you can simply calculate this conversion by the following formula:

$$\frac{\lfloor color\cdot steps\rfloor}{steps}$$

So you multiply the image colors by the number of steps you want to have, e.g. 8. Then your colors range from 0 to 8 instead of 0 to 1. Then you use the floor function to cut off the decimal part, i.e. 0.245 becomes 0, 3.712 becomes 3 and so on. Afterwards you divide it by the steps to convert it back to the 0 to 1 range.

Doing this in the shader nodetree looks like this: you take a Value where you can set the steps, a Vector Math node set to Scale to uniformly multiply all colors with this value. Then comes a Vector Math node set to Floor to cut off the decimals, then another Scale node, this time you first divide 1 by the steps value to get the reciprocal and plug this into the Scale node.

posterize vector math

You could now even select the Vector Math nodes and the Math > Divide node and hit Ctrl+G to make this a node group and rename the inputs etc. so that you have your own Posterize node in the Shader Editor.

posterize nodegroup

$\endgroup$
2
  • 1
    $\begingroup$ Ah yes that's so simple. works perfectly. Thank you $\endgroup$
    – SarahLee
    Nov 7, 2023 at 15:35
  • $\begingroup$ @SarahLee It could be even simpler, see my edit at the top of the answer. I was thinking overly complicated and didn't think of all the functions that are available in the Vector Math node. $\endgroup$ Nov 17, 2023 at 14:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .