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
use std::error::Error;
use std::rc::Rc;
use three_d::{GeometryMut, ThreeDResult};
use traffloat_def::node::NodeId;
use traffloat_def::{atlas, building};
use traffloat_types::geometry;
use traffloat_types::space::{Matrix, Position};
use crate::texture::MaybeTexture;
use crate::{mat, texture, Server, StdMeshes};
pub struct View {
pub id: NodeId,
pub position: Position,
pub shapes: Vec<building::Shape>,
pub color: [f32; 3],
}
struct PreparedInner {
models: Vec<Model>,
loading: Vec<MaybeTexture>,
}
pub struct Prepared {
pub view: View,
inner: PreparedInner,
}
struct NewModelContext<'t, S: Server> {
texture_pool: &'t texture::Pool,
gl: &'t three_d::Context,
server: &'t S,
meshes: &'t StdMeshes,
}
impl<'t, S: Server> Clone for NewModelContext<'t, S> {
fn clone(&self) -> Self {
Self {
texture_pool: self.texture_pool,
gl: self.gl,
server: self.server,
meshes: self.meshes,
}
}
}
impl<'t, S: Server> Copy for NewModelContext<'t, S> {}
pub type Model = three_d::Model<three_d::PhysicalMaterial>;
fn new_model(
NewModelContext { texture_pool, gl, server, meshes: _ }: NewModelContext<'_, impl Server>,
loading: &mut Vec<MaybeTexture>,
shape: &building::Shape,
mesh: &three_d::CPUMesh,
tf: Matrix,
) -> ThreeDResult<Model> {
let path = atlas::to_path("fancy", shape.texture().spritesheet_id());
let texture = texture_pool.request(gl, server, &path);
if texture.borrow().is_none() {
loading.push(Rc::clone(&texture));
}
let texture = {
let texture = texture.borrow();
match &*texture {
Some(Ok(texture)) => Some(texture.clone()),
_ => None,
}
};
let material = three_d::PhysicalMaterial {
albedo: three_d::Color::new(200, 200, 200, 255),
albedo_texture: texture,
metallic: 0.3,
roughness: 0.6,
..Default::default()
};
let mut model = Model::new_with_material(gl, mesh, material)?;
model.set_transformation(mat(tf));
Ok(model)
}
fn shape_to_models(
ctx: NewModelContext<'_, impl Server>,
shape: &building::Shape,
tf_base: Matrix,
models: &mut Vec<Model>,
loading: &mut Vec<MaybeTexture>,
) -> ThreeDResult<()> {
let tf = tf_base * shape.transform();
match shape.unit() {
geometry::Unit::Cube => {
models.push(new_model(ctx, loading, shape, &ctx.meshes.cube, tf)?);
}
geometry::Unit::Cylinder => {
models.push(new_model(ctx, loading, shape, &ctx.meshes.fused_cylinder, tf)?);
}
geometry::Unit::Sphere => {
models.push(new_model(ctx, loading, shape, &ctx.meshes.sphere, tf)?);
}
}
Ok(())
}
fn shapes_to_inner(
ctx: NewModelContext<'_, impl Server>,
view: &View,
) -> ThreeDResult<PreparedInner> {
let tf_base = Matrix::new_translation(&view.position.vector());
let mut models = Vec::new();
let mut loading = Vec::new();
for shape in &view.shapes {
shape_to_models(ctx, shape, tf_base, &mut models, &mut loading)?;
}
Ok(PreparedInner { models, loading })
}
impl Prepared {
pub fn new(
view: View,
meshes: &StdMeshes,
texture_pool: &texture::Pool,
gl: &three_d::Context,
server: &impl Server,
) -> Result<Self, Box<dyn Error>> {
let inner = shapes_to_inner(NewModelContext { texture_pool, gl, server, meshes }, &view)?;
Ok(Self { view, inner })
}
pub fn check_loading(
&mut self,
meshes: &StdMeshes,
texture_pool: &texture::Pool,
gl: &three_d::Context,
server: &impl Server,
) -> Result<(), Box<dyn Error>> {
for maybe in &mut self.inner.loading {
if maybe.borrow().is_some() {
self.inner = shapes_to_inner(
NewModelContext { texture_pool, gl, server, meshes },
&self.view,
)?;
break;
}
}
Ok(())
}
pub fn models(&self) -> &[Model] { &self.inner.models }
pub fn set_picked(&mut self, b: bool) {
for model in &mut self.inner.models {
model.material.albedo = if b {
three_d::Color::new(255, 255, 255, 255)
} else {
three_d::Color::new(200, 200, 200, 255)
};
}
}
}