Skip to content

Instantly share code, notes, and snippets.

@s-leroux
Last active November 5, 2018 14:17
Show Gist options
  • Save s-leroux/0f40b8625a5a49b02a45e8e67fe0d233 to your computer and use it in GitHub Desktop.
Save s-leroux/0f40b8625a5a49b02a45e8e67fe0d233 to your computer and use it in GitHub Desktop.
Extra Blend modes / Lightworks effects
//--------------------------------------------------------------//
// MoreBlend.fx
//
// Copyright (c) Sylvain Leroux. All Rights Reserved
//--------------------------------------------------------------//
int _LwksEffectInfo
<
string EffectGroup = "GenericPixelShader";
string Description = "Fixed Blend";
string Category = "Mixes";
> = 0;
//--------------------------------------------------------------//
// Params
//--------------------------------------------------------------//
int SetTechnique
<
string Description = "Method";
string Enum = "InFront";
> = 0;
float Opacity
<
string Description = "Fg Opacity";
float MinVal = 0.00;
float MaxVal = 1.00;
> = 1.0;
float Gamma
<
string Description = "Gamma";
float MinVal = 0.00;
float MaxVal = 5.00;
> = 2.2;
//--------------------------------------------------------------//
// Inputs
//--------------------------------------------------------------//
texture fg;
texture bg;
sampler FgSampler = sampler_state { Texture = <fg>; };
sampler BgSampler = sampler_state { Texture = <bg>; };
texture fg_prediv : RenderColorTarget;
sampler FgPredivSampler = sampler_state {
Texture = <fg_prediv>;
AddressU = Clamp;
AddressV = Clamp;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
texture fg_linear : RenderColorTarget;
sampler FgLinearSampler = sampler_state {
Texture = <fg_linear>;
AddressU = Clamp;
AddressV = Clamp;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
texture bg_linear : RenderColorTarget;
sampler BgLinearSampler = sampler_state {
Texture = <bg_linear>;
AddressU = Clamp;
AddressV = Clamp;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
texture out_linear : RenderColorTarget;
sampler OutLinearSampler = sampler_state {
Texture = <out_linear>;
AddressU = Clamp;
AddressV = Clamp;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
//--------------------------------------------------------------//
// Code
//--------------------------------------------------------------//
// Unmultiply operation to cancel initial black-background rendering
float4 unmultiply( float2 xy1 : TEXCOORD1, uniform sampler src) : COLOR
{
float4 fg = tex2D( src, xy1 );
if (fg.a == 0.0) {
return fg;
}
float4 ret = fg/fg.a;
ret.a = fg.a;
return ret;
}
float4 gamma_correct( float2 xy1 : TEXCOORD1, uniform sampler src, uniform float gamma) : COLOR
{
float4 c = tex2D( src, xy1 );
c.xyz = pow(c.xyz, gamma);
return c;
}
float4 InFront_main( float2 xy1 : TEXCOORD1, float2 xy2 : TEXCOORD2 ) : COLOR
{
float4 fg = tex2D( FgLinearSampler, xy1 );
float4 bg = tex2D( BgLinearSampler, xy2 );
float alpha = fg.a*Opacity;
if (alpha == 0.0) {
return bg;
}
if (alpha == 1.0) {
return fg;
}
float4 ret = lerp(bg, fg, alpha);
ret.a = 1.0;
return ret;
}
//--------------------------------------------------------------//
// Techniques
//--------------------------------------------------------------//
technique InFront {
pass Pass1
<
string Script = "RenderColorTarget0 = fg_prediv;";
>
{
PixelShader = compile PROFILE unmultiply(FgSampler);
}
pass Pass2
<
string Script = "RenderColorTarget0 = fg_linear;";
>
{
PixelShader = compile PROFILE gamma_correct(FgPredivSampler, Gamma);
}
pass Pass3
<
string Script = "RenderColorTarget0 = bg_linear;";
>
{
PixelShader = compile PROFILE gamma_correct(BgSampler, Gamma);
}
pass Pass4
<
string Script = "RenderColorTarget0 = out_linear;";
>
{
PixelShader = compile PROFILE InFront_main();
}
pass Pass5
{
PixelShader = compile PROFILE gamma_correct(OutLinearSampler, 1/Gamma);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment