Merge pull request #31728 from fire/improve-uvatlas

Tweak xatlas and import new version b4b5426
This commit is contained in:
Rémi Verschelde 2019-08-28 15:46:58 +02:00 committed by GitHub
commit 4a6b445a93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 1782 additions and 1732 deletions

View file

@ -59,7 +59,7 @@ bool xatlas_mesh_lightmap_unwrap_callback(float p_texel_size, const float *p_ver
xatlas::PackOptions pack_options; xatlas::PackOptions pack_options;
pack_options.maxChartSize = 4096; pack_options.maxChartSize = 4096;
pack_options.bruteForce = true; pack_options.blockAlign = true;
pack_options.texelsPerUnit = 1.0 / p_texel_size; pack_options.texelsPerUnit = 1.0 / p_texel_size;
xatlas::Atlas *atlas = xatlas::Create(); xatlas::Atlas *atlas = xatlas::Create();
@ -78,7 +78,7 @@ bool xatlas_mesh_lightmap_unwrap_callback(float p_texel_size, const float *p_ver
float h = *r_size_hint_y; float h = *r_size_hint_y;
if (w == 0 || h == 0) { if (w == 0 || h == 0) {
return false; //could not bake return false; //could not bake because there is no area
} }
const xatlas::Mesh &output = atlas->meshes[0]; const xatlas::Mesh &output = atlas->meshes[0];
@ -106,7 +106,7 @@ bool xatlas_mesh_lightmap_unwrap_callback(float p_texel_size, const float *p_ver
*r_index_count = output.indexCount; *r_index_count = output.indexCount;
//xatlas::Destroy(atlas); xatlas::Destroy(atlas);
printf("Done\n"); printf("Done\n");
return true; return true;
} }

View file

@ -513,7 +513,7 @@ File extracted from upstream release tarball:
## xatlas ## xatlas
- Upstream: https://github.com/jpcy/xatlas - Upstream: https://github.com/jpcy/xatlas
- Version: git (f65a664, 2019) - Version: git (b4b5426, 2019)
- License: MIT - License: MIT
Files extracted from upstream source: Files extracted from upstream source:

View file

@ -1,157 +0,0 @@
diff --git a/thirdparty/xatlas/xatlas.cpp b/thirdparty/xatlas/xatlas.cpp
index df5ef94db..eb0824a51 100644
--- a/thirdparty/xatlas/xatlas.cpp
+++ b/thirdparty/xatlas/xatlas.cpp
@@ -1276,6 +1276,9 @@ class Vertex
{
public:
uint32_t id;
+ // -- GODOT start --
+ uint32_t original_id;
+ // -- GODOT end --
Edge *edge;
Vertex *next;
Vertex *prev;
@@ -1283,7 +1286,10 @@ public:
Vector3 nor;
Vector2 tex;
- Vertex(uint32_t id) : id(id), edge(NULL), pos(0.0f), nor(0.0f), tex(0.0f)
+ // -- GODOT start --
+ //Vertex(uint32_t id) : id(id), edge(NULL), pos(0.0f), nor(0.0f), tex(0.0f)
+ Vertex(uint32_t id) : id(id), original_id(id), edge(NULL), pos(0.0f), nor(0.0f), tex(0.0f)
+ // -- GODOT end --
{
next = this;
prev = this;
@@ -1934,6 +1940,64 @@ public:
return f;
}
+ // -- GODOT start --
+ Face *addUniqueFace(uint32_t v0, uint32_t v1, uint32_t v2) {
+
+ int base_vertex = m_vertexArray.size();
+
+ uint32_t ids[3] = { v0, v1, v2 };
+
+ Vector3 base[3] = {
+ m_vertexArray[v0]->pos,
+ m_vertexArray[v1]->pos,
+ m_vertexArray[v2]->pos,
+ };
+
+ //make sure its not a degenerate
+ bool degenerate = distanceSquared(base[0], base[1]) < NV_EPSILON || distanceSquared(base[0], base[2]) < NV_EPSILON || distanceSquared(base[1], base[2]) < NV_EPSILON;
+ xaDebugAssert(!degenerate);
+
+ float min_x = 0;
+
+ for (int i = 0; i < 3; i++) {
+ if (i == 0 || m_vertexArray[v0]->pos.x < min_x) {
+ min_x = m_vertexArray[v0]->pos.x;
+ }
+ }
+
+ float max_x = 0;
+
+ for (int j = 0; j < m_vertexArray.size(); j++) {
+ if (j == 0 || m_vertexArray[j]->pos.x > max_x) { //vertex already exists
+ max_x = m_vertexArray[j]->pos.x;
+ }
+ }
+
+ //separate from everything else, in x axis
+ for (int i = 0; i < 3; i++) {
+
+ base[i].x -= min_x;
+ base[i].x += max_x + 10.0;
+ }
+
+ for (int i = 0; i < 3; i++) {
+ Vertex *v = new Vertex(m_vertexArray.size());
+ v->pos = base[i];
+ v->nor = m_vertexArray[ids[i]]->nor,
+ v->tex = m_vertexArray[ids[i]]->tex,
+
+ v->original_id = ids[i];
+ m_vertexArray.push_back(v);
+ }
+
+ uint32_t indexArray[3];
+ indexArray[0] = base_vertex + 0;
+ indexArray[1] = base_vertex + 1;
+ indexArray[2] = base_vertex + 2;
+ return addFace(indexArray, 3, 0, 3);
+ }
+ // -- GODOT end --
+
// These functions disconnect the given element from the mesh and delete it.
// @@ We must always disconnect edge pairs simultaneously.
@@ -2915,6 +2979,14 @@ Mesh *triangulate(const Mesh *inputMesh)
Vector2 p0 = polygonPoints[i0];
Vector2 p1 = polygonPoints[i1];
Vector2 p2 = polygonPoints[i2];
+
+ // -- GODOT start --
+ bool degenerate = distance(p0, p1) < NV_EPSILON || distance(p0, p2) < NV_EPSILON || distance(p1, p2) < NV_EPSILON;
+ if (degenerate) {
+ continue;
+ }
+ // -- GODOT end --
+
float d = clamp(dot(p0 - p1, p2 - p1) / (length(p0 - p1) * length(p2 - p1)), -1.0f, 1.0f);
float angle = acosf(d);
float area = triangleArea(p0, p1, p2);
@@ -2938,6 +3010,11 @@ Mesh *triangulate(const Mesh *inputMesh)
}
}
}
+ // -- GODOT start --
+ if (!bestIsValid)
+ break;
+ // -- GODOT end --
+
xaDebugAssert(minAngle <= 2 * PI);
// Clip best ear:
uint32_t i0 = (bestEar + size - 1) % size;
@@ -5606,7 +5683,10 @@ public:
}
if (chartMeshIndices[vertex->id] == ~0) {
chartMeshIndices[vertex->id] = m_chartMesh->vertexCount();
- m_chartToOriginalMap.push_back(vertex->id);
+ // -- GODOT start --
+ //m_chartToOriginalMap.push_back(vertex->id);
+ m_chartToOriginalMap.push_back(vertex->original_id);
+ // -- GODOT end --
m_chartToUnifiedMap.push_back(unifiedMeshIndices[unifiedVertex->id]);
halfedge::Vertex *v = m_chartMesh->addVertex(vertex->pos);
v->nor = vertex->nor;
@@ -5699,7 +5779,10 @@ public:
const halfedge::Vertex *vertex = it.current()->vertex;
if (chartMeshIndices[vertex->id] == ~0) {
chartMeshIndices[vertex->id] = m_chartMesh->vertexCount();
- m_chartToOriginalMap.push_back(vertex->id);
+ // -- GODOT start --
+ //m_chartToOriginalMap.push_back(vertex->id);
+ m_chartToOriginalMap.push_back(vertex->original_id);
+ // -- GODOT end --
halfedge::Vertex *v = m_chartMesh->addVertex(vertex->pos);
v->nor = vertex->nor;
v->tex = vertex->tex; // @@ Not necessary.
@@ -7573,6 +7656,14 @@ AddMeshError AddMesh(Atlas *atlas, const InputMesh &mesh, bool useColocalVertice
}
}
internal::halfedge::Face *face = heMesh->addFace(tri[0], tri[1], tri[2]);
+
+ // -- GODOT start --
+ if (!face && heMesh->errorCode == internal::halfedge::Mesh::ErrorCode::AlreadyAddedEdge) {
+ //there is still hope for this, no reason to not add, at least add as separate
+ face = heMesh->addUniqueFace(tri[0], tri[1], tri[2]);
+ }
+ // -- GODOT end --
+
if (!face) {
if (heMesh->errorCode == internal::halfedge::Mesh::ErrorCode::AlreadyAddedEdge)
error.code = AddMeshErrorCode::AlreadyAddedEdge;

View file

@ -1,14 +0,0 @@
diff --git a/thirdparty/xatlas/xatlas.h b/thirdparty/xatlas/xatlas.h
index 7e556c6c3..dbf8ca08c 100644
--- a/thirdparty/xatlas/xatlas.h
+++ b/thirdparty/xatlas/xatlas.h
@@ -3,6 +3,9 @@
#ifndef XATLAS_H
#define XATLAS_H
#include <float.h> // FLT_MAX
+// -- GODOT start --
+#include <limits.h> // INT_MAX, UINT_MAX
+// -- GODOT end --
namespace xatlas {

File diff suppressed because it is too large Load diff

View file

@ -48,8 +48,8 @@ struct Chart
{ {
uint32_t atlasIndex; // Sub-atlas index. uint32_t atlasIndex; // Sub-atlas index.
uint32_t flags; uint32_t flags;
uint32_t *indexArray; uint32_t *faceArray;
uint32_t indexCount; uint32_t faceCount;
uint32_t material; uint32_t material;
}; };
@ -73,9 +73,10 @@ struct Mesh
uint32_t vertexCount; uint32_t vertexCount;
}; };
static const uint32_t kImageChartIndexMask = 0x3FFFFFFF; static const uint32_t kImageChartIndexMask = 0x1FFFFFFF;
static const uint32_t kImageHasChartIndexBit = 0x40000000; static const uint32_t kImageHasChartIndexBit = 0x80000000;
static const uint32_t kImageIsPaddingBit = 0x80000000; static const uint32_t kImageIsBilinearBit = 0x40000000;
static const uint32_t kImageIsPaddingBit = 0x20000000;
// Empty on creation. Populated after charts are packed. // Empty on creation. Populated after charts are packed.
struct Atlas struct Atlas
@ -173,7 +174,6 @@ struct ChartOptions
float textureSeamMetricWeight = 0.5f; float textureSeamMetricWeight = 0.5f;
float maxThreshold = 2.0f; // If total of all metrics * weights > maxThreshold, don't grow chart. Lower values result in more charts. float maxThreshold = 2.0f; // If total of all metrics * weights > maxThreshold, don't grow chart. Lower values result in more charts.
uint32_t growFaceCount = 32; // Grow this many faces at a time.
uint32_t maxIterations = 1; // Number of iterations of the chart growing and seeding phases. Higher values result in better charts. uint32_t maxIterations = 1; // Number of iterations of the chart growing and seeding phases. Higher values result in better charts.
}; };
@ -188,12 +188,24 @@ void ParameterizeCharts(Atlas *atlas, ParameterizeFunc func = nullptr);
struct PackOptions struct PackOptions
{ {
// Leave space around charts for texels that would be sampled by bilinear filtering.
bool bilinear = true;
// Align charts to 4x4 blocks. Also improves packing speed, since there are fewer possible chart locations to consider.
bool blockAlign = false;
// Slower, but gives the best result. If false, use random chart placement. // Slower, but gives the best result. If false, use random chart placement.
bool bruteForce = false; bool bruteForce = false;
// Create Atlas::image // Create Atlas::image
bool createImage = false; bool createImage = false;
// Charts larger than this will be scaled down. 0 means no limit.
uint32_t maxChartSize = 0;
// Number of pixels to pad charts with.
uint32_t padding = 0;
// Unit to texel scale. e.g. a 1x1 quad with texelsPerUnit of 32 will take up approximately 32x32 texels in the atlas. // Unit to texel scale. e.g. a 1x1 quad with texelsPerUnit of 32 will take up approximately 32x32 texels in the atlas.
// If 0, an estimated value will be calculated to approximately match the given resolution. // If 0, an estimated value will be calculated to approximately match the given resolution.
// If resolution is also 0, the estimated value will approximately match a 1024x1024 atlas. // If resolution is also 0, the estimated value will approximately match a 1024x1024 atlas.
@ -203,15 +215,6 @@ struct PackOptions
// If not 0, and texelsPerUnit is not 0, generate one or more atlases with that exact resolution. // If not 0, and texelsPerUnit is not 0, generate one or more atlases with that exact resolution.
// If not 0, and texelsPerUnit is 0, texelsPerUnit is estimated to approximately match the resolution. // If not 0, and texelsPerUnit is 0, texelsPerUnit is estimated to approximately match the resolution.
uint32_t resolution = 0; uint32_t resolution = 0;
// Charts larger than this will be scaled down.
uint32_t maxChartSize = 1024;
// Align charts to 4x4 blocks. Also improves packing speed, since there are fewer possible chart locations to consider.
bool blockAlign = false;
// Number of pixels to pad charts with.
uint32_t padding = 0;
}; };
// Call after ParameterizeCharts. Can be called multiple times to re-pack charts with different options. // Call after ParameterizeCharts. Can be called multiple times to re-pack charts with different options.
@ -240,7 +243,8 @@ void SetProgressCallback(Atlas *atlas, ProgressFunc progressFunc = nullptr, void
// Custom memory allocation. // Custom memory allocation.
typedef void *(*ReallocFunc)(void *, size_t); typedef void *(*ReallocFunc)(void *, size_t);
void SetRealloc(ReallocFunc reallocFunc); typedef void (*FreeFunc)(void *);
void SetAlloc(ReallocFunc reallocFunc, FreeFunc freeFunc = nullptr);
// Custom print function. // Custom print function.
typedef int (*PrintFunc)(const char *, ...); typedef int (*PrintFunc)(const char *, ...);