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
use bevy::{
camera::visibility::RenderLayers,
prelude::*,
render::{
Extract, camera::ExtractedCamera, sync_world::TemporaryRenderEntity, view::ExtractedView,
},
};
use kurbo::Affine;
use super::{VelloSvgAnchor, asset::VelloSvg};
use crate::{
prelude::*,
render::{VelloEntityCountData, prepare::PreparedAffine},
};
#[derive(Component, Clone)]
pub struct ExtractedVelloSvg2d {
pub asset: VelloSvg,
pub asset_anchor: VelloSvgAnchor,
pub transform: GlobalTransform,
pub alpha: f32,
}
#[derive(Component, Clone)]
pub struct ExtractedUiVelloSvg {
pub asset: VelloSvg,
pub ui_transform: UiGlobalTransform,
pub alpha: f32,
pub ui_node: ComputedNode,
}
pub fn extract_world_svg_assets(
mut commands: Commands,
query_views: Query<
(&ExtractedCamera, Option<&RenderLayers>),
(With<Camera2d>, With<VelloView>),
>,
query_vectors: Extract<
Query<
(
&VelloSvg2d,
&VelloSvgAnchor,
&GlobalTransform,
Option<&RenderLayers>,
&ViewVisibility,
&InheritedVisibility,
),
Without<Node>,
>,
>,
assets: Extract<Res<Assets<VelloSvg>>>,
mut frame_data: ResMut<VelloEntityCountData>,
) {
let mut n_svgs = 0;
// Sort cameras by rendering order
let mut views: Vec<_> = query_views.iter().collect();
views.sort_unstable_by_key(|(camera, _)| camera.order);
for (
asset_handle,
asset_anchor,
transform,
render_layers,
view_visibility,
inherited_visibility,
) in query_vectors.iter()
{
// Skip if visibility conditions are not met
if !view_visibility.get() || !inherited_visibility.get() {
continue;
}
// Skip if asset isn't loaded.
let Some(asset) = assets.get(asset_handle.id()) else {
continue;
};
// Check if any camera renders this asset
let asset_render_layers = render_layers.unwrap_or_default();
if views.iter().any(|(_, camera_layers)| {
asset_render_layers.intersects(camera_layers.unwrap_or_default())
}) {
commands
.spawn(ExtractedVelloSvg2d {
asset: asset.to_owned(),
transform: *transform,
asset_anchor: *asset_anchor,
alpha: asset.alpha,
})
.insert(TemporaryRenderEntity);
n_svgs += 1;
}
}
frame_data.n_world_svgs = n_svgs;
}
pub fn extract_ui_svg_assets(
mut commands: Commands,
query_views: Query<
(&ExtractedCamera, Option<&RenderLayers>),
(With<Camera2d>, With<VelloView>),
>,
query_vectors: Extract<
Query<(
&UiVelloSvg,
&UiGlobalTransform,
&ComputedNode,
Option<&RenderLayers>,
&InheritedVisibility,
)>,
>,
assets: Extract<Res<Assets<VelloSvg>>>,
mut frame_data: ResMut<VelloEntityCountData>,
) {
let mut n_svgs = 0;
// Sort cameras by rendering order
let mut views: Vec<_> = query_views.iter().collect();
views.sort_unstable_by_key(|(camera, _)| camera.order);
for (asset_handle, ui_transform, ui_node, render_layers, inherited_visibility) in
query_vectors.iter()
{
// Skip if visibility conditions are not met.
// UI does not check view visibility, only inherited visibility.
if !inherited_visibility.get() {
continue;
}
// Skip if asset isn't loaded.
let Some(asset) = assets.get(asset_handle.id()) else {
continue;
};
// Check if any camera renders this asset
let asset_render_layers = render_layers.unwrap_or_default();
if views.iter().any(|(_, camera_layers)| {
asset_render_layers.intersects(camera_layers.unwrap_or_default())
}) {
commands
.spawn(ExtractedUiVelloSvg {
asset: asset.to_owned(),
ui_transform: *ui_transform,
ui_node: *ui_node,
alpha: asset.alpha,
})
.insert(TemporaryRenderEntity);
n_svgs += 1;
}
}
frame_data.n_ui_svgs = n_svgs;
}
pub fn prepare_asset_affines(
mut commands: Commands,
views: Query<(&ExtractedCamera, &ExtractedView), (With<Camera2d>, With<VelloView>)>,
render_entities: Query<(Entity, &ExtractedVelloSvg2d)>,
render_ui_entities: Query<(Entity, &ExtractedUiVelloSvg)>,
) {
for (camera, view) in views.iter() {
// Render UI
for (entity, render_entity) in render_ui_entities.iter() {
let ui_transform = render_entity.ui_transform;
// A transposed (flipped over its diagonal) PostScript matrix
// | a c e |
// | b d f |
// | 0 0 1 |
//
// Components
// | scale_x skew_x translate_x |
// | skew_y scale_y translate_y |
// | skew_z skew_z scale_z |
//
// rotate (z)
// | cos(θ) -sin(θ) translate_x |
// | sin(θ) cos(θ) translate_y |
// | skew_z skew_z scale_z |
//
// The order of operations is important, as it affects the final transformation matrix.
//
// Order of operations:
// 1. Scale
// 2. Rotate
// 3. Translate
let transform: [f64; 6] = {
// Convert UiGlobalTransform to Mat4
let mat2 = ui_transform.matrix2;
let translation = ui_transform.translation;
let model_matrix = Mat4::from_cols_array_2d(&[
[mat2.x_axis.x, mat2.x_axis.y, 0.0, 0.0],
[mat2.y_axis.x, mat2.y_axis.y, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[translation.x, translation.y, 0.0, 1.0],
]);
let local_center_matrix = Transform::from_translation(Vec3 {
x: render_entity.asset.width / 2.0,
y: render_entity.asset.height / 2.0,
z: 0.0,
})
.to_matrix()
.inverse();
// Fill the bevy_ui Node with the asset size
let aspect_fill_matrix = {
let asset_size =
Vec2::new(render_entity.asset.width, render_entity.asset.height);
let fill_scale = render_entity.ui_node.size() / asset_size;
let scale_factor = fill_scale.x.min(fill_scale.y); // Maintain aspect ratio
Mat4::from_scale(Vec3::new(scale_factor, scale_factor, 1.0))
};
// Transform chain: ui_transform (in logical px) → aspect_fill → local_center
let raw_transform = model_matrix * aspect_fill_matrix * local_center_matrix;
let transform = raw_transform.to_cols_array();
[
transform[0] as f64, // a // scale_x
transform[1] as f64, // b // skew_y
transform[4] as f64, // c // skew_x
transform[5] as f64, // d // scale_y
transform[12] as f64, // e // translate_x
transform[13] as f64, // f // translate_y
]
};
commands
.entity(entity)
.insert(PreparedAffine(Affine::new(transform)));
}
// Render World
for (entity, render_entity) in render_entities.iter() {
// A transposed (flipped over its diagonal) PostScript matrix
// | a c e |
// | b d f |
// | 0 0 1 |
//
// Components
// | scale_x skew_x translate_x |
// | skew_y scale_y translate_y |
// | skew_z skew_z scale_z |
//
// rotate (z)
// | cos(θ) -sin(θ) translate_x |
// | sin(θ) cos(θ) translate_y |
// | skew_z skew_z scale_z |
//
// The order of operations is important, as it affects the final transformation matrix.
//
// Order of operations:
// 1. Scale
// 2. Rotate
// 3. Translate
let transform: [f64; 6] = {
// Get the base world transform
let world_transform = render_entity.transform.compute_transform();
let Transform {
translation,
rotation,
scale,
} = world_transform;
// Calculate anchor offset in local space (Vello's top-left origin)
let anchor_local = match render_entity.asset_anchor {
VelloSvgAnchor::TopLeft => Vec3::ZERO,
VelloSvgAnchor::Left => Vec3::new(0.0, render_entity.asset.height / 2.0, 0.0),
VelloSvgAnchor::BottomLeft => Vec3::new(0.0, render_entity.asset.height, 0.0),
VelloSvgAnchor::Top => Vec3::new(render_entity.asset.width / 2.0, 0.0, 0.0),
VelloSvgAnchor::Center => Vec3::new(
render_entity.asset.width / 2.0,
render_entity.asset.height / 2.0,
0.0,
),
VelloSvgAnchor::Bottom => Vec3::new(
render_entity.asset.width / 2.0,
render_entity.asset.height,
0.0,
),
VelloSvgAnchor::TopRight => Vec3::new(render_entity.asset.width, 0.0, 0.0),
VelloSvgAnchor::Right => Vec3::new(
render_entity.asset.width,
render_entity.asset.height / 2.0,
0.0,
),
VelloSvgAnchor::BottomRight => {
Vec3::new(render_entity.asset.width, render_entity.asset.height, 0.0)
}
};
let mut anchor_matrix = Mat4::from_translation(-anchor_local);
// The anchor offset is in Vello's y-down coordinate space, but needs to be applied
// in the transform chain that operates in Bevy's y-up space. This y-flip compensates
// for the coordinate system difference before the final model_matrix y-flip (below).
anchor_matrix.w_axis.y *= -1.0;
let ndc_to_pixels_matrix = {
let size_pixels: UVec2 = camera.physical_viewport_size.unwrap();
let (pixels_x, pixels_y) = (size_pixels.x as f32, size_pixels.y as f32);
Mat4::from_cols_array_2d(&[
[pixels_x / 2.0, 0.0, 0.0, pixels_x / 2.0],
[0.0, pixels_y / 2.0, 0.0, pixels_y / 2.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
])
.transpose()
};
let view_proj_matrix = {
let mut view_mat = view.world_from_view.to_matrix();
// Flip Y-axis to match Vello's y-down coordinate space
view_mat.w_axis.y *= -1.0;
let proj_mat = view.clip_from_view;
proj_mat * view_mat.inverse()
};
// Build the model matrix with proper anchor handling
let translation_matrix = Mat4::from_translation(translation);
let rotation_matrix = Mat4::from_quat(rotation);
let scale_matrix = Mat4::from_scale(scale);
// Build model matrix: translate → rotate → scale → world_scale → camera_scale → anchor offset
let mut model_matrix =
translation_matrix * rotation_matrix * scale_matrix * anchor_matrix;
// Flip Y-axis to match Vello's y-down coordinate space
model_matrix.w_axis.y *= -1.0;
// Transform chain: world → world_scale → camera_scale → anchor → y-flip → view → projection → NDC → pixels
let raw_transform = ndc_to_pixels_matrix * view_proj_matrix * model_matrix;
let transform = raw_transform.to_cols_array();
// Negate skew_x and skew_y to match rotation of the Bevy's y-up world
[
transform[0] as f64, // a // scale_x
-transform[1] as f64, // b // skew_y
-transform[4] as f64, // c // skew_x
transform[5] as f64, // d // scale_y
transform[12] as f64, // e // translate_x
transform[13] as f64, // f // translate_y
]
};
commands
.entity(entity)
.insert(PreparedAffine(Affine::new(transform)));
}
}
}