|
1 | 1 | use bevy::{ |
2 | | - math::{Vec2, Vec3, Vec4}, |
| 2 | + math::{Affine3A, Mat4, Vec2, Vec3, Vec4}, |
3 | 3 | prelude::Entity, |
4 | 4 | render::render_resource::{Extent3d, TextureFormat}, |
5 | 5 | }; |
@@ -2013,6 +2013,202 @@ pub extern "C" fn processing_transform_reset(entity_id: u64) { |
2013 | 2013 | error::check(|| transform_reset(entity)); |
2014 | 2014 | } |
2015 | 2015 |
|
| 2016 | +/// Attach an orbit camera controller. |
| 2017 | +#[unsafe(no_mangle)] |
| 2018 | +pub extern "C" fn processing_orbit_camera(graphics_id: u64) { |
| 2019 | + error::clear_error(); |
| 2020 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2021 | + error::check(|| graphics_orbit_camera(graphics_entity)); |
| 2022 | +} |
| 2023 | + |
| 2024 | +/// Attach a free-flight camera controller. |
| 2025 | +#[unsafe(no_mangle)] |
| 2026 | +pub extern "C" fn processing_free_camera(graphics_id: u64) { |
| 2027 | + error::clear_error(); |
| 2028 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2029 | + error::check(|| graphics_free_camera(graphics_entity)); |
| 2030 | +} |
| 2031 | + |
| 2032 | +/// Attach a pan/zoom camera controller. |
| 2033 | +#[unsafe(no_mangle)] |
| 2034 | +pub extern "C" fn processing_pan_camera(graphics_id: u64) { |
| 2035 | + error::clear_error(); |
| 2036 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2037 | + error::check(|| graphics_pan_camera(graphics_entity)); |
| 2038 | +} |
| 2039 | + |
| 2040 | +/// Remove the active camera controller. |
| 2041 | +#[unsafe(no_mangle)] |
| 2042 | +pub extern "C" fn processing_disable_camera_controller(graphics_id: u64) { |
| 2043 | + error::clear_error(); |
| 2044 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2045 | + error::check(|| graphics_disable_camera_controller(graphics_entity)); |
| 2046 | +} |
| 2047 | + |
| 2048 | +/// Set the camera distance from its center (zoom). |
| 2049 | +#[unsafe(no_mangle)] |
| 2050 | +pub extern "C" fn processing_camera_set_distance(graphics_id: u64, distance: f32) { |
| 2051 | + error::clear_error(); |
| 2052 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2053 | + error::check(|| camera_set_distance(graphics_entity, distance)); |
| 2054 | +} |
| 2055 | + |
| 2056 | +/// Set the orbit camera's look-at center. |
| 2057 | +#[unsafe(no_mangle)] |
| 2058 | +pub extern "C" fn processing_camera_set_center(graphics_id: u64, x: f32, y: f32, z: f32) { |
| 2059 | + error::clear_error(); |
| 2060 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2061 | + error::check(|| camera_set_center(graphics_entity, Vec3::new(x, y, z))); |
| 2062 | +} |
| 2063 | + |
| 2064 | +/// Set the minimum camera distance. |
| 2065 | +#[unsafe(no_mangle)] |
| 2066 | +pub extern "C" fn processing_camera_set_min_distance(graphics_id: u64, min: f32) { |
| 2067 | + error::clear_error(); |
| 2068 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2069 | + error::check(|| camera_set_min_distance(graphics_entity, min)); |
| 2070 | +} |
| 2071 | + |
| 2072 | +/// Set the maximum camera distance. |
| 2073 | +#[unsafe(no_mangle)] |
| 2074 | +pub extern "C" fn processing_camera_set_max_distance(graphics_id: u64, max: f32) { |
| 2075 | + error::clear_error(); |
| 2076 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2077 | + error::check(|| camera_set_max_distance(graphics_entity, max)); |
| 2078 | +} |
| 2079 | + |
| 2080 | +/// Set the camera controller's sensitivity. |
| 2081 | +#[unsafe(no_mangle)] |
| 2082 | +pub extern "C" fn processing_camera_set_speed(graphics_id: u64, speed: f32) { |
| 2083 | + error::clear_error(); |
| 2084 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2085 | + error::check(|| camera_set_speed(graphics_entity, speed)); |
| 2086 | +} |
| 2087 | + |
| 2088 | +/// Reset the camera controller to its initial pose. |
| 2089 | +#[unsafe(no_mangle)] |
| 2090 | +pub extern "C" fn processing_camera_reset(graphics_id: u64) { |
| 2091 | + error::clear_error(); |
| 2092 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2093 | + error::check(|| camera_reset(graphics_entity)); |
| 2094 | +} |
| 2095 | + |
| 2096 | +/// Position the camera at eye, looking at center with the given up. |
| 2097 | +/// |
| 2098 | +/// An active camera controller overrides this each frame. |
| 2099 | +#[unsafe(no_mangle)] |
| 2100 | +pub extern "C" fn processing_camera( |
| 2101 | + graphics_id: u64, |
| 2102 | + eye_x: f32, |
| 2103 | + eye_y: f32, |
| 2104 | + eye_z: f32, |
| 2105 | + center_x: f32, |
| 2106 | + center_y: f32, |
| 2107 | + center_z: f32, |
| 2108 | + up_x: f32, |
| 2109 | + up_y: f32, |
| 2110 | + up_z: f32, |
| 2111 | +) { |
| 2112 | + error::clear_error(); |
| 2113 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2114 | + error::check(|| { |
| 2115 | + graphics_camera( |
| 2116 | + graphics_entity, |
| 2117 | + Vec3::new(eye_x, eye_y, eye_z), |
| 2118 | + Vec3::new(center_x, center_y, center_z), |
| 2119 | + Vec3::new(up_x, up_y, up_z), |
| 2120 | + ) |
| 2121 | + }); |
| 2122 | +} |
| 2123 | + |
| 2124 | +/// A column-major 4x4 matrix. |
| 2125 | +#[repr(C)] |
| 2126 | +pub struct Matrix { |
| 2127 | + pub m: [f32; 16], |
| 2128 | +} |
| 2129 | + |
| 2130 | +/// Right-multiply the model matrix by a column-major 4x4 matrix. |
| 2131 | +/// |
| 2132 | +/// # Safety |
| 2133 | +/// - matrix points to at least 16 f32. |
| 2134 | +#[unsafe(no_mangle)] |
| 2135 | +pub unsafe extern "C" fn processing_apply_matrix(graphics_id: u64, matrix: *const f32) { |
| 2136 | + error::clear_error(); |
| 2137 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2138 | + error::check(|| { |
| 2139 | + // SAFETY: Caller guarantees matrix points to 16 valid f32 elements |
| 2140 | + let cols: [f32; 16] = unsafe { std::slice::from_raw_parts(matrix, 16) } |
| 2141 | + .try_into() |
| 2142 | + .unwrap(); |
| 2143 | + let affine = Affine3A::from_mat4(Mat4::from_cols_array(&cols)); |
| 2144 | + graphics_record_command(graphics_entity, DrawCommand::ApplyMatrix(affine)) |
| 2145 | + }); |
| 2146 | +} |
| 2147 | + |
| 2148 | +/// The current model matrix, column-major. Flushes pending draws; identity on error. |
| 2149 | +#[unsafe(no_mangle)] |
| 2150 | +pub extern "C" fn processing_get_matrix(graphics_id: u64) -> Matrix { |
| 2151 | + error::clear_error(); |
| 2152 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2153 | + let m = error::check(|| graphics_get_matrix(graphics_entity).map(|mat| mat.to_cols_array())) |
| 2154 | + .unwrap_or_else(|| Mat4::IDENTITY.to_cols_array()); |
| 2155 | + Matrix { m } |
| 2156 | +} |
| 2157 | + |
| 2158 | +/// Model-space point to world-space X (modelX). |
| 2159 | +#[unsafe(no_mangle)] |
| 2160 | +pub extern "C" fn processing_model_x(graphics_id: u64, x: f32, y: f32, z: f32) -> f32 { |
| 2161 | + error::clear_error(); |
| 2162 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2163 | + error::check(|| graphics_model_point(graphics_entity, Vec3::new(x, y, z)).map(|p| p.x)) |
| 2164 | + .unwrap_or(0.0) |
| 2165 | +} |
| 2166 | + |
| 2167 | +/// Model-space point to world-space Y (modelY). |
| 2168 | +#[unsafe(no_mangle)] |
| 2169 | +pub extern "C" fn processing_model_y(graphics_id: u64, x: f32, y: f32, z: f32) -> f32 { |
| 2170 | + error::clear_error(); |
| 2171 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2172 | + error::check(|| graphics_model_point(graphics_entity, Vec3::new(x, y, z)).map(|p| p.y)) |
| 2173 | + .unwrap_or(0.0) |
| 2174 | +} |
| 2175 | + |
| 2176 | +/// Model-space point to world-space Z (modelZ). |
| 2177 | +#[unsafe(no_mangle)] |
| 2178 | +pub extern "C" fn processing_model_z(graphics_id: u64, x: f32, y: f32, z: f32) -> f32 { |
| 2179 | + error::clear_error(); |
| 2180 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2181 | + error::check(|| graphics_model_point(graphics_entity, Vec3::new(x, y, z)).map(|p| p.z)) |
| 2182 | + .unwrap_or(0.0) |
| 2183 | +} |
| 2184 | + |
| 2185 | +/// Model-space point to screen X in pixels (screenX). |
| 2186 | +#[unsafe(no_mangle)] |
| 2187 | +pub extern "C" fn processing_screen_x(graphics_id: u64, x: f32, y: f32, z: f32) -> f32 { |
| 2188 | + error::clear_error(); |
| 2189 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2190 | + error::check(|| graphics_screen_point(graphics_entity, Vec3::new(x, y, z)).map(|p| p.x)) |
| 2191 | + .unwrap_or(0.0) |
| 2192 | +} |
| 2193 | + |
| 2194 | +/// Model-space point to screen Y in pixels (screenY). |
| 2195 | +#[unsafe(no_mangle)] |
| 2196 | +pub extern "C" fn processing_screen_y(graphics_id: u64, x: f32, y: f32, z: f32) -> f32 { |
| 2197 | + error::clear_error(); |
| 2198 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2199 | + error::check(|| graphics_screen_point(graphics_entity, Vec3::new(x, y, z)).map(|p| p.y)) |
| 2200 | + .unwrap_or(0.0) |
| 2201 | +} |
| 2202 | + |
| 2203 | +/// Model-space point to screen depth in [0,1] (screenZ). |
| 2204 | +#[unsafe(no_mangle)] |
| 2205 | +pub extern "C" fn processing_screen_z(graphics_id: u64, x: f32, y: f32, z: f32) -> f32 { |
| 2206 | + error::clear_error(); |
| 2207 | + let graphics_entity = Entity::from_bits(graphics_id); |
| 2208 | + error::check(|| graphics_screen_point(graphics_entity, Vec3::new(x, y, z)).map(|p| p.z)) |
| 2209 | + .unwrap_or(0.0) |
| 2210 | +} |
| 2211 | + |
2016 | 2212 | pub const PROCESSING_ATTR_FORMAT_FLOAT: u8 = 1; |
2017 | 2213 | pub const PROCESSING_ATTR_FORMAT_FLOAT2: u8 = 2; |
2018 | 2214 | pub const PROCESSING_ATTR_FORMAT_FLOAT3: u8 = 3; |
|
0 commit comments