can you please fix this shader which is not correctly handling automaticcally changing frame and creating smooth blends consistently:
// Super Smooth Motion Blend Shader for OBS Shaderfilter (HLSL)
// --- Automatic Uniforms provided by obs-shaderfilter ---
// The texture of the current frame from the OBS source.
uniform texture2d image;
// The output of THIS filter from the previous frame.
// Note the spelling required by the plugin: "prevous" (missing the second 'i').
uniform texture2d prevous_output;
// Standard sampler state
uniform SamplerState textureSampler;
// --- User Controllable Properties (Appear in OBS UI) ---
uniform float blend_factor <
string label = "Blend Factor (Current Frame Amount)";
string widget = "slider";
float minimum = 0.0;
float maximum = 1.0;
float step = 0.01;
= 0.7; // 0.0 = Only previous frame, 1.0 = Only current frame.
uniform float diff_threshold <
string label = "Motion Threshold";
string widget = "slider";
float minimum = 0.0;
float maximum = 0.5;
float step = 0.005;
= 0.02; // Minimum difference required to apply blending (optimization).
// --- Shader Structure ---
// Standard vertex input structure
struct VertData {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
// --- Vertex Shader ---
// This is required by OBS. It handles the positioning of the texture.
VertData mainVs(VertData v_in)
{
VertData v_out;
v_out.pos = v_in.pos;
v_out.uv = v_in.uv;
return v_out;
}
// --- Pixel Shader ---
// This is where the blending magic happens.
float4 mainImage(VertData v_in) : TARGET {
// Get texture coordinates
float2 uv = v_in.uv;
// 1. Sample the current frame input
float4 current_color = image.Sample(textureSampler, uv);
// 2. Sample the previous frame output (automatically provided)
float4 prev_color = prevous_output.Sample(textureSampler, uv);
// Calculate the difference between the current and previous frame colors (RGB only)
float diff = distance(current_color.rgb, prev_color.rgb);
// Optimization: If the change is very small (below threshold),
// the scene is likely static here. Don't blend, just use the current frame.
if (diff < diff_threshold) {
return current_color;
}
// If there is significant movement, blend the frames:
else {
// lerp(A, B, factor): Blends between A (prev_color) and B (current_color).
// If blend_factor is 0.7, the result is 30% previous frame + 70% current frame.
return lerp(prev_color, current_color, blend_factor);
}
}
// --- Technique ---
// This is required. It tells OBS which vertex and pixel shaders to run.
technique Draw
{
pass
{
vertex_shader = mainVs(v_in);
pixel_shader = mainImage(v_in);
}
}