Repository navigation
Expand file tree
/
Copy pathdecal.rs
More file actions
134 lines (123 loc) · 4.5 KB
/
Copy pathdecal.rs
File metadata and controls
134 lines (123 loc) · 4.5 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
//! Decal rendering.
//! Note: On Wasm, this example only runs on WebGPU
use bevy::{
camera_controller::free_camera::{FreeCamera, FreeCameraPlugin},
core_pipeline::prepass::DepthPrepass,
pbr::decal::{ForwardDecal, ForwardDecalMaterial, ForwardDecalMaterialExt},
prelude::*,
};
use chacha20::ChaCha8Rng;
use rand::{RngExt, SeedableRng};
fn main() {
App::new()
.add_plugins((DefaultPlugins, FreeCameraPlugin))
.add_systems(Startup, setup)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut standard_materials: ResMut<Assets<StandardMaterial>>,
mut decal_standard_materials: ResMut<Assets<ForwardDecalMaterial<StandardMaterial>>>,
asset_server: Res<AssetServer>,
) {
// Spawn the forward decal
commands.spawn((
Name::new("Decal Blue"),
ForwardDecal,
MeshMaterial3d(decal_standard_materials.add(ForwardDecalMaterial {
base: StandardMaterial {
base_color_texture: Some(asset_server.load("textures/uv_checker_bw.png")),
base_color: Color::srgb(0.3, 0.5, 0.8),
alpha_mode: AlphaMode::Opaque,
depth_bias: 0.0,
..default()
},
extension: ForwardDecalMaterialExt {
depth_fade_factor: 1.0,
},
})),
Transform::from_scale(Vec3::splat(4.0)).with_translation(Vec3::new(-1.0, 0.0, -1.0)),
));
commands.spawn((
Name::new("Decal Green"),
ForwardDecal,
MeshMaterial3d(decal_standard_materials.add(ForwardDecalMaterial {
base: StandardMaterial {
base_color_texture: Some(asset_server.load("textures/uv_checker_bw.png")),
base_color: Color::srgba(0.0, 1.0, 0.0, 0.5),
alpha_mode: AlphaMode::Blend,
depth_bias: 2.0,
..default()
},
extension: ForwardDecalMaterialExt {
depth_fade_factor: 1.0,
},
})),
Transform::from_scale(Vec3::splat(4.0)).with_translation(Vec3::new(1.0, 0.0, -1.0)),
));
commands.spawn((
Name::new("Decal Red"),
ForwardDecal,
MeshMaterial3d(decal_standard_materials.add(ForwardDecalMaterial {
base: StandardMaterial {
base_color_texture: Some(asset_server.load("textures/uv_checker_bw.png")),
base_color: Color::srgba(1.0, 0.0, 0.0, 0.5),
alpha_mode: AlphaMode::Add,
depth_bias: 4.0,
..default()
},
extension: ForwardDecalMaterialExt {
depth_fade_factor: 1.0,
},
})),
Transform::from_scale(Vec3::splat(4.0)).with_translation(Vec3::new(0.0, 0.0, 1.0)),
));
commands.spawn((
Name::new("Camera"),
Camera3d::default(),
FreeCamera::default(),
// Must enable the depth prepass to render forward decals
DepthPrepass,
Transform::from_xyz(2.0, 9.5, 2.5).looking_at(Vec3::ZERO, Vec3::Y),
));
let white_material = standard_materials.add(Color::WHITE);
commands.spawn((
Name::new("Floor"),
Mesh3d(meshes.add(Rectangle::from_length(10.0))),
MeshMaterial3d(white_material.clone()),
Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
));
// Spawn a few cube with random rotations to showcase how the decals behave with non-flat geometry
let num_obs = 10;
let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
for i in 0..num_obs {
for j in 0..num_obs {
let rotation_axis: [f32; 3] = rng.random();
let rotation_vec: Vec3 = rotation_axis.into();
let rotation: u32 = rng.random_range(0..360);
let transform = Transform::from_xyz(
(-num_obs + 1) as f32 / 2.0 + i as f32,
-0.2,
(-num_obs + 1) as f32 / 2.0 + j as f32,
)
.with_rotation(Quat::from_axis_angle(
rotation_vec.normalize_or_zero(),
(rotation as f32).to_radians(),
));
commands.spawn((
Mesh3d(meshes.add(Cuboid::from_length(0.6))),
MeshMaterial3d(white_material.clone()),
transform,
));
}
}
commands.spawn((
Name::new("Light"),
PointLight {
shadow_maps_enabled: true,
..default()
},
Transform::from_xyz(4.0, 8.0, 4.0),
));
}