Team Spartan Cookies & Milk Forums
Help required with overrideBlend - Printable Version

+- Team Spartan Cookies & Milk Forums (https://teamscm.co.uk)
+-- Forum: Garry's Mod (https://teamscm.co.uk/forum-4.html)
+--- Forum: Starfall (https://teamscm.co.uk/forum-7.html)
+--- Thread: Help required with overrideBlend (/thread-3786.html)



Help required with overrideBlend - The17thDoctor - 29-11-2023

I must say render.overrideBlend gives me a real headache. Im having trouble seeing how this function behaves and neither the GMod Wiki nor the OpenGL docs are helping me.

What I want is to apply some color atop of a texture with varying alpha such as fully opaque parts of the texture will be e.g fully red and other parts just transparent red.


RE: Help required with overrideBlend - Person8880 - 01-12-2023

Sounds like what you want is to use source colour, and destination alpha, so something like:
-- First draw your transparency mask.
drawYourTransparencyMaskHere()

-- Then set the blend mode overrides.
render.overrideBlend(
    true,
    -- Use 1 * source colour (i.e. all of the colour that's going to be drawn), and none of the existing colour, and add those together.
    render.BLEND_ONE, render.BLEND_ZERO, render.BLENDFUNC_ADD,
    -- Use none of the source alpha, and 1 * the destination alpha (i.e. take the alpha value that's already drawn), and add those together.
    render.BLEND_ZERO, render.BLEND_ONE, render.BLENDFUNC_ADD
)

-- Now draw your red colour over the top - this should end up colouring everything red with transparency.
render.setColor( 255, 0, 0 )
render.drawRect( 0, 0, 512, 512 )
This assumes you're rendering this to a render target, as setting colour/alpha values in screen space wouldn't apply the transparency part in a visible way.

Bear in mind also that rendering a transparent texture will itself blend with whatever it's rendering on top of, if you render a perfect alpha gradient on top of a black background you might get more colour than you originally expected.


RE: Help required with overrideBlend - The17thDoctor - 01-12-2023

Thanks for the explanation, I find the comments very useful to understand what's happening in reality.

My main confusion was that I thought that BLEND_ZERO and BLEND_ONE meant either fully black or fully white. This added to the fact that source and destinations were unclear to me made this whole ordeal take much more time than needed.