Added xatlas as alternative to thekla, forced it on.

Did some hacks to it to avoid it from failing on bad geometry.
This commit is contained in:
Juan Linietsky 2018-09-29 09:46:26 -03:00
parent c83742ba86
commit 6cbdeedf57
8 changed files with 7763 additions and 1 deletions

View File

@ -191,6 +191,7 @@ opts.Add(BoolVariable('builtin_pcre2', "Use the built-in PCRE2 library)", True))
opts.Add(BoolVariable('builtin_recast', "Use the built-in Recast library", True))
opts.Add(BoolVariable('builtin_squish', "Use the built-in squish library", True))
opts.Add(BoolVariable('builtin_thekla_atlas', "Use the built-in thekla_altas library", True))
opts.Add(BoolVariable('builtin_xatlas', "Use the built-in xatlas library", True))
opts.Add(BoolVariable('builtin_zlib', "Use the built-in zlib library", True))
opts.Add(BoolVariable('builtin_zstd', "Use the built-in Zstd library", True))

View File

@ -1,5 +1,6 @@
def can_build(env, platform):
return (env['tools'] and platform not in ["android", "ios"])
#return (env['tools'] and platform not in ["android", "ios"])
return False
def configure(env):
pass

View File

@ -0,0 +1,46 @@
#!/usr/bin/env python
import platform
Import('env')
Import('env_modules')
env_xatlas_unwrap = env_modules.Clone()
# Thirdparty source files
if env['builtin_xatlas']:
thirdparty_dir = "#thirdparty/xatlas/"
thirdparty_sources = [
"xatlas.cpp",
]
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
env_xatlas_unwrap.Append(CPPPATH=[thirdparty_dir])
# upstream uses c++11
if (not env.msvc):
env_xatlas_unwrap.Append(CXXFLAGS="-std=c++11")
if env["platform"] == 'x11':
# if not specifically one of the *BSD, then use LINUX as default
if platform.system() == "FreeBSD":
env_xatlas_unwrap.Append(CCFLAGS=["-DNV_OS_FREEBSD", "-DPOSH_COMPILER_GCC"])
elif platform.system() == "OpenBSD":
env_xatlas_unwrap.Append(CCFLAGS=["-DNV_OS_OPENBSD", "-DPOSH_COMPILER_GCC"])
else:
env_xatlas_unwrap.Append(CCFLAGS=["-DNV_OS_LINUX", "-DPOSH_COMPILER_GCC"])
elif env["platform"] == 'osx':
env_xatlas_unwrap.Append(CCFLAGS=["-DNV_OS_DARWIN", "-DPOSH_COMPILER_GCC"])
elif env["platform"] == 'windows':
if env.msvc:
env_xatlas_unwrap.Append(CCFLAGS=["-DNV_OS_WIN32", "-DNV_CC_MSVC", "-DPOSH_COMPILER_MSVC" ])
else:
env_xatlas_unwrap.Append(CCFLAGS=["-DNV_OS_MINGW", "-DNV_CC_GNUC", "-DPOSH_COMPILER_GCC", "-U__STRICT_ANSI__"])
env.Append(LIBS=["dbghelp"])
env_thirdparty = env_xatlas_unwrap.Clone()
env_thirdparty.disable_warnings()
env_thirdparty.add_source_files(env.modules_sources, thirdparty_sources)
# Godot source files
env_xatlas_unwrap.add_source_files(env.modules_sources, "*.cpp")

View File

@ -0,0 +1,5 @@
def can_build(env, platform):
return (env['tools'] and platform not in ["android", "ios"])
def configure(env):
pass

View File

@ -0,0 +1,133 @@
/*************************************************************************/
/* register_types.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "register_types.h"
#include "core/error_macros.h"
#include "thirdparty/xatlas/xatlas.h"
#include <stdio.h>
#include <stdlib.h>
extern bool (*array_mesh_lightmap_unwrap_callback)(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, const int *p_face_materials, int p_index_count, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y);
bool xatlas_mesh_lightmap_unwrap_callback(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, const int *p_face_materials, int p_index_count, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y) {
//set up input mesh
xatlas::InputMesh input_mesh;
input_mesh.indexData = malloc(sizeof(int) * p_index_count);
input_mesh.indexCount = p_index_count;
input_mesh.indexFormat = xatlas::IndexFormat::Float; //really xatlas?
input_mesh.faceMaterialData = (uint16_t *)malloc(sizeof(uint16_t) * p_index_count);
for (int i = 0; i < p_index_count; i++) {
int *index = (int *)input_mesh.indexData;
index[i] = p_indices[i];
}
for (int i = 0; i < p_index_count / 3; i++) {
uint16_t *mat_index = (uint16_t *)input_mesh.faceMaterialData;
mat_index[i] = p_face_materials[i];
}
input_mesh.vertexCount = p_vertex_count;
input_mesh.vertexPositionData = malloc(sizeof(float) * p_vertex_count * 3);
input_mesh.vertexPositionStride = sizeof(float) * 3;
input_mesh.vertexNormalData = malloc(sizeof(float) * p_vertex_count * 3);
input_mesh.vertexNormalStride = sizeof(float) * 3;
//material is a better hint than this i guess?
input_mesh.vertexUvData = NULL;
input_mesh.vertexUvStride = 0;
for (int i = 0; i < p_vertex_count * 3; i++) {
float *vertex_ptr = (float *)input_mesh.vertexPositionData;
float *normal_ptr = (float *)input_mesh.vertexNormalData;
vertex_ptr[i] = p_vertices[i];
normal_ptr[i] = p_normals[i];
}
xatlas::CharterOptions chart_options;
xatlas::PackerOptions pack_options;
pack_options.texelArea = 1.0 / p_texel_size;
pack_options.quality = 4;
xatlas::Atlas *atlas = xatlas::Create();
printf("adding mesh..\n");
xatlas::AddMeshError err = xatlas::AddMesh(atlas, input_mesh);
ERR_EXPLAINC(xatlas::StringForEnum(err.code));
ERR_FAIL_COND_V(err.code != xatlas::AddMeshErrorCode::Success, false);
printf("generate..\n");
xatlas::Generate(atlas, chart_options, pack_options);
*r_size_hint_x = xatlas::GetWidth(atlas);
*r_size_hint_y = xatlas::GetHeight(atlas);
float w = *r_size_hint_x;
float h = *r_size_hint_y;
printf("final texsize: %f,%f\n", w, h);
const xatlas::OutputMesh *const *output_meshes = xatlas::GetOutputMeshes(atlas);
const xatlas::OutputMesh *output = output_meshes[0];
*r_vertex = (int *)malloc(sizeof(int) * output->vertexCount);
*r_uv = (float *)malloc(sizeof(float) * output->vertexCount * 2);
*r_index = (int *)malloc(sizeof(int) * output->indexCount);
for (int i = 0; i < output->vertexCount; i++) {
(*r_vertex)[i] = output->vertexArray[i].xref;
(*r_uv)[i * 2 + 0] = output->vertexArray[i].uv[0];
(*r_uv)[i * 2 + 1] = output->vertexArray[i].uv[1];
}
*r_vertex_count = output->vertexCount;
for (int i = 0; i < output->indexCount; i++) {
(*r_index)[i] = output->indexArray[i];
}
*r_index_count = output->indexCount;
//xatlas::Destroy(atlas);
free((void *)input_mesh.indexData);
free((void *)input_mesh.vertexPositionData);
free((void *)input_mesh.vertexNormalData);
free((void *)input_mesh.faceMaterialData);
printf("done");
return true;
}
void register_xatlas_unwrap_types() {
array_mesh_lightmap_unwrap_callback = xatlas_mesh_lightmap_unwrap_callback;
}
void unregister_xatlas_unwrap_types() {
}

View File

@ -0,0 +1,32 @@
/*************************************************************************/
/* register_types.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
void register_xatlas_unwrap_types();
void unregister_xatlas_unwrap_types();

7384
thirdparty/xatlas/xatlas.cpp vendored Normal file

File diff suppressed because it is too large Load Diff

160
thirdparty/xatlas/xatlas.h vendored Normal file
View File

@ -0,0 +1,160 @@
// This code is in the public domain -- castanyo@yahoo.es
#pragma once
#ifndef XATLAS_H
#define XATLAS_H
#include <float.h> // FLT_MAX
#include <limits.h>
#include <stdint.h>
namespace xatlas {
typedef void (*PrintFunc)(const char *, ...);
struct Atlas;
struct CharterOptions {
float proxyFitMetricWeight;
float roundnessMetricWeight;
float straightnessMetricWeight;
float normalSeamMetricWeight;
float textureSeamMetricWeight;
float maxChartArea;
float maxBoundaryLength;
CharterOptions() {
// These are the default values we use on The Witness.
proxyFitMetricWeight = 2.0f;
roundnessMetricWeight = 0.01f;
straightnessMetricWeight = 6.0f;
normalSeamMetricWeight = 4.0f;
textureSeamMetricWeight = 0.5f;
/*
proxyFitMetricWeight = 1.0f;
roundnessMetricWeight = 0.1f;
straightnessMetricWeight = 0.25f;
normalSeamMetricWeight = 1.0f;
textureSeamMetricWeight = 0.1f;
*/
maxChartArea = FLT_MAX;
maxBoundaryLength = FLT_MAX;
}
};
struct PackMethod {
enum Enum {
TexelArea, // texel_area determines resolution
ApproximateResolution, // guess texel_area to approximately match desired resolution
ExactResolution // run the packer multiple times to exactly match the desired resolution (slow)
};
};
struct PackerOptions {
PackMethod::Enum method;
// 0 - brute force
// 1 - 4096 attempts
// 2 - 2048
// 3 - 1024
// 4 - 512
// other - 256
// Avoid brute force packing, since it can be unusably slow in some situations.
int quality;
float texelArea; // This is not really texel area, but 1 / texel width?
uint32_t resolution;
bool blockAlign; // Align charts to 4x4 blocks.
bool conservative; // Pack charts with extra padding.
int padding;
PackerOptions() {
method = PackMethod::ApproximateResolution;
quality = 1;
texelArea = 8;
resolution = 512;
blockAlign = false;
conservative = false;
padding = 0;
}
};
struct AddMeshErrorCode {
enum Enum {
Success,
AlreadyAddedEdge, // index0 and index1 are the edge indices
DegenerateColocalEdge, // index0 and index1 are the edge indices
DegenerateEdge, // index0 and index1 are the edge indices
DuplicateEdge, // index0 and index1 are the edge indices
IndexOutOfRange, // index0 is the index
InvalidIndexCount, // not evenly divisible by 3 - expecting triangles
ZeroAreaFace,
ZeroLengthEdge // index0 and index1 are the edge indices
};
};
struct AddMeshError {
AddMeshErrorCode::Enum code;
uint32_t face;
uint32_t index0, index1;
};
struct IndexFormat {
enum Enum {
HalfFloat,
Float
};
};
struct InputMesh {
uint32_t vertexCount;
const void *vertexPositionData;
uint32_t vertexPositionStride;
const void *vertexNormalData; // optional
uint32_t vertexNormalStride; // optional
// optional
// The input UVs are provided as a hint to the chart generator.
const void *vertexUvData;
uint32_t vertexUvStride;
uint32_t indexCount;
const void *indexData;
IndexFormat::Enum indexFormat;
// optional. indexCount / 3 in length.
// Charter also uses material boundaries as a hint to cut charts.
const uint16_t *faceMaterialData;
};
struct OutputChart {
uint32_t *indexArray;
uint32_t indexCount;
};
struct OutputVertex {
float uv[2];
uint32_t xref; // Index of input vertex from which this output vertex originated.
};
struct OutputMesh {
OutputChart *chartArray;
uint32_t chartCount;
uint32_t *indexArray;
uint32_t indexCount;
OutputVertex *vertexArray;
uint32_t vertexCount;
};
void SetPrint(PrintFunc print);
Atlas *Create();
void Destroy(Atlas *atlas);
// useColocalVertices - generates fewer charts (good), but is more sensitive to bad geometry.
AddMeshError AddMesh(Atlas *atlas, const InputMesh &mesh, bool useColocalVertices = true);
void Generate(Atlas *atlas, CharterOptions charterOptions = CharterOptions(), PackerOptions packerOptions = PackerOptions());
uint32_t GetWidth(const Atlas *atlas);
uint32_t GetHeight(const Atlas *atlas);
uint32_t GetNumCharts(const Atlas *atlas);
const OutputMesh *const *GetOutputMeshes(const Atlas *atlas);
const char *StringForEnum(AddMeshErrorCode::Enum error);
} // namespace xatlas
#endif // XATLAS_H