Skip to content

Commit

Permalink
Global Wireframe & Cargo Fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Neo-Zhixing committed Sep 23, 2020
1 parent 2c44264 commit 334ecd2
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 76 deletions.
16 changes: 9 additions & 7 deletions crates/bevy_pbr/src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bevy_render::{
pipeline::{DynamicBinding, PipelineSpecialization, RenderPipeline, RenderPipelines},
render_graph::base::MainPass,
wireframe,
wireframe::WithWireframe,
};
use bevy_transform::prelude::{GlobalTransform, Transform};

Expand Down Expand Up @@ -53,13 +54,14 @@ impl Default for PbrComponents {
}
}

impl PbrComponents {
/// Display the entity with wireframe
pub fn with_wireframe(mut self) -> Self {
self.render_pipelines.pipelines.push(bevy_render::pipeline::RenderPipeline::specialized(
wireframe::WIREFRAME_PIPELINE_HANDLE,
Default::default(),
));
impl wireframe::WithWireframe for PbrComponents {
fn with_wireframe(mut self) -> Self {
self.render_pipelines
.pipelines
.push(bevy_render::pipeline::RenderPipeline::specialized(
wireframe::WIREFRAME_PIPELINE_HANDLE,
Default::default(),
));
self
}
}
Expand Down
90 changes: 35 additions & 55 deletions crates/bevy_render/src/wireframe/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
use bevy_asset::{Handle, Assets};
use crate::shader::{Shader, ShaderStage, ShaderStages};
use crate::pipeline::{PipelineDescriptor, RasterizationStateDescriptor, FrontFace, CullMode, PolygonMode, DepthStencilStateDescriptor, CompareFunction, StencilStateDescriptor, StencilStateFaceDescriptor, ColorStateDescriptor, BlendDescriptor, BlendFactor, BlendOperation, ColorWrite};
use crate::texture::TextureFormat;
use crate::{
pipeline::{
BlendDescriptor, BlendFactor, BlendOperation, ColorStateDescriptor, ColorWrite,
CompareFunction, CullMode, DepthStencilStateDescriptor, FrontFace, PipelineDescriptor,
PolygonMode, RasterizationStateDescriptor, RenderPipeline, StencilStateDescriptor,
StencilStateFaceDescriptor,
},
prelude::*,
shader::{Shader, ShaderStage, ShaderStages},
texture::TextureFormat,
};
use bevy_app::prelude::*;
use crate::prelude::*;
use bevy_asset::{Assets, Handle};
use bevy_ecs::{Entity, World, WorldWriter};

mod pipeline;

pub const WIREFRAME_PIPELINE_HANDLE: Handle<PipelineDescriptor> =
Handle::from_u128(0xb34b2eb2f5c0b730685123c7742bcd74);

#[derive(Default)]
#[derive(Debug, Default)]
pub struct WireframePlugin;

impl Plugin for WireframePlugin {
Expand All @@ -18,58 +28,28 @@ impl Plugin for WireframePlugin {
let mut pipelines = resources.get_mut::<Assets<PipelineDescriptor>>().unwrap();
pipelines.set(
WIREFRAME_PIPELINE_HANDLE,
build_wireframe_pipeline(&mut shaders),
pipeline::build_wireframe_pipeline(&mut shaders),
);
// mut shaders: ResMut<Assets<bevy_render::shader::Shader>>,
// mut pipelines: ResMut<Assets<bevy_render::pipeline::PipelineDescriptor>>,
}
}

fn build_wireframe_pipeline(shaders: &mut Assets<Shader>) -> PipelineDescriptor {
PipelineDescriptor {
rasterization_state: Some(RasterizationStateDescriptor {
front_face: FrontFace::Ccw,
cull_mode: CullMode::Back,
polygon_mode: PolygonMode::Line,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
clamp_depth: false,
}),
depth_stencil_state: Some(DepthStencilStateDescriptor {
format: TextureFormat::Depth32Float,
depth_write_enabled: true,
depth_compare: CompareFunction::Less,
stencil: StencilStateDescriptor {
front: StencilStateFaceDescriptor::IGNORE,
back: StencilStateFaceDescriptor::IGNORE,
read_mask: 0,
write_mask: 0,
},
}),
color_states: vec![ColorStateDescriptor {
format: TextureFormat::Bgra8UnormSrgb,
color_blend: BlendDescriptor {
src_factor: BlendFactor::SrcAlpha,
dst_factor: BlendFactor::OneMinusSrcAlpha,
operation: BlendOperation::Add,
},
alpha_blend: BlendDescriptor {
src_factor: BlendFactor::One,
dst_factor: BlendFactor::One,
operation: BlendOperation::Add,
},
write_mask: ColorWrite::ALL,
}],
..PipelineDescriptor::new(ShaderStages {
vertex: shaders.add(Shader::from_glsl(
ShaderStage::Vertex,
include_str!("wireframe.vert"),
)),
fragment: Some(shaders.add(Shader::from_glsl(
ShaderStage::Fragment,
include_str!("wireframe.frag"),
))),
})
#[derive(Debug, Default)]
pub struct GlobalWireframe;

impl WorldWriter for GlobalWireframe {
fn write(self: Box<Self>, world: &mut World) {
for (entity, mut render_pipelines) in
world.query_mut::<(Entity, &mut RenderPipelines)>().iter()
{
render_pipelines.pipelines.push(RenderPipeline::specialized(
WIREFRAME_PIPELINE_HANDLE,
Default::default(),
))
}
}
}

/// Trait indicating that the component can be rendered with wireframe overlay
pub trait WithWireframe {
fn with_wireframe(self) -> Self;
}
62 changes: 62 additions & 0 deletions crates/bevy_render/src/wireframe/pipeline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::{
pipeline::{
BlendDescriptor, BlendFactor, BlendOperation, ColorStateDescriptor, ColorWrite,
CompareFunction, CullMode, DepthStencilStateDescriptor, FrontFace, PipelineDescriptor,
PolygonMode, RasterizationStateDescriptor, StencilStateDescriptor,
StencilStateFaceDescriptor,
},
prelude::*,
shader::{Shader, ShaderStage, ShaderStages},
texture::TextureFormat,
};
use bevy_app::prelude::*;
use bevy_asset::{Assets, Handle};

pub(crate) fn build_wireframe_pipeline(shaders: &mut Assets<Shader>) -> PipelineDescriptor {
PipelineDescriptor {
rasterization_state: Some(RasterizationStateDescriptor {
front_face: FrontFace::Ccw,
cull_mode: CullMode::Back,
polygon_mode: PolygonMode::Line,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
clamp_depth: false,
}),
depth_stencil_state: Some(DepthStencilStateDescriptor {
format: TextureFormat::Depth32Float,
depth_write_enabled: true,
depth_compare: CompareFunction::Less,
stencil: StencilStateDescriptor {
front: StencilStateFaceDescriptor::IGNORE,
back: StencilStateFaceDescriptor::IGNORE,
read_mask: 0,
write_mask: 0,
},
}),
color_states: vec![ColorStateDescriptor {
format: TextureFormat::Bgra8UnormSrgb,
color_blend: BlendDescriptor {
src_factor: BlendFactor::SrcAlpha,
dst_factor: BlendFactor::OneMinusSrcAlpha,
operation: BlendOperation::Add,
},
alpha_blend: BlendDescriptor {
src_factor: BlendFactor::One,
dst_factor: BlendFactor::One,
operation: BlendOperation::Add,
},
write_mask: ColorWrite::ALL,
}],
..PipelineDescriptor::new(ShaderStages {
vertex: shaders.add(Shader::from_glsl(
ShaderStage::Vertex,
include_str!("wireframe.vert"),
)),
fragment: Some(shaders.add(Shader::from_glsl(
ShaderStage::Fragment,
include_str!("wireframe.frag"),
))),
})
}
}
1 change: 0 additions & 1 deletion crates/bevy_wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ impl Plugin for WgpuPlugin {
}
}
pub fn wgpu_render_system(resources: &mut Resources) -> impl FnMut(&mut World, &mut Resources) {

let mut wgpu_features = wgpu::Features::empty();
if let Some(device_features_res) = resources.get::<WgpuDeviceFeatures>() {
wgpu_features = device_features_res.features.iter().fold(
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_wgpu/src/wgpu_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct WgpuRenderer {
}

impl WgpuRenderer {
pub async fn new(features : wgpu::Features) -> Self {
pub async fn new(features: wgpu::Features) -> Self {
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
Expand All @@ -35,7 +35,7 @@ impl WgpuRenderer {
let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
features: features,
features,
limits: wgpu::Limits::default(),
shader_validation: true,
},
Expand Down
27 changes: 16 additions & 11 deletions examples/3d/3d_scene.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use bevy::prelude::*;

use bevy_render::wireframe::{GlobalWireframe, WithWireframe};
fn main() {
App::build()
.add_resource(Msaa { samples: 4 })
.add_resource(bevy_wgpu::WgpuDeviceFeatures {
features: vec![bevy_wgpu::DeviceFeatures::NonFillPolygonMode]
features: vec![bevy_wgpu::DeviceFeatures::NonFillPolygonMode],
})
.add_default_plugins()
.add_plugin(bevy_render::wireframe::WireframePlugin::default())
Expand Down Expand Up @@ -34,20 +34,25 @@ fn setup(
..Default::default()
})
// sphere
.spawn((PbrComponents {
mesh: meshes.add(Mesh::from(shape::Icosphere {
subdivisions: 4,
radius: 0.5,
})),
material: materials.add(Color::rgb(0.1, 0.4, 0.8).into()),
transform: Transform::from_translation(Vec3::new(1.5, 1.5, 1.5)),
..Default::default()
}).with_wireframe())
.spawn(
(PbrComponents {
mesh: meshes.add(Mesh::from(shape::Icosphere {
subdivisions: 4,
radius: 0.5,
})),
material: materials.add(Color::rgb(0.1, 0.4, 0.8).into()),
transform: Transform::from_translation(Vec3::new(1.5, 1.5, 1.5)),
..Default::default()
})
.with_wireframe(),
)
// light
.spawn(LightComponents {
transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)),
..Default::default()
})
// Uncomment to enable wireframe display globally
// .write_world(GlobalWireframe::default())
// camera
.spawn(Camera3dComponents {
transform: Transform::new(Mat4::face_toward(
Expand Down

0 comments on commit 334ecd2

Please sign in to comment.