Jump to content

Recommended Posts

Posted
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. :D 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

image.thumb.png.0a36d849c44d8d8c8bb8f76316f9a27d.png

 

And here is the original texture used in the above comparison.

image.png.4762734201c219a809a2077831168a11.png

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.