Skip to content

Commit

Permalink
Refactor, apply normal texture to material
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyuu17 committed Jun 15, 2024
1 parent 945e8ac commit de3f6a2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
36 changes: 19 additions & 17 deletions src/SWBF2/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ namespace SWBF2

SWBF2::Native::UcfbChunk::ReadUcfbFile("data/_lvl_pc/cor/cor1.lvl");

LoadMeshes();
LoadLevelMeshes();
}

void Level::LoadMeshes()
void Level::LoadLevelMeshes()
{
for (auto const &[id, model] : Native::Level::m_models)
{
Expand Down Expand Up @@ -65,27 +65,29 @@ namespace SWBF2

meshInstance->set_mesh(arrMesh);

auto tex_id = 0;
for (const auto &texName : segment.m_textureNames)
if (!segment.m_textureNames.empty())
{
if (texName.empty())
continue;
static godot::Ref<godot::StandardMaterial3D> emptyGodotRef;

auto &material = m_materialPool.getItem(texName);
if (material.is_null())
{
godot::UtilityFunctions::printerr(__FILE__, ":", __LINE__, ": No material found for ", texName.c_str());
continue;
}
const auto &mainTextureName = segment.m_textureNames[Native::ModelSegment::TEXTURE_DEFAULT];
const auto &bumpTextureName = segment.m_textureNames[Native::ModelSegment::TEXTURE_NORMAL];

godot::UtilityFunctions::print(__FILE__, ":", __LINE__, ": Found texture ", texName.c_str(), " for mesh ", id.c_str(), " with segment id ", segment_id);
auto &material = m_materialPool.getItem(mainTextureName);
if (material.is_valid())
{
if (!bumpTextureName.empty() && Native::Level::m_tex.contains(bumpTextureName))
{
auto &bumpTexture = Native::Level::m_tex[bumpTextureName].m_formats[0].m_faceLevels[0].m_gdTexture;

if (segment.m_material.m_flags & Native::Material::MATERIAL_TRANSPARENT)
material->set_transparency(godot::BaseMaterial3D::TRANSPARENCY_ALPHA);
material->set_feature(godot::BaseMaterial3D::FEATURE_NORMAL_MAPPING, true);
material->set_texture(godot::BaseMaterial3D::TEXTURE_NORMAL, bumpTexture);
}

meshInstance->set_surface_override_material(tex_id, material);
if (segment.m_material.m_flags & Native::Material::MATERIAL_TRANSPARENT)
material->set_transparency(godot::BaseMaterial3D::TRANSPARENCY_ALPHA);

tex_id++;
meshInstance->set_surface_override_material(0, material);
}
}

if (meshInstance->get_material_override().is_null())
Expand Down
2 changes: 1 addition & 1 deletion src/SWBF2/Level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace SWBF2

virtual void _ready() override;

void LoadMeshes();
void LoadLevelMeshes();

void _process(double delta_time) override;

Expand Down
8 changes: 8 additions & 0 deletions src/SWBF2/Native/Models/ModelSegment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ namespace SWBF2::Native

class ModelSegment {
public:
enum TextureType
{
TEXTURE_DEFAULT = 0,
TEXTURE_NORMAL,
TEXTURE_UNK3,
TEXTURE_UNK4
};

ModelSegmentInfo m_info;

Material m_material;
Expand Down

0 comments on commit de3f6a2

Please sign in to comment.