generated from cosmic-utils/cosmic-app-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96d6ead
commit f177be9
Showing
11 changed files
with
495 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use cosmic::iced_widget::shader::{self}; | ||
|
||
use crate::shaders::ShaderPipeline; | ||
|
||
// ---- Shader ---- | ||
pub struct ColorGraph { | ||
pub hue: f32, | ||
pub saturation: f32, | ||
pub value: f32, | ||
} | ||
|
||
impl<Message> shader::Program<Message> for ColorGraph { | ||
type State = (); | ||
type Primitive = Primitive; | ||
|
||
fn draw( | ||
&self, | ||
state: &Self::State, | ||
cursor: cosmic::iced_core::mouse::Cursor, | ||
bounds: cosmic::iced::Rectangle, | ||
) -> Self::Primitive { | ||
Primitive::new(self.hue, self.saturation, self.value) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Primitive { | ||
uniforms: Uniforms, | ||
} | ||
|
||
impl Primitive { | ||
pub fn new(hue: f32, saturation: f32, value: f32) -> Self { | ||
Self { | ||
uniforms: Uniforms { | ||
hue, | ||
saturation, | ||
value, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
impl shader::Primitive for Primitive { | ||
fn prepare( | ||
&self, | ||
format: shader::wgpu::TextureFormat, | ||
device: &shader::wgpu::Device, | ||
queue: &shader::wgpu::Queue, | ||
bounds: cosmic::iced::Rectangle, | ||
target_size: cosmic::iced::Size<u32>, | ||
scale_factor: f32, | ||
storage: &mut shader::Storage, | ||
) { | ||
if !storage.has::<ShaderPipeline<Uniforms, 0>>() { | ||
storage.store(ShaderPipeline::<Uniforms, 0>::new( | ||
device, | ||
queue, | ||
format, | ||
include_str!("hsv.wgsl"), | ||
)); | ||
} | ||
|
||
let pipeline = storage.get_mut::<ShaderPipeline<Uniforms, 0>>().unwrap(); | ||
pipeline.write(queue, &self.uniforms); | ||
} | ||
|
||
fn render( | ||
&self, | ||
storage: &shader::Storage, | ||
target: &shader::wgpu::TextureView, | ||
target_size: cosmic::iced::Size<u32>, | ||
viewport: cosmic::iced::Rectangle<u32>, | ||
encoder: &mut shader::wgpu::CommandEncoder, | ||
) { | ||
let pipeline = storage.get::<ShaderPipeline<Uniforms, 0>>().unwrap(); | ||
pipeline.render(target, encoder, viewport); | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)] | ||
#[repr(C)] | ||
struct Uniforms { | ||
hue: f32, | ||
saturation: f32, | ||
value: f32, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
struct HSV { | ||
hue: f32, | ||
saturation: f32, | ||
value: f32, | ||
} | ||
|
||
@group(0) @binding(0) var<uniform> hsv: HSV; | ||
|
||
@fragment | ||
fn fs_main( | ||
@builtin(position) _clip_pos: vec4<f32>, | ||
@location(0) uv: vec2<f32>, | ||
) -> @location(0) vec4<f32> { | ||
// uv.x = saturation | ||
// uv.y = value | ||
|
||
// HSV to RGB | ||
let c = uv.y * uv.x; | ||
let h_ = hsv.hue / 60.0; | ||
let x = c * (1.0 - abs(h_ % 2.0 - 1.0)); | ||
|
||
var r1 = 0.0; | ||
var g1 = 0.0; | ||
var b1 = 0.0; | ||
if 0.0 <= h_ && h_ < 1.0 { | ||
r1 = c; | ||
g1 = x; | ||
} else if 1.0 <= h_ && h_ < 2.0 { | ||
r1 = x; | ||
g1 = c; | ||
} else if 2.0 <= h_ && h_ < 3.0 { | ||
g1 = c; | ||
b1 = x; | ||
} else if 3.0 <= h_ && h_ < 4.0 { | ||
g1 = x; | ||
b1 = c; | ||
} else if 4.0 <= h_ && h_ < 5.0 { | ||
r1 = x; | ||
b1 = c; | ||
} else { | ||
r1 = c; | ||
b1 = x; | ||
} | ||
|
||
let m = uv.y - c; | ||
let color = vec4<f32>(r1 + m, g1 + m, b1 + m, 1.0); | ||
return color; | ||
} |
Oops, something went wrong.