From 4ce574ea78532fb1c36d7019eddd49246ae6a84e Mon Sep 17 00:00:00 2001 From: Tendsin Mende Date: Mon, 6 Sep 2021 00:56:51 +0200 Subject: [PATCH] initial sdf output --- sdf_editor/src/main.rs | 82 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 3 deletions(-) diff --git a/sdf_editor/src/main.rs b/sdf_editor/src/main.rs index 0823b6d..63b15e2 100644 --- a/sdf_editor/src/main.rs +++ b/sdf_editor/src/main.rs @@ -16,7 +16,16 @@ use{ terminal::{Terminal, TerminalCompositor, TerminalEvent, TerminalEditor}, list::{ListEditor}, tree_nav::{TreeNav} - } + }, + nako::{ + stream::{SecondaryStream2d, PrimaryStream2d}, + glam::{Vec2, Vec3}, + operations::{ + planar::primitives2d::Box2d, + volumetric::{Color, Union, Round}, + }, + }, + nakorender::{backend::{Backend, LayerInfo}, winit, camera::Camera2d} }; @@ -32,7 +41,7 @@ impl ColorCollector { let r = l.get(&0).unwrap_or(0); let g = l.get(&1).unwrap_or(0); let b = l.get(&2).unwrap_or(0); - + println!("Set r: {}", r); self.color.set((r as u8, g as u8, b as u8)); } } @@ -83,11 +92,14 @@ async fn main() { ); compositor.write().unwrap().push(color_editor.get_term_view().offset(Vector2::new(2, 2))); + + let color_view = color_port.outer().get_view(); async_std::task::spawn( async move { loop { term_port.update(); + color_port.update(); match term.next_event().await { TerminalEvent::Input(Event::Key(Key::Ctrl('c'))) | TerminalEvent::Input(Event::Key(Key::Ctrl('g'))) | @@ -118,7 +130,71 @@ async fn main() { } } ); + + async_std::task::spawn(async move { + term_writer.show().await.expect("output error!"); + }); + + + let event_loop = nakorender::winit::event_loop::EventLoop::new(); + let window = nakorender::winit::window::Window::new(&event_loop).unwrap(); + let mut renderer = nakorender::marp::MarpBackend::new(&window, &event_loop); + + let id = renderer.new_layer_2d(); + renderer.set_layer_order(&[id.into()]); + renderer.update_camera_2d(id, Camera2d{ + extent: Vec2::new(2.0, 2.0), + location: Vec2::new(-1.0, -1.0), + rotation: 0.0 + }); - term_writer.show().await.expect("output error!"); + event_loop.run(move |event, _target, control_flow|{ + //Set to polling for now, might be overwritten + //TODO: Maybe we want to use "WAIT" for the ui thread? However, the renderers don't work that hard + //if nothing changes. So should be okay for a alpha style programm. + *control_flow = winit::event_loop::ControlFlow::Poll; + + + + //now check if a rerender was requested, or if we worked on all + //events on that batch + match event{ + winit::event::Event::WindowEvent{window_id: _, event: winit::event::WindowEvent::Resized(newsize)} => { + //update layer to cover whole window again. + renderer.set_layer_info(id.into(), LayerInfo{ + extent: (newsize.width as usize, newsize.height as usize), + location: (0,0) + }); + } + winit::event::Event::MainEventsCleared => { + window.request_redraw(); + } + winit::event::Event::RedrawRequested(_) => { + let new_sdf = sdf_from_color(color_view.get()); + renderer.update_sdf_2d(id, new_sdf); + + renderer.render(&window); + } + _ => {}, + } + + }) } + + +fn sdf_from_color(c: (u8, u8, u8)) -> PrimaryStream2d{ + PrimaryStream2d::new() + .push( + SecondaryStream2d::new( + Union, + Box2d{extent: Vec2::new(0.5, 0.5)} + ).push_mod(Color( + Vec3::new( + (c.0 as f32 / 255.0).clamp(0.0, 1.0), + (c.1 as f32 / 255.0).clamp(0.0, 1.0), + (c.2 as f32 / 255.0).clamp(0.0, 1.0), + ) + )).push_mod(Round{radius: 0.2}).build() + ).build() +}