-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chart pack algorithm: We guess a initial texture size based on the sum of chart areas, and try different methods then pick a best one, if pack failed because of texture size, we grow the size until it fit.
- Loading branch information
Showing
8 changed files
with
1,134 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include <simpleuv/chartpacker.h> | ||
#include <cmath> | ||
extern "C" { | ||
#include <maxrects.h> | ||
} | ||
|
||
namespace simpleuv | ||
{ | ||
|
||
void ChartPacker::setCharts(const std::vector<std::pair<float, float>> &chartSizes) | ||
{ | ||
m_chartSizes = chartSizes; | ||
} | ||
|
||
const std::vector<std::tuple<float, float, float, float, bool>> &ChartPacker::getResult() | ||
{ | ||
return m_result; | ||
} | ||
|
||
double ChartPacker::calculateTotalArea() | ||
{ | ||
double totalArea = 0; | ||
for (const auto &chartSize: m_chartSizes) { | ||
totalArea += chartSize.first * chartSize.second; | ||
} | ||
return totalArea; | ||
} | ||
|
||
bool ChartPacker::tryPack(float textureSize) | ||
{ | ||
std::vector<maxRectsSize> rects; | ||
int width = textureSize * m_floatToIntFactor; | ||
int height = width; | ||
for (const auto &chartSize: m_chartSizes) { | ||
maxRectsSize r; | ||
r.width = chartSize.first * m_floatToIntFactor; | ||
r.height = chartSize.second * m_floatToIntFactor; | ||
rects.push_back(r); | ||
} | ||
const maxRectsFreeRectChoiceHeuristic methods[] = { | ||
rectBestShortSideFit, | ||
rectBestLongSideFit, | ||
rectBestAreaFit, | ||
rectBottomLeftRule, | ||
rectContactPointRule | ||
}; | ||
float occupancy = 0; | ||
float bestOccupancy = 0; | ||
std::vector<maxRectsPosition> bestResult; | ||
for (size_t i = 0; i < sizeof(methods) / sizeof(methods[0]); ++i) { | ||
std::vector<maxRectsPosition> result(rects.size()); | ||
if (!maxRects(width, height, rects.size(), rects.data(), methods[i], true, result.data(), &occupancy)) { | ||
continue; | ||
} | ||
if (occupancy >= bestOccupancy) { | ||
bestResult = result; | ||
bestOccupancy = occupancy; | ||
} | ||
} | ||
if (bestResult.size() != rects.size()) | ||
return false; | ||
m_result.resize(bestResult.size()); | ||
for (decltype(bestResult.size()) i = 0; i < bestResult.size(); ++i) { | ||
const auto &result = bestResult[i]; | ||
const auto &rect = rects[i]; | ||
m_result[i] = {result.left / textureSize, result.top / textureSize, rect.width / textureSize, rect.height / textureSize, result.rotated}; | ||
} | ||
return true; | ||
} | ||
|
||
void ChartPacker::pack() | ||
{ | ||
float initialGuessSize = std::sqrt(calculateTotalArea() * m_initialAreaGuessFactor); | ||
float textureSize = initialGuessSize; | ||
while (true) { | ||
if (tryPack(textureSize)) | ||
break; | ||
textureSize *= 1.0 + m_textureSizeGrowFactor; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#ifndef SIMPLEUV_CHART_PACKER_H | ||
#define SIMPLEUV_CHART_PACKER_H | ||
#include <vector> | ||
|
||
namespace simpleuv | ||
{ | ||
|
||
class ChartPacker | ||
{ | ||
public: | ||
void setCharts(const std::vector<std::pair<float, float>> &chartSizes); | ||
const std::vector<std::tuple<float, float, float, float, bool>> &getResult(); | ||
void pack(); | ||
bool tryPack(float textureSize); | ||
|
||
private: | ||
double calculateTotalArea(); | ||
|
||
std::vector<std::pair<float, float>> m_chartSizes; | ||
std::vector<std::tuple<float, float, float, float, bool>> m_result; | ||
float m_initialAreaGuessFactor = 1.0; | ||
float m_textureSizeGrowFactor = 0.1; | ||
float m_floatToIntFactor = 100; | ||
}; | ||
|
||
} | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ struct Vertex | |
|
||
struct Face | ||
{ | ||
int indicies[3]; | ||
size_t indicies[3]; | ||
}; | ||
|
||
struct TextureCoord | ||
|
Oops, something went wrong.