bevy-experiments/assets/shaders/material.wgsl

46 lines
1.1 KiB
WebGPU Shading Language

#import bevy_pbr::{
forward_io::{VertexOutput, FragmentOutput},
mesh_view_bindings::view,
}
struct VoluMaterial {
mesh_translation: vec3<f32>,
sphere_radius: f32,
color: vec4<f32>,
}
@group(2) @binding(100)
var<storage, read> volu_material: VoluMaterial;
fn raymarch_hit(position_: vec3<f32>, direction: vec3<f32>) -> bool {
var position = position_;
for (var i = 0; i < 200; i++) {
if sphere_hit(position) {
return true;
}
position += direction * 0.01;
}
return false;
}
fn sphere_hit(p: vec3<f32>) -> bool {
return distance(p, volu_material.mesh_translation) < volu_material.sphere_radius;
}
@fragment
fn fragment(
in: VertexOutput,
@builtin(front_facing) is_front: bool,
) -> FragmentOutput {
let view_direction = normalize(in.world_position.xyz - view.world_position);
var out: FragmentOutput;
if (raymarch_hit(in.world_position.xyz, view_direction)) {
out.color = volu_material.color;
} else {
out.color = vec4(1.0, 1.0, 1.0, 0.2);
}
return out;
}