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
use bevy::render::mesh::{Indices, Mesh, PrimitiveTopology};
#[derive(Debug, Clone, Copy)]
pub struct SingleVariableFunctionMesh {
pub f: fn(f32) -> f32,
pub x_start: f32,
pub x_end: f32,
pub vertices_polygon_upper_half: usize,
pub vertices_height: usize,
pub relative_height: f32,
}
impl Default for SingleVariableFunctionMesh {
fn default() -> Self {
SingleVariableFunctionMesh {
f: squircle,
x_start: -1.0,
x_end: 1.0,
vertices_polygon_upper_half: 30,
vertices_height: 1,
relative_height: 0.0,
}
}
}
impl From<SingleVariableFunctionMesh> for Mesh {
fn from(mathfunction: SingleVariableFunctionMesh) -> Self {
let ring = calculate_ring_of_vertices(
mathfunction.f,
mathfunction.x_start,
mathfunction.x_end,
mathfunction.vertices_polygon_upper_half,
);
let ring2 = calculate_ring_of_vertices(
mathfunction.f,
mathfunction.x_start,
mathfunction.x_end,
mathfunction.vertices_height,
);
let amount = ring.len();
let mut vertices: Vec<([f32; 3], [f32; 3], [f32; 2])> = Vec::with_capacity(amount + 1);
let mut indeces: Vec<u32> = Vec::with_capacity(amount);
let height = mathfunction.relative_height;
vertices.push(([0.0, -height, 0.0], [1.0, 1.0, 1.0], [0.0, 0.0]));
for segment in ring2.iter().take(mathfunction.vertices_height) {
for ver in ring.iter().take(amount) {
let vorzeichen = {
if ver[1] >= 0.0 {
1.0
} else {
-1.0
}
};
let vorzeichen2 = {
if ver[0] >= 0.0 {
1.0
} else {
-1.0
}
};
let x = ver[0] + vorzeichen2 * segment[1] * mathfunction.relative_height;
let y = segment[0] / (1.0 / mathfunction.relative_height);
let z = ver[1] + vorzeichen * segment[1] * mathfunction.relative_height;
vertices.push(([x, y, z], [1.0, 1.0, 1.0], [0.0, 0.0]));
}
}
vertices.push(([0.0, height, 0.0], [1.0, 1.0, 1.0], [0.0, 0.0]));
for segment in 0..mathfunction.vertices_height {
if segment == 0 {
for i in 0..amount {
if mathfunction.vertices_height != 1 {
if i == amount - 1 {
indeces.append(&mut vec![1, (i + 1).try_into().unwrap(), 0]);
} else {
indeces.append(&mut vec![
(i + 2).try_into().unwrap(),
(i + 1).try_into().unwrap(),
0,
]);
}
}
}
} else {
for i in 0..amount {
let tl = (segment * amount + i + 1) as u32;
let mut tr = (segment * amount + i + 2) as u32;
let bl = ((segment - 1) * amount + i + 1) as u32;
let mut br = ((segment - 1) * amount + i + 2) as u32;
if i == amount - 1 {
tr = (segment * amount + 1) as u32;
br = ((segment - 1) * amount + 1) as u32;
}
indeces.append(&mut vec![br, tr, tl]);
indeces.append(&mut vec![bl, br, tl]);
}
}
if segment == mathfunction.vertices_height - 1 {
for i in 0..amount {
if i == amount - 1 {
indeces.append(&mut vec![
(segment * amount + 2).try_into().unwrap(),
(segment * amount + i + 1).try_into().unwrap(),
(segment * amount + 1).try_into().unwrap(),
]);
} else {
indeces.append(&mut vec![
(segment * amount + 2).try_into().unwrap(),
(segment * amount + i + 1).try_into().unwrap(),
(segment * amount + i + 2).try_into().unwrap(),
]);
}
}
}
}
let positions: Vec<_> = vertices.iter().map(|(p, _, _)| *p).collect();
let normals: Vec<_> = vertices.iter().map(|(_, n, _)| *n).collect();
let uvs: Vec<_> = vertices.iter().map(|(_, _, uv)| *uv).collect();
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
mesh.set_indices(Some(Indices::U32(indeces)));
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals);
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs);
mesh
}
}
fn squircle(x: f32) -> f32 {
(1.0 - (x).abs().powf(4.0)).powf(0.25)
}
#[derive(Copy, Clone, Debug)]
struct Position {
x: f32,
y: f32,
slope_in_percentage: f32,
}
fn calculate_ring_of_vertices(
f: fn(f32) -> f32,
x_start: f32,
x_end: f32,
vertices: usize,
) -> Vec<[f32; 2]> {
assert!(x_start < x_end);
let delta = 0.000001;
let start = Position {
x: x_start,
y: f(x_start),
slope_in_percentage: ((f(x_start + delta) - f(x_start)) / (delta)).atan(),
};
let end = Position {
x: x_end,
y: f(x_end),
slope_in_percentage: ((f(x_end) - f(x_end - delta)) / (delta)).atan(),
};
let mut vec: Vec<Position> = Vec::with_capacity(vertices);
vec.push(start);
vec.push(end);
for _ in 2..vertices {
let (mut index, mut max_absolute_difference) = (1, 0.0);
for j in 1..vec.len() {
let new_x = vec[j - 1].x + (vec[j].x - vec[j - 1].x) / 2.0;
let new_m = ((f(new_x + delta) - f(new_x)) / (delta)).atan();
let absolute_difference = (new_m - vec[j].slope_in_percentage).abs()
+ (new_m - vec[j - 1].slope_in_percentage).abs();
if absolute_difference >= max_absolute_difference {
index = j;
max_absolute_difference = absolute_difference;
}
}
let new_x = vec[index - 1].x + (vec[index].x - vec[index - 1].x) / 2.0;
vec.insert(
index,
Position {
x: new_x,
y: f(new_x),
slope_in_percentage: ((f(new_x + delta) - f(new_x)) / (delta)).atan(),
},
);
}
let mut return_vec: Vec<[f32; 2]> = Vec::with_capacity(vertices);
for e in &vec {
return_vec.push([e.x, e.y]);
}
vec.reverse();
vec.remove(vertices - 1);
vec.remove(0);
for e in &vec {
return_vec.push([e.x, -e.y]);
}
return_vec
}