Add kart game

This commit is contained in:
2023-06-03 13:02:23 +02:00
parent 7bc752209b
commit 4d02185bd9
15 changed files with 1772 additions and 689 deletions

View File

@@ -1,13 +1,18 @@
use crate::lwo::material::MaterialUvInfo;
use crate::lwo::material::{MaterialProjectionMode, MaterialUvInfo};
use crate::lwo::surface_info::SurfaceInfo;
use godot::builtin::{Array, Dictionary, Vector2, Vector3};
use godot::engine::mesh::{ArrayFormat, PrimitiveType};
use crate::lwo::uv_baker::{project_cubic, project_planar};
use godot::builtin::{
Array, Dictionary, FromVariant, PackedVector2Array, PackedVector3Array, ToVariant, Vector2,
Vector3,
};
use godot::engine::mesh::{ArrayFormat, ArrayType, PrimitiveType};
use godot::engine::{ArrayMesh, SurfaceTool};
use godot::log::godot_print;
use godot::obj::{Gd, Share};
use godot::obj::{EngineEnum, Gd, Share};
use itertools::Itertools;
use lightwave_3d::lwo2::tags::polygon_list::PolygonList;
use std::collections::HashMap;
use std::iter::zip;
pub type SurfaceMapping<T> = HashMap<i32, HashMap<i32, T>>;
@@ -75,16 +80,52 @@ fn post_process_mesh(
tool.create_from(mesh.share().upcast(), surface_idx as i64);
tool.generate_normals(false);
tool.generate_tangents();
let mut arrays = tool.commit_to_arrays();
let mat = &materials[surface_id];
for (uv_type, info) in [
(ArrayType::ARRAY_TEX_UV, &mat.color_projection),
(ArrayType::ARRAY_TEX_UV2, &mat.diffuse_projection),
] {
match info {
Some(MaterialProjectionMode::Planar { transform, axis }) => {
let vertices = PackedVector3Array::from_variant(
&arrays.get(ArrayType::ARRAY_VERTEX.ord() as usize),
)
.to_vec();
let mut uvs = PackedVector2Array::new();
for vertex in vertices {
uvs.push(project_planar(vertex, *axis, *transform))
}
arrays.set(uv_type.ord() as usize, uvs.to_variant());
}
Some(MaterialProjectionMode::Cubic { transform }) => {
let vertices = PackedVector3Array::from_variant(
&arrays.get(ArrayType::ARRAY_VERTEX.ord() as usize),
)
.to_vec();
let normals = PackedVector3Array::from_variant(
&arrays.get(ArrayType::ARRAY_NORMAL.ord() as usize),
)
.to_vec();
let mut uvs = PackedVector2Array::new();
for (vertex, normal) in zip(vertices, normals) {
uvs.push(project_cubic(vertex, normal, *transform))
}
arrays.set(uv_type.ord() as usize, uvs.to_variant());
}
_ => (),
}
}
out_mesh.add_surface_from_arrays(
PrimitiveType::PRIMITIVE_TRIANGLES,
tool.commit_to_arrays(),
arrays,
Array::new(),
Dictionary::new(),
ArrayFormat::ARRAY_FORMAT_NORMAL,
);
let mat = &materials[surface_id];
out_mesh.surface_set_material(surface_idx as i64, mat.material.share().upcast())
}

View File

@@ -1,13 +1,11 @@
use godot::builtin::{Basis, Color, EulerOrder, ToVariant, Transform3D, Variant, Vector3};
use godot::builtin::{Basis, Color, EulerOrder, ToVariant, Transform3D, Vector3};
use godot::engine::{load, PlaceholderTexture2D, ShaderMaterial, Texture2D};
use godot::log::{godot_error, godot_print};
use godot::obj::{Gd, Share};
use lightwave_3d::lwo2::sub_tags::blocks::image_texture::{
ProjectionMode, SurfaceBlockImageTextureSubChunk,
};
use lightwave_3d::lwo2::sub_tags::blocks::texture_mapping::{
CoordinateSystem, FalloffType, TextureMappingSubChunk,
};
use lightwave_3d::lwo2::sub_tags::blocks::texture_mapping::{FalloffType, TextureMappingSubChunk};
use lightwave_3d::lwo2::sub_tags::blocks::{
SurfaceBlockHeaderSubChunk, SurfaceBlocks, TextureChannel,
};
@@ -15,10 +13,17 @@ use lightwave_3d::lwo2::sub_tags::surface_parameters::SurfaceParameterSubChunk;
use lightwave_3d::lwo2::tags::surface_definition::SurfaceDefinition;
use std::collections::HashMap;
#[derive(Debug)]
pub enum MaterialProjectionMode {
UvChannelName(String),
Planar { transform: Transform3D, axis: u16 },
Cubic { transform: Transform3D },
}
#[derive(Debug)]
pub struct MaterialUvInfo {
pub diffuse_channel: Option<String>,
pub color_channel: Option<String>,
pub diffuse_projection: Option<MaterialProjectionMode>,
pub color_projection: Option<MaterialProjectionMode>,
pub material: Gd<ShaderMaterial>,
pub id: u16,
}
@@ -30,8 +35,8 @@ impl MaterialUvInfo {
id: u16,
) -> Self {
let mut m = MaterialUvInfo {
diffuse_channel: None,
color_channel: None,
diffuse_projection: None,
color_projection: None,
material: ShaderMaterial::new(),
id,
};
@@ -47,8 +52,8 @@ impl MaterialUvInfo {
let mut chan = TextureChannel::Color;
let mut uv_channel = None;
let mut major_axis = 0;
let mut transform: Transform3D = Transform3D::IDENTITY;
let mut projection_mode = ProjectionMode::UV;
let mut mapping_info = Vec::<(&str, Variant)>::new();
for attr in header.data.block_attributes {
match attr {
SurfaceBlockHeaderSubChunk::Channel(c) => {
@@ -116,6 +121,7 @@ impl MaterialUvInfo {
};
}
TextureMappingSubChunk::Falloff(it) => {
/* TODO
mapping_info.push((
"falloff",
Vector3 {
@@ -135,9 +141,10 @@ impl MaterialUvInfo {
FalloffType::LinearZ => 4,
}
.to_variant(),
));
));*/
}
TextureMappingSubChunk::CoordinateSystem(it) => {
/* TODO
mapping_info.push((
"world_coords",
matches!(
@@ -145,7 +152,7 @@ impl MaterialUvInfo {
CoordinateSystem::WorldCoordinates
)
.to_variant(),
));
));*/
}
TextureMappingSubChunk::ReferenceObject(it) => {
if !matches!(it.object_name.as_str(), "" | "(none)")
@@ -156,16 +163,11 @@ impl MaterialUvInfo {
}
}
mapping_info.push((
"transform",
Transform3D {
basis: Basis::from_euler(EulerOrder::ZYX, rot)
.scaled(size),
origin: pos,
}
.affine_inverse()
.to_variant(),
));
transform = Transform3D {
basis: Basis::from_euler(EulerOrder::ZYX, rot).scaled(size),
origin: pos,
}
.affine_inverse()
}
SurfaceBlockImageTextureSubChunk::MajorAxis(axis) => {
major_axis = axis.data.texture_axis;
@@ -190,12 +192,7 @@ impl MaterialUvInfo {
}
}
}
/*godot_print!(
"TX: {:?} ({:?}) @ UV{:?}",
chan,
projection_mode,
uv_channel
);*/
let channel_name = match &chan {
TextureChannel::Color => "color",
// this is a bit confusing, but this is actually diffuse *lighting*
@@ -210,37 +207,29 @@ impl MaterialUvInfo {
TextureChannel::Translucency => "translucency",
TextureChannel::Luminosity => "luminosity",
};
m.material.set_shader_parameter(
format!("tex_{}_axis", channel_name).into(),
major_axis.to_variant(),
);
m.material.set_shader_parameter(
format!("tex_{}_projection", channel_name).into(),
match projection_mode {
ProjectionMode::Planar => 0,
ProjectionMode::Cylindrical => 1,
ProjectionMode::Spherical => 2,
ProjectionMode::Cubic => 3,
ProjectionMode::FrontProjection => 4,
ProjectionMode::UV => 5,
}
.to_variant(),
);
m.material.set_shader_parameter(
format!("tex_{}", channel_name).into(),
texture.to_variant(),
);
for (name, value) in mapping_info {
m.material.set_shader_parameter(
format!("tex_{}_projection_{}", channel_name, name).into(),
value,
);
}
let projection_info = match projection_mode {
ProjectionMode::UV => {
MaterialProjectionMode::UvChannelName(uv_channel.unwrap())
}
ProjectionMode::Cubic => MaterialProjectionMode::Cubic { transform },
ProjectionMode::Planar => MaterialProjectionMode::Planar {
transform,
axis: major_axis,
},
x => {
godot_error!("TODO: {:?}", x);
MaterialProjectionMode::UvChannelName("[[unsupported]]".into())
}
};
match chan {
TextureChannel::Diffuse => m.diffuse_channel = uv_channel,
TextureChannel::Color => m.color_channel = uv_channel,
TextureChannel::Diffuse => m.diffuse_projection = Some(projection_info),
TextureChannel::Color => m.color_projection = Some(projection_info),
_ => (),
}
}

View File

@@ -5,3 +5,4 @@ pub(crate) mod material;
pub(crate) mod object;
pub(crate) mod surface_info;
pub(crate) mod unique_vertex;
pub(crate) mod uv_baker;

View File

@@ -1,6 +1,6 @@
use crate::lwo::intermediate_layer::IntermediateLayer;
use crate::lwo::mapping::find_mapping;
use crate::lwo::material::MaterialUvInfo;
use crate::lwo::material::{MaterialProjectionMode, MaterialUvInfo};
use crate::lwo::unique_vertex::UniqueVertex;
use godot::builtin::{
PackedFloat32Array, PackedInt32Array, PackedVector2Array, PackedVector3Array, ToVariant,
@@ -78,13 +78,18 @@ impl SurfaceInfo {
pub fn collect_from_layer(layer: &IntermediateLayer, material: &MaterialUvInfo) -> Self {
let uv_names = [
material.color_channel.as_ref(),
material.diffuse_channel.as_ref(),
material.color_projection.as_ref(),
material.diffuse_projection.as_ref(),
];
let uv_subset = uv_names
.iter()
.map(|it| it.and_then(|it| layer.uv_mappings.iter().find(|(name, _)| name == it)))
.map(|it| {
it.and_then(|it| {
layer.uv_mappings.iter().find(|(name, _)|
matches!(it, MaterialProjectionMode::UvChannelName(it) if name == it))
})
})
.collect_vec();
let mut surface_info = SurfaceInfo {

View File

@@ -0,0 +1,49 @@
use godot::builtin::{Transform3D, Vector2, Vector3};
enum Axis {
X,
Y,
Z,
}
pub fn project_planar(vertex: Vector3, axis: u16, transform: Transform3D) -> Vector2 {
by_axis(
transform * vertex,
match axis {
0 => Axis::X,
1 => Axis::Y,
2 => Axis::Z,
_ => panic!(),
},
)
}
pub fn project_cubic(vertex: Vector3, normal: Vector3, transform: Transform3D) -> Vector2 {
let p = transform * vertex;
let n = (transform.basis * normal).abs().normalized();
let axis = if n.x > n.y && n.x > n.z {
Axis::X
} else if n.y > n.x && n.y > n.z {
Axis::Y
} else {
Axis::Z
};
by_axis(p, axis)
}
fn by_axis(v: Vector3, axis: Axis) -> Vector2 {
match axis {
Axis::X => Vector2 {
x: v.z + 0.5,
y: v.y + 0.5,
},
Axis::Y => Vector2 {
x: v.z + 0.5,
y: v.x + 0.5,
},
Axis::Z => Vector2 {
x: v.x + 0.5,
y: v.y + 0.5,
},
}
}

View File

@@ -45,7 +45,9 @@ impl Mhk3Map {
};
let mut files_to_convert = HashSet::new();
for file in archive.files.iter_mut() {
let convert = file.path.ends_with(".lwo");
let convert = file.path.ends_with(".lwo")
|| file.path.ends_with(".bmp")
|| file.path.ends_with(".dds");
file.path = sarc_path_to_gd(&file.path);
if convert {
files_to_convert.insert(file.path.clone());