forked from processing/libprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
1073 lines (959 loc) · 28.9 KB
/
lib.rs
File metadata and controls
1073 lines (959 loc) · 28.9 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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pub mod config;
pub mod error;
pub mod geometry;
mod graphics;
pub mod image;
pub mod render;
mod surface;
use std::{cell::RefCell, num::NonZero, path::PathBuf, sync::OnceLock};
use config::*;
#[cfg(not(target_arch = "wasm32"))]
use bevy::log::tracing_subscriber;
use bevy::{
app::{App, AppExit},
asset::{AssetEventSystems, io::AssetSourceBuilder},
prelude::*,
render::render_resource::{Extent3d, TextureFormat},
};
use render::{activate_cameras, clear_transient_meshes, flush_draw_commands};
use tracing::debug;
use crate::geometry::{AttributeFormat, AttributeValue};
use crate::graphics::flush;
use crate::{
graphics::GraphicsPlugin, image::ImagePlugin, render::command::DrawCommand,
surface::SurfacePlugin,
};
static IS_INIT: OnceLock<()> = OnceLock::new();
thread_local! {
static APP: RefCell<Option<App>> = const { RefCell::new(None) };
}
#[derive(Component)]
pub struct Flush;
fn app_mut<T>(cb: impl FnOnce(&mut App) -> error::Result<T>) -> error::Result<T> {
let res = APP.with(|app_cell| {
let mut app_borrow = app_cell.borrow_mut();
let app = app_borrow
.as_mut()
.ok_or(error::ProcessingError::AppAccess)?;
cb(app)
})?;
Ok(res)
}
/// Create a WebGPU surface from a macOS NSWindow handle.
#[cfg(target_os = "macos")]
pub fn surface_create_macos(
window_handle: u64,
width: u32,
height: u32,
scale_factor: f32,
) -> error::Result<Entity> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(
surface::create_surface_macos,
(window_handle, width, height, scale_factor),
)
.unwrap()
})
}
/// Create a WebGPU surface from a Windows HWND handle.
#[cfg(target_os = "windows")]
pub fn surface_create_windows(
window_handle: u64,
width: u32,
height: u32,
scale_factor: f32,
) -> error::Result<Entity> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(
surface::create_surface_windows,
(window_handle, width, height, scale_factor),
)
.unwrap()
})
}
/// Create a WebGPU surface from a Wayland window and display handle.
#[cfg(all(target_os = "linux", feature = "wayland"))]
pub fn surface_create_wayland(
window_handle: u64,
display_handle: u64,
width: u32,
height: u32,
scale_factor: f32,
) -> error::Result<Entity> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(
surface::create_surface_wayland,
(window_handle, display_handle, width, height, scale_factor),
)
.unwrap()
})
}
/// Create a WebGPU surface from an X11 window and display handle.
#[cfg(all(target_os = "linux", feature = "x11"))]
pub fn surface_create_x11(
window_handle: u64,
display_handle: u64,
width: u32,
height: u32,
scale_factor: f32,
) -> error::Result<Entity> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(
surface::create_surface_x11,
(window_handle, display_handle, width, height, scale_factor),
)
.unwrap()
})
}
/// Create a WebGPU surface from a web canvas element pointer.
#[cfg(target_arch = "wasm32")]
pub fn surface_create_web(
window_handle: u64,
width: u32,
height: u32,
scale_factor: f32,
) -> error::Result<Entity> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(
surface::create_surface_web,
(window_handle, width, height, scale_factor),
)
.unwrap()
})
}
pub fn surface_create_offscreen(
width: u32,
height: u32,
scale_factor: f32,
texture_format: TextureFormat,
) -> error::Result<Entity> {
app_mut(|app| {
let (size, data, texture_format) =
surface::prepare_offscreen(width, height, scale_factor, texture_format)?;
let world = app.world_mut();
let image_entity = world
.run_system_cached_with(image::create, (size, data, texture_format))
.unwrap();
world.entity_mut(image_entity).insert(surface::Surface);
Ok(image_entity)
})
}
/// Create a WebGPU surface from a canvas element ID
#[cfg(target_arch = "wasm32")]
pub fn surface_create_from_canvas(
canvas_id: &str,
width: u32,
height: u32,
) -> error::Result<Entity> {
use wasm_bindgen::JsCast;
use web_sys::HtmlCanvasElement;
// find the canvas element
let web_window = web_sys::window().ok_or(error::ProcessingError::InvalidWindowHandle)?;
let document = web_window
.document()
.ok_or(error::ProcessingError::InvalidWindowHandle)?;
let canvas = document
.get_element_by_id(canvas_id)
.ok_or(error::ProcessingError::InvalidWindowHandle)?
.dyn_into::<HtmlCanvasElement>()
.map_err(|_| error::ProcessingError::InvalidWindowHandle)?;
// box and leak the canvas to ensure the pointer remains valid
// TODO: this is maybe gross, let's find a better way to manage the lifetime
let canvas_box = Box::new(canvas);
let canvas_ptr = Box::into_raw(canvas_box) as u64;
// TODO: not sure if this is right to force here
let scale_factor = 1.0;
surface_create_web(canvas_ptr, width, height, scale_factor)
}
pub fn surface_destroy(graphics_entity: Entity) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(surface::destroy, graphics_entity)
.unwrap()
})
}
/// Update window size when resized.
pub fn surface_resize(graphics_entity: Entity, width: u32, height: u32) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(surface::resize, (graphics_entity, width, height))
.unwrap()
})
}
fn create_app(config: Config) -> App {
let mut app = App::new();
app.insert_resource(config.clone());
#[cfg(not(target_arch = "wasm32"))]
let plugins = DefaultPlugins
.build()
.disable::<bevy::winit::WinitPlugin>()
.disable::<bevy::log::LogPlugin>()
.set(WindowPlugin {
primary_window: None,
exit_condition: bevy::window::ExitCondition::DontExit,
..default()
});
#[cfg(target_arch = "wasm32")]
let plugins = DefaultPlugins
.build()
.disable::<bevy::winit::WinitPlugin>()
.disable::<bevy::log::LogPlugin>()
.set(WindowPlugin {
primary_window: None,
exit_condition: bevy::window::ExitCondition::DontExit,
..default()
});
if let Some(asset_path) = config.get(ConfigKey::AssetRootPath) {
app.register_asset_source(
"assets_directory",
AssetSourceBuilder::platform_default(asset_path, None),
);
}
app.add_plugins(plugins);
app.add_plugins((
ImagePlugin,
GraphicsPlugin,
SurfacePlugin,
geometry::GeometryPlugin,
));
app.add_systems(First, (clear_transient_meshes, activate_cameras))
.add_systems(Update, flush_draw_commands.before(AssetEventSystems));
app
}
fn is_already_init() -> error::Result<bool> {
let is_init = IS_INIT.get().is_some();
let thread_has_app = APP.with(|app_cell| app_cell.borrow().is_some());
if is_init && !thread_has_app {
return Err(error::ProcessingError::AppAccess);
}
if is_init && thread_has_app {
debug!("App already initialized");
return Ok(true);
}
Ok(false)
}
fn set_app(app: App) {
APP.with(|app_cell| {
IS_INIT.get_or_init(|| ());
*app_cell.borrow_mut() = Some(app);
});
}
/// Initialize the app, if not already initialized. Must be called from the main thread and cannot
/// be called concurrently from multiple threads.
#[cfg(not(target_arch = "wasm32"))]
pub fn init(config: Config) -> error::Result<()> {
setup_tracing()?;
if is_already_init()? {
return Ok(());
}
let mut app = create_app(config);
app.finish();
app.cleanup();
set_app(app);
Ok(())
}
/// Initialize the app asynchronously
#[cfg(target_arch = "wasm32")]
pub async fn init(config: Config) -> error::Result<()> {
use bevy::app::PluginsState;
setup_tracing()?;
if is_already_init()? {
return Ok(());
}
let mut app = create_app(config);
// we need to avoid blocking the main thread while waiting for plugins to initialize
while app.plugins_state() == PluginsState::Adding {
// yield to event loop
wasm_bindgen_futures::JsFuture::from(js_sys::Promise::new(&mut |resolve, _| {
web_sys::window()
.unwrap()
.set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, 0)
.unwrap();
}))
.await
.unwrap();
}
app.finish();
app.cleanup();
set_app(app);
Ok(())
}
/// Create a new graphics surface for rendering.
pub fn graphics_create(surface_entity: Entity, width: u32, height: u32) -> error::Result<Entity> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(graphics::create, (width, height, surface_entity))
.unwrap()
})
}
/// Begin a new draw pass for the graphics surface.
pub fn graphics_begin_draw(graphics_entity: Entity) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(graphics::begin_draw, graphics_entity)
.unwrap()
})
}
/// Flush current pending draw commands to the graphics surface.
pub fn graphics_flush(graphics_entity: Entity) -> error::Result<()> {
app_mut(|app| graphics::flush(app, graphics_entity))
}
/// End the current draw pass for the graphics surface.
pub fn graphics_end_draw(graphics_entity: Entity) -> error::Result<()> {
app_mut(|app| graphics::end_draw(app, graphics_entity))
}
/// Destroy the graphics surface and free its resources.
pub fn graphics_destroy(graphics_entity: Entity) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(graphics::destroy, graphics_entity)
.unwrap()
})
}
/// Read back pixel data from the graphics surface.
pub fn graphics_readback(graphics_entity: Entity) -> error::Result<Vec<LinearRgba>> {
app_mut(|app| {
graphics::flush(app, graphics_entity)?;
app.world_mut()
.run_system_cached_with(graphics::readback, graphics_entity)
.unwrap()
})
}
/// Update the graphics surface with new pixel data.
pub fn graphics_update(graphics_entity: Entity, pixels: &[LinearRgba]) -> error::Result<()> {
app_mut(|app| {
let world = app.world_mut();
let size = world
.get::<graphics::Graphics>(graphics_entity)
.ok_or(error::ProcessingError::GraphicsNotFound)?
.size;
let (data, px_size) = graphics::prepare_update_region(
world,
graphics_entity,
size.width,
size.height,
pixels,
)?;
world
.run_system_cached_with(
graphics::update_region_write,
(
graphics_entity,
0,
0,
size.width,
size.height,
data,
px_size,
),
)
.unwrap()
})
}
/// Update a region of the graphics surface with new pixel data.
pub fn graphics_update_region(
graphics_entity: Entity,
x: u32,
y: u32,
width: u32,
height: u32,
pixels: &[LinearRgba],
) -> error::Result<()> {
app_mut(|app| {
let world = app.world_mut();
let (data, px_size) =
graphics::prepare_update_region(world, graphics_entity, width, height, pixels)?;
world
.run_system_cached_with(
graphics::update_region_write,
(graphics_entity, x, y, width, height, data, px_size),
)
.unwrap()
})
}
pub fn exit(exit_code: u8) -> error::Result<()> {
app_mut(|app| {
app.world_mut().write_message(match exit_code {
0 => AppExit::Success,
_ => AppExit::Error(NonZero::new(exit_code).unwrap()),
});
// one final update to process the exit message
app.update();
Ok(())
})?;
// we need to drop the app in a deterministic manner to ensure resources are cleaned up
// otherwise we'll get wgpu graphics backend errors on exit
APP.with(|app_cell| {
let app = app_cell.borrow_mut().take();
drop(app);
});
Ok(())
}
fn setup_tracing() -> error::Result<()> {
// TODO: figure out wasm compatible tracing subscriber
#[cfg(not(target_arch = "wasm32"))]
{
let subscriber = tracing_subscriber::FmtSubscriber::new();
tracing::subscriber::set_global_default(subscriber)?;
}
Ok(())
}
/// Record a drawing command for a window
pub fn graphics_record_command(graphics_entity: Entity, cmd: DrawCommand) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(graphics::record_command, (graphics_entity, cmd))
.unwrap()
})
}
pub fn graphics_mode_3d(graphics_entity: Entity) -> error::Result<()> {
app_mut(|app| {
flush(app, graphics_entity)?;
app.world_mut()
.run_system_cached_with(graphics::mode_3d, graphics_entity)
.unwrap()
})
}
pub fn graphics_mode_2d(graphics_entity: Entity) -> error::Result<()> {
app_mut(|app| {
flush(app, graphics_entity)?;
app.world_mut()
.run_system_cached_with(graphics::mode_2d, graphics_entity)
.unwrap()
})
}
pub fn graphics_camera_position(
graphics_entity: Entity,
x: f32,
y: f32,
z: f32,
) -> error::Result<()> {
app_mut(|app| {
flush(app, graphics_entity)?;
app.world_mut()
.run_system_cached_with(graphics::camera_position, (graphics_entity, x, y, z))
.unwrap()
})
}
pub fn graphics_camera_look_at(
graphics_entity: Entity,
target_x: f32,
target_y: f32,
target_z: f32,
) -> error::Result<()> {
app_mut(|app| {
flush(app, graphics_entity)?;
app.world_mut()
.run_system_cached_with(
graphics::camera_look_at,
(graphics_entity, target_x, target_y, target_z),
)
.unwrap()
})
}
pub fn graphics_perspective(
graphics_entity: Entity,
fov: f32,
aspect_ratio: f32,
near: f32,
far: f32,
) -> error::Result<()> {
app_mut(|app| {
flush(app, graphics_entity)?;
app.world_mut()
.run_system_cached_with(
graphics::perspective,
(
graphics_entity,
PerspectiveProjection {
fov,
aspect_ratio,
near,
far,
},
),
)
.unwrap()
})
}
#[allow(clippy::too_many_arguments)]
pub fn graphics_ortho(
graphics_entity: Entity,
left: f32,
right: f32,
bottom: f32,
top: f32,
near: f32,
far: f32,
) -> error::Result<()> {
app_mut(|app| {
flush(app, graphics_entity)?;
app.world_mut()
.run_system_cached_with(
graphics::ortho,
(
graphics_entity,
graphics::OrthoArgs {
left,
right,
bottom,
top,
near,
far,
},
),
)
.unwrap()
})
}
/// Create a new image with given size and data.
pub fn image_create(
size: Extent3d,
data: Vec<u8>,
texture_format: TextureFormat,
) -> error::Result<Entity> {
app_mut(|app| {
Ok(app
.world_mut()
.run_system_cached_with(image::create, (size, data, texture_format))
.unwrap())
})
}
/// Load an image from disk.
#[cfg(not(target_arch = "wasm32"))]
pub fn image_load(path: &str) -> error::Result<Entity> {
let path = PathBuf::from(path);
app_mut(|app| {
app.world_mut()
.run_system_cached_with(image::load, path)
.unwrap()
})
}
#[cfg(target_arch = "wasm32")]
pub async fn image_load(path: &str) -> error::Result<Entity> {
use bevy::prelude::{Handle, Image};
let path = PathBuf::from(path);
let handle: Handle<Image> = app_mut(|app| Ok(image::load_start(app.world_mut(), path)))?;
// poll until loaded, yielding to event loop
loop {
let is_loaded = app_mut(|app| Ok(image::is_loaded(app.world(), &handle)))?;
if is_loaded {
break;
}
// yield to let fetch complete
wasm_bindgen_futures::JsFuture::from(js_sys::Promise::new(&mut |resolve, _| {
web_sys::window()
.unwrap()
.set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, 0)
.unwrap();
}))
.await
.unwrap();
// run an update to process asset events
app_mut(|app| {
app.update();
Ok(())
})?;
}
app_mut(|app| {
app.world_mut()
.run_system_cached_with(image::from_handle, handle)
.unwrap()
})
}
/// Resize an existing image to new size.
pub fn image_resize(entity: Entity, new_size: Extent3d) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(image::resize, (entity, new_size))
.unwrap()
})
}
/// Read back image data from GPU to CPU.
pub fn image_readback(entity: Entity) -> error::Result<Vec<LinearRgba>> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(image::readback, entity)
.unwrap()
})
}
/// Update an existing image with new pixel data.
pub fn image_update(entity: Entity, pixels: &[LinearRgba]) -> error::Result<()> {
app_mut(|app| {
let world = app.world_mut();
let size = world
.get::<image::Image>(entity)
.ok_or(error::ProcessingError::ImageNotFound)?
.size;
let (data, px_size) =
image::prepare_update_region(world, entity, size.width, size.height, pixels)?;
world
.run_system_cached_with(
image::update_region_write,
(entity, 0, 0, size.width, size.height, data, px_size),
)
.unwrap()
})
}
/// Update a region of an existing image with new pixel data.
pub fn image_update_region(
entity: Entity,
x: u32,
y: u32,
width: u32,
height: u32,
pixels: &[LinearRgba],
) -> error::Result<()> {
app_mut(|app| {
let world = app.world_mut();
let (data, px_size) = image::prepare_update_region(world, entity, width, height, pixels)?;
world
.run_system_cached_with(
image::update_region_write,
(entity, x, y, width, height, data, px_size),
)
.unwrap()
})
}
/// Destroy an existing image and free its resources.
pub fn image_destroy(entity: Entity) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(image::destroy, entity)
.unwrap()
})
}
pub fn geometry_layout_create() -> error::Result<Entity> {
app_mut(|app| {
Ok(app
.world_mut()
.run_system_cached_with(geometry::layout::create, ())
.unwrap())
})
}
pub fn geometry_layout_add_position(entity: Entity) -> error::Result<()> {
app_mut(|app| geometry::layout::add_position(app.world_mut(), entity))
}
pub fn geometry_layout_add_normal(entity: Entity) -> error::Result<()> {
app_mut(|app| geometry::layout::add_normal(app.world_mut(), entity))
}
pub fn geometry_layout_add_color(entity: Entity) -> error::Result<()> {
app_mut(|app| geometry::layout::add_color(app.world_mut(), entity))
}
pub fn geometry_layout_add_uv(entity: Entity) -> error::Result<()> {
app_mut(|app| geometry::layout::add_uv(app.world_mut(), entity))
}
pub fn geometry_layout_add_attribute(
layout_entity: Entity,
attr_entity: Entity,
) -> error::Result<()> {
app_mut(|app| geometry::layout::add_attribute(app.world_mut(), layout_entity, attr_entity))
}
pub fn geometry_layout_destroy(entity: Entity) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::layout::destroy, entity)
.unwrap();
Ok(())
})
}
pub fn geometry_attribute_create(
name: impl Into<String>,
format: AttributeFormat,
) -> error::Result<Entity> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::attribute::create, (name.into(), format))
.unwrap()
})
}
pub fn geometry_attribute_position() -> Entity {
app_mut(|app| {
Ok(app
.world()
.resource::<geometry::BuiltinAttributes>()
.position)
})
.unwrap()
}
pub fn geometry_attribute_normal() -> Entity {
app_mut(|app| Ok(app.world().resource::<geometry::BuiltinAttributes>().normal)).unwrap()
}
pub fn geometry_attribute_color() -> Entity {
app_mut(|app| Ok(app.world().resource::<geometry::BuiltinAttributes>().color)).unwrap()
}
pub fn geometry_attribute_uv() -> Entity {
app_mut(|app| Ok(app.world().resource::<geometry::BuiltinAttributes>().uv)).unwrap()
}
pub fn geometry_attribute_destroy(entity: Entity) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::attribute::destroy, entity)
.unwrap()?;
Ok(())
})
}
pub fn geometry_create(topology: geometry::Topology) -> error::Result<Entity> {
app_mut(|app| {
Ok(app
.world_mut()
.run_system_cached_with(geometry::create, topology)
.unwrap())
})
}
pub fn geometry_create_with_layout(
layout_entity: Entity,
topology: geometry::Topology,
) -> error::Result<Entity> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::create_with_layout, (layout_entity, topology))
.unwrap()
})
}
pub fn geometry_normal(entity: Entity, nx: f32, ny: f32, nz: f32) -> error::Result<()> {
app_mut(|app| geometry::normal(app.world_mut(), entity, nx, ny, nz))
}
pub fn geometry_color(entity: Entity, r: f32, g: f32, b: f32, a: f32) -> error::Result<()> {
app_mut(|app| geometry::color(app.world_mut(), entity, r, g, b, a))
}
pub fn geometry_uv(entity: Entity, u: f32, v: f32) -> error::Result<()> {
app_mut(|app| geometry::uv(app.world_mut(), entity, u, v))
}
pub fn geometry_attribute(
geo_entity: Entity,
attr_entity: Entity,
value: AttributeValue,
) -> error::Result<()> {
app_mut(|app| geometry::attribute(app.world_mut(), geo_entity, attr_entity, value))
}
pub fn geometry_attribute_float(
geo_entity: Entity,
attr_entity: Entity,
v: f32,
) -> error::Result<()> {
geometry_attribute(geo_entity, attr_entity, AttributeValue::Float(v))
}
pub fn geometry_attribute_float2(
geo_entity: Entity,
attr_entity: Entity,
x: f32,
y: f32,
) -> error::Result<()> {
geometry_attribute(geo_entity, attr_entity, AttributeValue::Float2([x, y]))
}
pub fn geometry_attribute_float3(
geo_entity: Entity,
attr_entity: Entity,
x: f32,
y: f32,
z: f32,
) -> error::Result<()> {
geometry_attribute(geo_entity, attr_entity, AttributeValue::Float3([x, y, z]))
}
pub fn geometry_attribute_float4(
geo_entity: Entity,
attr_entity: Entity,
x: f32,
y: f32,
z: f32,
w: f32,
) -> error::Result<()> {
geometry_attribute(
geo_entity,
attr_entity,
AttributeValue::Float4([x, y, z, w]),
)
}
pub fn geometry_vertex(entity: Entity, x: f32, y: f32, z: f32) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::vertex, (entity, x, y, z))
.unwrap()
})
}
pub fn geometry_index(entity: Entity, i: u32) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::index, (entity, i))
.unwrap()
})
}
pub fn geometry_vertex_count(entity: Entity) -> error::Result<u32> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::vertex_count, entity)
.unwrap()
})
}
pub fn geometry_index_count(entity: Entity) -> error::Result<u32> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::index_count, entity)
.unwrap()
})
}
pub fn geometry_get_positions(
entity: Entity,
start: usize,
end: usize,
) -> error::Result<Vec<[f32; 3]>> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::get_positions, (entity, start..end))
.unwrap()
})
}
pub fn geometry_get_normals(
entity: Entity,
start: usize,
end: usize,
) -> error::Result<Vec<[f32; 3]>> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::get_normals, (entity, start..end))
.unwrap()
})
}
pub fn geometry_get_colors(
entity: Entity,
start: usize,
end: usize,
) -> error::Result<Vec<[f32; 4]>> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::get_colors, (entity, start..end))
.unwrap()
})
}
pub fn geometry_get_uvs(entity: Entity, start: usize, end: usize) -> error::Result<Vec<[f32; 2]>> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::get_uvs, (entity, start..end))
.unwrap()
})
}
pub fn geometry_get_indices(entity: Entity, start: usize, end: usize) -> error::Result<Vec<u32>> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::get_indices, (entity, start..end))
.unwrap()
})
}
pub fn geometry_destroy(entity: Entity) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::destroy, entity)
.unwrap()
})
}
pub fn geometry_set_vertex(
entity: Entity,
index: u32,
x: f32,
y: f32,
z: f32,
) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::set_vertex, (entity, index, x, y, z))
.unwrap()
})
}
pub fn geometry_set_normal(
entity: Entity,
index: u32,
nx: f32,
ny: f32,
nz: f32,
) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::set_normal, (entity, index, nx, ny, nz))
.unwrap()
})
}
pub fn geometry_set_color(
entity: Entity,
index: u32,
r: f32,
g: f32,
b: f32,
a: f32,
) -> error::Result<()> {
app_mut(|app| {
app.world_mut()
.run_system_cached_with(geometry::set_color, (entity, index, r, g, b, a))