Skip to content

Commit

Permalink
wip 3d
Browse files Browse the repository at this point in the history
  • Loading branch information
IceSentry committed Feb 24, 2023
1 parent 95ebbde commit 4b2b826
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 43 deletions.
2 changes: 2 additions & 0 deletions crates/bevy_gizmos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ bevy_utils = { path = "../bevy_utils", version = "0.9.0" }
bevy_core = { path = "../bevy_core", version = "0.9.0" }
bevy_reflect = { path = "../bevy_reflect", version = "0.9.0" }
bevy_core_pipeline = { path = "../bevy_core_pipeline", version = "0.9.0" }

radsort = "0.1"
65 changes: 57 additions & 8 deletions crates/bevy_gizmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::mem;

use bevy_app::{CoreSet, Plugin};
use bevy_asset::{load_internal_asset, Assets, Handle, HandleUntyped};
use bevy_core_pipeline::{core_2d, core_3d};
use bevy_ecs::{
prelude::{Component, DetectChanges},
schedule::IntoSystemConfig,
Expand All @@ -12,7 +13,8 @@ use bevy_math::Mat4;
use bevy_reflect::TypeUuid;
use bevy_render::{
mesh::Mesh,
render_phase::AddRenderCommand,
render_graph::RenderGraph,
render_phase::{sort_phase_system, AddRenderCommand, DrawFunctions},
render_resource::{PrimitiveTopology, Shader, SpecializedMeshPipelines},
Extract, ExtractSchedule, RenderApp, RenderSet,
};
Expand All @@ -24,12 +26,14 @@ use bevy_sprite::{Mesh2dHandle, Mesh2dUniform};

pub mod gizmos;

mod node;

#[cfg(feature = "bevy_sprite")]
mod pipeline_2d;
#[cfg(feature = "bevy_pbr")]
mod pipeline_3d;

use crate::gizmos::GizmoStorage;
use crate::{gizmos::GizmoStorage, node::GizmoNode};

/// The `bevy_gizmos` prelude.
pub mod prelude {
Expand Down Expand Up @@ -62,22 +66,67 @@ impl Plugin for GizmoPlugin {

render_app
.add_render_command::<Transparent2d, DrawGizmoLines>()
.init_resource::<GizmoLinePipeline>()
.init_resource::<SpecializedMeshPipelines<GizmoLinePipeline>>()
.init_resource::<GizmoPipeline2d>()
.init_resource::<SpecializedMeshPipelines<GizmoPipeline2d>>()
.add_system(queue_gizmos_2d.in_set(RenderSet::Queue));
}

#[cfg(feature = "bevy_pbr")]
{
use bevy_core_pipeline::core_3d::Opaque3d;
use pipeline_3d::*;

render_app
.add_render_command::<Opaque3d, DrawGizmoLines>()
.init_resource::<GizmoPipeline>()
.init_resource::<SpecializedMeshPipelines<GizmoPipeline>>()
.init_resource::<GizmoPipeline3d>()
.init_resource::<SpecializedMeshPipelines<GizmoPipeline3d>>()
.init_resource::<DrawFunctions<GizmoLine3d>>()
.add_render_command::<GizmoLine3d, DrawGizmoLines>()
.add_system(sort_phase_system::<GizmoLine3d>)
.add_system_to_schedule(ExtractSchedule, extract_gizmo_line_3d_camera_phase)
.add_system(queue_gizmos_3d.in_set(RenderSet::Queue));
}

#[cfg(feature = "bevy_sprite")]
{
let gizmo_node = GizmoNode::new(&mut render_app.world);
let mut binding = render_app.world.resource_mut::<RenderGraph>();
let graph = binding.get_sub_graph_mut(core_2d::graph::NAME).unwrap();

let node_name = "gizmo_node";

graph.add_node(node_name, gizmo_node);
graph.add_slot_edge(
graph.input_node().id,
core_2d::graph::input::VIEW_ENTITY,
node_name,
GizmoNode::IN_VIEW,
);
graph.add_node_edge(
core_2d::graph::node::END_MAIN_PASS_POST_PROCESSING,
node_name,
);
}

#[cfg(feature = "bevy_pbr")]
{
let gizmo_node = GizmoNode::new(&mut render_app.world);
let mut binding = render_app.world.resource_mut::<RenderGraph>();
let graph = binding.get_sub_graph_mut(core_3d::graph::NAME).unwrap();

let node_name = "gizmo_node";

graph.add_node(node_name, gizmo_node);
graph.add_slot_edge(
graph.input_node().id,
core_3d::graph::input::VIEW_ENTITY,
node_name,
GizmoNode::IN_VIEW,
);
graph.add_node_edge(
core_3d::graph::node::END_MAIN_PASS_POST_PROCESSING,
node_name,
);
graph.add_node_edge(node_name, core_3d::graph::node::UPSCALING);
}
}
}

Expand Down
82 changes: 82 additions & 0 deletions crates/bevy_gizmos/src/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use bevy_core_pipeline::core_3d::Camera3dDepthLoadOp;
use bevy_ecs::prelude::*;
use bevy_render::{
render_graph::{Node, NodeRunError, RenderGraphContext, SlotInfo, SlotType},
render_phase::RenderPhase,
render_resource::{LoadOp, Operations, RenderPassDepthStencilAttachment, RenderPassDescriptor},
renderer::RenderContext,
view::{ExtractedView, ViewDepthTexture, ViewTarget},
};

use crate::pipeline_3d::GizmoLine3d;

pub struct GizmoNode {
view_query: QueryState<
(
&'static ViewTarget,
&'static RenderPhase<GizmoLine3d>,
&'static ViewDepthTexture,
),
With<ExtractedView>,
>,
}

impl GizmoNode {
pub const IN_VIEW: &'static str = "view";

pub fn new(world: &mut World) -> Self {
Self {
view_query: QueryState::new(world),
}
}
}

impl Node for GizmoNode {
fn input(&self) -> Vec<SlotInfo> {
vec![SlotInfo::new(Self::IN_VIEW, SlotType::Entity)]
}

fn update(&mut self, world: &mut World) {
self.view_query.update_archetypes(world);
}

fn run(
&self,
graph: &mut RenderGraphContext,
render_context: &mut RenderContext,
world: &World,
) -> Result<(), NodeRunError> {
let view_entity = graph.get_input_entity(Self::IN_VIEW)?;
let Ok((
view_target,
gizmo_phase,
depth,
)) = self.view_query.get_manual(world, view_entity) else {
return Ok(());
};

{
#[cfg(feature = "trace")]
let _main_opaque_pass_3d_span = info_span!("gizmo_line_3d_pass").entered();

let mut render_pass = render_context.begin_tracked_render_pass(RenderPassDescriptor {
label: Some("gizmo_line_3d"),
color_attachments: &[Some(view_target.get_color_attachment(Operations {
load: LoadOp::Load,
store: true,
}))],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &depth.view,
depth_ops: Some(Operations {
load: Camera3dDepthLoadOp::Load.into(),
store: false,
}),
stencil_ops: None,
}),
});

gizmo_phase.render(&mut render_pass, world, view_entity);
}
Ok(())
}
}
12 changes: 6 additions & 6 deletions crates/bevy_gizmos/src/pipeline_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ use bevy_utils::FloatOrd;
use crate::{GizmoMesh, LINE_SHADER_HANDLE};

#[derive(Resource)]
pub(crate) struct GizmoLinePipeline {
pub(crate) struct GizmoPipeline2d {
mesh_pipeline: Mesh2dPipeline,
shader: Handle<Shader>,
}

impl FromWorld for GizmoLinePipeline {
impl FromWorld for GizmoPipeline2d {
fn from_world(render_world: &mut World) -> Self {
GizmoLinePipeline {
GizmoPipeline2d {
mesh_pipeline: render_world.resource::<Mesh2dPipeline>().clone(),
shader: LINE_SHADER_HANDLE.typed(),
}
}
}

impl SpecializedMeshPipeline for GizmoLinePipeline {
impl SpecializedMeshPipeline for GizmoPipeline2d {
type Key = Mesh2dPipelineKey;

fn specialize(
Expand Down Expand Up @@ -95,9 +95,9 @@ pub(crate) type DrawGizmoLines = (
#[allow(clippy::too_many_arguments)]
pub(crate) fn queue_gizmos_2d(
draw_functions: Res<DrawFunctions<Transparent2d>>,
pipeline: Res<GizmoLinePipeline>,
pipeline: Res<GizmoPipeline2d>,
pipeline_cache: Res<PipelineCache>,
mut specialized_pipelines: ResMut<SpecializedMeshPipelines<GizmoLinePipeline>>,
mut specialized_pipelines: ResMut<SpecializedMeshPipelines<GizmoPipeline2d>>,
gpu_meshes: Res<RenderAssets<Mesh>>,
msaa: Res<Msaa>,
mesh_handles: Query<(Entity, &Mesh2dHandle), With<GizmoMesh>>,
Expand Down
Loading

0 comments on commit 4b2b826

Please sign in to comment.