-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathorder_independent_transparency.rs
More file actions
346 lines (324 loc) · 10.8 KB
/
order_independent_transparency.rs
File metadata and controls
346 lines (324 loc) · 10.8 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//! A simple 3D scene showing how alpha blending can break and how order independent transparency (OIT) can fix it.
//!
//! See [`OrderIndependentTransparencyPlugin`] for the trade-offs of using OIT.
//!
//! [`OrderIndependentTransparencyPlugin`]: bevy::core_pipeline::oit::OrderIndependentTransparencyPlugin
use bevy::{
camera::visibility::RenderLayers,
color::palettes::css::{BLUE, GREEN, RED, YELLOW},
core_pipeline::{oit::OrderIndependentTransparencySettings, prepass::DepthPrepass},
prelude::*,
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, (toggle_oit, cycle_scenes))
.run();
}
/// set up a simple 3D scene
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// camera
commands.spawn((
Camera3d::default(),
Transform::from_xyz(0.0, 0.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
// Add this component to this camera to render transparent meshes using OIT
OrderIndependentTransparencySettings::default(),
RenderLayers::layer(1),
// Msaa currently doesn't work with OIT
Msaa::Off,
// Optional: depth prepass can help OIT filter out fragments occluded by opaque objects
DepthPrepass,
));
// light
commands.spawn((
PointLight {
shadow_maps_enabled: false,
..default()
},
Transform::from_xyz(4.0, 8.0, 4.0),
RenderLayers::layer(1),
));
// spawn help text
commands.spawn((
Text::default(),
Node {
position_type: PositionType::Absolute,
top: px(12),
left: px(12),
..default()
},
RenderLayers::layer(1),
children![
TextSpan::new("Press T to toggle OIT\n"),
TextSpan::new("OIT Enabled"),
TextSpan::new("\nPress C to cycle test scenes"),
],
));
// spawn default scene
spawn_spheres(&mut commands, &mut meshes, &mut materials);
}
fn toggle_oit(
mut commands: Commands,
text: Single<Entity, With<Text>>,
keyboard_input: Res<ButtonInput<KeyCode>>,
q: Single<(Entity, Has<OrderIndependentTransparencySettings>), With<Camera3d>>,
mut text_writer: TextUiWriter,
) {
if keyboard_input.just_pressed(KeyCode::KeyT) {
let (e, has_oit) = *q;
*text_writer.text(*text, 2) = if has_oit {
// Removing the component will completely disable OIT for this camera
commands
.entity(e)
.remove::<OrderIndependentTransparencySettings>();
"OIT disabled".to_string()
} else {
// Adding the component to the camera will render any transparent meshes
// with OIT instead of alpha blending
commands
.entity(e)
.insert(OrderIndependentTransparencySettings::default());
"OIT enabled".to_string()
};
}
}
fn cycle_scenes(
mut commands: Commands,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
q: Query<Entity, With<Mesh3d>>,
mut scene_id: Local<usize>,
asset_server: Res<AssetServer>,
) {
if keyboard_input.just_pressed(KeyCode::KeyC) {
// despawn current scene
for e in &q {
commands.entity(e).despawn();
}
// increment scene_id
*scene_id = (*scene_id + 1) % 4;
// spawn next scene
match *scene_id {
0 => spawn_spheres(&mut commands, &mut meshes, &mut materials),
1 => spawn_quads(&mut commands, &mut meshes, &mut materials),
2 => spawn_occlusion_test(&mut commands, &mut meshes, &mut materials),
3 => {
spawn_auto_instancing_test(
&mut commands,
&mut meshes,
&mut materials,
asset_server,
);
}
_ => unreachable!(),
}
}
}
/// Spawns 3 overlapping spheres
/// Technically, when using `alpha_to_coverage` with MSAA this particular example wouldn't break,
/// but it breaks when disabling MSAA and is enough to show the difference between OIT enabled vs disabled.
fn spawn_spheres(
commands: &mut Commands,
meshes: &mut Assets<Mesh>,
materials: &mut Assets<StandardMaterial>,
) {
let pos_a = Vec3::new(-1.0, 0.75, 0.0);
let pos_b = Vec3::new(0.0, -0.75, 0.0);
let pos_c = Vec3::new(1.0, 0.75, 0.0);
let offset = Vec3::new(0.0, 0.0, 0.0);
let sphere_handle = meshes.add(Sphere::new(2.0).mesh());
let alpha = 0.25;
let render_layers = RenderLayers::layer(1);
commands.spawn((
Mesh3d(sphere_handle.clone()),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: RED.with_alpha(alpha).into(),
alpha_mode: AlphaMode::Blend,
..default()
})),
Transform::from_translation(pos_a + offset),
render_layers.clone(),
));
commands.spawn((
Mesh3d(sphere_handle.clone()),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: GREEN.with_alpha(alpha).into(),
alpha_mode: AlphaMode::Blend,
..default()
})),
Transform::from_translation(pos_b + offset),
render_layers.clone(),
));
commands.spawn((
Mesh3d(sphere_handle.clone()),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: BLUE.with_alpha(alpha).into(),
alpha_mode: AlphaMode::Blend,
..default()
})),
Transform::from_translation(pos_c + offset),
render_layers.clone(),
));
}
fn spawn_quads(
commands: &mut Commands,
meshes: &mut Assets<Mesh>,
materials: &mut Assets<StandardMaterial>,
) {
let quad_handle = meshes.add(Rectangle::new(3.0, 3.0).mesh());
let render_layers = RenderLayers::layer(1);
let xform = |x, y, z| {
Transform::from_rotation(Quat::from_rotation_y(0.5))
.mul_transform(Transform::from_xyz(x, y, z))
};
commands.spawn((
Mesh3d(quad_handle.clone()),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: RED.with_alpha(0.5).into(),
alpha_mode: AlphaMode::Blend,
..default()
})),
xform(1.0, -0.1, 0.),
render_layers.clone(),
));
commands.spawn((
Mesh3d(quad_handle.clone()),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: BLUE.with_alpha(0.8).into(),
alpha_mode: AlphaMode::Blend,
..default()
})),
xform(0.5, 0.2, -0.5),
render_layers.clone(),
));
commands.spawn((
Mesh3d(quad_handle.clone()),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: GREEN.with_green(1.0).with_alpha(0.5).into(),
alpha_mode: AlphaMode::Blend,
..default()
})),
xform(0.0, 0.4, -1.),
render_layers.clone(),
));
commands.spawn((
Mesh3d(quad_handle.clone()),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: YELLOW.with_alpha(0.3).into(),
alpha_mode: AlphaMode::Blend,
..default()
})),
xform(-0.5, 0.6, -1.1),
render_layers.clone(),
));
commands.spawn((
Mesh3d(quad_handle.clone()),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: BLUE.with_alpha(0.2).into(),
alpha_mode: AlphaMode::Blend,
..default()
})),
xform(-0.8, 0.8, -1.2),
render_layers.clone(),
));
}
/// Spawn a combination of opaque cubes and transparent spheres.
/// This is useful to make sure transparent meshes drawn with OIT
/// are properly occluded by opaque meshes.
fn spawn_occlusion_test(
commands: &mut Commands,
meshes: &mut Assets<Mesh>,
materials: &mut Assets<StandardMaterial>,
) {
let sphere_handle = meshes.add(Sphere::new(1.0).mesh());
let cube_handle = meshes.add(Cuboid::from_size(Vec3::ONE).mesh());
let cube_material = materials.add(Color::srgb(0.8, 0.7, 0.6));
let render_layers = RenderLayers::layer(1);
// front
let x = -2.5;
commands.spawn((
Mesh3d(cube_handle.clone()),
MeshMaterial3d(cube_material.clone()),
Transform::from_xyz(x, 0.0, 2.0),
render_layers.clone(),
));
commands.spawn((
Mesh3d(sphere_handle.clone()),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: RED.with_alpha(0.5).into(),
alpha_mode: AlphaMode::Blend,
..default()
})),
Transform::from_xyz(x, 0., 0.),
render_layers.clone(),
));
// intersection
commands.spawn((
Mesh3d(cube_handle.clone()),
MeshMaterial3d(cube_material.clone()),
Transform::from_xyz(x, 0.0, 1.0),
render_layers.clone(),
));
commands.spawn((
Mesh3d(sphere_handle.clone()),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: RED.with_alpha(0.5).into(),
alpha_mode: AlphaMode::Blend,
..default()
})),
Transform::from_xyz(0., 0., 0.),
render_layers.clone(),
));
// back
let x = 2.5;
commands.spawn((
Mesh3d(cube_handle.clone()),
MeshMaterial3d(cube_material.clone()),
Transform::from_xyz(x, 0.0, -2.0),
render_layers.clone(),
));
commands.spawn((
Mesh3d(sphere_handle.clone()),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: RED.with_alpha(0.5).into(),
alpha_mode: AlphaMode::Blend,
..default()
})),
Transform::from_xyz(x, 0., 0.),
render_layers.clone(),
));
}
fn spawn_auto_instancing_test(
commands: &mut Commands,
meshes: &mut Assets<Mesh>,
materials: &mut Assets<StandardMaterial>,
asset_server: Res<AssetServer>,
) {
let render_layers = RenderLayers::layer(1);
let cube = meshes.add(Cuboid::new(1.0, 1.0, 1.0));
let material_handle = materials.add(StandardMaterial {
alpha_mode: AlphaMode::Blend,
base_color_texture: Some(asset_server.load("textures/slice_square.png")),
..Default::default()
});
let mut bundles = Vec::with_capacity(3 * 3 * 3);
for z in -1..=1 {
for y in -1..=1 {
for x in -1..=1 {
bundles.push((
Mesh3d(cube.clone()),
MeshMaterial3d(material_handle.clone()),
Transform::from_xyz(x as f32 * 2.0, y as f32 * 2.0, z as f32 * 2.0),
render_layers.clone(),
));
}
}
}
commands.spawn_batch(bundles);
}