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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
//! # 🌐 World API
//!
//! A world consists of entities, which each simply is a set of components such as transform,
//! render properties, physical properties and so on. The World API lets you create, change
//! manipulate and destroy entities.
//!
//! For an overview, check out our [Developer Guide](https://ark.embark.dev/guide/api/world/index.html)
//!
//! If you're looking for immediate mode rendering instead of having persistent entities or physics managed
//! for you, you're probably more interested in the [Render API](https://ark.embark.dev/api/ark_api/render/index.html).
//! You can even combine the two. One possibility would be to do rendering of World API entities manually using the Render API
//! `Render` components, or simply to render transient visual effects using the Render API on top of a world.
//!
//! The documentation of `Entity` is a good starting point to figure out what can be done.
//!
//! ## Example
//!
//! Quick example of setting up a ball on a square piece of ground:
//!
//! ```
//! let ground_mesh = ...;
//! let ground_entity = Entity::create("ground");
//! ground_entity.render().mesh().set(ground_mesh);
//! ground_entity.physics().enable_static(PhysicsShape::Box);
//!
//! let ball_mesh = ...;
//! let ball_entity = Entity::create("ball");
//! ball_entity.render().mesh().set(ball_mesh);
//! ball_entity
//! .transform()
//! .position()
//! .set(Vec3::new(0.0, 2.5, -40.0));
//! ball_entity.physics().enable_dynamic(10.0, PhysicsShape::Sphere);
//! ball_entity.physics().add_force(
//! ForceType::Force,
//! ForceMode::Impulse,
//! Vec3::new(0.0, 0.0, 50000.0),
//! );
//! ```
mod ffi {
pub use crate::ffi::{world_v0 as v0, world_v0::*};
pub use crate::ffi::{world_v1 as v1, world_v1::*};
pub use crate::ffi::{world_v2 as v2, world_v2::*};
pub use crate::ffi::{world_v3 as v3, world_v3::*};
pub use crate::ffi::{world_v4 as v4, world_v4::*};
}
use bytemuck::Zeroable;
pub use ffi::{
AudioClipInfo, BoneWeightFlags, CameraProjectionMode, CombineMode, ComponentMetaDataEntry,
ComponentType, ComponentsMetaDataEntry, CreateDataType, D6Axis, D6Drive, D6MotionType,
DynamicLockFlags, EntityTemplate, ForceMode, ForceType, JointLimitType, MeshStyleFlags,
MeshVisibilityFlags, ParameterMetaDataEntry, ParameterSemantic, RigidBodyMode, SdfSkinInfo,
Space, TriggerEvent, Value, ValueData, ValueType, WorldDataType,
{AnimationCurve, AnimationOptions, SpatialQueryOptions},
};
use crate::{Error, Quat, Vec2, Vec3, Vec4};
pub use ffi::MaterialDesc;
#[doc(hidden)]
pub use ffi::v3::API as FFI_API;
pub use ffi::v3::BoneTransform;
pub use ffi::v4::{
CollisionInfo, EntityBounds, EntityDebugOptions, EntityMeshStyleTint, EntityPair,
EntityRenderEnable, EntityStateFlags, EntityTransformConformal3, EntityValueType,
EntityVelocity, MeasuredTextSize, RaycastQueryOptions, RaycastQueryWithOptions,
};
mod mesh;
pub use mesh::*;
mod entity;
pub use entity::*;
mod value_converter;
pub use value_converter::*;
#[macro_use]
mod value_accessor;
pub use value_accessor::*;
mod entity_messenger;
pub use entity_messenger::*;
#[macro_use]
mod components;
pub use components::*;
mod data;
pub use data::*;
mod player;
pub use player::*;
mod reflection;
pub use reflection::*;
mod param_type;
pub use param_type::*;
mod query;
pub use query::*;
use crate::api::world::entity_messenger::AnimationReplyFn;
use std::{
cell::RefCell,
ops::{Deref, DerefMut},
};
///////////////////////////////////////////////////////////////////////////////
#[derive(Copy, Clone, Debug, PartialEq)]
/// The limits for a parameter.
pub struct LimitPair {
/// Lower limit.
pub lower: f32,
/// Upper limit.
pub upper: f32,
}
#[derive(Copy, Clone, Debug, PartialEq)]
/// A position and orientation, together.
pub struct Pose {
/// The position of the pose.
pub pos: Vec3,
/// The rotation of the pose.
pub orientation: Quat,
}
/// Query parameters for doing raycasts.
#[derive(Copy, Clone, Debug)]
pub struct RaycastQuery {
/// Ray to check for obstacles along.
pub ray: macaw::Ray3,
/// Layers to include in the check.
/// Defaults to everything.
pub layer_mask: EntityLayerMask,
/// Optional entity to ignore.
/// Even if the ray intersects this entity, no hit will be reported for it.
/// Defaults to None.
pub ignore_entity: Option<Entity>,
/// No hits above this distance will be reported.
/// Defaults to infinity.
pub max_distance: f32,
/// Additional options for this raycast query.
pub options: RaycastQueryOptions,
/// If >0, sweep a sphere rather than a ray.
pub spherecast_radius: f32,
}
impl Default for RaycastQuery {
fn default() -> Self {
Self {
ray: macaw::Ray3::ZERO,
layer_mask: EntityLayerMask::everything(),
ignore_entity: None,
max_distance: std::f32::INFINITY,
options: RaycastQueryOptions::empty(),
spherecast_radius: 0.0,
}
}
}
/// A single hit from a raycast query.
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct RaycastHit {
/// Point in world space that was hit.
///
/// This could technically be calculated by using the original ray together with the `distance` field.
/// But that would require callers to keep track of their query object and also ensure their ray directions are normalized
/// which is not as convenient.
pub point: Vec3,
/// The distance along the ray.
///
/// The hits are sorted by this distance when returned from raycast and spherecast queries.
/// In a raycast this is equal to `ray.origin.distance(hit.position)`,
/// but in a spherecast it can be different.
pub distance: f32,
/// World space normal of the surface that was hit.
pub normal: Vec3,
/// Entity that was hit.
pub entity: Entity,
}
impl RaycastQuery {
fn as_ffi(&self) -> ffi::v4::SpherecastQuery {
ffi::v4::SpherecastQuery {
ray: ffi::Ray {
origin: self.ray.origin.into(),
dir: self.ray.dir.into(),
},
layer_mask: self.layer_mask.value(),
ignore_entity: self.ignore_entity.unwrap_or_else(Entity::invalid).as_ffi(),
max_distance: self.max_distance,
options: self.options,
spherecast_radius: self.spherecast_radius,
_pad: Default::default(),
}
}
}
impl RaycastHit {
fn from_ffi(hit: ffi::v3::RaycastHit) -> Self {
Self {
point: hit.point.into(),
distance: hit.distance,
normal: hit.normal.into(),
entity: Entity::from_ffi(hit.entity),
}
}
}
/// Mesh style. Differs from `world::MeshStyle` in that `MeshStyle` is an entity, but
/// `MeshStyleData` is plain data.
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct MeshStyleData {
/// Diffuse color tint
pub diffuse_tint: Vec4,
/// Style flags
pub flags: MeshStyleFlags,
}
impl MeshStyleData {
/// Creates a mesh style with a diffuse tint
pub fn new(diffuse_tint: Vec4) -> Self {
Self {
diffuse_tint,
flags: MeshStyleFlags::default(),
}
}
}
impl Default for MeshStyleData {
fn default() -> Self {
Self {
diffuse_tint: Vec4::ONE,
flags: MeshStyleFlags::default(),
}
}
}
/// Create `MeshStyle` through builder pattern
#[derive(Default, Debug, Clone, Copy)]
pub struct MeshStyleDataBuilder {
style: MeshStyleData,
}
impl MeshStyleDataBuilder {
/// Creates a new builder
pub fn new() -> Self {
Self::default()
}
/// Adds diffuse color tinting
pub fn with_diffuse_tint(&mut self, tint: Vec4) -> &mut Self {
self.style.diffuse_tint = tint;
self
}
/// Sets lighting toggle
pub fn with_lighting(&mut self, e: bool) -> &mut Self {
self.style.flags.set(MeshStyleFlags::LIGHTING, e);
self
}
/// Sets alpha blending toggle
pub fn with_alpha_blending(&mut self, e: bool) -> &mut Self {
self.style.flags.set(MeshStyleFlags::ALPHA_BLENDING, e);
self
}
/// Sets pre-multiplied alpha toggle
pub fn with_premultiplied_alpha(&mut self, e: bool) -> &mut Self {
self.style.flags.set(MeshStyleFlags::PREMULTIPLIED_ALPHA, e);
self
}
/// Sets flat shading toggle. Flat shading gives smooth objects a faceted look, by not interpolating
/// normals across surfaces before lighting.
pub fn with_flat_shading(&mut self, e: bool) -> &mut Self {
self.style.flags.set(MeshStyleFlags::FLAT_SHADING, e);
self
}
/// Will make the mesh face the camera at all times, can be handy for things like particles or ui.
pub fn with_billboard_rendering(&mut self, e: bool) -> &mut Self {
self.style.flags.set(MeshStyleFlags::BILLBOARD, e);
self
}
/// Will make the mesh two-sided (backface culling is not applied).
/// Only recommended for solid meshes (and even then, when you don't need this, don't use it).
pub fn with_two_sided(&mut self, e: bool) -> &mut Self {
self.style.flags.set(MeshStyleFlags::TWO_SIDED, e);
self
}
/// Builds mesh style
pub fn build(&self) -> MeshStyleData {
self.style
}
}
/// Error value that is returned from get/set entity value functions.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum EntityValueError {
/// The component parameter value is a NaN value.
NanValue,
/// The component parameter value is an infinite value.
InfiniteParamValue,
/// The supplied component type is invalid.
InvalidComponentType,
/// The supplied component parameter id is invalid.
InvalidComponentParameterId,
/// The component did not exist.
InvalidComponent,
/// Invalid supplied entity key
InvalidEntityKey,
/// Invalid parent of parent (a parent was pointing to an invalid entity)
InvalidParentOfParent,
/// Trying to set an entity value that is incompatible with the component parameter value type.
IncompatibleValueType,
/// Trying to set an entity value that is incompatible with the component parameter value.
IncompatibleValue,
/// Could not set the entity component parameter value.
Unsettable,
/// Could not get the entity component parameter value.
Ungettable,
/// The component was not available.
NotAvailable,
/// Unknown error occurred.
Unknown(String),
}
impl std::fmt::Display for EntityValueError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NanValue => {
write!(f, "The component parameter value is a NaN value.")
}
Self::InfiniteParamValue => {
write!(f, "The component parameter value is an infinite value.")
}
Self::InvalidComponentType => {
write!(f, "The supplied component type is invalid.")
}
Self::InvalidComponentParameterId => {
write!(f, "The supplied component parameter id is invalid.")
}
Self::InvalidComponent => write!(f, "The component did not exist."),
Self::InvalidEntityKey => write!(f, "Invalid supplied entity key."),
Self::InvalidParentOfParent => write!(
f,
" Invalid parent of parent (a parent was pointing to an invalid entity)."
),
Self::IncompatibleValueType => {
write!(f, "Trying to set an entity value that is incompatible with the component parameter value type.")
}
Self::IncompatibleValue => {
write!(f, "Trying to set an entity value that is incompatible with the component parameter value.")
}
Self::Unsettable => {
write!(f, "Could not set the entity component parameter value.")
}
Self::Ungettable => {
write!(f, "Could not get the entity component parameter value.")
}
Self::NotAvailable => {
write!(f, "The component was not available.")
}
Self::Unknown(e) => {
write!(f, "{e}")
}
}?;
Ok(())
}
}
impl std::error::Error for EntityValueError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
impl From<ffi::v4::EntityValueError> for EntityValueError {
fn from(error: ffi::v4::EntityValueError) -> Self {
match error {
ffi::v4::EntityValueError::NanValue => Self::NanValue,
ffi::v4::EntityValueError::InfiniteParamValue => Self::InfiniteParamValue,
ffi::v4::EntityValueError::InvalidComponentType => Self::InvalidComponentType,
ffi::v4::EntityValueError::InvalidComponentParameterId => {
Self::InvalidComponentParameterId
}
ffi::v4::EntityValueError::InvalidComponent => Self::InvalidComponent,
ffi::v4::EntityValueError::InvalidEntityKey => Self::InvalidEntityKey,
ffi::v4::EntityValueError::InvalidParentOfParent => Self::InvalidParentOfParent,
ffi::v4::EntityValueError::IncompatibleValueType => Self::IncompatibleValueType,
ffi::v4::EntityValueError::IncompatibleValue => Self::IncompatibleValue,
ffi::v4::EntityValueError::Unsettable => Self::Unsettable,
ffi::v4::EntityValueError::Ungettable => Self::Ungettable,
ffi::v4::EntityValueError::NotAvailable => Self::NotAvailable,
e @ (ffi::v4::EntityValueError::Undefined | _) => {
Self::Unknown(format!("Unknown error code '{e:?}' returned."))
}
}
}
}
/// World implements the low-level entity API and the mesh API.
///
/// For ease of development, the wrappers (`Entity`, `Environment` etc) are recommended for
/// entity management.
pub struct World {
// We double buffer this to keep track of what was destroyed previous frame.
entities_destroyed_this_frame: Vec<Entity>,
entities_destroyed_previous_frame: Vec<Entity>,
}
static mut WORLD: Option<RefCell<World>> = None;
impl World {
fn new() -> Self {
Self {
entities_destroyed_this_frame: vec![],
entities_destroyed_previous_frame: vec![],
}
}
/// Get the `World` singleton.
pub fn instance_mut() -> impl DerefMut<Target = Self> {
unsafe { WORLD.get_or_insert_with(|| RefCell::new(Self::new())) }.borrow_mut()
}
/// Get the `World` singleton.
pub fn instance() -> impl Deref<Target = Self> {
unsafe { WORLD.get_or_insert_with(|| RefCell::new(Self::new())) }.borrow()
}
fn next_frame(&mut self) {
std::mem::swap(
&mut self.entities_destroyed_previous_frame,
&mut self.entities_destroyed_this_frame,
);
self.entities_destroyed_this_frame.clear();
}
/// Creates a world entity, from one of several templates (`entity_type`).
///
/// * `name`: only relevant for keeping track of your object during development
///
/// * `entity_template`: Chooses between one of a few "templates" (sets of precreated components):
/// - `EntityTemplate::Empty`
/// - `EntityTemplate::Object`
/// - `EntityTemplate::Environment`
/// - `EntityTemplate::MeshStyle`
/// - `EntityTemplate::Camera`
#[inline]
pub fn create_entity(name: &str, entity_template: EntityTemplate) -> Entity {
Entity::from_ffi(ffi::create_entity(name, entity_template))
}
/// Adds a component to an entity.
#[inline]
pub fn add_component(entity: Entity, component_type: ComponentType) {
ffi::add_component(entity.0, component_type);
}
/// Removes a component from an entity.
#[inline]
pub fn remove_component(entity: Entity, component_type: ComponentType) {
ffi::remove_component(entity.0, component_type);
}
/// Checks whether an entity has a component.
#[inline]
pub fn has_component(entity: Entity, component_type: ComponentType) -> bool {
ffi::has_component(entity.0, component_type) != 0
}
/// Destroys an entity. Attempting to access the entity after this is an error.
#[inline]
pub fn destroy_entity(entity: Entity) {
ffi::destroy_entity(entity.0);
}
/// Clones an entity with all its components and returns the handle to the new entity.
#[inline]
pub fn clone_entity(name: &str, entity_src: Entity) -> Entity {
let mut dest = Entity::invalid().as_ffi();
ffi::clone_entities(name, std::slice::from_mut(&mut dest), &[entity_src.0]);
Entity::from_ffi(dest)
}
/// Clones multiple entities with all their components and returns the handles to the new entities.
///
/// The name passed will be concatenated to the name of each source entity.
#[inline]
pub fn clone_entities(name: &str, entity_dst: &mut [Entity], entity_src: &[Entity]) {
let src = unsafe { &*(entity_src as *const [Entity] as *const [ffi::EntityHandle]) };
let dst = unsafe { &mut *(entity_dst as *mut [Entity] as *mut [ffi::EntityHandle]) };
ffi::clone_entities(name, dst, src);
}
/// Copies a component from an entity to another. Creates the component if it doesn't exists.
#[inline]
pub fn copy_component(entity_dst: Entity, entity_src: Entity, component_type: ComponentType) {
ffi::copy_component(entity_dst.0, entity_src.0, component_type);
}
/// Lets you check whether an `EntityHandle` points to a valid entity.
///
/// If it has existed but been destroyed, the return value is false.
#[inline]
pub fn is_valid_entity(entity: Entity) -> bool {
ffi::is_valid_entity(entity.0) != 0
}
/// Sets the value of a property of a component belonging to an entity.
///
/// This can panic if an invalid entity handle, component type, parameter id, or value was supplied.
/// Make sure the entity and component actually exist and use the right value for it.
///
/// Use `try_set_entity_value` for a non panicking version of this function.
#[inline]
pub fn set_entity_value(
entity: Entity,
component_type: ComponentType,
param_id: u32,
value: &Value,
) {
ffi::set_entity_value_checked(entity.0, component_type, param_id, value);
}
/// Tries to set the value of a property of a component belonging to an entity.
///
/// Returns `EntityValueError` if the set operation failed.
#[inline]
pub fn try_set_entity_value(
entity: Entity,
component_type: ComponentType,
param_id: u32,
value: &Value,
) -> Result<(), EntityValueError> {
let mut error = ffi::v4::EntityValueError::Undefined;
let ffi_result =
ffi::try_set_entity_value(entity.0, component_type.into(), param_id, value, &mut error);
match ffi_result {
Ok(_) => Ok(()),
Err(_e) => Err(error.into()),
}
}
/// Gets the value of a property of a component belonging to an entity.
///
/// This can panic if an invalid entity handle, component type, or parameter id was supplied.
/// Make sure the entity and component actually exist.
///
/// Use `try_set_entity_value` for a non panicking version of this function.
#[inline]
pub fn get_entity_value(entity: Entity, component_type: ComponentType, param_id: u32) -> Value {
let mut value = unsafe { std::mem::zeroed::<Value>() };
ffi::get_entity_value_checked(entity.0, component_type, param_id, &mut value);
value
}
/// Tries to get the value of a property of a component belonging to an entity.
///
/// Returns `EntityValueError` if the get operation failed.
#[inline]
pub fn try_get_entity_value(
entity: Entity,
component_type: ComponentType,
param_id: u32,
) -> Result<Value, EntityValueError> {
let mut error = ffi::v4::EntityValueError::Undefined;
let mut value = unsafe { std::mem::zeroed::<Value>() };
let ffi_result = ffi::try_get_entity_value(
entity.0,
component_type.into(),
param_id,
&mut value,
&mut error,
);
match ffi_result {
Ok(_) => Ok(value),
Err(_e) => Err(error.into()),
}
}
/// Retrieves values of the entities passed through the slice `entities`.
///
/// Careful attention that the `out_data` element size and layout matches the corresponding `EntityValueType` struct, should be
/// taken.
#[inline]
pub fn get_entity_values<T: Sized>(
entities: &[Entity],
value_type: ffi::v4::EntityValueType,
out_data: &mut Vec<T>,
) -> Result<(), Error> {
if std::mem::size_of::<T>() != value_type.size_of_element() as usize {
return Err(Error::InvalidArguments);
}
out_data.reserve(entities.len());
// Our Entity wrapper is just a transparent newtype wrapper around the ffi::EntityHandle
// so we can safely transmute a slice of it. Also we want to cast the typed slice into
// a slice of bytes.
let (entities, out_data_slice) = unsafe {
out_data.set_len(entities.len());
(
&*(entities as *const [Entity] as *const [ffi::EntityHandle]),
std::slice::from_raw_parts_mut(
out_data.as_mut_ptr().cast::<u8>(),
out_data.len() * std::mem::size_of::<T>(),
),
)
};
ffi::v4::get_entity_values(entities, value_type, out_data_slice);
Ok(())
}
/// Sets the values of the entities passed through the slice `entities`. The length
/// of the `out_data` slice needs to be the same as `entities` or a single value in which case
/// it will be applied to all entities.
///
/// Careful attention that the `in_data` element size and layout matches the corresponding `EntityValueType` struct, should be
/// taken.
pub fn set_entity_values<T: Sized>(
entities: &[Entity],
value_type: ffi::v4::EntityValueType,
in_data: &[T],
) -> Result<(), Error> {
if std::mem::size_of::<T>() != value_type.size_of_element() as usize {
return Err(Error::InvalidArguments);
}
// Our Entity wrapper is just a transparent newtype wrapper around the ffi::EntityHandle
// so we can safely transmute a slice of it. Also we want to cast the typed slice into
// a slice of bytes.
let (entities, in_data_slice) = unsafe {
(
&*(entities as *const [Entity] as *const [ffi::EntityHandle]),
std::slice::from_raw_parts(
in_data.as_ptr().cast::<u8>(),
in_data.len() * std::mem::size_of::<T>(),
),
)
};
ffi::v4::set_entity_values(entities, value_type, in_data_slice);
Ok(())
}
/// Sets the local transforms of the entities passed through the slice `entities`. The length
/// of the `input` slice needs to be the same as `entities` or a single value in which case
/// it will be applied to all entities.
///
pub fn set_entity_local_transforms(
entities: &[Entity],
input: &[ffi::v4::EntityTransformConformal3],
) {
Self::set_entity_values(
entities,
ffi::v4::EntityValueType::EntityLocalTransformConformal3,
input,
)
.expect("Failed to execute set_entity_values. Invalid arguments.");
}
/// Retrieves the local transforms of the entities passed through the slice `entities`.
pub fn get_entity_local_transforms(
entities: &[Entity],
output: &mut Vec<ffi::v4::EntityTransformConformal3>,
) {
Self::get_entity_values(
entities,
ffi::v4::EntityValueType::EntityLocalTransformConformal3,
output,
)
.expect("Failed to execute get_entity_values. Invalid arguments.");
}
/// Sets the world transforms of the entities passed through the slice `entities`. The length
/// of the `input` slice needs to be the same as `entities` or a single value in which case
/// it will be applied to all entities.
///
pub fn set_entity_world_transforms(
entities: &[Entity],
input: &[ffi::v4::EntityTransformConformal3],
) {
Self::set_entity_values(
entities,
ffi::v4::EntityValueType::EntityWorldTransformConformal3,
input,
)
.expect("Failed to execute set_entity_values. Invalid arguments.");
}
/// Retrieves the world transforms of the entities passed through the slice `entities`.
pub fn get_entity_world_transforms(
entities: &[Entity],
output: &mut Vec<ffi::v4::EntityTransformConformal3>,
) {
Self::get_entity_values(
entities,
ffi::v4::EntityValueType::EntityWorldTransformConformal3,
output,
)
.expect("Failed to execute get_entity_values. Invalid arguments.");
}
/// Retrieves the state of the local transforms of the entities passed through the slice `entities`.
///
/// It is useful to call this method before [`World::get_entity_local_transforms`] to figure out which
/// entities actually have the value.
pub fn get_entities_state(entities: &[Entity], output: &mut Vec<ffi::v4::EntityStateFlags>) {
Self::get_entity_values(entities, ffi::v4::EntityValueType::EntityStateFlags, output)
.expect("Failed to execute get_entity_values. Invalid arguments.");
}
/// Retrieves the local bounds of the entities passed through the slice `entities`.
pub fn get_entity_local_bounds(entities: &[Entity], output: &mut Vec<ffi::v4::EntityBounds>) {
Self::get_entity_values(
entities,
ffi::v4::EntityValueType::EntityLocalBounds,
output,
)
.expect("Failed to execute get_entity_values. Invalid arguments.");
}
/// Retrieves the physics velocities of the entities passed through the slice `entities`.
pub fn get_entity_physics_velocities(
entities: &[Entity],
output: &mut Vec<ffi::v4::EntityVelocity>,
) {
Self::get_entity_values(
entities,
ffi::v4::EntityValueType::EntityPhysicsVelocity,
output,
)
.expect("Failed to execute get_entity_values. Invalid arguments.");
}
/// Sets the mesh style tints referenced from the entities passed through the slice `entities`. The length
/// of the `input` slice needs to be the same as `entities` or a single value in which case
/// it will be applied to all the entities.
///
/// Note: Will set the tint on the mesh style that is used by this entity. This assumes
/// each entity uses its own `MeshStyle`.
///
pub fn set_entity_render_tints(entities: &[Entity], input: &[ffi::v4::EntityMeshStyleTint]) {
Self::set_entity_values(entities, ffi::v4::EntityValueType::EntityRenderTint, input)
.expect("Failed to execute set_entity_values. Invalid arguments.");
}
/// Sets if rendering is enabled for the entities passed through the slice `entities`. The length
/// of the `input` slice needs to be the same as `entities` or a single value in which case
/// it will be applied to all the entities.
///
pub fn set_entity_render_enables(entities: &[Entity], input: &[ffi::v4::EntityRenderEnable]) {
Self::set_entity_values(
entities,
ffi::v4::EntityValueType::EntityRenderEnable,
input,
)
.expect("Failed to execute set_entity_values. Invalid arguments.");
}
/// Creates a physics body in the middle of the frame
///
/// Takes parameters from the Physics component.
/// Automatically used by the World API in `enable_dynamic` and similar functions.
pub fn create_body_immediate(entity: Entity) {
ffi::v3::create_body_immediate(entity.0);
}
/// Lets you query some properties of a raw mesh, including its bounding volume.
#[inline]
pub fn get_mesh_properties(mesh_handle: DataHandle) -> MeshProperties {
ffi::get_mesh_properties(mesh_handle.as_ffi())
}
/// Lets you query the names of any morph targets associated with the mesh.
///
/// It is recommended to store the return value instead of calling it every frame.
#[inline]
pub fn get_mesh_morph_target_names(mesh_handle: DataHandle) -> Vec<String> {
let count = ffi::get_mesh_morph_target_count(mesh_handle.as_ffi());
let mut retval = Vec::with_capacity(count as usize);
for i in 0..count {
let name_len = ffi::get_mesh_morph_target_name_len(mesh_handle.as_ffi(), i);
let mut v = vec![0; name_len as usize];
ffi::get_mesh_morph_target_name(mesh_handle.as_ffi(), i, &mut v);
retval.push(String::from_utf8(v).unwrap());
}
retval
}
/// Lets you check whether a `DataHandle` points to a valid mesh.
///
/// If it has existed but been destroyed, the return value is false.
pub fn is_valid_mesh(mesh_handle: DataHandle) -> bool {
ffi::is_valid_mesh(mesh_handle.0) != 0
}
/// Casts a ray through the world, returning the first hit.
///
/// You can also use this to do spherecasts (a.k.a sphere sweeps or "thick" raycasts)
/// by setting [`RaycastQuery::spherecast_radius`].
pub fn raycast(raycast: &RaycastQuery) -> Option<RaycastHit> {
let hit = RaycastHit::from_ffi(ffi::v4::spherecast(&raycast.as_ffi()));
hit.entity.is_valid().then_some(hit)
}
/// Casts multiple rays through the world, returning the first hit for each.
/// This is faster than calling `raycast` multiple times.
///
/// You can also use this to do spherecasts (a.k.a sphere sweeps or "thick" raycasts)
/// by setting [`RaycastQuery::spherecast_radius`].
pub fn raycast_batched(
raycasts: impl Iterator<Item = RaycastQuery>,
) -> Vec<Option<RaycastHit>> {
// TODO: This is an annoying amount of unnecessary allocations and copies.
// It would be nicer if we could just pass a [RaycastQuery] directly.
// queries have almost the same memory representation, just a Option<Entity> replaced by a potentially invalid entity.
// Raycast hits have exactly the same memory representation, but we do also wrap them in an Option.
let raycasts_ffi = raycasts.map(|r| r.as_ffi()).collect::<Vec<_>>();
let mut hits_ffi = Vec::<ffi::v3::RaycastHit>::with_capacity(raycasts_ffi.len());
// SAFETY: The host will assign all items, and the ffi types do not have destructors or anything
// Note that if `spherecast_batched` returns an API error then this data may remain uninitialized
// but that's ok because we unwrap the result.
#[allow(clippy::uninit_vec)]
unsafe {
hits_ffi.set_len(raycasts_ffi.len());
}
ffi::v4::spherecast_batched(&raycasts_ffi, &mut hits_ffi);
hits_ffi
.into_iter()
.map(|h| {
let hit = RaycastHit::from_ffi(h);
hit.entity.is_valid().then_some(hit)
})
.collect::<Vec<_>>()
}
/// Casts a ray through the world (SLOW!).
///
/// Reports the first hit or optionally any hit, which can be somewhat faster to compute,
/// but which hit you get is not guaranteed in any way).
///
/// One entity can be excluded to avoid self-hits.
#[deprecated(note = "Use raycast instead")]
pub fn ray_cast(
ray_origin: Vec3,
ray_dir: Vec3,
min_t: f32,
max_t: f32,
any_hit: bool,
exclude_entity: Entity,
) -> Option<(Entity, f32)> {
let ray = ffi::Ray {
origin: ray_origin.into(),
dir: ray_dir.into(),
};
let mut t: f32 = -1.0;
let handle = ffi::ray_cast(
&ray,
min_t,
max_t,
u32::from(any_hit),
&mut t,
exclude_entity.as_ffi(),
);
Entity::try_from_ffi(handle).map(|entity| (entity, t))
}
/// Sends multiple messages to an entity.
/// TODO (nummelin): Should probably batch this like `retrieve_messages`
#[inline]
pub fn send_messages(entity: Entity, messages: &[ffi::Message]) {
ffi::send_messages(
std::mem::size_of::<ffi::Message>() as u32,
&[ffi::MessageRangeAndReceiver {
entity: entity.as_ffi(),
start_index: 0u32,
length: messages.len() as u32,
}],
messages,
);
}
/// Returns a Vec of counts for each entity passed and the total count.
fn get_message_counts(entities: &[Entity]) -> (Vec<u32>, u32) {
let mut message_counts = vec![0u32; entities.len()];
let mut total = 0u32;
ffi::get_message_counts(
std::mem::size_of::<ffi::Message>() as u32,
unsafe { &*(entities as *const [Entity] as *const [ffi::EntityHandle]) },
&mut message_counts,
&mut total,
);
(message_counts, total)
}
/// Returns a list of message counts for each entity and all messages concatenated in the same order.
pub fn retrieve_messages(entities: &[Entity]) -> (Vec<u32>, Vec<ffi::Message>) {
let (counts, total) = Self::get_message_counts(entities);
if total == 0 {
return Default::default();
}
let mut out_messages = vec![ffi::Message::invalid(); total as usize];
ffi::retrieve_messages(
std::mem::size_of::<ffi::Message>() as u32,
unsafe { &*(entities as *const [Entity] as *const [ffi::EntityHandle]) },
&mut out_messages,
);
(counts, out_messages)
}
/// Gets all components an entity has, returns the number of components.
#[inline]
pub fn get_components(entity: Entity, components: &mut [ComponentType]) -> u32 {
ffi::get_components(entity.0, components)
}
/// Gets all components an entity has
pub fn get_components_vec(entity: Entity) -> Vec<ComponentType> {
let mut c = [ComponentType::Invalid; 128];
let size = Self::get_components(entity, &mut c) as usize;
c[0..size].to_vec()
}
/// Creates a new data object from a `CreateDataType` and slice of bytes.
#[inline]
pub fn create_data(create_data_type: CreateDataType, data: &[u8]) -> DataHandle {
DataHandle::from_ffi(ffi::create_data(create_data_type, data))
}
/// Gets data from the data referenced by the handle. Always returns the number of bytes even if the output is empty
/// (so you can allocate the right amount)
#[inline]
pub fn retrieve_data(
data_handle: DataHandle,
retrieve_data_type: ffi::RetrieveDataType,
out_data: &mut [u8],
) -> u32 {
ffi::retrieve_data(data_handle.0, retrieve_data_type, out_data)
}
/// Retrieves data by this `DataHandle` and `RetrieveDataType` as a Vec of bytes.
pub fn retrieve_data_vec(
data_handle: DataHandle,
retrieve_data_type: ffi::RetrieveDataType,
) -> Vec<u8> {
let len = Self::retrieve_data(data_handle, retrieve_data_type, &mut []);
let mut data: Vec<u8> = vec![0u8; len as usize];
let new_len = Self::retrieve_data(data_handle, retrieve_data_type, &mut data);
assert!(new_len == len);
data
}
/// Decreases the ref count of a data object. When ref count reaches zero it is destroyed. Attempting to access the data after this is an error.
#[inline]
pub fn destroy_data(data: DataHandle) {
ffi::destroy_data(data.0);
}
/// Increases the ref count of a data object.
#[inline]
pub fn retain_data(data: DataHandle) {
ffi::retain_data(data.0);
}
/// Lets you check whether a `DataHandle` points to a valid data object. If it has existed
/// but been destroyed, the return value is false.
#[inline]
pub fn is_valid_data(data_handle: DataHandle) -> bool {
ffi::is_valid_data(data_handle.0) != 0
}
/// Sets a debug name of this data object. Useful for debugging memory usage and leaks.
#[inline]
pub fn set_data_debug_name(data_handle: DataHandle, name: &str) {
ffi::v4::set_data_debug_name(data_handle.0, name);
}
/// Gets a debug name of this data object.
#[inline]
pub fn get_data_debug_name(data_handle: DataHandle) -> String {
ffi::v4::get_data_debug_name(data_handle.0)
}
/// Sets debug options of this entity such as whether to enable physics shape debug rendering or not.
/// An empty slice will mean to apply the options to all entities in the world.
#[inline]
pub fn set_entity_debug_options(entities: &[Entity], options: EntityDebugOptions) {
ffi::v4::set_entity_debug_options(
unsafe { &*(entities as *const [Entity] as *const [ffi::EntityHandle]) },
&options,
);
}
pub(crate) fn notify_destroyed(&mut self, entity: Entity) {
self.entities_destroyed_this_frame.push(entity);
}
pub(crate) fn entities_destroyed_this_and_last_frame(&self) -> impl Iterator<Item = &Entity> {
self.entities_destroyed_previous_frame
.iter()
.chain(self.entities_destroyed_this_frame.iter())
}
fn pre_update() {
puffin::profile_function!();
{
let mut instance = Self::instance_mut();
instance.next_frame();
}
EntityArena::global().destroy_pending_entities();
EntityMessenger::get().process_messages();
EntityMessageDispatcher::global().update();
}
fn post_update() {
puffin::profile_function!();
EntityMessenger::get().send_messages();
}
/// Pass your world update function to this. Will make sure
/// messages are received, sent and transient entities are destroyed in
/// the right order.
pub fn update<F: FnOnce()>(f: F) {
puffin::profile_function!();
Self::pre_update();
f();
Self::post_update();
}
/// Measures the layout size of a chunk of formatted text. Formatting is embedded directly
/// into the string using a simple markup language, documented elsewhere.
pub fn measure_formatted_text(formatted_text: &FormattedText) -> MeasuredTextSize {
let mut text_size = MeasuredTextSize {
min_x: 0.0,
min_y: 0.0,
width: 0.0,
height: 0.0,
};
ffi::v4::measure_formatted_text(formatted_text.data.get_data_handle().0, &mut text_size);
text_size
}
/// Get pairs of entities that have collided this frame.
pub fn get_collided_entity_pairs(out: &mut Vec<ffi::EntityPair>) {
let start_len = out.len();
let count = ffi::get_collided_entity_pairs(&mut []);
out.resize(start_len + count as usize, ffi::EntityPair::zeroed());
let count2 = ffi::get_collided_entity_pairs(&mut out[start_len..]);
debug_assert!(count == count2);
}
/// Get collision info for a pairs of entities that have collided this frame.
pub fn get_collision_info(entities: &[ffi::EntityPair], out: &mut Vec<ffi::CollisionInfo>) {
let start_len = out.len();
out.resize(start_len + entities.len(), ffi::CollisionInfo::zeroed());
let num = ffi::get_collision_info(entities, &mut out[start_len..]);
debug_assert!(num <= entities.len().try_into().unwrap());
out.truncate(start_len + num as usize);
}
/// Get collisions that have happened this frame.
/// This is slightly faster than `get_collided_entity_pairs` and `get_collision_info` combined.
pub fn get_all_collision_info(out: &mut Vec<ffi::CollisionInfo>) {
let start_len = out.len();
let num = ffi::get_all_collision_info(&mut []);
out.resize(start_len + num as usize, ffi::CollisionInfo::zeroed());
let num2 = ffi::get_all_collision_info(&mut out[start_len..]);
debug_assert!(num == num2);
out.truncate(start_len + num2 as usize);
}
/// Get all entity pairs that have stopped colliding this frame.
pub fn get_ended_collision_entity_pairs(out_pairs: &mut Vec<ffi::EntityPair>) {
let start_len = out_pairs.len();
let num = ffi::get_ended_collision_entity_pairs(&mut []);
out_pairs.resize(start_len + num as usize, ffi::EntityPair::zeroed());
let num2 = ffi::get_ended_collision_entity_pairs(&mut out_pairs[start_len..]);
debug_assert!(num == num2);
out_pairs.truncate(start_len + num2 as usize);
}
/// Get entity pairs that have started triggering this frame.
pub fn get_started_trigger_entity_pairs(out: &mut Vec<ffi::EntityPair>) {
let start_len = out.len();
let num = ffi::get_started_trigger_entity_pairs(&mut []);
out.resize(start_len + num as usize, ffi::EntityPair::zeroed());
let num2 = ffi::get_started_trigger_entity_pairs(&mut out[start_len..]);
debug_assert!(num == num2);
out.truncate(start_len + num2 as usize);
}
/// Get all entity pairs that have ended triggering this frame.
pub fn get_ended_trigger_entity_pairs(out: &mut Vec<ffi::EntityPair>) {
let start_len = out.len();
let num = ffi::get_ended_trigger_entity_pairs(&mut []);
out.resize(start_len + num as usize, ffi::EntityPair::zeroed());
let num2 = ffi::get_ended_trigger_entity_pairs(&mut out[start_len..]);
debug_assert!(num == num2);
out.truncate(start_len + num2 as usize);
}
}