This commit is contained in:
Quaternions 2024-02-25 23:25:40 -08:00
parent ef6ae30394
commit 1ac594f195

View File

@ -25,11 +25,12 @@ fn main()->Result<(),anyhow::Error>{
} }
} }
struct Vector3(glam::Vec3); #[derive(Clone,Copy)]
struct Vector3(glam::Vec3A);
impl Vector3{ impl Vector3{
fn new(x:f32,y:f32,z:f32)->Self{ fn new(x:f32,y:f32,z:f32)->Self{
Self(glam::vec3(x,y,z)) Self(glam::vec3a(x,y,z))
} }
} }
@ -56,46 +57,54 @@ impl mlua::UserData for Vector3{
fn add_methods<'lua,M: mlua::UserDataMethods<'lua,Self>>(methods:&mut M){ fn add_methods<'lua,M: mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
//methods.add_method("area",|_,this,()| Ok(this.length * this.width)); //methods.add_method("area",|_,this,()| Ok(this.length * this.width));
//methods.add_meta_method(mlua::MetaMethod::Add, |_, this, val:&Self| Ok(Self(this.0+val.0))); methods.add_meta_function(mlua::MetaMethod::Add,|_,(this,val):(Self,Self)|Ok(Self(this.0+val.0)));
} }
} }
impl<'lua> mlua::FromLua<'lua> for &Vector3{ impl<'lua> mlua::FromLua<'lua> for Vector3{
fn from_lua(value:mlua::prelude::LuaValue<'lua>,lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{ fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
value.as_userdata() match value {
.ok_or(mlua::prelude::LuaError::FromLuaConversionError{from:"LuaValue",to:"UserData",message:None})? mlua::Value::UserData(ud) => Ok(*ud.borrow::<Self>()?),
.user_value() _ => unreachable!(),
}
} }
} }
struct CFrame{ #[derive(Clone,Copy)]
translation:glam::Vec3, struct CFrame(glam::Affine3A);
rotation:glam::Mat3,
}
impl CFrame{ impl CFrame{
fn new(x:f32,y:f32,z:f32)->Self{ fn new(x:f32,y:f32,z:f32)->Self{
Self{ Self(glam::Affine3A::from_translation(glam::vec3(x,y,z)))
translation:glam::vec3(x,y,z),
rotation:glam::Mat3::IDENTITY,
} }
fn angles(x:f32,y:f32,z:f32)->Self{
Self(glam::Affine3A::from_mat3(glam::Mat3::from_euler(glam::EulerRot::YXZ,y,x,z)))
} }
} }
impl mlua::UserData for CFrame{ impl mlua::UserData for CFrame{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){ fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
fields.add_field_method_get("p", |ctx, this| Ok(Vector3(this.translation))); //CFrame.p
fields.add_field_method_get("p",|_,this|Ok(Vector3(this.0.translation)));
} }
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){ fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
//methods.add_method("area",|_,this,()|Ok(this.length*this.width)); //methods.add_method("area",|_,this,()|Ok(this.length*this.width));
methods.add_meta_method(mlua::MetaMethod::Mul, |_, this, val:&Vector3| Ok(Vector3(this.rotation*val.0+this.translation))); //methods.add_meta_method(mlua::MetaMethod::Mul,|_,this,val:&Vector3|Ok(Vector3(this.0.matrix3*val.0+this.0.translation)));
methods.add_meta_function(mlua::MetaMethod::Mul,|_,(this,val):(Self,Self)|Ok(Self(this.0*val.0)));
}
}
impl<'lua> mlua::FromLua<'lua> for CFrame{
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
match value {
mlua::Value::UserData(ud) => Ok(*ud.borrow::<Self>()?),
_ => unreachable!(),
}
} }
} }
fn bake(script_path:std::path::PathBuf)->Result<(),anyhow::Error>{ fn bake(script_path:std::path::PathBuf)->Result<(),anyhow::Error>{
//read the file //read the file
let script={ let script={
let mut file=std::fs::File::open(script_path)?; let mut file=std::fs::File::open(script_path)?;
@ -106,35 +115,55 @@ fn bake(script_path:std::path::PathBuf)->Result<(),anyhow::Error>{
let lua=mlua::Lua::new(); let lua=mlua::Lua::new();
//global environment
let globals=lua.globals();
//Vector3 //Vector3
let vector3=lua.create_table()?; {
let vector3_table=lua.create_table()?;
let new_vec=lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|{ //Vector3.new
let v=ctx.create_userdata(Vector3::new(x,y,z)); vector3_table.raw_set("new",
Ok(v) lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|
})?; Ok(ctx.create_userdata(Vector3::new(x,y,z)))
)?
)?;
vector3.raw_set("new",new_vec)?; globals.set("Vector3",vector3_table)?;
}
lua.globals().set("Vector3",vector3)?;
//CFrame //CFrame
let cframe=lua.create_table()?; {
let cframe_table=lua.create_table()?;
let new_cf=lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|{ //CFrame.new
let v=ctx.create_userdata(CFrame::new(x,y,z)); cframe_table.raw_set("new",
Ok(v) lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|
})?; Ok(ctx.create_userdata(CFrame::new(x,y,z)))
)?
)?;
cframe.raw_set("new",new_cf)?; //CFrame.Angles
cframe_table.raw_set("Angles",
lua.create_function(|ctx,(x,y,z):(f32,f32,f32)|
Ok(ctx.create_userdata(CFrame::angles(x,y,z)))
)?
)?;
lua.globals().set("CFrame",cframe)?; globals.set("CFrame",cframe_table)?;
}
//script
{
let script_table=lua.create_table()?;
//script.Name
script_table.raw_set("Name","Placeholder")?;
globals.set("script",script_table)?;
}
match lua.load(script).exec(){ match lua.load(script).exec(){
Ok(())=>(), Ok(())=>(),
Err(e)=>{ Err(e)=>println!("Lua Error: {e}"),
println!("Error: {e}");
}
} }
Ok(()) Ok(())