-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathing.rs
More file actions
150 lines (135 loc) · 4.25 KB
/
Copy pathpathing.rs
File metadata and controls
150 lines (135 loc) · 4.25 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
use audionimbus::{
BakedDataIdentifier, BakedDataVariation, Context, DefaultRayTracer, PathBakeParams, PathBaker,
Point, ProbeBatch, Scene, Sphere, StaticMesh, StaticMeshSettings, Triangle,
};
use crate::core::mapping::{EAR_HEIGHT, FLOOR_HEIGHT};
use crate::core::scene2d::{MaterialKind, Wall};
use crate::sim::builder::{SceneGeometry, build_geometry};
pub const PATHING_IDENTIFIER: BakedDataIdentifier = BakedDataIdentifier::Pathing {
variation: BakedDataVariation::Dynamic,
};
const PROBE_SPACING: f32 = 1.5;
const PROBE_MARGIN: f32 = 1.0;
const PROBE_RADIUS: f32 = 1.0;
const DEFAULT_HALF_EXTENT: f32 = 5.0;
const BAKE_NUM_SAMPLES: u32 = 8;
const BAKE_RADIUS: f32 = 0.5;
const BAKE_THRESHOLD: f32 = 0.1;
const BAKE_VISIBILITY_RANGE: f32 = 20.0;
const BAKE_PATH_RANGE: f32 = 40.0;
pub fn bake_probe_batch(context: &Context, walls: &[Wall]) -> Option<ProbeBatch> {
let scene = probe_scene(context, walls)?;
let mut probe_batch = ProbeBatch::try_new(context).ok()?;
for center in probe_grid(walls) {
probe_batch.add_probe(Sphere {
center,
radius: PROBE_RADIUS,
});
}
probe_batch.commit();
if probe_batch.committed_num_probes() == 0 {
return None;
}
let baker = PathBaker::<DefaultRayTracer>::new();
baker
.bake(
context,
&mut probe_batch,
&scene,
PathBakeParams {
identifier: PATHING_IDENTIFIER,
num_samples: BAKE_NUM_SAMPLES,
radius: BAKE_RADIUS,
threshold: BAKE_THRESHOLD,
visibility_range: BAKE_VISIBILITY_RANGE,
path_range: BAKE_PATH_RANGE,
num_threads: 1,
},
)
.ok()?;
Some(probe_batch)
}
fn probe_grid(walls: &[Wall]) -> Vec<Point> {
let (min_x, max_x, min_z, max_z) = bounds(walls);
let mut probes = Vec::new();
let mut x = min_x;
while x <= max_x {
let mut z = min_z;
while z <= max_z {
probes.push(Point::new(x, EAR_HEIGHT, z));
z += PROBE_SPACING;
}
x += PROBE_SPACING;
}
probes
}
fn probe_scene(context: &Context, walls: &[Wall]) -> Option<Scene<DefaultRayTracer>> {
let mut geometry = build_geometry(walls, false);
append_upward_floor(&mut geometry, walls);
let mut scene: Scene<DefaultRayTracer> = Scene::try_new(context).ok()?;
let mesh = StaticMesh::try_new(
&scene,
&StaticMeshSettings {
vertices: &geometry.vertices,
triangles: &geometry.triangles,
material_indices: &geometry.material_indices,
materials: &geometry.materials,
},
)
.ok()?;
scene.add_static_mesh(mesh);
scene.commit();
Some(scene)
}
fn append_upward_floor(geometry: &mut SceneGeometry, walls: &[Wall]) {
let (min_x, max_x, min_z, max_z) = bounds(walls);
let base = geometry.vertices.len() as i32;
geometry
.vertices
.push(Point::new(min_x, FLOOR_HEIGHT, min_z));
geometry
.vertices
.push(Point::new(max_x, FLOOR_HEIGHT, min_z));
geometry
.vertices
.push(Point::new(max_x, FLOOR_HEIGHT, max_z));
geometry
.vertices
.push(Point::new(min_x, FLOOR_HEIGHT, max_z));
geometry
.triangles
.push(Triangle::new(base, base + 2, base + 1));
geometry
.triangles
.push(Triangle::new(base, base + 3, base + 2));
let material = MaterialKind::Concrete.index();
geometry.material_indices.extend([material; 2]);
}
fn bounds(walls: &[Wall]) -> (f32, f32, f32, f32) {
if walls.is_empty() {
return (
-DEFAULT_HALF_EXTENT,
DEFAULT_HALF_EXTENT,
-DEFAULT_HALF_EXTENT,
DEFAULT_HALF_EXTENT,
);
}
let mut min_x = f32::MAX;
let mut max_x = f32::MIN;
let mut min_z = f32::MAX;
let mut max_z = f32::MIN;
for wall in walls {
for point in [wall.a, wall.b] {
min_x = min_x.min(point.x);
max_x = max_x.max(point.x);
min_z = min_z.min(point.y);
max_z = max_z.max(point.y);
}
}
(
min_x - PROBE_MARGIN,
max_x + PROBE_MARGIN,
min_z - PROBE_MARGIN,
max_z + PROBE_MARGIN,
)
}