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) ---
// --- 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;
code
// 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);
}
}
Assistant
You're on the right track with your motion-blending shader, but there are a few subtle issues that can cause inconsistent or non-smooth blending between frames:
🔍 Key Issues in Your Shader:
Blending logic is based only on per-pixel RGB difference – this can lead to abrupt transitions between blended and non-blended areas.
No smooth transition between blending and not blending (i.e., using if(diff < threshold) causes hard edges).
Blend factor isn't dynamic – it doesn’t adjust based on actual motion amount.
No temporal persistence – the previous frame's output is used as-is, but no feedback loop for long-term smoothing.
✅ Improvements:
Replace the hard if condition with a smooth blend weight using saturate() and smoothstep().
Use dynamic interpolation weight, so pixels that change more get more of the current frame.
Add a feedback mechanism to stabilize over time.
✨ Fixed & Improved Shader:
hlsl
// Super Smooth Motion Blend Shader for OBS Shaderfilter (HLSL)
// --- Automatic Uniforms provided by obs-shaderfilter ---
uniform texture2d image;
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).
uniform float smoothness <
string label = "Smoothness of Transition";
string widget = "slider";
float minimum = 0.0;
float maximum = 0.1;
float step = 0.001;
> = 0.01;
// Controls how sharp or smooth the transition from static to moving regions is
// --- Shader Structure ---
struct VertData {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
VertData mainVs(VertData v_in)
{
VertData v_out;
v_out.pos = v_in.pos;
v_out.uv = v_in.uv;
return v_out;
}
float4 mainImage(VertData v_in) : TARGET {
float2 uv = v_in.uv;
float4 current_color = image.Sample(textureSampler, uv);
float4 prev_color = prevous_output.Sample(textureSampler, uv);
// Calculate color difference
float diff = distance(current_color.rgb, prev_color.rgb);
// Compute blend weight smoothly instead of using an if-statement
float blend_weight = smoothstep(diff_threshold, diff_threshold + smoothness, diff);
// Interpolate based on motion intensity
float4 blended_color = lerp(prev_color, current_color, blend_weight);
// Optional: mix with original current frame using blend_factor
float4 final_color = lerp(blended_color, current_color, blend_factor);
return final_color;
}
technique Draw
{
pass
{
vertex_shader = mainVs(v_in);
pixel_shader = mainImage(v_in);
}
}
🧠 Explanation of Fixes:
smoothstep(): Instead of using a binary decision (if (diff < threshold)), we create a smooth gradient between 0 and 1 depending on how much motion is present. This avoids harsh transitions.
blend_weight: This determines how much of the current frame should be shown at each pixel based on motion.
Final mix with blend_factor: Allows user control over how much the result leans toward full blending vs. just showing the current frame.
📈 Bonus Tip – For Even Smoother Results Over Time:
If you want to accumulate history across multiple frames (like a trail effect), you’d need to use a second render target or feedback loop, which is more advanced and may require external tools like custom plugins or RenderDoc debugging.