Skip to content

Commit

Permalink
noggit: ui: clearing_tool: add option to remove textures with alpha v…
Browse files Browse the repository at this point in the history
…alues below a certain threshold
  • Loading branch information
Adspartan committed Jun 2, 2024
1 parent 1b43baa commit 7fff1e0
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 21 deletions.
7 changes: 7 additions & 0 deletions src/noggit/MapChunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1079,12 +1079,19 @@ void MapChunk::eraseTextures()
texture_set_changed();
texture_set->eraseTextures();
}

void MapChunk::remove_texture_duplicates()
{
texture_set_changed();
texture_set->removeDuplicate();
}

void MapChunk::remove_unused_textures(float threshold)
{
texture_set_changed();
texture_set->eraseUnusedTextures(threshold);
}

void MapChunk::change_texture_flag(scoped_blp_texture_reference const& tex, std::size_t flag, bool add)
{
texture_set_changed();
Expand Down
1 change: 1 addition & 0 deletions src/noggit/MapChunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ class MapChunk
void switchTexture(scoped_blp_texture_reference const& oldTexture, scoped_blp_texture_reference newTexture);
void eraseTextures();
void remove_texture_duplicates();
void remove_unused_textures(float threshold);
void change_texture_flag(scoped_blp_texture_reference const& tex, std::size_t flag, bool add);
void clear_texture_flags();

Expand Down
21 changes: 14 additions & 7 deletions src/noggit/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2036,15 +2036,16 @@ bool World::replaceTexture(math::vector_3d const& pos, float radius, scoped_blp_
}

void World::clear_on_chunks ( math::vector_3d const& pos, float radius, bool height, bool textures, bool duplicate_textures
, bool texture_flags, bool liquids, bool m2s, bool wmos, bool shadows, bool mccv, bool impassible_flag, bool holes
, bool textures_below_threshold, float alpha_threshold, bool texture_flags, bool liquids
, bool m2s, bool wmos, bool shadows, bool mccv, bool impassible_flag, bool holes
)
{
for_all_chunks_in_range
( pos, radius
, [&](MapChunk* chunk)
{
clear_on_chunk ( chunk, height, textures, duplicate_textures, texture_flags
, liquids, shadows, mccv, impassible_flag, holes
clear_on_chunk ( chunk, height, textures, duplicate_textures, textures_below_threshold, alpha_threshold
, texture_flags, liquids, shadows, mccv, impassible_flag, holes
);
return true;
}
Expand All @@ -2059,7 +2060,8 @@ void World::clear_on_chunks ( math::vector_3d const& pos, float radius, bool hei
}
}
void World::clear_on_tiles ( math::vector_3d const& pos, float radius, bool height, bool textures, bool duplicate_textures
, bool texture_flags, bool liquids, bool m2s, bool wmos, bool shadows, bool mccv, bool impassible_flag, bool holes
, bool textures_below_threshold, float alpha_threshold, bool texture_flags, bool liquids
, bool m2s, bool wmos, bool shadows, bool mccv, bool impassible_flag, bool holes
)
{
for_all_tiles_in_range
Expand All @@ -2075,8 +2077,8 @@ void World::clear_on_tiles ( math::vector_3d const& pos, float radius, bool heig
for_all_chunks_on_tile({ tile->xbase + CHUNKSIZE, 0.f, tile->zbase + CHUNKSIZE },
[&](MapChunk* chunk)
{
clear_on_chunk ( chunk, height, textures, duplicate_textures, texture_flags
, liquids, shadows, mccv, impassible_flag, holes
clear_on_chunk ( chunk, height, textures, duplicate_textures, textures_below_threshold, alpha_threshold
, texture_flags, liquids, shadows, mccv, impassible_flag, holes
);
return true;
}
Expand All @@ -2088,7 +2090,8 @@ void World::clear_on_tiles ( math::vector_3d const& pos, float radius, bool heig
}

void World::clear_on_chunk( MapChunk* chunk, bool height, bool textures, bool duplicate_textures
, bool texture_flags, bool liquids, bool shadows, bool mccv, bool impassible_flag, bool holes
, bool textures_below_threshold, float alpha_threshold, bool texture_flags
, bool liquids, bool shadows, bool mccv, bool impassible_flag, bool holes
)
{
if (height)
Expand All @@ -2103,6 +2106,10 @@ void World::clear_on_chunk( MapChunk* chunk, bool height, bool textures, bool du
{
chunk->remove_texture_duplicates();
}
if (textures_below_threshold)
{
chunk->remove_unused_textures(alpha_threshold);
}
if (texture_flags)
{
chunk->clear_texture_flags();
Expand Down
14 changes: 9 additions & 5 deletions src/noggit/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,13 @@ class World
bool sprayTexture(math::vector_3d const& pos, Brush *brush, float strength, float pressure, float spraySize, float sprayPressure, scoped_blp_texture_reference texture);
bool replaceTexture(math::vector_3d const& pos, float radius, scoped_blp_texture_reference const& old_texture, scoped_blp_texture_reference new_texture);

void clear_on_chunks ( math::vector_3d const& pos, float radius, bool height, bool textures, bool duplicate_textures, bool texture_flags
, bool liquids, bool m2s, bool wmos, bool shadows, bool mccv, bool impassible_flag, bool holes
void clear_on_chunks ( math::vector_3d const& pos, float radius, bool height, bool textures, bool duplicate_textures
, bool textures_below_threshold, float alpha_threshold, bool texture_flags, bool liquids
, bool m2s, bool wmos, bool shadows, bool mccv, bool impassible_flag, bool holes
);
void clear_on_tiles ( math::vector_3d const& pos, float radius, bool height, bool textures, bool duplicate_textures, bool texture_flags
, bool liquids, bool m2s, bool wmos, bool shadows, bool mccv, bool impassible_flag, bool holes
void clear_on_tiles ( math::vector_3d const& pos, float radius, bool height, bool textures, bool duplicate_textures
, bool textures_below_threshold, float alpha_threshold, bool texture_flags, bool liquids
, bool m2s, bool wmos, bool shadows, bool mccv, bool impassible_flag, bool holes
);

void eraseTextures(math::vector_3d const& pos);
Expand Down Expand Up @@ -386,9 +388,11 @@ class World

private:
void clear_on_chunk( MapChunk* chunk, bool height, bool textures, bool duplicate_textures
, bool texture_flags, bool liquids, bool shadows, bool mccv, bool impassible_flag, bool holes
, bool textures_below_threshold, float alpha_threshold, bool texture_flags, bool liquids
, bool shadows, bool mccv, bool impassible_flag, bool holes
);


void update_models_by_filename();

bool _need_wmo_liquid_update = true;
Expand Down
13 changes: 10 additions & 3 deletions src/noggit/texture_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,20 @@ math::vector_3d TextureSet::anim_param(int layer) const
return { -anim_dir_x[dir_index], anim_dir_y[dir_index], speed };
}

bool TextureSet::eraseUnusedTextures()
bool TextureSet::eraseUnusedTextures(float threshold)
{
if (threshold >= 255.f)
{
eraseTextures();
}

if (nTextures < 2)
{
return false;
}

int i_threshold = static_cast<int>(threshold);

std::set<int> visible_tex;

if (tmp_edit_values)
Expand All @@ -325,7 +332,7 @@ bool TextureSet::eraseUnusedTextures()
{
// use 0.01 to account for floating point imprecision
// while not preventing very low pressure brush from painting correctly
if (amaps[layer][i] > 0.01f)
if (amaps[layer][i] >= threshold)
{
visible_tex.emplace(layer);
break; // texture visible, go to the next layer
Expand All @@ -342,7 +349,7 @@ bool TextureSet::eraseUnusedTextures()
{
uint8_t a = alphamaps[n]->getAlpha(i);
sum += a;
if (a > 0)
if (a > i_threshold)
{
visible_tex.emplace(n + 1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/noggit/texture_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TextureSet
void eraseTexture(size_t id);
void eraseTextures();
// return true if at least 1 texture has been erased
bool eraseUnusedTextures();
bool eraseUnusedTextures(float threshold = 0.01f);
void swap_layers(int layer_1, int layer_2);
void replace_texture(scoped_blp_texture_reference const& texture_to_replace, scoped_blp_texture_reference replacement_texture);
bool paintTexture(float xbase, float zbase, float x, float z, Brush* brush, float strength, float pressure, scoped_blp_texture_reference texture);
Expand Down
51 changes: 46 additions & 5 deletions src/noggit/ui/clearing_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ namespace noggit
clearing_tool::clearing_tool(QWidget* parent)
: QWidget(parent)
, _radius(15.0f)
, _texture_threshold(1.0f)
, _clear_height(false)
, _clear_textures(false)
, _clear_duplicate_textures(false)
, _clear_textures_under_threshold(false)
, _clear_texture_flags(false)
, _clear_liquids(false)
, _clear_m2s(false)
Expand All @@ -46,6 +48,7 @@ namespace noggit
clearing_option_layout->addWidget(new checkbox("Height", &_clear_height, clearing_option_group));
clearing_option_layout->addWidget(new checkbox("Textures", &_clear_textures, clearing_option_group));
clearing_option_layout->addWidget(new checkbox("Texture Duplicates", &_clear_duplicate_textures, clearing_option_group));
clearing_option_layout->addWidget(new checkbox("Textures below Threshold", &_clear_textures_under_threshold, clearing_option_group));
clearing_option_layout->addWidget(new checkbox("Liquids", &_clear_liquids, clearing_option_group));
clearing_option_layout->addWidget(new checkbox("M2s", &_clear_m2s, clearing_option_group));
clearing_option_layout->addWidget(new checkbox("WMOs", &_clear_wmos, clearing_option_group));
Expand Down Expand Up @@ -79,18 +82,36 @@ namespace noggit

layout->addRow(mode_option_group);

_radius_spin = new QDoubleSpinBox (this);
auto parameters_group(new QGroupBox("Parameters", this));
auto parameters_layout(new QFormLayout(parameters_group));

_radius_spin = new QDoubleSpinBox (parameters_group);
_radius_spin->setRange (0.0f, 1000.0f);
_radius_spin->setDecimals (2);
_radius_spin->setDecimals(2);
_radius_spin->setValue (_radius);

layout->addRow ("Radius:", _radius_spin);
parameters_layout->addRow("Radius:", _radius_spin);

_radius_slider = new QSlider (Qt::Orientation::Horizontal, this);
_radius_slider = new QSlider (Qt::Orientation::Horizontal, parameters_group);
_radius_slider->setRange (0, 1000);
_radius_slider->setSliderPosition (_radius);

layout->addRow (_radius_slider);
parameters_layout->addRow(_radius_slider);

_texture_threshold_spin = new QDoubleSpinBox(parameters_group);
_texture_threshold_spin->setRange(0., 255.);
_texture_threshold_spin->setValue(_texture_threshold);

parameters_layout->addRow("Texture Alpha Threshold:", _texture_threshold_spin);

_texture_threshold_slider = new QSlider(Qt::Orientation::Horizontal, parameters_group);
_texture_threshold_slider->setRange(0, 255);
_texture_threshold_slider->setSliderPosition(_texture_threshold);

parameters_layout->addRow(_texture_threshold_slider);


layout->addRow(parameters_group);



Expand All @@ -112,6 +133,22 @@ namespace noggit
}
);

connect ( _texture_threshold_spin, qOverload<double> (&QDoubleSpinBox::valueChanged)
, [&] (double v)
{
_texture_threshold = v;
QSignalBlocker const blocker(_texture_threshold_slider);
_texture_threshold_slider->setSliderPosition((int)std::round(v));
}
);
connect ( _texture_threshold_slider, &QSlider::valueChanged
, [&] (int v)
{
_texture_threshold = v;
QSignalBlocker const blocker(_texture_threshold_spin);
_texture_threshold_spin->setValue(v);
}
);
setMinimumWidth(sizeHint().width());

}
Expand All @@ -125,6 +162,8 @@ namespace noggit
, _clear_height.get()
, _clear_textures.get()
, _clear_duplicate_textures.get()
, _clear_textures_under_threshold.get()
, _texture_threshold
, _clear_texture_flags.get()
, _clear_liquids.get()
, _clear_m2s.get()
Expand All @@ -142,6 +181,8 @@ namespace noggit
, _clear_height.get()
, _clear_textures.get()
, _clear_duplicate_textures.get()
, _clear_textures_under_threshold.get()
, _texture_threshold
, _clear_texture_flags.get()
, _clear_liquids.get()
, _clear_m2s.get()
Expand Down
4 changes: 4 additions & 0 deletions src/noggit/ui/clearing_tool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ namespace noggit

private:
float _radius;
float _texture_threshold;
int _mode = 0;

bool_toggle_property _clear_height;
bool_toggle_property _clear_textures;
bool_toggle_property _clear_duplicate_textures;
bool_toggle_property _clear_textures_under_threshold;
bool_toggle_property _clear_texture_flags;
bool_toggle_property _clear_liquids;
bool_toggle_property _clear_m2s;
Expand All @@ -46,6 +48,8 @@ namespace noggit

QSlider* _radius_slider;
QDoubleSpinBox* _radius_spin;
QSlider* _texture_threshold_slider;
QDoubleSpinBox* _texture_threshold_spin;

};
}
Expand Down

0 comments on commit 7fff1e0

Please sign in to comment.