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
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::*;