diff options
| author | s-ol <s+removethis@s-ol.nu> | 2025-12-28 18:19:33 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-12-28 18:19:33 +0000 |
| commit | 0b562eef57d34dba72f1d7f3dd9e733d30ac1f3c (patch) | |
| tree | 9d5c099ab6a7651a88baf3bf645b4278e3e6e540 /src | |
| parent | expand and organize builtin functions (diff) | |
| download | nodetoy-0b562eef57d34dba72f1d7f3dd9e733d30ac1f3c.tar.gz nodetoy-0b562eef57d34dba72f1d7f3dd9e733d30ac1f3c.zip | |
add resolution, time inputs
Diffstat (limited to 'src')
| -rw-r--r-- | src/library.rs | 21 | ||||
| -rw-r--r-- | src/main.rs | 6 | ||||
| -rw-r--r-- | src/preview.rs | 47 |
3 files changed, 43 insertions, 31 deletions
diff --git a/src/library.rs b/src/library.rs index 18c0ef9..ef161be 100644 --- a/src/library.rs +++ b/src/library.rs @@ -1051,7 +1051,8 @@ impl fmt::Display for AnyConstant { #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum Input { UV, - // Resolution, + Resolution, + Time, } impl ConcreteNode for Input { @@ -1067,7 +1068,13 @@ impl ConcreteNode for Input { } fn signature(&self, _connected: &[Option<Type>]) -> TypeSignature { - TypeSignature::new([], [Vector(Float(Single), D2)]) + TypeSignature::new( + [], + [match self { + Input::UV | Input::Resolution => Vector(Float(Single), D2), + Input::Time => Scalar(Float(Single)), + }], + ) } fn compile( @@ -1084,6 +1091,8 @@ impl ConcreteNode for Input { "{out_typ} {out_name} = {};", match self { Input::UV => "in_uv", + Input::Resolution => "in_resolution", + Input::Time => "in_time", } ) } @@ -1092,17 +1101,15 @@ impl NodeIndex for Input { const TITLE: &'static str = "Inputs"; fn all() -> impl Iterator<Item = Input> { - [ - Input::UV, - // Input::Resolution, - ] - .into_iter() + [Input::UV, Input::Resolution, Input::Time].into_iter() } } impl fmt::Display for Input { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Input::UV => write!(f, "UV Coordinates"), + Input::Resolution => write!(f, "Output Resolution"), + Input::Time => write!(f, "Time in Seconds"), } } } diff --git a/src/main.rs b/src/main.rs index a40fb21..37bddee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -287,6 +287,12 @@ impl DemoViewer { in vec2 in_uv; out vec4 out_color; +layout(std140, binding = 0) uniform Uniforms {{ + vec2 in_resolution; + float in_time; + float _pad3; +}} uniforms; + void main() {{ " )?; diff --git a/src/preview.rs b/src/preview.rs index abd9611..f1d4820 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -7,9 +7,7 @@ use eframe::{ egui_wgpu::{self, wgpu}, }; -pub struct Custom3d { - angle: f32, -} +pub struct Custom3d; impl Custom3d { pub fn new<'a>(cc: &'a eframe::CreationContext<'a>) -> Option<Self> { @@ -37,7 +35,7 @@ void main() { )); resources.insert(format); - Some(Self { angle: 0.0 }) + Some(Self) } } @@ -62,8 +60,8 @@ void main() { // The paint callback is called after finish prepare and is given access to egui's main render pass, // which can be used to issue draw commands. struct CustomTriangleCallback { - angle: f32, frag_shader: Option<String>, + time: f32, } impl egui_wgpu::CallbackTrait for CustomTriangleCallback { @@ -71,7 +69,7 @@ impl egui_wgpu::CallbackTrait for CustomTriangleCallback { &self, device: &wgpu::Device, queue: &wgpu::Queue, - _screen_descriptor: &egui_wgpu::ScreenDescriptor, + screen: &egui_wgpu::ScreenDescriptor, _egui_encoder: &mut wgpu::CommandEncoder, resources: &mut egui_wgpu::CallbackResources, ) -> Vec<wgpu::CommandBuffer> { @@ -84,7 +82,7 @@ impl egui_wgpu::CallbackTrait for CustomTriangleCallback { let triangle_resources: &mut TriangleRenderResources = resources.get_mut().unwrap(); triangle_resources } - .prepare(device, queue, self.angle); + .prepare(device, queue, screen, self.time); Vec::new() } @@ -102,17 +100,17 @@ impl egui_wgpu::CallbackTrait for CustomTriangleCallback { impl Custom3d { pub fn custom_painting(&mut self, ui: &mut egui::Ui, frag_shader: Option<String>) { - let (rect, response) = - ui.allocate_exact_size(egui::Vec2::splat(300.0), egui::Sense::drag()); + let (rect, _) = ui.allocate_exact_size(egui::Vec2::splat(300.0), egui::Sense::drag()); - self.angle += response.drag_motion().x * 0.01; ui.painter().add(egui_wgpu::Callback::new_paint_callback( rect, CustomTriangleCallback { - angle: self.angle, frag_shader, + time: ui.ctx().input(|i| i.time) as f32, }, )); + + ui.ctx().request_repaint(); } } @@ -132,13 +130,6 @@ impl TriangleRenderResources { label: Some("custom3d"), source: wgpu::ShaderSource::Glsl { shader: "#version 450 -layout(std140, binding = 0) uniform Uniforms { - float angle; - float _pad1; - float _pad2; - float _pad3; -} uniforms; - out vec2 out_uv; const vec2 v_positions[3] = vec2[3]( @@ -156,8 +147,6 @@ const vec4 v_colors[3] = vec4[3]( void main() { vec2 pos = v_positions[gl_VertexIndex]; gl_Position = vec4(pos, 0.0, 1.0); - - gl_Position.x *= cos(uniforms.angle); out_uv = gl_Position.xy / 2.0 + 0.5; }" .into(), @@ -178,7 +167,7 @@ void main() { label: Some("custom3d"), entries: &[wgpu::BindGroupLayoutEntry { binding: 0, - visibility: wgpu::ShaderStages::VERTEX, + visibility: wgpu::ShaderStages::FRAGMENT, ty: wgpu::BindingType::Buffer { ty: wgpu::BufferBindingType::Uniform, has_dynamic_offset: false, @@ -240,12 +229,22 @@ void main() { } } - fn prepare(&mut self, _device: &wgpu::Device, queue: &wgpu::Queue, angle: f32) { - // Update our uniform buffer with the angle from the UI + fn prepare( + &mut self, + _device: &wgpu::Device, + queue: &wgpu::Queue, + screen: &egui_wgpu::ScreenDescriptor, + time: f32, + ) { queue.write_buffer( &self.uniform_buffer, 0, - bytemuck::cast_slice(&[angle, 0.0, 0.0, 0.0]), + bytemuck::cast_slice(&[ + screen.size_in_pixels[0] as f32, + screen.size_in_pixels[1] as f32, + time, + 0.0, + ]), ); } |
