mirror of
https://github.com/Relintai/mesh_data_resource.git
synced 2025-02-20 17:14:31 +01:00
Removed xatlas completely, I couldn't get it to just respect charts without creating new ones.
This commit is contained in:
parent
5521afffa7
commit
49b594130d
3
SCsub
3
SCsub
@ -13,9 +13,6 @@ if os.path.isdir('../props'):
|
||||
if os.path.isdir('../mesh_utils'):
|
||||
module_env.Append(CPPDEFINES=['MESH_UTILS_PRESENT'])
|
||||
|
||||
if env["module_xatlas_unwrap_enabled"]:
|
||||
module_env.Append(CPPDEFINES=['XATLAS_PRESENT'])
|
||||
|
||||
module_env.add_source_files(env.modules_sources,"register_types.cpp")
|
||||
|
||||
module_env.add_source_files(env.modules_sources,"mesh_data_resource.cpp")
|
||||
|
@ -25,12 +25,6 @@ SOFTWARE.
|
||||
#include "core/variant.h"
|
||||
#include "core/version.h"
|
||||
|
||||
#ifdef XATLAS_PRESENT
|
||||
#include "core/local_vector.h"
|
||||
#include "scene/resources/surface_tool.h"
|
||||
#include "thirdparty/xatlas/xatlas.h"
|
||||
#endif
|
||||
|
||||
#if VERSION_MAJOR >= 4
|
||||
#define PoolVector Vector
|
||||
#endif
|
||||
@ -144,183 +138,9 @@ void MeshDataResource::recompute_aabb() {
|
||||
_aabb = aabb;
|
||||
}
|
||||
|
||||
struct MeshDataResourceUnwrapSurface {
|
||||
Vector<SurfaceTool::Vertex> vertices;
|
||||
uint32_t format;
|
||||
};
|
||||
|
||||
bool MeshDataResource::uv_unwrap(float p_texel_size) {
|
||||
#ifdef XATLAS_PRESENT
|
||||
if (_arrays.size() != ArrayMesh::ARRAY_MAX) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PoolVector3Array pool_vertices = _arrays[Mesh::ARRAY_VERTEX];
|
||||
|
||||
if (pool_vertices.size() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PoolVector3Array pool_normals = _arrays[Mesh::ARRAY_NORMAL];
|
||||
|
||||
ERR_FAIL_COND_V_MSG(pool_vertices.size() != pool_normals.size(), false, "Normals are required for lightmap unwrap.");
|
||||
|
||||
PoolIntArray pool_indices = _arrays[Mesh::ARRAY_INDEX];
|
||||
|
||||
MeshDataResourceUnwrapSurface mdrsurface;
|
||||
|
||||
mdrsurface.vertices = SurfaceTool::create_vertex_array_from_triangle_arrays(_arrays);
|
||||
|
||||
LocalVector<Vector3> vertices;
|
||||
LocalVector<Vector3> normals;
|
||||
LocalVector<int> indices;
|
||||
|
||||
vertices.resize(pool_vertices.size());
|
||||
normals.resize(pool_normals.size());
|
||||
|
||||
for (int i = 0; i < pool_vertices.size(); ++i) {
|
||||
vertices[i] = pool_vertices[i];
|
||||
normals[i] = pool_normals[i];
|
||||
}
|
||||
|
||||
indices.resize(pool_indices.size());
|
||||
|
||||
for (int i = 0; i < pool_indices.size(); ++i) {
|
||||
indices[i] = pool_indices[i];
|
||||
}
|
||||
|
||||
// set up input mesh
|
||||
xatlas::MeshDecl input_mesh;
|
||||
|
||||
input_mesh.indexData = indices.ptr();
|
||||
input_mesh.indexCount = indices.size();
|
||||
input_mesh.indexFormat = xatlas::IndexFormat::UInt32;
|
||||
|
||||
input_mesh.vertexCount = vertices.size();
|
||||
input_mesh.vertexPositionData = vertices.ptr();
|
||||
input_mesh.vertexPositionStride = sizeof(float) * 3;
|
||||
input_mesh.vertexNormalData = normals.ptr();
|
||||
input_mesh.vertexNormalStride = sizeof(uint32_t) * 3;
|
||||
input_mesh.vertexUvData = nullptr;
|
||||
input_mesh.vertexUvStride = 0;
|
||||
|
||||
xatlas::ChartOptions chart_options;
|
||||
chart_options.fixWinding = true;
|
||||
|
||||
xatlas::PackOptions pack_options;
|
||||
pack_options.padding = 1;
|
||||
pack_options.maxChartSize = 4094; // Lightmap atlassing needs 2 for padding between meshes, so 4096-2
|
||||
pack_options.blockAlign = true;
|
||||
pack_options.texelsPerUnit = 1.0 / p_texel_size;
|
||||
|
||||
xatlas::Atlas *atlas = xatlas::Create();
|
||||
|
||||
xatlas::AddMeshError err = xatlas::AddMesh(atlas, input_mesh, 1);
|
||||
ERR_FAIL_COND_V_MSG(err != xatlas::AddMeshError::Success, false, xatlas::StringForEnum(err));
|
||||
|
||||
xatlas::Generate(atlas, chart_options, pack_options);
|
||||
|
||||
int size_x = atlas->width;
|
||||
int size_y = atlas->height;
|
||||
|
||||
float w = 1;
|
||||
float h = 1;
|
||||
|
||||
if (w == 0 || h == 0) {
|
||||
xatlas::Destroy(atlas);
|
||||
return false; //could not bake because there is no area
|
||||
}
|
||||
|
||||
const xatlas::Mesh &output = atlas->meshes[0];
|
||||
|
||||
int *r_vertex = (int *)malloc(sizeof(int) * output.vertexCount);
|
||||
ERR_FAIL_NULL_V_MSG(r_vertex, false, "Out of memory.");
|
||||
float *r_uv = (float *)malloc(sizeof(float) * output.vertexCount * 2);
|
||||
ERR_FAIL_NULL_V_MSG(r_uv, false, "Out of memory.");
|
||||
int *r_index = (int *)malloc(sizeof(int) * output.indexCount);
|
||||
ERR_FAIL_NULL_V_MSG(r_index, false, "Out of memory.");
|
||||
|
||||
float max_x = 0;
|
||||
float max_y = 0;
|
||||
for (uint32_t i = 0; i < output.vertexCount; i++) {
|
||||
r_vertex[i] = output.vertexArray[i].xref;
|
||||
r_uv[i * 2 + 0] = output.vertexArray[i].uv[0] / w;
|
||||
r_uv[i * 2 + 1] = output.vertexArray[i].uv[1] / h;
|
||||
max_x = MAX(max_x, output.vertexArray[i].uv[0]);
|
||||
max_y = MAX(max_y, output.vertexArray[i].uv[1]);
|
||||
}
|
||||
|
||||
int r_vertex_count = output.vertexCount;
|
||||
|
||||
for (uint32_t i = 0; i < output.indexCount; i++) {
|
||||
r_index[i] = output.indexArray[i];
|
||||
}
|
||||
|
||||
int r_index_count = output.indexCount;
|
||||
|
||||
/*
|
||||
|
||||
print_verbose("Mesh: Gen indices: " + itos(gen_index_count));
|
||||
//go through all indices
|
||||
for (int i = 0; i < gen_index_count; i += 3) {
|
||||
ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 0]], (int)uv_indices.size(), ERR_BUG);
|
||||
ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 1]], (int)uv_indices.size(), ERR_BUG);
|
||||
ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 2]], (int)uv_indices.size(), ERR_BUG);
|
||||
|
||||
ERR_FAIL_COND_V(uv_indices[gen_vertices[gen_indices[i + 0]]].first != uv_indices[gen_vertices[gen_indices[i + 1]]].first || uv_indices[gen_vertices[gen_indices[i + 0]]].first != uv_indices[gen_vertices[gen_indices[i + 2]]].first, ERR_BUG);
|
||||
|
||||
int surface = uv_indices[gen_vertices[gen_indices[i + 0]]].first;
|
||||
|
||||
for (int j = 0; j < 3; j++) {
|
||||
SurfaceTool::Vertex v = lightmap_surfaces[surface].vertices[uv_indices[gen_vertices[gen_indices[i + j]]].second];
|
||||
|
||||
if (lightmap_surfaces[surface].format & ARRAY_FORMAT_COLOR) {
|
||||
surfaces_tools[surface]->add_color(v.color);
|
||||
}
|
||||
if (lightmap_surfaces[surface].format & ARRAY_FORMAT_TEX_UV) {
|
||||
surfaces_tools[surface]->add_uv(v.uv);
|
||||
}
|
||||
if (lightmap_surfaces[surface].format & ARRAY_FORMAT_NORMAL) {
|
||||
surfaces_tools[surface]->add_normal(v.normal);
|
||||
}
|
||||
if (lightmap_surfaces[surface].format & ARRAY_FORMAT_TANGENT) {
|
||||
Plane t;
|
||||
t.normal = v.tangent;
|
||||
t.d = v.binormal.dot(v.normal.cross(v.tangent)) < 0 ? -1 : 1;
|
||||
surfaces_tools[surface]->add_tangent(t);
|
||||
}
|
||||
if (lightmap_surfaces[surface].format & ARRAY_FORMAT_BONES) {
|
||||
surfaces_tools[surface]->add_bones(v.bones);
|
||||
}
|
||||
if (lightmap_surfaces[surface].format & ARRAY_FORMAT_WEIGHTS) {
|
||||
surfaces_tools[surface]->add_weights(v.weights);
|
||||
}
|
||||
|
||||
Vector2 uv2(gen_uvs[gen_indices[i + j] * 2 + 0], gen_uvs[gen_indices[i + j] * 2 + 1]);
|
||||
surfaces_tools[surface]->add_uv2(uv2);
|
||||
|
||||
surfaces_tools[surface]->add_vertex(v.vertex);
|
||||
}
|
||||
}
|
||||
|
||||
//generate surfaces
|
||||
for (unsigned int i = 0; i < surfaces_tools.size(); i++) {
|
||||
surfaces_tools[i]->index();
|
||||
surfaces_tools[i]->commit(Ref<ArrayMesh>((ArrayMesh *)this), lightmap_surfaces[i].format);
|
||||
}
|
||||
*/
|
||||
|
||||
xatlas::Destroy(atlas);
|
||||
|
||||
::free(r_vertex);
|
||||
::free(r_index);
|
||||
::free(r_uv);
|
||||
|
||||
return true;
|
||||
|
||||
#else
|
||||
//NYI
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
MeshDataResource::MeshDataResource() {
|
||||
|
Loading…
Reference in New Issue
Block a user