Skip to content

Instantly share code, notes, and snippets.

@nekomimi-daimao
Created November 28, 2019 21:12
Show Gist options
  • Save nekomimi-daimao/59811f30f633cbd02faf647857c8f41b to your computer and use it in GitHub Desktop.
Save nekomimi-daimao/59811f30f633cbd02faf647857c8f41b to your computer and use it in GitHub Desktop.
Unity Rust multiply_point
fn multiple_float(a: f32, b: f32) -> f64 {
((a as f64) * (b as f64))
}
#[no_mangle]
pub extern fn multiply_point(m: Matrix4x4, v: Vector3) -> Vector3 {
let x = multiple_float(m.m00, v.x) + multiple_float(m.m01, v.y) + multiple_float(m.m02, v.z) + m.m03 as f64;
let y = multiple_float(m.m10, v.x) + multiple_float(m.m11, v.y) + multiple_float(m.m12, v.z) + m.m13 as f64;
let z = multiple_float(m.m20, v.x) + multiple_float(m.m21, v.y) + multiple_float(m.m22, v.z) + m.m23 as f64;
let a = multiple_float(m.m30, v.x) + multiple_float(m.m31, v.y) + multiple_float(m.m32, v.z) + m.m33 as f64;
let num = 1.0 / a;
Vector3 { x: (x * num) as f32, y: (y * num) as f32, z: (z * num) as f32 }
}
#[no_mangle]
pub extern fn multiply_point_r(m: Matrix4x4, v: Vector3) -> Vector3 {
let x = multiple_float(m.m00, v.x) + multiple_float(m.m01, v.y) + multiple_float(m.m02, v.z) + m.m03 as f64;
let y = multiple_float(m.m10, v.x) + multiple_float(m.m11, v.y) + multiple_float(m.m12, v.z) + m.m13 as f64;
let z = multiple_float(m.m20, v.x) + multiple_float(m.m21, v.y) + multiple_float(m.m22, v.z) + m.m23 as f64;
let a = multiple_float(m.m30, v.x) + multiple_float(m.m31, v.y) + multiple_float(m.m32, v.z) + m.m33 as f64;
let num = 1.0 / a;
Vector3 { x: (x * num) as f32, y: (y * num) as f32, z: (z * num) as f32 }
}
#[no_mangle]
pub extern fn multiply_point_without_cast(m: Matrix4x4, v: Vector3) -> Vector3 {
let x = m.m00 * v.x + m.m01 * v.y + m.m02 * v.z + m.m03;
let y = m.m10 * v.x + m.m11 * v.y + m.m12 * v.z + m.m13;
let z = m.m20 * v.x + m.m21 * v.y + m.m22 * v.z + m.m23;
let a = 1.0 / (m.m30 * v.x + m.m31 * v.y + m.m32 * v.z + m.m33);
Vector3 { x: (x * a), y: (y * a), z: (z * a) }
}
#[repr(C)]
pub struct Vector3 {
x: f32,
y: f32,
z: f32,
}
#[repr(C)]
pub struct Matrix4x4 {
m00: f32,
m10: f32,
m20: f32,
m30: f32,
m01: f32,
m11: f32,
m21: f32,
m31: f32,
m02: f32,
m12: f32,
m22: f32,
m32: f32,
m03: f32,
m13: f32,
m23: f32,
m33: f32,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment