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
use ambient_core::{camera::*, transform::*, ui_scene};
use ambient_ecs::{components, EntityId, Networked, Store, SystemGroup};
use ambient_element::{element_component, Element, Hooks};
use ambient_std::shapes::BoundingBox;
use glam::{Quat, Vec3};
use winit::event::Event;

use crate::{free::free_camera_system, spherical::spherical_camera_system};

pub mod free;
pub mod spherical;

components!("camera", {
    @[Networked, Store]
    camera_movement_speed: f32,
});

pub fn init_all_components() {
    free::init_components();
    init_components();
    spherical::init_components();
}

pub fn assets_camera_systems() -> SystemGroup<Event<'static, ()>> {
    SystemGroup::new("assets_camera_systems", vec![Box::new(free_camera_system()), Box::new(spherical_camera_system())])
}

#[element_component]
pub fn UICamera(_: &mut Hooks) -> Element {
    Element::new()
        .init_default(local_to_world())
        .init_default(inv_local_to_world())
        .init(near(), -1.)
        .init(far(), 1.0)
        .init_default(orthographic())
        .init(orthographic_left(), 0.)
        .init(orthographic_right(), 100.)
        .init(orthographic_top(), 0.)
        .init(orthographic_bottom(), 100.)
        .init(orthographic_rect(), OrthographicRect { left: 0.0, right: 100., top: 0., bottom: 100. })
        .init_default(projection())
        .init_default(projection_view())
        .init_default(translation())
        .init_default(rotation())
        .init(orthographic_from_window(), EntityId::resources())
        .init_default(ui_scene())
        .init(active_camera(), 0.)
}

#[element_component]
pub fn LookatCamera(_: &mut Hooks, eye: Vec3, lookat: Vec3, up: Vec3) -> Element {
    Element::new()
        .init_default(local_to_world())
        .init_default(inv_local_to_world())
        .init(near(), 0.1)
        .init(fovy(), 1.0)
        .init(perspective_infinite_reverse(), ())
        .init(aspect_ratio(), 1.)
        .init(aspect_ratio_from_window(), EntityId::resources())
        .init_default(projection())
        .init_default(projection_view())
        .with(translation(), eye)
        .with(lookat_target(), lookat)
        .with(lookat_up(), up)
}

#[element_component]
pub fn FreeCamera(_: &mut Hooks, position: Vec3, rotation: Quat) -> Element {
    Element::new()
        .init_default(local_to_world())
        .init_default(inv_local_to_world())
        .init(near(), 0.1)
        .init(fovy(), 1.0)
        .init(perspective_infinite_reverse(), ())
        .init(aspect_ratio(), 1.)
        .init(aspect_ratio_from_window(), EntityId::resources())
        .init_default(projection())
        .init_default(projection_view())
        .with(ambient_core::transform::translation(), position)
        .with(ambient_core::transform::rotation(), rotation)
}

#[element_component]
pub fn FittedOrthographicCamera(_: &mut Hooks, eye: Vec3, lookat: Vec3, up: Vec3, fit: BoundingBox, aspect: f32) -> Element {
    Element::new().extend(Camera::fitted_ortographic(eye, lookat, up, fit, aspect).to_entity_data())
}