ark-api-ffi 0.16.0

Ark low-level Wasm FFI API
Documentation
define_api_id!(0x1d69_ec55_ed04_461d, "world-v3");

pub use crate::world_v0::BoneWeightFlags;
pub use crate::world_v0::EntityHandle;
pub use crate::world_v0::MeshHandle;
pub use crate::world_v0::Ray;
pub use crate::world_v0::ENTITY_HANDLE_INVALID;
use crate::FFIResult;
use bytemuck::Pod;
use bytemuck::Zeroable;

#[ark_api_macros::ark_bindgen(imports = "ark-world-v3")]
mod world {
    use super::*;

    #[derive(Copy, Clone, Default, Debug, Pod, Zeroable)]
    #[repr(C)]
    pub struct RaycastQuery {
        pub ray: Ray,
        pub layer_mask: u64,
        pub ignore_entity: EntityHandle,
        pub max_distance: f32,
        pub _pad: u32,
    }

    #[derive(Copy, Clone, Default, Debug, Pod, Zeroable)]
    #[repr(C)]
    pub struct RaycastHit {
        pub point: [f32; 3],

        pub normal: [f32; 3],

        /// Entity that was hit.
        /// If nothing was hit then EntityHandle will be invalid
        pub entity: EntityHandle,

        /// 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,
        pub _pad: u32,
    }

    impl RaycastHit {
        pub fn invalid() -> Self {
            Self {
                point: [std::f32::NAN; 3],
                normal: [std::f32::NAN; 3],
                entity: ENTITY_HANDLE_INVALID,
                distance: std::f32::NAN,
                _pad: 0,
            }
        }
    }

    /// Describes the transform of a bone. More compact and more defined than a Mat4.
    ///
    /// Currently uses a pos + quat representation, although this may changed in the future.
    /// Aligned to 16 bytes to be able to copy these directly to GPU.
    #[derive(Copy, Clone, Debug, Pod, Zeroable)]
    #[repr(C, align(16))]
    pub struct BoneTransform {
        pub rot: [f32; 4],
        pub pos: [f32; 3],
        pub _pad: u32,
    }

    /// WorldData description type used when initializing an SdfProgram WorldData
    #[derive(Debug, Copy, Clone, Pod, Zeroable)]
    #[repr(C)]
    pub struct SdfProgramDesc {
        pub num_opcodes: u32,
        pub opcodes_ptr: u32,
        pub num_constants: u32,
        pub constants_ptr: u32,
    }

    /// WorldData description type used when initializing an SdfSkin WorldData
    #[derive(Debug, Copy, Clone, Pod, Zeroable)]
    #[repr(C)]
    pub struct SdfSkinDesc {
        pub base_sdf_entity: EntityHandle,
        pub num_bone_entities: u32,
        pub bone_entities_ptr: u32,
        pub num_rest_from_shape: u32,
        pub rest_from_shape_ptr: u32,
    }

    /// WorldData description type used when initializing an SdfSkin WorldData.
    /// With an extra field for mesh replacement.
    #[derive(Debug, Copy, Clone, Pod, Zeroable)]
    #[repr(C)]
    pub struct SdfSkinDesc2 {
        pub base_sdf_entity: EntityHandle,
        pub num_bone_entities: u32,
        pub bone_entities_ptr: u32,
        pub num_rest_from_shape: u32,
        pub rest_from_shape_ptr: u32,
        pub replacement_mesh: MeshHandle,
        pub bone_weight_flags: BoneWeightFlags,
        pub bone_weight_bias: f32,
        pub bone_weight_falloff: f32,
        pub reserved0: u32,
    }

    #[derive(Debug, Copy, Clone, Pod, Zeroable)]
    #[repr(C)]
    pub struct MorphTargetDesc {
        pub num_weights: u32,
        pub weights_ptr: u32,
        pub names_ptr: u32,
    }

    #[derive(Debug, Copy, Clone, Pod, Zeroable)]
    #[repr(C)]
    pub struct WorldMaterialsDesc {
        pub materials_len: u32,
        pub materials_ptr: u32,
    }

    extern "C" {
        /// Returns whether the entity `a` is physically in contact with the entity `b`.
        ///
        /// If b is invalid, checks whether `a` is in contact with any other handle.
        #[deprecated_infallible]
        pub fn entities_in_contact(a: EntityHandle, b: EntityHandle) -> bool;

        /// Returns the number of entities the entity `a` is in contact with.
        #[deprecated_infallible]
        pub fn entity_contact_count(a: EntityHandle) -> u32;

        /// Writes the entities in contact to the provided array. The actual count is returned.
        #[deprecated_infallible]
        pub fn retrieve_entities_in_contact(a: EntityHandle, entities: &mut [EntityHandle]) -> u32;

        #[deprecated_infallible]
        pub fn raycast(raycast: &RaycastQuery) -> RaycastHit;

        #[deprecated_infallible]
        pub fn raycast_batched(raycasts: &[RaycastQuery], hits: &mut [RaycastHit]);

        /// Makes it possible to create rigid bodies in the middle of a frame.

        /// Will use all the parameters already set up on the Physics component, just like the
        /// "old" way.
        #[deprecated_infallible]
        pub fn create_body_immediate(e: EntityHandle);

        /// Writes directly to the collision matrix.
        #[deprecated_infallible]
        pub fn set_collision_matrix_rows(first_row: u32, rows: &[u64]);

        /// Creates a mesh from an included GLTF file, from the module directory.
        #[deprecated(note = "use `create_data` instead")] // since 2021-10-30
        pub fn create_mesh_from_gltf(gltf_data: &[u8], buffer_data: &[u8])
            -> FFIResult<MeshHandle>;
    }
}

pub use world::*;