I've implemented a simple shader with the following properties: - Material (ambient, diffuse and specular) - Base color which defines the color of the object - Light Color
Fragment shader: (vColor is the base color of the object)
vec3 lightDir = normalize(vec3($uLightPos.xyz - vPosInEye.xyz));
vec3 viewerPos = normalize(vec3(-vPosInEye.xyz));
vec3 specularReflection = normalize(-reflect(lightDir, vNormal));
float diffFactor = max(dot(vNormal, lightDir), 0.0);
vec4 Id = $uIdColor * diffFactor * vColor ;
vec4 Ia = $uIaColor * vColor;
vec4 Is = clamp($uIsColor * pow(max(dot(specularReflection, viewerPos), 0.0), $uIsShininess), 0.0, 1.0);
gl_FragColor = (Id + Ia + Is) + $uLightColor;
As you can see I multiply the ambient color of the material with the base color of the object. This seems to be right because I want to mix these colors together.
In the book "Open GL ES 2.0 Programming guide" this is implemented the same way. In the last line I add the color of the light.
I tried to multiply the color at first but then I got strange result: yellow * blue resulted in black (which is clear because yellow is (1.0, 1.0, 0) and blue (0.0, 0.0, 1.0) so I replaced * with +. This seems to produce the correct color (white).
So my question is: Is there a rule of thumb when I have to multiply two colors together and when to add them?
The Multiply mode simply multiplies each component in the two layers. The Color Burn mode divides the inverted bottom layer by the top layer, and then inverts the result. This darkens the top layer increasing the contrast to reflect the color of the bottom layer. The darker the bottom layer, the more its color is used.
g = g3 * 255; color3. b = b3 * 255; color3. a = a3 * 255; this is the correct procedure to multiply two colors.
You need to multiply when you want to modulate one color by another. For example if you have a blue material, you want to multiply the light falling on it by the color of the material because it should only reflect blue light. If you have yellow light on a blue material you'd expect to get black because blue materials won't relect yellow light. (In real life you never have materials that are this perfect, they might be (0.5 0.5, 0.75) ...
If you have two light sources for example then you'll probably add them as you want to total the contribution from each. The light you see is simply the total of both lights, so add them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With