Skip to content

Commit

Permalink
skins with broken materials
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzart committed Oct 18, 2023
1 parent 2b6cac2 commit a9104cf
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 70 deletions.
1 change: 1 addition & 0 deletions bff-gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum Artifact {
channels: u16,
},
Mesh(Arc<CpuModel>),
Skin(Arc<CpuModel>),
}

fn main() -> Result<(), eframe::Error> {
Expand Down
3 changes: 3 additions & 0 deletions bff-gui/src/panels/central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ pub fn view(
Artifact::Mesh(model) => {
ui.add(MeshView::new(Arc::clone(model)));
}
Artifact::Skin(skin) => {
ui.add(MeshView::new(Arc::clone(skin)));
}
}
}
}
Expand Down
255 changes: 201 additions & 54 deletions bff-gui/src/panels/left.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ pub fn resource_list(
serde_json::to_string_pretty::<Class>(&class)
.unwrap(),
);
if let Some(a) = create_artifact(class, &resource.name)
if let Some(a) =
create_artifact(bigfile, class, &resource.name)
{
response.artifact_created = Some(a);
}
Expand All @@ -108,7 +109,7 @@ pub fn resource_list(
response
}

fn create_artifact(class: Class, resource_name: &Name) -> Option<Artifact> {
fn create_artifact(bigfile: &BigFile, class: Class, resource_name: &Name) -> Option<Artifact> {
match class {
Class::Bitmap(box_bitmap) => match *box_bitmap {
bff::class::bitmap::Bitmap::BitmapV1_291_03_06PC(bitmap) => {
Expand Down Expand Up @@ -149,75 +150,221 @@ fn create_artifact(class: Class, resource_name: &Name) -> Option<Artifact> {
channels,
})
}
Class::Mesh(box_mesh) => {
match *box_mesh {
bff::class::mesh::Mesh::MeshV1_291_03_06PC(mesh) => {
let (positions, (uvs, (normals, tangents))): (Vec<Vec3>, (Vec<Vec2>, (Vec<Vec3>, Vec<Vec4>))) = mesh.body.mesh_buffer.vertex_buffers.iter().flat_map(|buf| &buf.vertex_structs).map(|vs| match vs {
bff::class::mesh::v1_291_03_06_pc::VertexStruct::VertexStruct24 {
position, uv, ..
} => (position, uv, &[0u8; 3], [0u8; 4]),
bff::class::mesh::v1_291_03_06_pc::VertexStruct::VertexStruct36 {
position,
uv,
normal,
tangent,
tangent_padding,
..
Class::Mesh(box_mesh) => match get_mesh(*box_mesh) {
Some(tri_meshes) => {
let primitives = tri_meshes
.into_iter()
.map(|m| {
let triangles = three_d_asset::geometry::Geometry::Triangles(m);
three_d_asset::Primitive {
name: "mesh".to_string(),
transformation: three_d_asset::Mat4::from_translation([0.0; 3].into()),
animations: vec![],
geometry: triangles,
material_index: None,
}
| bff::class::mesh::v1_291_03_06_pc::VertexStruct::VertexStruct48 {
position,
uv,
normal,
tangent,
tangent_padding,
..
})
.collect();
let model = CpuModel {
name: resource_name.to_string(),
geometries: primitives,
materials: vec![],
};
Some(Artifact::Mesh(Arc::new(model)))
}
None => None,
},
Class::Skin(box_skin) => match *box_skin {
bff::class::skin::Skin::SkinV1_291_03_06PC(skin) => {
let tri_meshes: Vec<three_d_asset::Primitive> = skin
.body
.mesh_crc32s
.iter()
.flat_map(|n| {
let res = bigfile.objects.get(n).unwrap();
let class: Class = res
.try_into_version_platform(
bigfile.manifest.version.clone(),
bigfile.manifest.platform,
)
.unwrap();
match class {
Class::Mesh(box_mesh) => get_mesh(*box_mesh).unwrap(),
_ => panic!("not a mesh?"),
}
})
.enumerate()
.map(|(i, mesh)| {
let triangles = three_d_asset::geometry::Geometry::Triangles(mesh);
three_d_asset::Primitive {
name: format!("skin-part{}", i),
transformation: three_d_asset::Mat4::from_translation([0.0; 3].into()),
animations: vec![],
geometry: triangles,
material_index: Some(i),
}
| bff::class::mesh::v1_291_03_06_pc::VertexStruct::VertexStruct60 {
position,
uv,
normal,
tangent,
tangent_padding,
..
} => (position, uv, normal, [&tangent[..], &[*tangent_padding]].concat().try_into().unwrap()),
bff::class::mesh::v1_291_03_06_pc::VertexStruct::VertexStructUnknown { .. } => {
(&[0f32; 3], &[0f32; 2], &[0u8; 3], [0u8; 4])
})
.collect();
let materials = skin
.body
.skin_sections
.iter()
.flat_map(|section| &section.skin_sub_sections.inner)
.enumerate()
.map(|(i, subsection)| {
if let Some(res) = bigfile.objects.get(&subsection.material_crc32) {
let class: Class = res
.try_into_version_platform(
bigfile.manifest.version.clone(),
bigfile.manifest.platform,
)
.unwrap();
match class {
Class::Material(box_material) => match *box_material {
bff::class::material::Material::MaterialV1_291_03_06PC(
material,
) => three_d::renderer::material::CpuMaterial {
name: format!("{}-mat{}", subsection.material_crc32, i),
albedo: material.body.diffuse_color.into(),
emissive: material.body.emissive_color.into(),
..Default::default()
},
_ => todo!(),
},
_ => panic!("not a material?"),
}
} else {
three_d::renderer::material::CpuMaterial {
name: format!("{}-mat", subsection.material_crc32),
..Default::default()
}
}
}).map(|(p, u, n, t)| (Vec3::from(*p), (Vec2::from(*u), ({let mut norm = n.map(|i| (i as f32 - 128.0) / 128.0); norm[2] *= -1.0; Vec3::from(norm)}, Vec4::from(t.map(|i| (i as f32 - 128.0) / 128.0)))))).unzip();
})
.collect();

let model = three_d::renderer::object::CpuModel {
name: resource_name.to_string(),
geometries: tri_meshes,
materials,
};
Some(Artifact::Skin(Arc::new(model)))
}
_ => None,
},
_ => None,
}
}

fn get_mesh(mesh: bff::class::mesh::Mesh) -> Option<Vec<three_d_asset::TriMesh>> {
match mesh {
bff::class::mesh::Mesh::MeshV1_291_03_06PC(mesh) => {
let tri_meshes = mesh
.body
.mesh_buffer
.vertex_groups
.iter()
.map(|group| {
// println!("{}", mesh.body.mesh_buffer.vertex_buffers.len());
let (positions, (uvs, (normals, tangents))): (
Vec<Vec3>,
(Vec<Vec2>, (Vec<Vec3>, Vec<Vec4>)),
) = mesh
.body
.mesh_buffer
.vertex_buffers
.iter()
.flat_map(|buf| &buf.vertex_structs)
.collect::<Vec<&bff::class::mesh::v1_291_03_06_pc::VertexStruct>>()
[group.vertex_offset_in_groups as usize
..group.vertex_offset_in_groups as usize + group.vertex_count as usize]
.iter()
.map(|vs| {
match vs {
bff::class::mesh::v1_291_03_06_pc::VertexStruct::VertexStruct24 {
position,
uv,
..
} => (position, uv, &[0u8; 3], [0u8; 4]),
bff::class::mesh::v1_291_03_06_pc::VertexStruct::VertexStruct36 {
position,
uv,
normal,
tangent,
tangent_padding,
..
}
| bff::class::mesh::v1_291_03_06_pc::VertexStruct::VertexStruct48 {
position,
uv,
normal,
tangent,
tangent_padding,
..
}
| bff::class::mesh::v1_291_03_06_pc::VertexStruct::VertexStruct60 {
position,
uv,
normal,
tangent,
tangent_padding,
..
} => (
position,
uv,
normal,
[&tangent[..], &[*tangent_padding]]
.concat()
.try_into()
.unwrap(),
),
bff::class::mesh::v1_291_03_06_pc::VertexStruct::VertexStructUnknown {
..
} => (&[0f32; 3], &[0f32; 2], &[0u8; 3], [0u8; 4]),
}
})
.map(|(p, u, n, t)| {
(
Vec3::from(*p),
(
Vec2::from(*u),
(
{
let mut norm = n.map(|i| (i as f32 - 128.0) / 128.0);
norm[2] *= -1.0;
Vec3::from(norm)
},
Vec4::from(t.map(|i| (i as f32 - 128.0) / 128.0)),
),
),
)
})
.unzip();
let indices: Vec<u16> = mesh
.body
.mesh_buffer
.index_buffers
.iter()
.flat_map(|buf| &buf.tris)
.flat_map(|tri| tri.indices)
.map(|i| u16::try_from(i).unwrap_or(0))
.collect::<Vec<i16>>()[group
.index_buffer_offset_in_shorts
as usize
..group.index_buffer_offset_in_shorts as usize
+ group.face_count as usize * 3]
.iter()
.map(|i| u16::try_from(*i).unwrap_or(0) - group.vertex_offset_in_groups)
.collect();
let tri_mesh = three_d::geometry::CpuMesh {
three_d::geometry::CpuMesh {
positions: three_d::Positions::F32(positions),
indices: three_d::Indices::U16(indices),
normals: Some(normals),
tangents: Some(tangents),
uvs: Some(uvs),
colors: None,
};
let triangles = three_d_asset::geometry::Geometry::Triangles(tri_mesh);
let primitive = three_d_asset::Primitive {
name: "mesh".to_string(),
transformation: three_d_asset::Mat4::from_translation([0.0; 3].into()),
animations: vec![],
geometry: triangles,
material_index: None,
};
let model = CpuModel {
name: resource_name.to_string(),
geometries: vec![primitive],
materials: vec![],
};
Some(Artifact::Mesh(Arc::new(model)))
}
_ => None,
}
}
})
.collect();
Some(tri_meshes)
}
_ => None,
}
Expand Down
6 changes: 3 additions & 3 deletions bff-gui/src/views/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl ConvertedFrameInput<'_> {
pub struct ThreeDApp {
context: Context,
camera: Camera,
pub model: Model<NormalMaterial>,
pub model: Model<PhysicalMaterial>,
}

impl ThreeDApp {
Expand All @@ -180,8 +180,8 @@ impl ThreeDApp {
fn cpu_model_to_gpu(
cpu_model: &CpuModel,
context: &three_d::core::Context,
) -> Model<NormalMaterial> {
let model = Model::<NormalMaterial>::new(context, cpu_model).unwrap();
) -> Model<PhysicalMaterial> {
let model = Model::<PhysicalMaterial>::new(context, cpu_model).unwrap();
// model.iter_mut().for_each(|m| {
// m.material = NormalMaterial::new(context, &three_d_asset::PbrMaterial::default());
// });
Expand Down
6 changes: 3 additions & 3 deletions bff/src/class/material/v1_291_03_06_pc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::helpers::{Mat3f, RGB, RGBA};
#[derive(BinRead, Debug, Serialize, BinWrite, Deserialize, ReferencedNames)]
#[br(import(_link_header: &()))]
pub struct MaterialBodyV1_291_03_06PC {
diffuse_color: RGBA,
emissive_color: RGB,
pub diffuse_color: RGBA,
pub emissive_color: RGB,
cdcdcdcd: u32,
uv_transform_matrix: Mat3f,
unknown1s: [f32; 8],
Expand All @@ -24,7 +24,7 @@ pub struct MaterialBodyV1_291_03_06PC {
3 => 2,
_ => 4,
})]
textures: Vec<u32>,
pub textures: Vec<u32>,
}

pub type MaterialV1_291_03_06PC = TrivialClass<(), MaterialBodyV1_291_03_06PC>;
8 changes: 4 additions & 4 deletions bff/src/class/mesh/v1_291_03_06_pc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ pub struct IndexBuffer {
pub struct VertexGroup {
zeroes: Vec3<u32>,
primitive: u32,
vertex_offset_in_groups: u16,
pub vertex_offset_in_groups: u16,
unknown0: u16,
vertex_count: u32,
index_buffer_offset_in_shorts: u32,
face_count: u32,
pub vertex_count: u32,
pub index_buffer_offset_in_shorts: u32,
pub face_count: u32,
unknown1: u32,
unknown2: u32,
vertex_size: u16,
Expand Down
Loading

0 comments on commit a9104cf

Please sign in to comment.