Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
1//! Example of a fragment shader for a custom material setup to correctly handle
2//! order independent transparency.
3//! See examples/3d/order_independent_transpatency.rs
5// As an hard-coded always transparent material it is only meant to be forward rendered
6import bevy_pbr::render::forward_io::{VertexOutput, FragmentOutput};
7import bevy_pbr::render::utils;
9@if(MATERIAL_OIT_ENABLED)
10import bevy_core_pipeline::oit::draw::oit_draw;
12@group(constants::MATERIAL_BIND_GROUP) @binding(0) var<uniform> material_color: vec4<f32>;
14@fragment
15fn fragment(
16 in: VertexOutput,
17) -> @location(0) vec4<f32> {
19 // This produces a noise-like varying transparency.
20 // Using world coordinates for simplicity, but local mesh coordinates
21 // would be required if the mesh moved.
22 let alpha = gradient_noise(in.world_position.xyz, 5.0);
23 let color = vec4f(material_color.rgb, alpha);
25@if(MATERIAL_OIT_ENABLED) {
26 // The input color of `oit_draw` should be alpha-premultiplied.
27 oit_draw(in.position, vec4f(color.rgb * color.a, color.a));
28 discard;
31 return color;
34fn gradient_noise(coord: vec3f, scale: f32) -> f32 {
35 let grid_coord = vec3u(scale * abs(coord));
36 let f = fract(scale * abs(coord));
38 let g0 = rand3(grid_coord + vec3u(0, 0, 0));
39 let g1 = rand3(grid_coord + vec3u(1, 0, 0));
40 let g2 = rand3(grid_coord + vec3u(0, 1, 0));
41 let g3 = rand3(grid_coord + vec3u(1, 1, 0));
42 let g4 = rand3(grid_coord + vec3u(0, 0, 1));
43 let g5 = rand3(grid_coord + vec3u(1, 0, 1));
44 let g6 = rand3(grid_coord + vec3u(0, 1, 1));
45 let g7 = rand3(grid_coord + vec3u(1, 1, 1));
47 let noise = mix(
48 mix(
49 mix(dot(g0, f - vec3f(0.0, 0.0, 0.0)),
50 dot(g1, f - vec3f(1.0, 0.0, 0.0)), f.x),
51 mix(dot(g2, f - vec3f(0.0, 1.0, 0.0)),
52 dot(g3, f - vec3f(1.0, 1.0, 0.0)), f.x),
53 f.y),
54 mix(
55 mix(dot(g4, f - vec3f(0.0, 0.0, 1.0)),
56 dot(g5, f - vec3f(1.0, 0.0, 1.0)), f.x),
57 mix(dot(g6, f - vec3f(0.0, 1.0, 1.0)),
58 dot(g7, f - vec3f(1.0, 1.0, 1.0)), f.x),
59 f.y),
60 f.z);
62 return noise;
65fn rand3(seed: vec3u) -> vec3f {
66 var rng: u32 = (seed.x * 3966231743u) ^ (seed.y * 3928936651u) ^ (seed.z * 53134757u);
67 return rand_vec3f(&rng);
70fn rand_vec3f(state: ptr<function, u32>) -> vec3<f32> {
71 return vec3f(utils::rand_f(state), utils::rand_f(state), utils::rand_f(state));
Contents