Skip to content

experiment: Add camera aspect ratio option #603

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
63 changes: 43 additions & 20 deletions apps/desktop/src/routes/camera.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {

namespace CameraWindow {
export type Size = "sm" | "lg";
export type Shape = "round" | "square";
export type Shape = "round" | "square" | "full";
export type State = {
size: Size;
shape: Shape;
Expand Down Expand Up @@ -114,29 +114,42 @@ function Page() {
});

const [windowSize] = createResource(
() => state.size,
async (size) => {
() => [state.size, state.shape, latestFrame()?.data.width, latestFrame()?.data.height] as const,
async ([size, shape, frameWidth, frameHeight]) => {
const monitor = await currentMonitor();

const windowSize = size === "sm" ? 230 : 400;
const windowHeight = windowSize + BAR_HEIGHT;
const base = size === "sm" ? 230 : 400;
const aspect = frameWidth && frameHeight ? frameWidth / frameHeight : 1;
const windowWidth =
shape === "full"
? aspect >= 1
? base * aspect
: base
: base;
const windowHeight =
shape === "full"
? aspect >= 1
? base
: base / aspect
: base;
const totalHeight = windowHeight + BAR_HEIGHT;

if (!monitor) return;

const scalingFactor = monitor.scaleFactor;
const width = monitor.size.width / scalingFactor - windowSize - 100;
const height = monitor.size.height / scalingFactor - windowHeight - 100;
const width = monitor.size.width / scalingFactor - windowWidth - 100;
const height = monitor.size.height / scalingFactor - totalHeight - 100;

const currentWindow = getCurrentWindow();
currentWindow.setSize(new LogicalSize(windowSize, windowHeight));
currentWindow.setSize(new LogicalSize(windowWidth, totalHeight));
currentWindow.setPosition(
new LogicalPosition(
width + monitor.position.toLogical(scalingFactor).x,
height + monitor.position.toLogical(scalingFactor).y
)
);

return { width, height, size: windowSize };
return { width, height, size: base, windowWidth, windowHeight };
}
);

Expand Down Expand Up @@ -175,9 +188,11 @@ function Page() {
<IconCapEnlarge class="size-5.5" />
</ControlButton>
<ControlButton
pressed={state.shape === "square"}
pressed={state.shape !== "round"}
onClick={() =>
setState("shape", (s) => (s === "round" ? "square" : "round"))
setState("shape", (s) =>
s === "round" ? "square" : s === "square" ? "full" : "round"
)
}
>
<IconCapSquare class="size-5.5" />
Expand Down Expand Up @@ -205,25 +220,33 @@ function Page() {
const aspectRatio =
latestFrame().data.width / latestFrame().data.height;

const windowWidth = windowSize.latest?.size ?? 0;
const base = windowSize.latest?.size ?? 0;
const winWidth = windowSize.latest?.windowWidth ?? base;
const winHeight = windowSize.latest?.windowHeight ?? base;

if (state.shape === "full") {
return {
width: `${winWidth}px`,
height: `${winHeight}px`,
transform: state.mirrored ? "scaleX(-1)" : "scaleX(1)",
};
}

const size = (() => {
if (aspectRatio > 1)
return {
width: windowWidth * aspectRatio,
height: windowWidth,
width: base * aspectRatio,
height: base,
};
else
return {
width: windowWidth,
height: windowWidth * aspectRatio,
width: base,
height: base * aspectRatio,
};
})();

const left =
aspectRatio > 1 ? (size.width - windowWidth) / 2 : 0;
const top =
aspectRatio > 1 ? 0 : (windowWidth - size.height) / 2;
const left = aspectRatio > 1 ? (size.width - base) / 2 : 0;
const top = aspectRatio > 1 ? 0 : (base - size.height) / 2;

return {
width: `${size.width}px`,
Expand Down
6 changes: 6 additions & 0 deletions apps/desktop/src/routes/editor/ConfigSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,12 @@ function CameraConfig(props: { scrollRef: HTMLDivElement }) {
onChange={(mirror) => setProject("camera", "mirror", mirror)}
/>
</Subfield>
<Subfield name="Use Camera Aspect Ratio">
<Toggle
checked={project.camera.use_camera_aspect}
onChange={(v) => setProject("camera", "use_camera_aspect", v)}
/>
</Subfield>
</div>
</Field>
{/** Dashed divider */}
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/utils/tauri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export type AuthenticationInvalid = null
export type BackgroundConfiguration = { source: BackgroundSource; blur: number; padding: number; rounding: number; inset: number; crop: Crop | null; shadow?: number; advancedShadow?: ShadowConfiguration | null }
export type BackgroundSource = { type: "wallpaper"; path: string | null } | { type: "image"; path: string | null } | { type: "color"; value: [number, number, number] } | { type: "gradient"; from: [number, number, number]; to: [number, number, number]; angle?: number }
export type Bounds = { x: number; y: number; width: number; height: number }
export type Camera = { hide: boolean; mirror: boolean; position: CameraPosition; size: number; zoom_size: number | null; rounding?: number; shadow?: number; advanced_shadow?: ShadowConfiguration | null }
export type Camera = { hide: boolean; mirror: boolean; position: CameraPosition; size: number; zoom_size: number | null; rounding?: number; shadow?: number; advanced_shadow?: ShadowConfiguration | null; use_camera_aspect?: boolean }
export type CameraPosition = { x: CameraXPosition; y: CameraYPosition }
export type CameraXPosition = "left" | "center" | "right"
export type CameraYPosition = "top" | "bottom"
Expand Down
3 changes: 3 additions & 0 deletions crates/project/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ pub struct Camera {
pub shadow: f32,
#[serde(default)]
pub advanced_shadow: Option<ShadowConfiguration>,
#[serde(default)]
pub use_camera_aspect: bool,
}

impl Camera {
Expand Down Expand Up @@ -262,6 +264,7 @@ impl Default for Camera {
opacity: 44.2,
blur: 10.5,
}),
use_camera_aspect: false,
}
}
}
Expand Down
43 changes: 32 additions & 11 deletions crates/rendering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,10 +815,25 @@ impl ProjectUniforms {
let zoomed_size =
(zoom.t as f32) * zoom_size * base_size + (1.0 - zoom.t as f32) * base_size;

let size = [
min_axis * zoomed_size + CAMERA_PADDING,
min_axis * zoomed_size + CAMERA_PADDING,
];
let aspect = frame_size[0] / frame_size[1];
let size = if project.camera.use_camera_aspect {
if aspect >= 1.0 {
[
(min_axis * zoomed_size + CAMERA_PADDING) * aspect,
min_axis * zoomed_size + CAMERA_PADDING,
]
} else {
[
min_axis * zoomed_size + CAMERA_PADDING,
(min_axis * zoomed_size + CAMERA_PADDING) / aspect,
]
}
} else {
[
min_axis * zoomed_size + CAMERA_PADDING,
min_axis * zoomed_size + CAMERA_PADDING,
]
};

let position = {
let x = match &project.camera.position.x {
Expand Down Expand Up @@ -847,18 +862,24 @@ impl ProjectUniforms {
CompositeVideoFrameUniforms {
output_size,
frame_size,
crop_bounds: [
(frame_size[0] - frame_size[1]) / 2.0,
0.0,
frame_size[0] - (frame_size[0] - frame_size[1]) / 2.0,
frame_size[1],
],
crop_bounds: if project.camera.use_camera_aspect {
[0.0, 0.0, frame_size[0], frame_size[1]]
} else {
[
(frame_size[0] - frame_size[1]) / 2.0,
0.0,
frame_size[0] - (frame_size[0] - frame_size[1]) / 2.0,
frame_size[1],
]
},
target_bounds,
target_size: [
target_bounds[2] - target_bounds[0],
target_bounds[3] - target_bounds[1],
],
rounding_px: project.camera.rounding / 100.0 * 0.5 * size[0],
rounding_px: project.camera.rounding / 100.0
* 0.5
* size[0].min(size[1]),
mirror_x: if project.camera.mirror { 1.0 } else { 0.0 },
velocity_uv: [0.0, 0.0],
motion_blur_amount,
Expand Down
Loading