Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions assets/shaders/custom_material_chromatic_aberration.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@ var texture: texture_2d<f32>;
@group(1) @binding(1)
var our_sampler: sampler;

@group(1) @binding(2)
var<uniform> offset_r: vec2<f32>;
@group(1) @binding(3)
var<uniform> offset_g: vec2<f32>;
@group(1) @binding(4)
var<uniform> offset_b: vec2<f32>;

@fragment
fn fragment(
@builtin(position) position: vec4<f32>,
#import bevy_sprite::mesh2d_vertex_output
) -> @location(0) vec4<f32> {
// Get screen position with coordinates from 0 to 1
let uv = position.xy / vec2<f32>(view.width, view.height);
let offset_strength = 0.02;

// Sample each color channel with an arbitrary shift
var output_color = vec4<f32>(
textureSample(texture, our_sampler, uv + vec2<f32>(offset_strength, -offset_strength)).r,
textureSample(texture, our_sampler, uv + vec2<f32>(-offset_strength, 0.0)).g,
textureSample(texture, our_sampler, uv + vec2<f32>(0.0, offset_strength)).b,
textureSample(texture, our_sampler, uv + offset_r).r,
textureSample(texture, our_sampler, uv + offset_g).g,
textureSample(texture, our_sampler, uv + offset_b).b,
1.0
);
);

return output_color;
}
23 changes: 23 additions & 0 deletions assets/shaders/screen_vertex.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#import bevy_sprite::mesh2d_view_bindings
#import bevy_sprite::mesh2d_bindings

// NOTE: Bindings must come before functions that use them!
#import bevy_sprite::mesh2d_functions

struct Vertex {
@location(0) position: vec3<f32>,
@location(2) uv: vec2<f32>,
};

struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
#import bevy_sprite::mesh2d_vertex_output
}

@vertex
fn vertex(vertex: Vertex) -> VertexOutput {
var out: VertexOutput;
out.uv = vertex.uv;
out.clip_position = vec4<f32>((out.uv - vec2<f32>(0.5, 0.5)) * 2.0, 0.0, 1.0);
return out;
}
3 changes: 2 additions & 1 deletion examples/3d/render_to_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bevy::{
render_resource::{
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
},
texture::BevyDefault,
view::RenderLayers,
},
};
Expand Down Expand Up @@ -49,7 +50,7 @@ fn setup(
label: None,
size,
dimension: TextureDimension::D2,
format: TextureFormat::Bgra8UnormSrgb,
format: TextureFormat::bevy_default(),
mip_level_count: 1,
sample_count: 1,
usage: TextureUsages::TEXTURE_BINDING
Expand Down
Loading