mirror of
https://github.com/Relintai/voxelman.git
synced 2025-01-27 15:19:18 +01:00
Added mesh simplifier to the build, fixed compile, and started work on bindings.
This commit is contained in:
parent
3ef382b29f
commit
b0b38396c4
5
SCsub
5
SCsub
@ -7,6 +7,7 @@ module_env = env.Clone()
|
|||||||
if os.path.isdir('../mesh_data_resource'):
|
if os.path.isdir('../mesh_data_resource'):
|
||||||
module_env.Append(CPPDEFINES=['MESH_DATA_RESOURCE_PRESENT'])
|
module_env.Append(CPPDEFINES=['MESH_DATA_RESOURCE_PRESENT'])
|
||||||
|
|
||||||
|
module_env.Append(CPPFLAGS=["-std=c++11"])
|
||||||
|
|
||||||
sources = [
|
sources = [
|
||||||
|
|
||||||
@ -31,6 +32,9 @@ sources = [
|
|||||||
"meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.cpp",
|
"meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.cpp",
|
||||||
"meshers/transvoxel_uv_mesher/transvoxel_tables.cpp",
|
"meshers/transvoxel_uv_mesher/transvoxel_tables.cpp",
|
||||||
|
|
||||||
|
"meshers/mesh_simplifiers/mesh_simplifier.cpp",
|
||||||
|
"meshers/mesh_simplifiers/fast_quadratic_mesh_simplifier.cpp",
|
||||||
|
|
||||||
"world/voxel_world.cpp",
|
"world/voxel_world.cpp",
|
||||||
"world/voxel_chunk.cpp",
|
"world/voxel_chunk.cpp",
|
||||||
"world/voxel_structure.cpp",
|
"world/voxel_structure.cpp",
|
||||||
@ -56,7 +60,6 @@ sources = [
|
|||||||
"clutter/ground_clutter_foliage.cpp",
|
"clutter/ground_clutter_foliage.cpp",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
if ARGUMENTS.get('custom_modules_shared', 'no') == 'yes':
|
if ARGUMENTS.get('custom_modules_shared', 'no') == 'yes':
|
||||||
# Shared lib compilation
|
# Shared lib compilation
|
||||||
module_env.Append(CCFLAGS=['-fPIC'])
|
module_env.Append(CCFLAGS=['-fPIC'])
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include "mesh_simplifier.h"
|
#include "fast_quadratic_mesh_simplifier.h"
|
||||||
|
|
||||||
#include "../voxel_mesher.h"
|
#include "../voxel_mesher.h"
|
||||||
|
|
||||||
@ -6,7 +6,7 @@
|
|||||||
//Mesh Simplification
|
//Mesh Simplification
|
||||||
//Ported from https://github.com/Whinarn/UnityMeshSimplifier
|
//Ported from https://github.com/Whinarn/UnityMeshSimplifier
|
||||||
//Original license: MIT License Copyright (c) 2017 Mattias Edlund
|
//Original license: MIT License Copyright (c) 2017 Mattias Edlund
|
||||||
void MeshSimplifier::initialize(Ref<VoxelMesher> mesher) {
|
void FastQuadraticMeshSimplifier::initialize(Ref<VoxelMesher> mesher) {
|
||||||
_mesher = mesher;
|
_mesher = mesher;
|
||||||
|
|
||||||
_vertices = mesher->get_vertices();
|
_vertices = mesher->get_vertices();
|
||||||
@ -36,7 +36,7 @@ void MeshSimplifier::initialize(Ref<VoxelMesher> mesher) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MeshSimplifier::refresh_vertices() {
|
void FastQuadraticMeshSimplifier::refresh_vertices() {
|
||||||
_vertices.resize(_mu_vertices.size());
|
_vertices.resize(_mu_vertices.size());
|
||||||
for (int i = 0; i < _mu_vertices.size(); ++i) {
|
for (int i = 0; i < _mu_vertices.size(); ++i) {
|
||||||
MUVertex vert = _mu_vertices[i];
|
MUVertex vert = _mu_vertices[i];
|
||||||
@ -49,9 +49,9 @@ void MeshSimplifier::refresh_vertices() {
|
|||||||
//private ResizableArray<Vertex> vertices = null;
|
//private ResizableArray<Vertex> vertices = null;
|
||||||
|
|
||||||
//Mesh Simplification
|
//Mesh Simplification
|
||||||
//Ported from https://github.com/Whinarn/UnityMeshSimplifier
|
//Ported from https://github.com/Whinarn/UnityFastQuadraticMeshSimplifier
|
||||||
//Original license: MIT License Copyright (c) 2017 Mattias Edlund
|
//Original license: MIT License Copyright (c) 2017 Mattias Edlund
|
||||||
void MeshSimplifier::SimplifyMesh(float quality) {
|
void FastQuadraticMeshSimplifier::SimplifyMesh(float quality) {
|
||||||
quality = CLAMP(quality, 0, 1);
|
quality = CLAMP(quality, 0, 1);
|
||||||
|
|
||||||
int deletedTris = 0;
|
int deletedTris = 0;
|
||||||
@ -74,7 +74,7 @@ void MeshSimplifier::SimplifyMesh(float quality) {
|
|||||||
|
|
||||||
// Clear dirty flag
|
// Clear dirty flag
|
||||||
for (int i = 0; i < _mu_triangles.size(); ++i) {
|
for (int i = 0; i < _mu_triangles.size(); ++i) {
|
||||||
_mu_triangles[i].dirty = false;
|
_mu_triangles[i].set_dirty(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// All triangles with edges below the threshold will be removed
|
// All triangles with edges below the threshold will be removed
|
||||||
@ -95,9 +95,9 @@ void MeshSimplifier::SimplifyMesh(float quality) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Mesh Simplification
|
//Mesh Simplification
|
||||||
//Ported from https://github.com/Whinarn/UnityMeshSimplifier
|
//Ported from https://github.com/Whinarn/UnityFastQuadraticMeshSimplifier
|
||||||
//Original license: MIT License Copyright (c) 2017 Mattias Edlund
|
//Original license: MIT License Copyright (c) 2017 Mattias Edlund
|
||||||
void MeshSimplifier::SimplifyMeshLossless() {
|
void FastQuadraticMeshSimplifier::SimplifyMeshLossless() {
|
||||||
int deletedTris = 0;
|
int deletedTris = 0;
|
||||||
PoolVector<bool> deleted0;
|
PoolVector<bool> deleted0;
|
||||||
PoolVector<bool> deleted1;
|
PoolVector<bool> deleted1;
|
||||||
@ -109,7 +109,7 @@ void MeshSimplifier::SimplifyMeshLossless() {
|
|||||||
|
|
||||||
// Clear dirty flag
|
// Clear dirty flag
|
||||||
for (int i = 0; i < _mu_triangles.size(); ++i) {
|
for (int i = 0; i < _mu_triangles.size(); ++i) {
|
||||||
_mu_triangles[i].dirty = false;
|
_mu_triangles[i].set_dirty(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// All triangles with edges below the threshold will be removed
|
// All triangles with edges below the threshold will be removed
|
||||||
@ -134,7 +134,7 @@ void MeshSimplifier::SimplifyMeshLossless() {
|
|||||||
//Debug.LogFormat("Finished simplification with triangle count {0}", this.triangles.Length);
|
//Debug.LogFormat("Finished simplification with triangle count {0}", this.triangles.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MeshSimplifier::UpdateMesh(int iteration) {
|
void FastQuadraticMeshSimplifier::UpdateMesh(int iteration) {
|
||||||
if (iteration > 0) // compact triangles
|
if (iteration > 0) // compact triangles
|
||||||
{
|
{
|
||||||
int dst = 0;
|
int dst = 0;
|
||||||
@ -160,9 +160,9 @@ void MeshSimplifier::UpdateMesh(int iteration) {
|
|||||||
|
|
||||||
int vsize = 0;
|
int vsize = 0;
|
||||||
for (int i = 0; i < _mu_vertices.size(); i++) {
|
for (int i = 0; i < _mu_vertices.size(); i++) {
|
||||||
_mu_vertices[i].borderEdge = false;
|
_mu_vertices[i].set_border_edge(false);
|
||||||
_mu_vertices[i].uvSeamEdge = false;
|
_mu_vertices[i].set_uv_seam_edge(false);
|
||||||
_mu_vertices[i].uvFoldoverEdge = false;
|
_mu_vertices[i].set_uv_foldover_edge(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ofs;
|
int ofs;
|
||||||
@ -203,7 +203,7 @@ void MeshSimplifier::UpdateMesh(int iteration) {
|
|||||||
for (int j = 0; j < vsize; j++) {
|
for (int j = 0; j < vsize; j++) {
|
||||||
if (vcount[j] == 1) {
|
if (vcount[j] == 1) {
|
||||||
id = vids[j];
|
id = vids[j];
|
||||||
_mu_vertices[id].borderEdge = true;
|
_mu_vertices[id].set_border_edge(true);
|
||||||
++borderVertexCount;
|
++borderVertexCount;
|
||||||
|
|
||||||
if (enableSmartLink) {
|
if (enableSmartLink) {
|
||||||
@ -260,16 +260,16 @@ void MeshSimplifier::UpdateMesh(int iteration) {
|
|||||||
double sqrMagnitude = sqrX + sqrY + sqrZ;
|
double sqrMagnitude = sqrX + sqrY + sqrZ;
|
||||||
|
|
||||||
if (sqrMagnitude <= vertexLinkDistanceSqr) {
|
if (sqrMagnitude <= vertexLinkDistanceSqr) {
|
||||||
borderVertices.get(j).index = -1; // NOTE: This makes sure that the "other" vertex is not processed again
|
borderVertices.get(j).set_index(-1); // NOTE: This makes sure that the "other" vertex is not processed again
|
||||||
_mu_vertices[myIndex].borderEdge = false;
|
_mu_vertices[myIndex].set_border_edge(false);
|
||||||
_mu_vertices[otherIndex].borderEdge = false;
|
_mu_vertices[otherIndex].set_border_edge(false);
|
||||||
|
|
||||||
if (AreUVsTheSame(0, myIndex, otherIndex)) {
|
if (AreUVsTheSame(0, myIndex, otherIndex)) {
|
||||||
_mu_vertices[myIndex].uvFoldoverEdge = true;
|
_mu_vertices[myIndex].set_uv_foldover_edge(true);
|
||||||
_mu_vertices[otherIndex].uvFoldoverEdge = true;
|
_mu_vertices[otherIndex].set_uv_foldover_edge(true);
|
||||||
} else {
|
} else {
|
||||||
_mu_vertices[myIndex].uvSeamEdge = true;
|
_mu_vertices[myIndex].set_uv_seam_edge(true);
|
||||||
_mu_vertices[otherIndex].uvSeamEdge = true;
|
_mu_vertices[otherIndex].set_uv_seam_edge(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
int otherTriangleCount = _mu_vertices[otherIndex].tcount;
|
int otherTriangleCount = _mu_vertices[otherIndex].tcount;
|
||||||
@ -323,32 +323,32 @@ void MeshSimplifier::UpdateMesh(int iteration) {
|
|||||||
for (int i = 0; i < _mu_triangles.size(); ++i) {
|
for (int i = 0; i < _mu_triangles.size(); ++i) {
|
||||||
// Calc Edge Error
|
// Calc Edge Error
|
||||||
MUTriangle triangle = _mu_triangles[i];
|
MUTriangle triangle = _mu_triangles[i];
|
||||||
_mu_triangles[i].err0 = CalculateError(_mu_vertices[triangle.v0], _mu_vertices[triangle.v1], &dummy);
|
_mu_triangles[i].set_err0(CalculateError(_mu_vertices[triangle.v0], _mu_vertices[triangle.v1], &dummy));
|
||||||
_mu_triangles[i].err1 = CalculateError(_mu_vertices[triangle.v1], _mu_vertices[triangle.v2], &dummy);
|
_mu_triangles[i].set_err1(CalculateError(_mu_vertices[triangle.v1], _mu_vertices[triangle.v2], &dummy));
|
||||||
_mu_triangles[i].err2 = CalculateError(_mu_vertices[triangle.v2], _mu_vertices[triangle.v0], &dummy);
|
_mu_triangles[i].set_err2(CalculateError(_mu_vertices[triangle.v2], _mu_vertices[triangle.v0], &dummy));
|
||||||
_mu_triangles[i].err3 = MeshSimplifier::Min3(_mu_triangles[i].err0, _mu_triangles[i].err1, _mu_triangles[i].err2);
|
_mu_triangles[i].set_err3(FastQuadraticMeshSimplifier::Min3(_mu_triangles[i].err0, _mu_triangles[i].err1, _mu_triangles[i].err2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MeshSimplifier::UpdateReferences() {
|
void FastQuadraticMeshSimplifier::UpdateReferences() {
|
||||||
// Init Reference ID list
|
// Init Reference ID list
|
||||||
for (int i = 0; i < _mu_vertices.size(); i++) {
|
for (int i = 0; i < _mu_vertices.size(); i++) {
|
||||||
_mu_vertices[i].tstart = 0;
|
_mu_vertices[i].set_tstart(0);
|
||||||
_mu_vertices[i].tcount = 0;
|
_mu_vertices[i].set_tcount(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < _mu_triangles.size(); i++) {
|
for (int i = 0; i < _mu_triangles.size(); i++) {
|
||||||
++_mu_vertices[_mu_triangles[i].v0].tcount;
|
_mu_vertices[_mu_triangles[i].v0].set_tcount(_mu_vertices[_mu_triangles[i].v0].tcount + 1);
|
||||||
++_mu_vertices[_mu_triangles[i].v1].tcount;
|
_mu_vertices[_mu_triangles[i].v1].set_tcount(_mu_vertices[_mu_triangles[i].v1].tcount + 1);
|
||||||
++_mu_vertices[_mu_triangles[i].v2].tcount;
|
_mu_vertices[_mu_triangles[i].v2].set_tcount(_mu_vertices[_mu_triangles[i].v2].tcount + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int tstart = 0;
|
int tstart = 0;
|
||||||
for (int i = 0; i < _mu_vertices.size(); i++) {
|
for (int i = 0; i < _mu_vertices.size(); i++) {
|
||||||
_mu_vertices[i].tstart = tstart;
|
_mu_vertices[i].set_tstart(tstart);
|
||||||
tstart += _mu_vertices[i].tcount;
|
tstart += _mu_vertices[i].tcount;
|
||||||
_mu_vertices[i].tcount = 0;
|
_mu_vertices[i].set_tcount(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write References
|
// Write References
|
||||||
@ -368,20 +368,20 @@ void MeshSimplifier::UpdateReferences() {
|
|||||||
_mu_refs[start1 + count1].Set(i, 1);
|
_mu_refs[start1 + count1].Set(i, 1);
|
||||||
_mu_refs[start2 + count2].Set(i, 2);
|
_mu_refs[start2 + count2].Set(i, 2);
|
||||||
|
|
||||||
++_mu_vertices[v0].tcount;
|
_mu_vertices[v0].set_tcount(_mu_vertices[v0].tcount + 1);
|
||||||
++_mu_vertices[v1].tcount;
|
_mu_vertices[v1].set_tcount(_mu_vertices[v1].tcount + 1);
|
||||||
++_mu_vertices[v2].tcount;
|
_mu_vertices[v2].set_tcount(_mu_vertices[v2].tcount + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Finally compact mesh before exiting.
|
/// Finally compact mesh before exiting.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void MeshSimplifier::CompactMesh() {
|
void FastQuadraticMeshSimplifier::CompactMesh() {
|
||||||
int dst = 0;
|
int dst = 0;
|
||||||
|
|
||||||
for (int i = 0; i < _mu_vertices.size(); i++) {
|
for (int i = 0; i < _mu_vertices.size(); i++) {
|
||||||
_mu_vertices[i].tcount = 0;
|
_mu_vertices[i].set_tcount(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < _mu_triangles.size(); i++) {
|
for (int i = 0; i < _mu_triangles.size(); i++) {
|
||||||
@ -415,9 +415,9 @@ void MeshSimplifier::CompactMesh() {
|
|||||||
int newTriangleIndex = ++dst;
|
int newTriangleIndex = ++dst;
|
||||||
_mu_triangles[newTriangleIndex] = triangle;
|
_mu_triangles[newTriangleIndex] = triangle;
|
||||||
|
|
||||||
_mu_vertices[triangle.v0].tcount = 1;
|
_mu_vertices[triangle.v0].set_tcount(1);
|
||||||
_mu_vertices[triangle.v1].tcount = 1;
|
_mu_vertices[triangle.v1].set_tcount(1);
|
||||||
_mu_vertices[triangle.v2].tcount = 1;
|
_mu_vertices[triangle.v2].set_tcount(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -463,9 +463,9 @@ void MeshSimplifier::CompactMesh() {
|
|||||||
if (_indices.size() > 0) _indices.resize(dst);
|
if (_indices.size() > 0) _indices.resize(dst);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MeshSimplifier::AreUVsTheSame(int channel, int indexA, int indexB) {
|
bool FastQuadraticMeshSimplifier::AreUVsTheSame(int channel, int indexA, int indexB) {
|
||||||
if (_uv2s.size() > 0) {
|
if (_uv2s.size() > 0) {
|
||||||
Vector2 vertUV = _uv2s[channel];
|
//Vector2 vertUV = _uv2s[channel];
|
||||||
|
|
||||||
Vector2 uvA = _uv2s[indexA];
|
Vector2 uvA = _uv2s[indexA];
|
||||||
Vector2 uvB = _uv2s[indexB];
|
Vector2 uvB = _uv2s[indexB];
|
||||||
@ -476,7 +476,7 @@ bool MeshSimplifier::AreUVsTheSame(int channel, int indexA, int indexB) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Remove vertices and mark deleted triangles
|
/// Remove vertices and mark deleted triangles
|
||||||
int MeshSimplifier::RemoveVertexPass(int startTrisCount, int targetTrisCount, double threshold, PoolVector<bool> deleted0, PoolVector<bool> deleted1, int deletedTris) {
|
int FastQuadraticMeshSimplifier::RemoveVertexPass(int startTrisCount, int targetTrisCount, double threshold, PoolVector<bool> &deleted0, PoolVector<bool> &deleted1, int deletedTris) {
|
||||||
Vector3 p;
|
Vector3 p;
|
||||||
Vector3 barycentricCoord;
|
Vector3 barycentricCoord;
|
||||||
for (int tid = 0; tid < _mu_triangles.size(); tid++) {
|
for (int tid = 0; tid < _mu_triangles.size(); tid++) {
|
||||||
@ -519,9 +519,9 @@ int MeshSimplifier::RemoveVertexPass(int startTrisCount, int targetTrisCount, do
|
|||||||
deleted1.resize(_mu_vertices[i1].tcount); // normals temporarily
|
deleted1.resize(_mu_vertices[i1].tcount); // normals temporarily
|
||||||
|
|
||||||
// Don't remove if flipped
|
// Don't remove if flipped
|
||||||
if (Flipped(&p, i0, i1, &(_mu_vertices[i0]), deleted0))
|
if (Flipped(p, i0, i1, _mu_vertices[i0], deleted0))
|
||||||
continue;
|
continue;
|
||||||
if (Flipped(&p, i1, i0, &(_mu_vertices[i1]), deleted1))
|
if (Flipped(p, i1, i0, _mu_vertices[i1], deleted1))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Calculate the barycentric coordinates within the triangle
|
// Calculate the barycentric coordinates within the triangle
|
||||||
@ -544,8 +544,8 @@ int MeshSimplifier::RemoveVertexPass(int startTrisCount, int targetTrisCount, do
|
|||||||
}
|
}
|
||||||
|
|
||||||
int tstart = _mu_refs.size();
|
int tstart = _mu_refs.size();
|
||||||
deletedTris = UpdateTriangles(i0, ia0, &(_mu_vertices[i0]), deleted0, deletedTris);
|
deletedTris = UpdateTriangles(i0, ia0, _mu_vertices[i0], deleted0, deletedTris);
|
||||||
deletedTris = UpdateTriangles(i0, ia0, &(_mu_vertices[i1]), deleted1, deletedTris);
|
deletedTris = UpdateTriangles(i0, ia0, _mu_vertices[i1], deleted1, deletedTris);
|
||||||
|
|
||||||
int tcount = _mu_refs.size() - tstart;
|
int tcount = _mu_refs.size() - tstart;
|
||||||
if (tcount <= _mu_vertices[i0].tcount) {
|
if (tcount <= _mu_vertices[i0].tcount) {
|
||||||
@ -558,10 +558,10 @@ int MeshSimplifier::RemoveVertexPass(int startTrisCount, int targetTrisCount, do
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// append
|
// append
|
||||||
_mu_vertices[i0].tstart = tstart;
|
_mu_vertices[i0].set_tstart(tstart);
|
||||||
}
|
}
|
||||||
|
|
||||||
_mu_vertices[i0].tcount = tcount;
|
_mu_vertices[i0].set_tcount(tcount);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -573,11 +573,11 @@ int MeshSimplifier::RemoveVertexPass(int startTrisCount, int targetTrisCount, do
|
|||||||
return deletedTris;
|
return deletedTris;
|
||||||
}
|
}
|
||||||
|
|
||||||
double MeshSimplifier::VertexError(SymmetricMatrix q, double x, double y, double z) {
|
double FastQuadraticMeshSimplifier::VertexError(SymmetricMatrix q, double x, double y, double z) {
|
||||||
return q.m0 * x * x + 2 * q.m1 * x * y + 2 * q.m2 * x * z + 2 * q.m3 * x + q.m4 * y * y + 2 * q.m5 * y * z + 2 * q.m6 * y + q.m7 * z * z + 2 * q.m8 * z + q.m9;
|
return q.m0 * x * x + 2 * q.m1 * x * y + 2 * q.m2 * x * z + 2 * q.m3 * x + q.m4 * y * y + 2 * q.m5 * y * z + 2 * q.m6 * y + q.m7 * z * z + 2 * q.m8 * z + q.m9;
|
||||||
}
|
}
|
||||||
|
|
||||||
double MeshSimplifier::CalculateError(MUVertex vert0, MUVertex vert1, Vector3 *result) {
|
double FastQuadraticMeshSimplifier::CalculateError(MUVertex vert0, MUVertex vert1, Vector3 *result) {
|
||||||
// compute interpolated vertex
|
// compute interpolated vertex
|
||||||
SymmetricMatrix q = (vert0.q + vert1.q);
|
SymmetricMatrix q = (vert0.q + vert1.q);
|
||||||
bool borderEdge = (vert0.borderEdge & vert1.borderEdge);
|
bool borderEdge = (vert0.borderEdge & vert1.borderEdge);
|
||||||
@ -599,7 +599,7 @@ double MeshSimplifier::CalculateError(MUVertex vert0, MUVertex vert1, Vector3 *r
|
|||||||
double error2 = VertexError(q, p2.x, p2.y, p2.z);
|
double error2 = VertexError(q, p2.x, p2.y, p2.z);
|
||||||
double error3 = VertexError(q, p3.x, p3.y, p3.z);
|
double error3 = VertexError(q, p3.x, p3.y, p3.z);
|
||||||
|
|
||||||
error = MeshSimplifier::Min3(error1, error2, error3);
|
error = FastQuadraticMeshSimplifier::Min3(error1, error2, error3);
|
||||||
if (error == error3) {
|
if (error == error3) {
|
||||||
result->x = p3.x;
|
result->x = p3.x;
|
||||||
result->y = p3.y;
|
result->y = p3.y;
|
||||||
@ -621,20 +621,20 @@ double MeshSimplifier::CalculateError(MUVertex vert0, MUVertex vert1, Vector3 *r
|
|||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
int MeshSimplifier::UpdateTriangles(int i0, int ia0, MUVertex *v, PoolVector<bool> deleted, int p_deletedTriangles) {
|
int FastQuadraticMeshSimplifier::UpdateTriangles(int i0, int ia0, const MUVertex &v, PoolVector<bool> &deleted, int p_deletedTriangles) {
|
||||||
Vector3 p;
|
Vector3 p;
|
||||||
int deletedTriangles = p_deletedTriangles;
|
int deletedTriangles = p_deletedTriangles;
|
||||||
int tcount = v->tcount;
|
int tcount = v.tcount;
|
||||||
|
|
||||||
for (int k = 0; k < tcount; k++) {
|
for (int k = 0; k < tcount; k++) {
|
||||||
MURef r = _mu_refs[v->tstart + k];
|
MURef r = _mu_refs[v.tstart + k];
|
||||||
int tid = r.tid;
|
int tid = r.tid;
|
||||||
MUTriangle t = _mu_triangles[tid];
|
MUTriangle t = _mu_triangles[tid];
|
||||||
if (t.deleted)
|
if (t.deleted)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (deleted[k]) {
|
if (deleted[k]) {
|
||||||
_mu_triangles[tid].deleted = true;
|
_mu_triangles[tid].set_deleted(true);
|
||||||
++deletedTriangles;
|
++deletedTriangles;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -648,7 +648,7 @@ int MeshSimplifier::UpdateTriangles(int i0, int ia0, MUVertex *v, PoolVector<boo
|
|||||||
t.err0 = CalculateError(_mu_vertices[t.v0], _mu_vertices[t.v1], &p);
|
t.err0 = CalculateError(_mu_vertices[t.v0], _mu_vertices[t.v1], &p);
|
||||||
t.err1 = CalculateError(_mu_vertices[t.v1], _mu_vertices[t.v2], &p);
|
t.err1 = CalculateError(_mu_vertices[t.v1], _mu_vertices[t.v2], &p);
|
||||||
t.err2 = CalculateError(_mu_vertices[t.v2], _mu_vertices[t.v0], &p);
|
t.err2 = CalculateError(_mu_vertices[t.v2], _mu_vertices[t.v0], &p);
|
||||||
t.err3 = MeshSimplifier::Min3(t.err0, t.err1, t.err2);
|
t.err3 = FastQuadraticMeshSimplifier::Min3(t.err0, t.err1, t.err2);
|
||||||
|
|
||||||
_mu_triangles[tid] = t;
|
_mu_triangles[tid] = t;
|
||||||
_mu_refs.push_back(r);
|
_mu_refs.push_back(r);
|
||||||
@ -657,11 +657,11 @@ int MeshSimplifier::UpdateTriangles(int i0, int ia0, MUVertex *v, PoolVector<boo
|
|||||||
return deletedTriangles;
|
return deletedTriangles;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MeshSimplifier::Flipped(Vector3 *p, int i0, int i1, MUVertex *v0, PoolVector<bool> &deleted) {
|
bool FastQuadraticMeshSimplifier::Flipped(const Vector3 &p, int i0, int i1, const MUVertex &v0, PoolVector<bool> &deleted) {
|
||||||
int tcount = v0->tcount;
|
int tcount = v0.tcount;
|
||||||
|
|
||||||
for (int k = 0; k < tcount; k++) {
|
for (int k = 0; k < tcount; k++) {
|
||||||
MURef r = _mu_refs[v0->tstart + k];
|
MURef r = _mu_refs[v0.tstart + k];
|
||||||
if (_mu_triangles[r.tid].deleted)
|
if (_mu_triangles[r.tid].deleted)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -673,9 +673,9 @@ bool MeshSimplifier::Flipped(Vector3 *p, int i0, int i1, MUVertex *v0, PoolVecto
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 d1 = _mu_vertices[id1].p - (*p);
|
Vector3 d1 = _mu_vertices[id1].p - p;
|
||||||
d1.normalize();
|
d1.normalize();
|
||||||
Vector3 d2 = _mu_vertices[id2].p - (*p);
|
Vector3 d2 = _mu_vertices[id2].p - p;
|
||||||
d2.normalize();
|
d2.normalize();
|
||||||
double dot = d1.dot(d2);
|
double dot = d1.dot(d2);
|
||||||
if (Math::abs(dot) > 0.999)
|
if (Math::abs(dot) > 0.999)
|
||||||
@ -692,7 +692,7 @@ bool MeshSimplifier::Flipped(Vector3 *p, int i0, int i1, MUVertex *v0, PoolVecto
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 MeshSimplifier::CalculateBarycentricCoords(Vector3 const &point, Vector3 const &a, Vector3 const &b, Vector3 const &c) {
|
Vector3 FastQuadraticMeshSimplifier::CalculateBarycentricCoords(Vector3 const &point, Vector3 const &a, Vector3 const &b, Vector3 const &c) {
|
||||||
Vector3 v0 = (Vector3)(b - a), v1 = (Vector3)(c - a), v2 = (Vector3)(point - a);
|
Vector3 v0 = (Vector3)(b - a), v1 = (Vector3)(c - a), v2 = (Vector3)(point - a);
|
||||||
float d00 = v0.dot(v0);
|
float d00 = v0.dot(v0);
|
||||||
float d01 = v0.dot(v1);
|
float d01 = v0.dot(v1);
|
||||||
@ -707,7 +707,7 @@ Vector3 MeshSimplifier::CalculateBarycentricCoords(Vector3 const &point, Vector3
|
|||||||
return Vector3(u, v, w);
|
return Vector3(u, v, w);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MeshSimplifier::InterpolateVertexAttributes(int dst, int i0, int i1, int i2, Vector3 &barycentricCoord) {
|
void FastQuadraticMeshSimplifier::InterpolateVertexAttributes(int dst, int i0, int i1, int i2, Vector3 &barycentricCoord) {
|
||||||
if (_normals.size() > 0) {
|
if (_normals.size() > 0) {
|
||||||
_normals[dst] = (_normals[i0] * barycentricCoord.x) + (_normals[i1] * barycentricCoord.y) + (_normals[i2] * barycentricCoord.z).normalized();
|
_normals[dst] = (_normals[i0] * barycentricCoord.x) + (_normals[i1] * barycentricCoord.y) + (_normals[i2] * barycentricCoord.z).normalized();
|
||||||
}
|
}
|
||||||
@ -725,7 +725,7 @@ void MeshSimplifier::InterpolateVertexAttributes(int dst, int i0, int i1, int i2
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MeshSimplifier::MeshSimplifier() {
|
FastQuadraticMeshSimplifier::FastQuadraticMeshSimplifier() {
|
||||||
maxIterationCount = 100;
|
maxIterationCount = 100;
|
||||||
agressiveness = 7.0;
|
agressiveness = 7.0;
|
||||||
enableSmartLink = true;
|
enableSmartLink = true;
|
@ -1,5 +1,9 @@
|
|||||||
#ifndef MESH_SIMPLIFIER_H
|
#ifndef FAST_QUADRATIC_MESH_SIMPLIFIER_H
|
||||||
#define MESH_SIMPLIFIER_H
|
#define FAST_QUADRATIC_MESH_SIMPLIFIER_H
|
||||||
|
|
||||||
|
#include "mesh_simplifier.h"
|
||||||
|
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
#include "mesh_utils.h"
|
#include "mesh_utils.h"
|
||||||
|
|
||||||
@ -8,7 +12,8 @@
|
|||||||
|
|
||||||
class VoxelMesher;
|
class VoxelMesher;
|
||||||
|
|
||||||
class MeshSimplifier {
|
class FastQuadraticMeshSimplifier : public MeshSimplifier {
|
||||||
|
GDCLASS(FastQuadraticMeshSimplifier, MeshSimplifier);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void initialize(Ref<VoxelMesher> mesher);
|
void initialize(Ref<VoxelMesher> mesher);
|
||||||
@ -17,13 +22,13 @@ public:
|
|||||||
void SimplifyMeshLossless();
|
void SimplifyMeshLossless();
|
||||||
void UpdateMesh(int iteration);
|
void UpdateMesh(int iteration);
|
||||||
void UpdateReferences();
|
void UpdateReferences();
|
||||||
int RemoveVertexPass(int startTrisCount, int targetTrisCount, double threshold, PoolVector<bool> deleted0, PoolVector<bool> deleted1, int deletedTris);
|
int RemoveVertexPass(int startTrisCount, int targetTrisCount, double threshold, PoolVector<bool> &deleted0, PoolVector<bool> &deleted1, int deletedTris);
|
||||||
void CompactMesh();
|
void CompactMesh();
|
||||||
bool AreUVsTheSame(int channel, int indexA, int indexB);
|
bool AreUVsTheSame(int channel, int indexA, int indexB);
|
||||||
double VertexError(SymmetricMatrix q, double x, double y, double z);
|
double VertexError(SymmetricMatrix q, double x, double y, double z);
|
||||||
double CalculateError(MUVertex vert0, MUVertex vert1, Vector3 *result);
|
double CalculateError(MUVertex vert0, MUVertex vert1, Vector3 *result);
|
||||||
int UpdateTriangles(int i0, int ia0, MUVertex *v, PoolVector<bool> deleted, int deletedTriangles);
|
int UpdateTriangles(int i0, int ia0, const MUVertex &v, PoolVector<bool> &deleted, int deletedTriangles);
|
||||||
bool Flipped(Vector3 *p, int i0, int i1, MUVertex *v0, PoolVector<bool> &deleted);
|
bool Flipped(const Vector3 &p, int i0, int i1, const MUVertex &v0, PoolVector<bool> &deleted);
|
||||||
static Vector3 CalculateBarycentricCoords(Vector3 const &point, Vector3 const &a, Vector3 const &b, Vector3 const &c);
|
static Vector3 CalculateBarycentricCoords(Vector3 const &point, Vector3 const &a, Vector3 const &b, Vector3 const &c);
|
||||||
void InterpolateVertexAttributes(int dst, int i0, int i1, int i2, Vector3 &barycentricCoord);
|
void InterpolateVertexAttributes(int dst, int i0, int i1, int i2, Vector3 &barycentricCoord);
|
||||||
|
|
||||||
@ -31,7 +36,7 @@ public:
|
|||||||
return (val1 < val2 ? (val1 < val3 ? val1 : val3) : (val2 < val3 ? val2 : val3));
|
return (val1 < val2 ? (val1 < val3 ? val1 : val3) : (val2 < val3 ? val2 : val3));
|
||||||
}
|
}
|
||||||
|
|
||||||
MeshSimplifier();
|
FastQuadraticMeshSimplifier();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PoolVector<Vector3> _vertices;
|
PoolVector<Vector3> _vertices;
|
4
meshers/mesh_simplifiers/mesh_simplifier.cpp
Normal file
4
meshers/mesh_simplifiers/mesh_simplifier.cpp
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
#include "mesh_simplifier.h"
|
||||||
|
|
||||||
|
MeshSimplifier::MeshSimplifier() {
|
||||||
|
}
|
19
meshers/mesh_simplifiers/mesh_simplifier.h
Normal file
19
meshers/mesh_simplifiers/mesh_simplifier.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#ifndef MESH_SIMPLIFIER_H
|
||||||
|
#define MESH_SIMPLIFIER_H
|
||||||
|
|
||||||
|
#include "core/reference.h"
|
||||||
|
|
||||||
|
#include "core/pool_vector.h"
|
||||||
|
#include "core/resource.h"
|
||||||
|
|
||||||
|
class MeshSimplifier : public Reference {
|
||||||
|
GDCLASS(MeshSimplifier, Reference);
|
||||||
|
|
||||||
|
public:
|
||||||
|
MeshSimplifier();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
static void _bind_methods() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -54,7 +54,7 @@ struct SymmetricMatrix {
|
|||||||
/// The m44 component.
|
/// The m44 component.
|
||||||
double m9;
|
double m9;
|
||||||
|
|
||||||
_FORCE_INLINE_ const double &operator[](int p_index) const {
|
_FORCE_INLINE_ const double get(int p_index) const {
|
||||||
CRASH_BAD_INDEX(p_index, 10);
|
CRASH_BAD_INDEX(p_index, 10);
|
||||||
|
|
||||||
switch (p_index) {
|
switch (p_index) {
|
||||||
@ -213,12 +213,12 @@ struct SymmetricMatrix {
|
|||||||
int a21, int a22, int a23,
|
int a21, int a22, int a23,
|
||||||
int a31, int a32, int a33) {
|
int a31, int a32, int a33) {
|
||||||
double det =
|
double det =
|
||||||
this[a11] * this[a22] * this[a33] +
|
get(a11) * get(a22) * get(a33) +
|
||||||
this[a13] * this[a21] * this[a32] +
|
get(a13) * get(a21) * get(a32) +
|
||||||
this[a12] * this[a23] * this[a31] -
|
get(a12) * get(a23) * get(a31) -
|
||||||
this[a13] * this[a22] * this[a31] -
|
get(a13) * get(a22) * get(a31) -
|
||||||
this[a11] * this[a23] * this[a32] -
|
get(a11) * get(a23) * get(a32) -
|
||||||
this[a12] * this[a21] * this[a33];
|
get(a12) * get(a21) * get(a33);
|
||||||
return det;
|
return det;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,7 +275,7 @@ struct MUTriangle {
|
|||||||
return (p_index == 0 ? v0 : (p_index == 1 ? v1 : v2));
|
return (p_index == 0 ? v0 : (p_index == 1 ? v1 : v2));
|
||||||
}
|
}
|
||||||
|
|
||||||
_FORCE_INLINE_ int set(int p_index, int value) {
|
_FORCE_INLINE_ void set(int p_index, int value) {
|
||||||
CRASH_BAD_INDEX(p_index, 3);
|
CRASH_BAD_INDEX(p_index, 3);
|
||||||
|
|
||||||
switch (p_index) {
|
switch (p_index) {
|
||||||
@ -289,8 +289,44 @@ struct MUTriangle {
|
|||||||
v2 = value;
|
v2 = value;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
_FORCE_INLINE_ void set_dirty(bool p_value) {
|
||||||
|
dirty = p_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
_FORCE_INLINE_ void set_err0(double p_value) {
|
||||||
|
err0 = p_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
_FORCE_INLINE_ void set_err1(double p_value) {
|
||||||
|
err1 = p_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
_FORCE_INLINE_ void set_err2(double p_value) {
|
||||||
|
err2 = p_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
_FORCE_INLINE_ void set_err3(double p_value) {
|
||||||
|
err3 = p_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
_FORCE_INLINE_ void set_deleted(double p_value) {
|
||||||
|
deleted = p_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
MUTriangle() {
|
||||||
|
v0 = 0;
|
||||||
|
v1 = 0;
|
||||||
|
v2 = 0;
|
||||||
|
subMeshIndex = 0;
|
||||||
|
|
||||||
|
va0 = 0;
|
||||||
|
va1 = 0;
|
||||||
|
va2 = 0;
|
||||||
|
|
||||||
|
err0 = err1 = err2 = err3 = 0;
|
||||||
|
deleted = dirty = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
MUTriangle(int p_v0, int p_v1, int p_v2, int p_subMeshIndex) {
|
MUTriangle(int p_v0, int p_v1, int p_v2, int p_subMeshIndex) {
|
||||||
@ -353,6 +389,34 @@ struct MUVertex {
|
|||||||
bool uvSeamEdge;
|
bool uvSeamEdge;
|
||||||
bool uvFoldoverEdge;
|
bool uvFoldoverEdge;
|
||||||
|
|
||||||
|
_FORCE_INLINE_ void set_tstart(int p_value) {
|
||||||
|
tstart = p_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
_FORCE_INLINE_ void set_tcount(int p_value) {
|
||||||
|
tcount = p_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
_FORCE_INLINE_ void set_border_edge(bool p_value) {
|
||||||
|
borderEdge = p_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
_FORCE_INLINE_ void set_uv_seam_edge(bool p_value) {
|
||||||
|
uvSeamEdge = p_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
_FORCE_INLINE_ void set_uv_foldover_edge(bool p_value) {
|
||||||
|
uvFoldoverEdge = p_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
MUVertex() {
|
||||||
|
tstart = 0;
|
||||||
|
tcount = 0;
|
||||||
|
borderEdge = true;
|
||||||
|
uvSeamEdge = false;
|
||||||
|
uvFoldoverEdge = false;
|
||||||
|
}
|
||||||
|
|
||||||
MUVertex(float x, float y, float z) {
|
MUVertex(float x, float y, float z) {
|
||||||
p = Vector3(x, y, z);
|
p = Vector3(x, y, z);
|
||||||
tstart = 0;
|
tstart = 0;
|
||||||
@ -391,6 +455,10 @@ struct BorderVertex {
|
|||||||
int index;
|
int index;
|
||||||
int hash;
|
int hash;
|
||||||
|
|
||||||
|
_FORCE_INLINE_ void set_index(int p_value) {
|
||||||
|
index = p_value;
|
||||||
|
}
|
||||||
|
|
||||||
BorderVertex() {
|
BorderVertex() {
|
||||||
index = 0;
|
index = 0;
|
||||||
hash = 0;
|
hash = 0;
|
||||||
@ -403,7 +471,7 @@ struct BorderVertex {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct BorderVertexComparer {
|
struct BorderVertexComparer {
|
||||||
_FORCE_INLINE_ bool operator()(const BorderVertex &a, const BorderVertex &b) const { return x.hash < y.hash; }
|
_FORCE_INLINE_ bool operator()(const BorderVertex &a, const BorderVertex &b) const { return a.hash < b.hash; }
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -16,6 +16,9 @@
|
|||||||
#include "meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.h"
|
#include "meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.h"
|
||||||
#include "meshers/voxel_mesher.h"
|
#include "meshers/voxel_mesher.h"
|
||||||
|
|
||||||
|
#include "meshers/mesh_simplifiers/fast_quadratic_mesh_simplifier.h"
|
||||||
|
#include "meshers/mesh_simplifiers/mesh_simplifier.h"
|
||||||
|
|
||||||
#include "world/environment_data.h"
|
#include "world/environment_data.h"
|
||||||
#include "world/voxel_chunk.h"
|
#include "world/voxel_chunk.h"
|
||||||
#include "world/voxel_chunk_prop_data.h"
|
#include "world/voxel_chunk_prop_data.h"
|
||||||
@ -52,6 +55,9 @@ void register_voxelman_types() {
|
|||||||
ClassDB::register_class<VoxelSurfaceSimple>();
|
ClassDB::register_class<VoxelSurfaceSimple>();
|
||||||
ClassDB::register_class<VoxelSurfaceMerger>();
|
ClassDB::register_class<VoxelSurfaceMerger>();
|
||||||
|
|
||||||
|
ClassDB::register_class<MeshSimplifier>();
|
||||||
|
ClassDB::register_class<FastQuadraticMeshSimplifier>();
|
||||||
|
|
||||||
ClassDB::register_class<VoxelmanLibrary>();
|
ClassDB::register_class<VoxelmanLibrary>();
|
||||||
ClassDB::register_class<VoxelmanLibrarySimple>();
|
ClassDB::register_class<VoxelmanLibrarySimple>();
|
||||||
ClassDB::register_class<VoxelmanLibraryMerger>();
|
ClassDB::register_class<VoxelmanLibraryMerger>();
|
||||||
|
Loading…
Reference in New Issue
Block a user