2020-07-10 08:37:06 +00:00
|
|
|
use crate::Node;
|
2020-05-13 23:42:27 +00:00
|
|
|
use bevy_asset::{Assets, Handle};
|
2020-07-10 08:37:06 +00:00
|
|
|
use bevy_ecs::Resources;
|
2020-05-03 19:35:07 +00:00
|
|
|
use bevy_render::{
|
2020-07-17 01:26:21 +00:00
|
|
|
camera::ActiveCameras,
|
|
|
|
pipeline::*,
|
|
|
|
render_graph::{base, CameraNode, PassNode, RenderGraph, RenderResourcesNode},
|
2020-05-03 19:35:07 +00:00
|
|
|
shader::{Shader, ShaderStage, ShaderStages},
|
2020-07-10 08:37:06 +00:00
|
|
|
texture::TextureFormat,
|
2020-05-03 19:35:07 +00:00
|
|
|
};
|
2020-04-25 00:46:54 +00:00
|
|
|
|
2020-05-03 19:35:07 +00:00
|
|
|
pub const UI_PIPELINE_HANDLE: Handle<PipelineDescriptor> =
|
2020-05-04 18:20:12 +00:00
|
|
|
Handle::from_u128(323432002226399387835192542539754486265);
|
2020-05-03 19:35:07 +00:00
|
|
|
|
2020-05-13 23:42:27 +00:00
|
|
|
pub fn build_ui_pipeline(shaders: &mut Assets<Shader>) -> PipelineDescriptor {
|
2020-05-03 19:35:07 +00:00
|
|
|
PipelineDescriptor {
|
|
|
|
rasterization_state: Some(RasterizationStateDescriptor {
|
2020-07-20 08:33:30 +00:00
|
|
|
front_face: FrontFace::Ccw,
|
|
|
|
cull_mode: CullMode::Back,
|
2020-05-03 19:35:07 +00:00
|
|
|
depth_bias: 0,
|
|
|
|
depth_bias_slope_scale: 0.0,
|
|
|
|
depth_bias_clamp: 0.0,
|
|
|
|
}),
|
|
|
|
depth_stencil_state: Some(DepthStencilStateDescriptor {
|
|
|
|
format: TextureFormat::Depth32Float,
|
2020-05-06 20:49:07 +00:00
|
|
|
depth_write_enabled: true,
|
|
|
|
depth_compare: CompareFunction::Less,
|
2020-05-03 19:35:07 +00:00
|
|
|
stencil_front: StencilStateFaceDescriptor::IGNORE,
|
|
|
|
stencil_back: StencilStateFaceDescriptor::IGNORE,
|
|
|
|
stencil_read_mask: 0,
|
|
|
|
stencil_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!("ui.vert"),
|
|
|
|
)),
|
|
|
|
fragment: Some(shaders.add(Shader::from_glsl(
|
|
|
|
ShaderStage::Fragment,
|
|
|
|
include_str!("ui.frag"),
|
|
|
|
))),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 05:30:07 +00:00
|
|
|
pub mod node {
|
|
|
|
pub const UI_CAMERA: &'static str = "ui_camera";
|
2020-06-25 17:13:00 +00:00
|
|
|
pub const NODE: &'static str = "node";
|
2020-05-30 05:30:07 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 22:58:06 +00:00
|
|
|
pub mod camera {
|
2020-05-30 05:30:07 +00:00
|
|
|
pub const UI_CAMERA: &'static str = "UiCamera";
|
|
|
|
}
|
|
|
|
|
2020-05-03 19:35:07 +00:00
|
|
|
pub trait UiRenderGraphBuilder {
|
|
|
|
fn add_ui_graph(&mut self, resources: &Resources) -> &mut Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UiRenderGraphBuilder for RenderGraph {
|
|
|
|
fn add_ui_graph(&mut self, resources: &Resources) -> &mut Self {
|
2020-05-14 01:05:18 +00:00
|
|
|
let mut pipelines = resources.get_mut::<Assets<PipelineDescriptor>>().unwrap();
|
2020-05-13 23:42:27 +00:00
|
|
|
let mut shaders = resources.get_mut::<Assets<Shader>>().unwrap();
|
2020-05-16 07:21:04 +00:00
|
|
|
pipelines.set(UI_PIPELINE_HANDLE, build_ui_pipeline(&mut shaders));
|
2020-06-23 23:52:50 +00:00
|
|
|
|
|
|
|
// setup ui camera
|
|
|
|
self.add_system_node(node::UI_CAMERA, CameraNode::new(camera::UI_CAMERA));
|
2020-07-17 01:26:21 +00:00
|
|
|
self.add_node_edge(node::UI_CAMERA, base::node::MAIN_PASS)
|
2020-06-23 23:52:50 +00:00
|
|
|
.unwrap();
|
2020-06-25 17:13:00 +00:00
|
|
|
self.add_system_node(node::NODE, RenderResourcesNode::<Node>::new(true));
|
2020-07-17 01:26:21 +00:00
|
|
|
self.add_node_edge(node::NODE, base::node::MAIN_PASS)
|
2020-06-25 17:13:00 +00:00
|
|
|
.unwrap();
|
2020-06-23 22:58:06 +00:00
|
|
|
let mut active_cameras = resources.get_mut::<ActiveCameras>().unwrap();
|
2020-07-17 01:26:21 +00:00
|
|
|
let main_pass_node: &mut PassNode = self.get_node_mut(base::node::MAIN_PASS).unwrap();
|
2020-06-23 23:52:50 +00:00
|
|
|
main_pass_node.add_camera(camera::UI_CAMERA);
|
2020-06-23 22:58:06 +00:00
|
|
|
active_cameras.add(camera::UI_CAMERA);
|
2020-05-03 19:35:07 +00:00
|
|
|
self
|
|
|
|
}
|
2020-05-04 18:20:12 +00:00
|
|
|
}
|