Skip to content

Commit

Permalink
Fix blocky mesh model didn't use materials directly in the mesh resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Jul 9, 2024
1 parent 28e2e67 commit a3d9165
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
17 changes: 13 additions & 4 deletions meshers/blocky/voxel_blocky_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ Ref<Material> VoxelBlockyModel::get_material_override(int index) const {
return _surface_params[index].material_override;
}

bool VoxelBlockyModel::has_material_override() const {
for (const SurfaceParams &sp : _surface_params) {
if (sp.material_override.is_valid()) {
return true;
}
}
return false;
}

void VoxelBlockyModel::set_mesh_collision_enabled(int surface_index, bool enabled) {
// TODO Can't check for `_surface_count` instead, because there is no guarantee about the order in which Godot will
// set properties when loading the resource. The mesh could be set later, so we can't know the number of surfaces.
Expand Down Expand Up @@ -265,12 +274,12 @@ void VoxelBlockyModel::bake(BakedData &baked_data, bool bake_tangents, MaterialI
for (unsigned int surface_index = 0; surface_index < model.surface_count; ++surface_index) {
if (surface_index < _surface_count) {
const SurfaceParams &surface_params = _surface_params[surface_index];
const Ref<Material> material = surface_params.material_override;

BakedData::Surface &surface = model.surfaces[surface_index];

const unsigned int material_index = materials.get_or_create_index(material);
surface.material_id = material_index;
if (surface_params.material_override.is_valid()) {
const unsigned int material_index = materials.get_or_create_index(surface_params.material_override);
surface.material_id = material_index;
}

surface.collision_enabled = surface_params.collision_enabled;
}
Expand Down
1 change: 1 addition & 0 deletions meshers/blocky/voxel_blocky_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class VoxelBlockyModel : public Resource {

void set_material_override(int index, Ref<Material> material);
Ref<Material> get_material_override(int index) const;
bool has_material_override() const;

void set_mesh_collision_enabled(int surface_index, bool enabled);
bool is_mesh_collision_enabled(int surface_index) const;
Expand Down
10 changes: 8 additions & 2 deletions meshers/blocky/voxel_blocky_model_cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,16 @@ void bake_cube_geometry(
const VoxelBlockyModelCube &config,
VoxelBlockyModel::BakedData &baked_data,
Vector2i p_atlas_size,
VoxelBlockyModel::MaterialIndexer &material_indexer,
bool bake_tangents
) {
const float height = config.get_height();

baked_data.model.surface_count = 1;
VoxelBlockyModel::BakedData::Surface &surface = baked_data.model.surfaces[0];
// The only way to specify matererials in this model is via "material overrides", since there is no base mesh.
// Even if none are specified, we should at least index the "empty" material.
surface.material_id = material_indexer.get_or_create_index(config.get_material_override(0));

for (unsigned int side = 0; side < Cube::SIDE_COUNT; ++side) {
VoxelBlockyModel::BakedData::SideSurface &side_surface = surface.sides[side];
Expand Down Expand Up @@ -198,7 +202,7 @@ void bake_cube_geometry(

void VoxelBlockyModelCube::bake(BakedData &baked_data, bool bake_tangents, MaterialIndexer &materials) const {
baked_data.clear();
bake_cube_geometry(*this, baked_data, _atlas_size_in_tiles, bake_tangents);
bake_cube_geometry(*this, baked_data, _atlas_size_in_tiles, materials, bake_tangents);
VoxelBlockyModel::bake(baked_data, bake_tangents, materials);
}

Expand All @@ -211,7 +215,9 @@ Ref<Mesh> VoxelBlockyModelCube::get_preview_mesh() const {

VoxelBlockyModel::BakedData baked_data;
baked_data.color = get_color();
bake_cube_geometry(*this, baked_data, _atlas_size_in_tiles, bake_tangents);
StdVector<Ref<Material>> materials;
MaterialIndexer material_indexer{ materials };
bake_cube_geometry(*this, baked_data, _atlas_size_in_tiles, material_indexer, bake_tangents);

Ref<Mesh> mesh = make_mesh_from_baked_data(baked_data, bake_tangents);

Expand Down

0 comments on commit a3d9165

Please sign in to comment.