Scurvyez Posted March 31 Posted March 31 float3 contrast_negate_blend(float3 a, float3 b) { // a float3 here is just the RGB of a color 👍 // calculate luminosity (brightness) using the standard luminance formula for the blend color using the dot product // luminance = 0.299 * R + 0.587 * G + 0.114 * B float blendLuminance = dot(b, float3(0.299, 0.587, 0.114)); // the inverse blend color, if needed (see below for follow-up) float3 invertedBlend = 1.0 - b; // check the blend color's luminosity, either more or less than/equal 0.5 // if blendLuminance <= 0.5, we do a single blend operation // else, we do the opposite blend operation (inverted colors!) // step(0.5, dot(a, float3(0.299, 0.587, 0.114)), determines which background pixels are dark or bright // step() acts as a mask // returns 0 for dark pixels (background luminance < 0.5) // returns 1 for bright pixels (background luminance >= 0.5) // lerp(), blends between the two colors a and b using a weight (result of step() function) // when step() = 0, we get a // when step() = 1, we get b // when step() = between 0 and 1, we get a mix return (blendLuminance <= 0.5) ? lerp(invertedBlend, b, step(0.5, dot(a, float3(0.299, 0.587, 0.114)))) : lerp(b, invertedBlend, step(0.5, dot(a, float3(0.299, 0.587, 0.114)))); } TLDR; If the blend color (color of your texture using "color negate") is dark, it negates the bright areas of the background and keeps the dark areas normal. If the blend color is bright, it negates the bright areas of the background and keeps the dark areas normal. Picture below shows a texture on the right over some background texture in Affinity Designer and those same two textures in the same visual order relative to the camera in Unity where I recreated the effect with the above code in a shader. It's a 1 : 1 recreation. I was frustrated when I couldn't find a quick solution to this so, in classic necro-post fashion.. I spent a couple hours banging my head against the keyboard till I got it. lol And here is the original texture used in the above comparison. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.