add texture_transform, switch model_transform to Affine3A

This commit is contained in:
Quaternions 2023-09-26 16:56:19 -07:00
parent 31156aadfb
commit 836749df47
5 changed files with 85 additions and 43 deletions

View File

@ -269,12 +269,12 @@ type TreyMesh = Aabb;
pub struct ModelPhysics {
//A model is a thing that has a hitbox. can be represented by a list of TreyMesh-es
//in this iteration, all it needs is extents.
transform: glam::Mat4,
model_transform: glam::Affine3A,
}
impl ModelPhysics {
pub fn new(transform:glam::Mat4) -> Self {
Self{transform}
pub fn new(model_transform:glam::Affine3A) -> Self {
Self{model_transform}
}
pub fn unit_vertices(&self) -> [glam::Vec3;8] {
Aabb::unit_vertices()
@ -282,7 +282,7 @@ impl ModelPhysics {
pub fn mesh(&self) -> TreyMesh {
let mut aabb=Aabb::new();
for &vertex in self.unit_vertices().iter() {
aabb.grow(glam::Vec4Swizzles::xyz(self.transform*vertex.extend(1.0)));
aabb.grow(self.model_transform.transform_point3(vertex));
}
return aabb;
}
@ -303,7 +303,7 @@ impl ModelPhysics {
return aabb;
}
pub fn face_normal(&self,face:TreyMeshFace) -> glam::Vec3 {
glam::Vec4Swizzles::xyz(Aabb::normal(face).extend(0.0))//this is wrong for scale
Aabb::normal(face)//this is wrong for scale
}
}

View File

@ -79,24 +79,21 @@ pub fn generate_modeldatas_roblox(dom:rbx_dom_weak::WeakDom) -> Result<(Vec<Mode
object.properties.get("Shape"),//this will also skip unions
)
{
let model_instance=ModelInstance {
transform:glam::Mat4::from_translation(
let model_transform=glam::Affine3A::from_translation(
glam::Vec3::new(cf.position.x,cf.position.y,cf.position.z)
)
* glam::Mat4::from_mat3(
* glam::Affine3A::from_mat3(
glam::Mat3::from_cols(
glam::Vec3::new(cf.orientation.x.x,cf.orientation.y.x,cf.orientation.z.x),
glam::Vec3::new(cf.orientation.x.y,cf.orientation.y.y,cf.orientation.z.y),
glam::Vec3::new(cf.orientation.x.z,cf.orientation.y.z,cf.orientation.z.z),
),
)
* glam::Mat4::from_scale(
* glam::Affine3A::from_scale(
glam::Vec3::new(size.x,size.y,size.z)/2.0
),
color: glam::vec4(color3.r as f32/255f32, color3.g as f32/255f32, color3.b as f32/255f32, 1.0-*transparency),
};
);
if object.name=="MapStart"{
spawn_point=glam::Vec4Swizzles::xyz(model_instance.transform*glam::Vec3::Y.extend(1.0))+glam::vec3(0.0,2.5,0.0);
spawn_point=model_transform.transform_point3(glam::Vec3::Y)+glam::vec3(0.0,2.5,0.0);
println!("Found MapStart{:?}",spawn_point);
}
if *transparency==1.0||shape.to_u32()!=1 {
@ -105,10 +102,34 @@ pub fn generate_modeldatas_roblox(dom:rbx_dom_weak::WeakDom) -> Result<(Vec<Mode
temp_objects.clear();
recursive_collect_superclass(&mut temp_objects, &dom, object,"Decal");
let mut texture_transform=glam::Affine2::IDENTITY;
let mut i_can_only_load_one_texture_per_model=None;
for &decal_ref in &temp_objects{
if let Some(decal)=dom.get_by_ref(decal_ref){
if let Some(rbx_dom_weak::types::Variant::Content(content)) = decal.properties.get("Texture") {
if decal.class=="Texture"{
//generate tranform
if let (
Some(rbx_dom_weak::types::Variant::Float32(ox)),
Some(rbx_dom_weak::types::Variant::Float32(oy)),
Some(rbx_dom_weak::types::Variant::Float32(sx)),
Some(rbx_dom_weak::types::Variant::Float32(sy)),
) = (
decal.properties.get("OffsetStudsU"),
decal.properties.get("OffsetStudsV"),
decal.properties.get("StudsPerTileU"),
decal.properties.get("StudsPerTileV"),
)
{
//pretend we don't need to know the face
texture_transform=glam::Affine2::from_translation(
glam::vec2(*ox/size.x,*oy/size.y)
)
*glam::Affine2::from_scale(
glam::vec2(*sx/size.x,*sy/size.y)
);
}
}
if let Ok(asset_id)=content.clone().into_string().parse::<RobloxAssetId>(){
if let Some(&texture_id)=texture_id_from_asset_id.get(&asset_id.0){
i_can_only_load_one_texture_per_model=Some(texture_id);
@ -125,6 +146,11 @@ pub fn generate_modeldatas_roblox(dom:rbx_dom_weak::WeakDom) -> Result<(Vec<Mode
}
}
}
let model_instance=ModelInstance {
model_transform,
texture_transform,
color: glam::vec4(color3.r as f32/255f32, color3.g as f32/255f32, color3.b as f32/255f32, 1.0-*transparency),
};
match i_can_only_load_one_texture_per_model{
//push to existing texture model
Some(texture_id)=>modeldatas[(texture_id+1) as usize].instances.push(model_instance),

View File

@ -164,7 +164,7 @@ impl GraphicsData {
fn generate_model_physics(&mut self,modeldatas:&Vec<ModelData>){
self.physics.models.append(&mut modeldatas.iter().map(|m|
//make aabb and run vertices to get realistic bounds
m.instances.iter().map(|t|body::ModelPhysics::new(t.transform))
m.instances.iter().map(|t|body::ModelPhysics::new(t.model_transform))
).flatten().collect());
println!("Physics Objects: {}",self.physics.models.len());
}
@ -285,12 +285,19 @@ impl GraphicsData {
}
}
fn get_instances_buffer_data(instances:&Vec<ModelInstance>) -> Vec<f32> {
const SIZE: usize=4*4+4;//let size=std::mem::size_of::<ModelInstance>();
let mut raw = Vec::with_capacity(SIZE*instances.len());
const MODEL_BUFFER_SIZE:usize=4*4 + 2*2 + 2+2 + 4;//let size=std::mem::size_of::<ModelInstance>();
fn get_instances_buffer_data(instances:&[ModelInstance]) -> Vec<f32> {
let mut raw = Vec::with_capacity(MODEL_BUFFER_SIZE*instances.len());
for (i,mi) in instances.iter().enumerate(){
let mut v = raw.split_off(SIZE*i);
raw.extend_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&mi.transform)[..]);
let mut v = raw.split_off(MODEL_BUFFER_SIZE*i);
//model_transform
raw.extend_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&glam::Mat4::from(mi.model_transform))[..]);
//texture_matrix
raw.extend_from_slice(&AsRef::<[f32; 2*2]>::as_ref(&mi.texture_transform.matrix2)[..]);
//texture_offset
raw.extend_from_slice(AsRef::<[f32; 2]>::as_ref(&mi.texture_transform.translation));
raw.extend_from_slice(&[0.0,0.0]);
//color
raw.extend_from_slice(AsRef::<[f32; 4]>::as_ref(&mi.color));
raw.append(&mut v);
}
@ -320,34 +327,41 @@ impl framework::Example for GraphicsData {
modeldatas.append(&mut model::generate_modeldatas(unit_cube.clone(),ModelData::COLOR_FLOATS_WHITE));
println!("models.len = {:?}", modeldatas.len());
modeldatas[0].instances.push(ModelInstance{
transform:glam::Mat4::from_translation(glam::vec3(10.,0.,-10.)),
model_transform:glam::Affine3A::from_translation(glam::vec3(10.,0.,-10.)),
texture_transform:glam::Affine2::IDENTITY,
color:ModelData::COLOR_VEC4_WHITE,
});
//quad monkeys
modeldatas[1].instances.push(ModelInstance{
transform:glam::Mat4::from_translation(glam::vec3(10.,5.,10.)),
model_transform:glam::Affine3A::from_translation(glam::vec3(10.,5.,10.)),
texture_transform:glam::Affine2::IDENTITY,
color:ModelData::COLOR_VEC4_WHITE,
});
modeldatas[1].instances.push(ModelInstance{
transform:glam::Mat4::from_translation(glam::vec3(20.,5.,10.)),
model_transform:glam::Affine3A::from_translation(glam::vec3(20.,5.,10.)),
texture_transform:glam::Affine2::IDENTITY,
color:glam::vec4(1.0,0.0,0.0,1.0),
});
modeldatas[1].instances.push(ModelInstance{
transform:glam::Mat4::from_translation(glam::vec3(10.,5.,20.)),
model_transform:glam::Affine3A::from_translation(glam::vec3(10.,5.,20.)),
texture_transform:glam::Affine2::IDENTITY,
color:glam::vec4(0.0,1.0,0.0,1.0),
});
modeldatas[1].instances.push(ModelInstance{
transform:glam::Mat4::from_translation(glam::vec3(20.,5.,20.)),
model_transform:glam::Affine3A::from_translation(glam::vec3(20.,5.,20.)),
texture_transform:glam::Affine2::IDENTITY,
color:glam::vec4(0.0,0.0,1.0,1.0),
});
//teapot
modeldatas[2].instances.push(ModelInstance{
transform:glam::Mat4::from_translation(glam::vec3(-10.,5.,10.)),
model_transform:glam::Affine3A::from_translation(glam::vec3(-10.,5.,10.)),
texture_transform:glam::Affine2::IDENTITY,
color:ModelData::COLOR_VEC4_WHITE,
});
//ground
modeldatas[3].instances.push(ModelInstance{
transform:glam::Mat4::from_translation(glam::vec3(0.,0.,0.))*glam::Mat4::from_scale(glam::vec3(160.0, 1.0, 160.0)),
model_transform:glam::Affine3A::from_translation(glam::vec3(0.,0.,0.))*glam::Affine3A::from_scale(glam::vec3(160.0, 1.0, 160.0)),
texture_transform:glam::Affine2::IDENTITY,
color:ModelData::COLOR_VEC4_WHITE,
});

View File

@ -7,10 +7,10 @@ pub struct Vertex {
pub normal: [f32; 3],
pub color: [f32; 4],
}
#[derive(Clone)]
pub struct ModelInstance {
pub transform: glam::Mat4,
pub model_transform: glam::Affine3A,
pub texture_transform: glam::Affine2,
pub color: glam::Vec4,
}

View File

@ -42,8 +42,10 @@ fn vs_sky(@builtin(vertex_index) vertex_index: u32) -> SkyOutput {
}
struct ModelInstance{
transform:mat4x4<f32>,
//texture_transform:mat3x3<f32>,
model_transform:mat4x4<f32>,
//simple to save 4 bytes, so do it. above would need to be spit out into components.
texture_matrix:mat2x2<f32>,
texture_offset:vec2<f32>,
color:vec4<f32>,
}
//my fancy idea is to create a megatexture for each model that includes all the textures each intance will need
@ -75,11 +77,11 @@ fn vs_entity_texture(
@location(2) normal: vec3<f32>,
@location(3) color: vec4<f32>,
) -> EntityOutputTexture {
var position: vec4<f32> = model_instances[instance].transform * vec4<f32>(pos, 1.0);
var position: vec4<f32> = model_instances[instance].model_transform * vec4<f32>(pos, 1.0);
var result: EntityOutputTexture;
result.normal = (model_instances[instance].transform * vec4<f32>(normal, 0.0)).xyz;
result.texture=texture;//(model_instances[instance].texture_transform * vec3<f32>(texture, 1.0)).xy;
result.color=model_instances[instance].color * color;
result.normal = (model_instances[instance].model_transform * vec4<f32>(normal, 0.0)).xyz;
result.texture = model_instances[instance].texture_matrix * texture + model_instances[instance].texture_offset;
result.color = model_instances[instance].color * color;
result.view = position.xyz - camera.cam_pos.xyz;
result.position = camera.proj * camera.view * position;
return result;