From ee219d1759d79bb85c4ec1143aae01027d5c507f Mon Sep 17 00:00:00 2001 From: victor <52110451+cs50victor@users.noreply.github.com> Date: Wed, 31 Jan 2024 12:58:38 -0500 Subject: [PATCH] feat: canvas aspect ratio (#17) * feat: canvas aspect ratio * chore: lint --- crates/bevy_headless/src/utils.rs | 6 ++++++ crates/new_media/src/controls.rs | 2 +- crates/new_media/src/main.rs | 3 ++- crates/new_media/src/server.rs | 3 ++- demo/app/layout.tsx | 4 ++-- demo/app/page.tsx | 4 +--- demo/components/Canvas.tsx | 20 ++++++++++++-------- demo/components/LoadingPlaceholder.tsx | 8 ++++++++ demo/components/WebSocketExample.tsx | 12 ++++++++---- 9 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 demo/components/LoadingPlaceholder.tsx diff --git a/crates/bevy_headless/src/utils.rs b/crates/bevy_headless/src/utils.rs index e641fe3..9ce6351 100644 --- a/crates/bevy_headless/src/utils.rs +++ b/crates/bevy_headless/src/utils.rs @@ -64,6 +64,12 @@ impl CurrImage { let (w, h) = self.img_buffer.dimensions(); [w, h] } + + pub fn aspect_ratio(&self) -> [u32; 2] { + let (_w, _h) = self.img_buffer.dimensions(); + // TODO: calculate later + [16, 9] + } } #[derive(Debug, Default, Resource, Event)] diff --git a/crates/new_media/src/controls.rs b/crates/new_media/src/controls.rs index 75ab3d4..2216742 100644 --- a/crates/new_media/src/controls.rs +++ b/crates/new_media/src/controls.rs @@ -26,7 +26,7 @@ pub fn update_world_from_input( input_receiver: Res, mut camera: Query<&mut Transform, With>, ) { - let speed = 0.1; + let speed = 1.0; if let Ok(input) = input_receiver.rx.try_recv() { log::info!("user input : {input}"); match input.as_str() { diff --git a/crates/new_media/src/main.rs b/crates/new_media/src/main.rs index fd0189e..db0bbf4 100644 --- a/crates/new_media/src/main.rs +++ b/crates/new_media/src/main.rs @@ -30,7 +30,7 @@ fn setup_gaussian_cloud( ) { // let remote_file = Some("https://huggingface.co/datasets/cs50victor/splats/resolve/main/train/point_cloud/iteration_7000/point_cloud.gcloud"); // TODO: figure out how to load remote files later - let splat_file = "splats/playroom/point_cloud/iteration_7000/point_cloud.gcloud"; + let splat_file = "splats/garden/point_cloud/iteration_7000/point_cloud.gcloud"; log::info!("loading {}", splat_file); let cloud = asset_server.load(splat_file.to_string()); @@ -80,6 +80,7 @@ fn main() { .filter_module("bevy_ws_server", log::LevelFilter::Debug) .init(); + // TODO: change this a preset of resoltions with their aspect ratios let config = AppConfig { width: 1920, height: 1080 }; Engine::new() diff --git a/crates/new_media/src/server.rs b/crates/new_media/src/server.rs index 38e2e34..cc04fd8 100644 --- a/crates/new_media/src/server.rs +++ b/crates/new_media/src/server.rs @@ -68,7 +68,8 @@ pub fn receive_message( let resp = Message::Text( json!({ "image": curr_img.to_web_base64().unwrap(), - "dimension": curr_img.dimensions() + "dimension": curr_img.dimensions(), + "aspect_ratio": curr_img.aspect_ratio() }) .to_string(), ); diff --git a/demo/app/layout.tsx b/demo/app/layout.tsx index 8262b02..67e8ba2 100644 --- a/demo/app/layout.tsx +++ b/demo/app/layout.tsx @@ -1,5 +1,5 @@ import type { Metadata } from 'next'; -import { Inter, Roboto_Mono } from 'next/font/google'; +import { Bricolage_Grotesque, Inter, Roboto_Mono } from 'next/font/google'; import { TailwindIndicator } from '~/components/TailwindIndicator'; import { tw } from '~/utils/tw'; @@ -11,7 +11,7 @@ const inter = Inter({ variable: '--font-sans', }); -const display = Roboto_Mono({ +const display = Bricolage_Grotesque({ subsets: ['latin'], variable: '--font-display', }); diff --git a/demo/app/page.tsx b/demo/app/page.tsx index 0a420a7..203dde5 100644 --- a/demo/app/page.tsx +++ b/demo/app/page.tsx @@ -2,10 +2,8 @@ import WebSocketExample from "~/components/WebSocketExample"; export default function Page() { return ( -
-
+
-
) } diff --git a/demo/components/Canvas.tsx b/demo/components/Canvas.tsx index 5197d97..11e49fb 100644 --- a/demo/components/Canvas.tsx +++ b/demo/components/Canvas.tsx @@ -2,15 +2,18 @@ import { useEffect, useRef, useState } from "react"; import { ServerWSResponse } from "./WebSocketExample"; +import { tw } from "~/utils/tw"; -export const Canvas=({img_metadata}:{img_metadata: ServerWSResponse })=>{ +export const Canvas=({img_metadata, className}:{img_metadata: ServerWSResponse, className?: string })=>{ const canvasRef = useRef(null); const [cursorPosition, setCursorPosition] = useState<[number, number]>() - const aspectRatio = 16/9; + const [w, h] = img_metadata.dimension; useEffect(() => { if (img_metadata){ const ctx = canvasRef.current?.getContext('2d'); + // ctx?.canvas.width = window.innerWidth; + // ctx?.canvas.height = window.innerWidth; const img = new Image(); img.src = img_metadata.image; [img.width, img.height] = img_metadata.dimension @@ -23,19 +26,20 @@ export const Canvas=({img_metadata}:{img_metadata: ServerWSResponse })=>{ return ( <> { - const [x,y] = [event.clientX, event.clientY] - setCursorPosition([Math.round(x),Math.round(y)]) - console.log(event.clientX, event.clientY) + setCursorPosition([Math.round(event.clientX), Math.round(event.clientY)]) }} onPointerLeave={(_)=>{ setCursorPosition(undefined) }} /> +

+ cursor position : [{" "} {cursorPosition?.[0] ?? "_"}, {cursorPosition?.[1] ?? "_"}{" "}] +

) } diff --git a/demo/components/LoadingPlaceholder.tsx b/demo/components/LoadingPlaceholder.tsx new file mode 100644 index 0000000..2260397 --- /dev/null +++ b/demo/components/LoadingPlaceholder.tsx @@ -0,0 +1,8 @@ +export const LoadingScreen=()=>{ + return ( +
+

NEW MEDIA

+

trying to connect to server...

+
+ ) +} diff --git a/demo/components/WebSocketExample.tsx b/demo/components/WebSocketExample.tsx index 2d24ca7..a9ec203 100644 --- a/demo/components/WebSocketExample.tsx +++ b/demo/components/WebSocketExample.tsx @@ -3,10 +3,12 @@ import { useEffect, useState } from 'react'; import { Canvas } from './Canvas'; import { Controller } from './Controller'; +import { LoadingScreen } from './LoadingPlaceholder'; export interface ServerWSResponse { image: string dimension: [number, number] + aspect_ratio: [number, number] } export default function WebSocketExample({port = 8080}:{port?:number}){ @@ -48,12 +50,14 @@ export default function WebSocketExample({port = 8080}:{port?:number}){ }, []); return ( -
+ <> { imgMetadata ? ( - - ) :

NEW MEDIA | trying to connect to server...

+
+ +
+ ) : () } {socket ? : null} -
+ ) };