mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 14:40:19 +00:00
deeaf64897
# Objective I noticed different examples descriptions were not using the same structure: ![different_wordings_examples](https://user-images.githubusercontent.com/2290685/169487055-ab76743e-3400-486f-b672-e8f60455b8e4.png) This results in sentences that a reader has to read differently each time, which might result in information being hard to find, especially foreign language users. Original discord discussion: https://discord.com/channels/691052431525675048/976846499889705020 ## Solution - Use less different words, similar structure and being straight to the point. --- ## Changelog - Examples descriptions more accessible.
138 lines
4.6 KiB
Rust
138 lines
4.6 KiB
Rust
//! A shader and a material that uses it.
|
|
|
|
use bevy::{
|
|
ecs::system::{lifetimeless::SRes, SystemParamItem},
|
|
pbr::MaterialPipeline,
|
|
prelude::*,
|
|
reflect::TypeUuid,
|
|
render::{
|
|
render_asset::{PrepareAssetError, RenderAsset},
|
|
render_resource::{
|
|
encase, BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout,
|
|
BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindingType, Buffer,
|
|
BufferBindingType, BufferInitDescriptor, BufferUsages, ShaderSize, ShaderStages,
|
|
ShaderType,
|
|
},
|
|
renderer::RenderDevice,
|
|
},
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.add_plugin(MaterialPlugin::<CustomMaterial>::default())
|
|
.add_startup_system(setup)
|
|
.run();
|
|
}
|
|
|
|
/// set up a simple 3D scene
|
|
fn setup(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<CustomMaterial>>,
|
|
) {
|
|
// cube
|
|
commands.spawn().insert_bundle(MaterialMeshBundle {
|
|
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
|
|
transform: Transform::from_xyz(0.0, 0.5, 0.0),
|
|
material: materials.add(CustomMaterial {
|
|
color: Color::GREEN,
|
|
}),
|
|
..default()
|
|
});
|
|
|
|
// camera
|
|
commands.spawn_bundle(PerspectiveCameraBundle {
|
|
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
..default()
|
|
});
|
|
}
|
|
|
|
// This is the struct that will be passed to your shader
|
|
#[derive(Debug, Clone, TypeUuid)]
|
|
#[uuid = "f690fdae-d598-45ab-8225-97e2a3f056e0"]
|
|
pub struct CustomMaterial {
|
|
color: Color,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct GpuCustomMaterial {
|
|
_buffer: Buffer,
|
|
bind_group: BindGroup,
|
|
}
|
|
|
|
// The implementation of [`Material`] needs this impl to work properly.
|
|
impl RenderAsset for CustomMaterial {
|
|
type ExtractedAsset = CustomMaterial;
|
|
type PreparedAsset = GpuCustomMaterial;
|
|
type Param = (SRes<RenderDevice>, SRes<MaterialPipeline<Self>>);
|
|
fn extract_asset(&self) -> Self::ExtractedAsset {
|
|
self.clone()
|
|
}
|
|
|
|
fn prepare_asset(
|
|
extracted_asset: Self::ExtractedAsset,
|
|
(render_device, material_pipeline): &mut SystemParamItem<Self::Param>,
|
|
) -> Result<Self::PreparedAsset, PrepareAssetError<Self::ExtractedAsset>> {
|
|
let color = Vec4::from_slice(&extracted_asset.color.as_linear_rgba_f32());
|
|
|
|
let byte_buffer = [0u8; Vec4::SIZE.get() as usize];
|
|
let mut buffer = encase::UniformBuffer::new(byte_buffer);
|
|
buffer.write(&color).unwrap();
|
|
|
|
let buffer = render_device.create_buffer_with_data(&BufferInitDescriptor {
|
|
contents: buffer.as_ref(),
|
|
label: None,
|
|
usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
|
|
});
|
|
let bind_group = render_device.create_bind_group(&BindGroupDescriptor {
|
|
entries: &[BindGroupEntry {
|
|
binding: 0,
|
|
resource: buffer.as_entire_binding(),
|
|
}],
|
|
label: None,
|
|
layout: &material_pipeline.material_layout,
|
|
});
|
|
|
|
Ok(GpuCustomMaterial {
|
|
_buffer: buffer,
|
|
bind_group,
|
|
})
|
|
}
|
|
}
|
|
|
|
impl Material for CustomMaterial {
|
|
// When creating a custom material, you need to define either a vertex shader, a fragment shader or both.
|
|
// If you don't define one of them it will use the default mesh shader which can be found at
|
|
// <https://github.com/bevyengine/bevy/blob/latest/crates/bevy_pbr/src/render/mesh.wgsl>
|
|
|
|
// For this example we don't need a vertex shader
|
|
// fn vertex_shader(asset_server: &AssetServer) -> Option<Handle<Shader>> {
|
|
// // Use the same path as the fragment shader since wgsl let's you define both shader in the same file
|
|
// Some(asset_server.load("shaders/custom_material.wgsl"))
|
|
// }
|
|
|
|
fn fragment_shader(asset_server: &AssetServer) -> Option<Handle<Shader>> {
|
|
Some(asset_server.load("shaders/custom_material.wgsl"))
|
|
}
|
|
|
|
fn bind_group(render_asset: &<Self as RenderAsset>::PreparedAsset) -> &BindGroup {
|
|
&render_asset.bind_group
|
|
}
|
|
|
|
fn bind_group_layout(render_device: &RenderDevice) -> BindGroupLayout {
|
|
render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
|
|
entries: &[BindGroupLayoutEntry {
|
|
binding: 0,
|
|
visibility: ShaderStages::FRAGMENT,
|
|
ty: BindingType::Buffer {
|
|
ty: BufferBindingType::Uniform,
|
|
has_dynamic_offset: false,
|
|
min_binding_size: Some(Vec4::min_size()),
|
|
},
|
|
count: None,
|
|
}],
|
|
label: None,
|
|
})
|
|
}
|
|
}
|