Subtractive blending

Posted By: MatAllum

Subtractive blending - 01/30/15 03:37

I've found it very simple (with some help, I recall) to activate standard additive blending in a shader:
Code:
pass base
	{
		SrcBlend = One;
		DestBlend = One;
		BlendOp = Add;
		
		VertexShader=compile vs_1_1 mainVS();
		PixelShader=compile ps_1_1 mainPS();
	}



But is there an easy way to handle "subtractive" blending? For instance, if I have a light gray blob texture, I want it not to show up as a bright light, but as a color-saturated dark patch (which is generally how it looks when you uniformly subtract from a color).

Changing BlendOp to Subtract definitely does not do the trick, as it makes my scene resemble a disco on drugs. The closest I've found is Min, which gives the color saturation I want but seems to make anything bright look washed-out (yet also dim, as planned).
Posted By: Kartoffel

Re: Subtractive blending - 01/30/15 06:00

A - B = A + (-B) ;P
Posted By: MatAllum

Re: Subtractive blending - 01/30/15 12:22

Makes perfect sense. Sadly, I tried it, and negative color values seem to be clamped to 0.

There also doesn't seem to be a valid BlendFunc that returns a negative number. Maybe I'm missing something?
Posted By: Kartoffel

Re: Subtractive blending - 01/30/15 12:47

Well sorry, I thought it'd work.

I'll See if I can find a solution when I'm at home.
Posted By: MasterQ32

Re: Subtractive blending - 01/30/15 12:51

Just a though: Use floating point render targets with HDR instead of 8 bit unsigned.
I experienced that 8 bit render targets will clamp before(!) blending, float points don't clamp.

maybe this could work
Posted By: MatAllum

Re: Subtractive blending - 01/30/15 19:29

A shader question in another game development forum seems to imply that the ReverseSubtract blendop would do the job perfectly. Unfortunately, this produces a shader compiling error saying it doesn't know what ReverseSubtract is...
Posted By: Superku

Re: Subtractive blending - 01/31/15 05:11

A horrible workaround: Make sure the entity gets rendered last (I assume they get rendered in the order they were created), then grab the pixel value at that position and do the subtract math yourself.
Posted By: MatAllum

Re: Subtractive blending - 02/01/15 17:42

Seems like I've seen effects like this in game engines from the early 2000s. Nevertheless, considering this is for a vignette effect, perhaps I should just learn to create postprocessing shaders.
© 2024 lite-C Forums