diff --git a/examples/example.rs b/examples/example.rs index a5499b0..735cc01 100644 --- a/examples/example.rs +++ b/examples/example.rs @@ -11,7 +11,7 @@ fn main() { let shader = Shader::from( // Tip: Try changing the shader! // Options: CIRCLE, RAINBOW, OCEAN - OCEAN + CIRCLE ); // Execute shader diff --git a/examples/shaders/src/circle.rs b/examples/shaders/src/circle.rs index 9b2d066..e2dfa22 100644 --- a/examples/shaders/src/circle.rs +++ b/examples/shaders/src/circle.rs @@ -36,5 +36,5 @@ pub fn circle( constants: &CircleConstants, frag_coord: Vec2) -> Vec4 { 1.0-smoothstep(0.0,0.01,d.abs()) ); - col.extend(1.0) + to_linear(col.extend(1.0)) } \ No newline at end of file diff --git a/examples/shaders/src/common.rs b/examples/shaders/src/common.rs index 6b3b86a..f9c9be1 100644 --- a/examples/shaders/src/common.rs +++ b/examples/shaders/src/common.rs @@ -42,4 +42,22 @@ pub fn sin3(v: Vec3) -> Vec3 { pub fn reflect(ray: Vec3, normal: Vec3) -> Vec3 { ray - normal * 2.0 * ray.dot(normal) +} + +// NOTE: This function is for converting particularly stubborn Shadertoy shaders to the proper linear color space. +pub fn to_linear(color: Vec4) -> Vec4 { + vec4( + to_linear_f32(color.x), + to_linear_f32(color.y), + to_linear_f32(color.z), + color.w, + ) +} + +fn to_linear_f32(color: f32) -> f32 { + if color <= 0.04045 { + color / 12.92 + } else { + ((color + 0.055) / 1.055).powf(2.4) + } } \ No newline at end of file diff --git a/examples/shaders/src/ocean.rs b/examples/shaders/src/ocean.rs index fc4ff7f..ed1688a 100644 --- a/examples/shaders/src/ocean.rs +++ b/examples/shaders/src/ocean.rs @@ -165,7 +165,7 @@ fn aces_tonemap(color: Vec3) -> Vec3 { let v = m1 * color; let a = v * (v + 0.0245786) - 0.000090537; let b = v * (0.983729 * v + 0.4329510) + 0.238081; - (m2 * (a / b)).clamp(Vec3::ZERO, Vec3::ONE).powf(1.0 / 2.2) + (m2 * (a / b)).clamp(Vec3::ZERO, Vec3::ONE)//.powf(1.0 / 2.2) } // ** "Entry point" (effectively) diff --git a/examples/shaders/src/rainbow.rs b/examples/shaders/src/rainbow.rs index 57a1446..cb4267e 100644 --- a/examples/shaders/src/rainbow.rs +++ b/examples/shaders/src/rainbow.rs @@ -45,5 +45,5 @@ pub fn rainbow( final_color += col * d; } - final_color.extend(1.0) + to_linear(final_color.extend(1.0)) } \ No newline at end of file diff --git a/src/graphics.rs b/src/graphics.rs index b5ea5e5..06c0d1a 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -130,8 +130,7 @@ impl + Copy + Clone + Pod + Zeroable> State { let surface_format = surface_caps.formats.iter() .copied() - // HACK (thedocruby) This should be using sRGB, but it's not working properly for some reason - .find(|f| !f.is_srgb()) + .find(|f| f.is_srgb()) .unwrap_or(surface_caps.formats[0]); let mut config = wgpu::SurfaceConfiguration {