-
Notifications
You must be signed in to change notification settings - Fork 249
/
paint_callback.rs
238 lines (215 loc) · 7.7 KB
/
paint_callback.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use bevy::{
asset::{embedded_asset, AssetPath},
prelude::*,
render::{
mesh::PrimitiveTopology,
render_resource::{
BlendState, CachedRenderPipelineId, ColorTargetState, ColorWrites, FragmentState,
MultisampleState, PipelineCache, PolygonMode, PrimitiveState, RenderPipelineDescriptor,
SpecializedRenderPipeline, SpecializedRenderPipelines,
},
RenderApp,
},
};
use bevy_egui::{
egui_node::{EguiBevyPaintCallback, EguiBevyPaintCallbackImpl, EguiPipelineKey},
EguiContexts, EguiPlugin, EguiRenderToTextureHandle,
};
use std::path::Path;
use wgpu_types::{Extent3d, TextureUsages};
fn main() {
App::new()
.add_plugins((DefaultPlugins, EguiPlugin, CustomPipelinePlugin))
.add_systems(Startup, setup_worldspace)
.add_systems(
Update,
(ui_example_system, ui_render_to_texture_example_system),
)
.run();
}
struct CustomPipelinePlugin;
impl Plugin for CustomPipelinePlugin {
fn build(&self, app: &mut App) {
embedded_asset!(app, "examples/", "paint_callback.wgsl");
app.get_sub_app_mut(RenderApp)
.unwrap()
.insert_resource(SpecializedRenderPipelines::<CustomPipeline>::default())
.init_resource::<CustomPipeline>();
}
}
struct CustomPaintCallback;
#[derive(Component)]
struct CustomPaintPipelineIdComp {
pipeline_id: CachedRenderPipelineId,
}
impl EguiBevyPaintCallbackImpl for CustomPaintCallback {
fn update(
&self,
_info: egui::PaintCallbackInfo,
window_entity: Entity,
key: EguiPipelineKey,
world: &mut World,
) {
let pipeline_id =
world.resource_scope(
|world,
mut specialized_custom_pipelines: Mut<
SpecializedRenderPipelines<CustomPipeline>,
>| {
let specialized_pipeline = world.get_resource().unwrap();
let pipeline_cache = world.get_resource().unwrap();
let pipeline_id = specialized_custom_pipelines.specialize(
pipeline_cache,
specialized_pipeline,
key,
);
world
.entity_mut(window_entity)
.insert(CustomPaintPipelineIdComp { pipeline_id });
pipeline_id
},
);
let mut pipeline_cache = world.get_resource_mut::<PipelineCache>().unwrap();
pipeline_cache.block_on_render_pipeline(pipeline_id);
}
fn render<'pass>(
&self,
_info: egui::PaintCallbackInfo,
render_pass: &mut bevy::render::render_phase::TrackedRenderPass<'pass>,
window_entity: Entity,
_key: EguiPipelineKey,
world: &'pass World,
) {
let Some(pipeline) = world
.get_entity(window_entity)
.and_then(|entity| entity.get::<CustomPaintPipelineIdComp>())
.and_then(|comp| {
world
.get_resource::<PipelineCache>()
.and_then(|cache| cache.get_render_pipeline(comp.pipeline_id))
})
else {
return;
};
render_pass.set_render_pipeline(pipeline);
render_pass.draw(0..3, 0..1);
}
}
#[derive(Debug, Resource)]
struct CustomPipeline {
shader: Handle<Shader>,
}
impl FromWorld for CustomPipeline {
fn from_world(world: &mut World) -> Self {
let shader = world.resource::<AssetServer>().load(
AssetPath::from_path(Path::new("paint_callback/paint_callback.wgsl"))
.with_source("embedded"),
);
Self { shader }
}
}
impl SpecializedRenderPipeline for CustomPipeline {
type Key = EguiPipelineKey;
fn specialize(&self, key: Self::Key) -> RenderPipelineDescriptor {
RenderPipelineDescriptor {
label: Some("custom pipeline".into()),
layout: vec![],
push_constant_ranges: Vec::new(),
vertex: bevy::render::render_resource::VertexState {
shader: self.shader.clone(),
shader_defs: vec![],
entry_point: "vertex".into(),
buffers: vec![],
},
primitive: PrimitiveState {
topology: PrimitiveTopology::TriangleStrip,
strip_index_format: None,
front_face: bevy::render::render_resource::FrontFace::Ccw,
cull_mode: None,
unclipped_depth: false,
polygon_mode: PolygonMode::Fill,
conservative: false,
},
depth_stencil: None,
multisample: MultisampleState::default(),
fragment: Some(FragmentState {
shader: self.shader.clone(),
shader_defs: vec![],
entry_point: "fragment".into(),
targets: vec![Some(ColorTargetState {
format: key.texture_format,
blend: Some(BlendState::ALPHA_BLENDING),
write_mask: ColorWrites::ALL,
})],
}),
}
}
}
fn ui_example_system(mut ctx: EguiContexts) {
for id in 0..4 {
egui::Window::new(id.to_string()).show(ctx.ctx_mut(), |ui| {
let (resp, painter) =
ui.allocate_painter(egui::Vec2 { x: 200., y: 200. }, egui::Sense::hover());
painter.add(EguiBevyPaintCallback::new_paint_callback(
resp.rect,
CustomPaintCallback,
));
});
}
}
// The following systems are used to render UI in world space to demonstrate that paint callbacks
// work for them as well (they aren't needed to set up pain callbacks for regular screen-space UI,
// so feel free to skip them):
fn setup_worldspace(
mut images: ResMut<Assets<Image>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut commands: Commands,
) {
let output_texture = images.add({
let size = Extent3d {
width: 256,
height: 256,
depth_or_array_layers: 1,
};
let mut output_texture = Image {
// You should use `0` so that the pixels are transparent.
data: vec![0; (size.width * size.height * 4) as usize],
..default()
};
output_texture.texture_descriptor.usage |= TextureUsages::RENDER_ATTACHMENT;
output_texture.texture_descriptor.size = size;
output_texture
});
commands.spawn(PbrBundle {
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0).mesh()),
material: materials.add(StandardMaterial {
base_color: Color::WHITE,
base_color_texture: Some(Handle::clone(&output_texture)),
alpha_mode: AlphaMode::Blend,
// Remove this if you want it to use the world's lighting.
unlit: true,
..default()
}),
..default()
});
commands.spawn(EguiRenderToTextureHandle(output_texture));
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(1.5, 1.5, 1.5).looking_at(Vec3::new(0., 0., 0.), Vec3::Y),
..default()
});
}
fn ui_render_to_texture_example_system(
mut contexts: Query<&mut bevy_egui::EguiContext, With<EguiRenderToTextureHandle>>,
) {
for mut ctx in contexts.iter_mut() {
egui::Window::new("Worldspace UI").show(ctx.get_mut(), |ui| {
let (resp, painter) =
ui.allocate_painter(egui::Vec2 { x: 200., y: 200. }, egui::Sense::hover());
painter.add(EguiBevyPaintCallback::new_paint_callback(
resp.rect,
CustomPaintCallback,
));
});
}
}