| 1 |
|
| 2 |
|
| 3 |
use std::f32::consts::FRAC_PI_2; |
| 4 |
|
| 5 |
use bevy::{ |
| 6 |
color::palettes::{css::RED, tailwind::GRAY_600}, |
| 7 |
mesh::{SphereKind, SphereMeshBuilder}, |
| 8 |
pbr::{ExtendedMaterial, MaterialExtension, MeshMaterial3d}, |
| 9 |
prelude::*, |
| 10 |
render::render_resource::{AsBindGroup, ShaderType}, |
| 11 |
shader::ShaderRef, |
| 12 |
utils::default, |
| 13 |
}; |
| 14 |
|
| 15 |
|
| 16 |
static SHADER_ASSET_PATH: &str = "shaders/extended_material_bindless.wesl"; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#[derive(Asset, Clone, Reflect, AsBindGroup)] |
| 59 |
#[data(50, ExampleBindlessExtensionUniform, binding_array(101))] |
| 60 |
#[bindless(index_table(range(50..53), binding(100)))] |
| 61 |
struct ExampleBindlessExtension { |
| 62 |
|
| 63 |
modulate_color: Color, |
| 64 |
|
| 65 |
#[texture(51)] |
| 66 |
#[sampler(52)] |
| 67 |
modulate_texture: Option<Handle<Image>>, |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
#[derive(Clone, Default, ShaderType)] |
| 73 |
struct ExampleBindlessExtensionUniform { |
| 74 |
|
| 75 |
|
| 76 |
modulate_color: Vec4, |
| 77 |
} |
| 78 |
|
| 79 |
impl MaterialExtension for ExampleBindlessExtension { |
| 80 |
fn fragment_shader() -> ShaderRef { |
| 81 |
SHADER_ASSET_PATH.into() |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
impl<'a> From<&'a ExampleBindlessExtension> for ExampleBindlessExtensionUniform { |
| 86 |
fn from(material_extension: &'a ExampleBindlessExtension) -> Self { |
| 87 |
|
| 88 |
|
| 89 |
ExampleBindlessExtensionUniform { |
| 90 |
modulate_color: LinearRgba::from(material_extension.modulate_color).to_vec4(), |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
fn main() { |
| 97 |
App::new() |
| 98 |
.add_plugins(DefaultPlugins) |
| 99 |
.add_plugins(MaterialPlugin::< |
| 100 |
ExtendedMaterial<StandardMaterial, ExampleBindlessExtension>, |
| 101 |
>::default()) |
| 102 |
.add_systems(Startup, setup) |
| 103 |
.add_systems(Update, rotate_sphere) |
| 104 |
.run(); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
fn setup( |
| 109 |
mut commands: Commands, |
| 110 |
asset_server: Res<AssetServer>, |
| 111 |
mut meshes: ResMut<Assets<Mesh>>, |
| 112 |
mut materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, ExampleBindlessExtension>>>, |
| 113 |
) { |
| 114 |
|
| 115 |
commands.spawn(( |
| 116 |
Mesh3d(meshes.add(SphereMeshBuilder::new( |
| 117 |
1.0, |
| 118 |
SphereKind::Uv { |
| 119 |
sectors: 20, |
| 120 |
stacks: 20, |
| 121 |
}, |
| 122 |
))), |
| 123 |
MeshMaterial3d(materials.add(ExtendedMaterial { |
| 124 |
base: StandardMaterial { |
| 125 |
base_color: GRAY_600.into(), |
| 126 |
..default() |
| 127 |
}, |
| 128 |
extension: ExampleBindlessExtension { |
| 129 |
modulate_color: RED.into(), |
| 130 |
modulate_texture: Some(asset_server.load("textures/uv_checker_bw.png")), |
| 131 |
}, |
| 132 |
})), |
| 133 |
Transform::from_xyz(0.0, 0.5, 0.0), |
| 134 |
)); |
| 135 |
|
| 136 |
|
| 137 |
commands.spawn(( |
| 138 |
DirectionalLight::default(), |
| 139 |
Transform::from_xyz(1.0, 1.0, 1.0).looking_at(Vec3::ZERO, Vec3::Y), |
| 140 |
)); |
| 141 |
|
| 142 |
|
| 143 |
commands.spawn(( |
| 144 |
Camera3d::default(), |
| 145 |
Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), |
| 146 |
)); |
| 147 |
} |
| 148 |
|
| 149 |
fn rotate_sphere(mut meshes: Query<&mut Transform, With<Mesh3d>>, time: Res<Time>) { |
| 150 |
for mut transform in &mut meshes { |
| 151 |
transform.rotation = |
| 152 |
Quat::from_euler(EulerRot::YXZ, -time.elapsed_secs(), FRAC_PI_2 * 3.0, 0.0); |
| 153 |
} |
| 154 |
} |