Skip to content

Commit

Permalink
Merge pull request #195 from AmbientRun/element-wasm
Browse files Browse the repository at this point in the history
Basic element support in guest
  • Loading branch information
FredrikNoren committed Mar 2, 2023
2 parents 38773e5 + ef35372 commit 0f1e342
Show file tree
Hide file tree
Showing 59 changed files with 1,201 additions and 259 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
},
"args": [
"run",
"guest/rust/examples/image"
"guest/rust/examples/text",
"--debug"
],
"cwd": "${workspaceFolder}",
"sourceLanguages": [
Expand Down
103 changes: 100 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions app/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ fn MainApp(
) -> Element {
let resolution = use_window_physical_resolution(hooks);

hooks.provide_context(GameClientNetworkStats::default);
hooks.provide_context(GameClientServerStats::default);
let update_network_stats = hooks.provide_context(GameClientNetworkStats::default);
let update_server_stats = hooks.provide_context(GameClientServerStats::default);

*hooks.world.resource_mut(window_title()) = "Ambient".to_string();

Expand All @@ -65,6 +65,8 @@ fn MainApp(
}))),
on_loaded: cb(move |_game_state, _game_client| Ok(Box::new(|| {}))),
error_view: cb(move |error| Dock(vec![Text::el("Error").header_style(), Text::el(error)]).el()),
on_network_stats: cb(move |stats| update_network_stats(stats)),
on_server_stats: cb(move |stats| update_server_stats(stats)),
systems_and_resources: cb(|| (systems(), Entity::new())),
create_rpc_registry: cb(shared::create_rpc_registry),
on_in_entities: None,
Expand Down
3 changes: 1 addition & 2 deletions app/src/shared/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{io::Write, sync::Arc};
use ambient_audio::AudioListener;
use ambient_core::{
camera::{active_camera, aspect_ratio_from_window},
main_scene, runtime,
runtime,
};
use ambient_ecs::{query, query_mut, Entity, SystemGroup};
use ambient_element::{element_component, Element, Hooks};
Expand Down Expand Up @@ -106,7 +106,6 @@ pub fn client_systems() -> SystemGroup {
id,
Entity::new()
.with(active_camera(), 0.)
.with(main_scene(), ())
.with(audio_listener(), Arc::new(Mutex::new(AudioListener::new(Mat4::IDENTITY, Vec3::X * 0.2))))
.with(aspect_ratio_from_window(), ()),
)
Expand Down
36 changes: 10 additions & 26 deletions crates/cameras/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ambient_core::{camera::*, transform::*, ui_scene, window_logical_size};
use ambient_ecs::{components, query_mut, Description, Name, Networked, Store, SystemGroup};
use ambient_core::{camera::*, transform::*, ui_scene};
use ambient_ecs::{components, Networked, Store, SystemGroup};
use ambient_element::{element_component, Element, Hooks};
use ambient_std::shapes::BoundingBox;
use glam::{Mat4, Quat, Vec3};
use glam::{Quat, Vec3};
use winit::event::Event;

use crate::{free::free_camera_system, spherical::spherical_camera_system};
Expand All @@ -13,8 +13,6 @@ pub mod spherical;
components!("camera", {
@[Networked, Store]
camera_movement_speed: f32,
@[Networked, Store, Name["UI camera"], Description["This entity is a camera that is used to render UI.\nEnsure that you have the remaining camera components."]]
ui_camera: (),
});

pub fn init_all_components() {
Expand All @@ -24,26 +22,7 @@ pub fn init_all_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()), Box::new(ui_camera_system())],
)
}

pub fn ui_camera_system() -> SystemGroup<Event<'static, ()>> {
SystemGroup::new(
"ui_camera_system",
vec![query_mut((orthographic_rect(), local_to_world()), (ui_camera(),)).to_system(|q, world, qs, _| {
let window_size = world.resource(window_logical_size()).as_vec2();
for (_, (orth, ltw), (_,)) in q.iter(world, qs) {
*ltw = Mat4::from_translation((window_size / 2.).extend(0.));
orth.left = -window_size.x / 2.;
orth.right = window_size.x / 2.;
orth.top = -window_size.y / 2.;
orth.bottom = window_size.y / 2.;
}
})],
)
SystemGroup::new("assets_camera_systems", vec![Box::new(free_camera_system()), Box::new(spherical_camera_system())])
}

#[element_component]
Expand All @@ -53,12 +32,17 @@ pub fn UICamera(_: &mut Hooks) -> Element {
.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_default(ui_camera())
.init_default(orthographic_from_window())
.init_default(ui_scene())
}

Expand Down
Loading

0 comments on commit 0f1e342

Please sign in to comment.