Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't let frames go into the negative for CameraMotionBlur #2032

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions packages/motion-blur/src/CameraMotionBlur.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ export type CameraMotionBlurProps = {
samples?: number;
};

/**
* If the current frame is 0, then it is rendered with low opacity,
* and the trailing elements have frame numbers -1, -2, -3, -4, etc.
* To fix this, we instead reduce the number of samples to not go into negative territory.
*/
const getNumberOfSamples = ({
shutterFraction,
samples,
currentFrame,
}: {
shutterFraction: number;
samples: number;
currentFrame: number;
}) => {
const maxOffset = shutterFraction * samples;
const maxTimeReverse = currentFrame - maxOffset;
const factor = Math.min(1, Math.max(0, maxTimeReverse / maxOffset + 1));
return Math.max(1, Math.round(Math.min(factor * samples, samples)));
};

/**
* @description Produces natural looking motion blur similar to what would be produced by a film camera.
* @see [Documentation](https://www.remotion.dev/docs/motion-blur/camera-motion-blur)
Expand Down Expand Up @@ -60,17 +80,24 @@ export const CameraMotionBlur: React.FC<CameraMotionBlurProps> = ({

const shutterFraction = shutterAngle / 360;

const actualSamples = getNumberOfSamples({
currentFrame,
samples,
shutterFraction,
});

return (
<AbsoluteFill style={{isolation: 'isolate'}}>
{new Array(samples).fill(true).map((_, i) => {
{new Array(actualSamples).fill(true).map((_, i) => {
const sample = i + 1;
const sampleFrameOffset = shutterFraction * (sample / samples);
const sampleFrameOffset = shutterFraction * (sample / actualSamples);

return (
<AbsoluteFill
key={`frame-${i.toString()}`}
style={{
mixBlendMode: 'plus-lighter',
filter: `opacity(${1 / samples})`,
filter: `opacity(${1 / actualSamples})`,
}}
>
<Freeze frame={currentFrame - sampleFrameOffset}>{children}</Freeze>
Expand Down