Skip to content

Commit

Permalink
Counter ugly shadertoy jank
Browse files Browse the repository at this point in the history
  • Loading branch information
thedocruby committed Oct 25, 2023
1 parent e6def67 commit 2ea3027
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn main() {
let shader = Shader::from(
// Tip: Try changing the shader!
// Options: CIRCLE, RAINBOW, OCEAN
OCEAN
CIRCLE
);

// Execute shader
Expand Down
2 changes: 1 addition & 1 deletion examples/shaders/src/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
18 changes: 18 additions & 0 deletions examples/shaders/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion examples/shaders/src/ocean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/shaders/src/rainbow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ pub fn rainbow(
final_color += col * d;
}

final_color.extend(1.0)
to_linear(final_color.extend(1.0))
}
3 changes: 1 addition & 2 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ impl<T: From<Constants> + Copy + Clone + Pod + Zeroable> State<T> {

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 {
Expand Down

0 comments on commit 2ea3027

Please sign in to comment.