voxelman/meshers/voxel_mesher.h
2020-01-20 11:45:46 +01:00

184 lines
5.3 KiB
C++

#ifndef VOXEL_TOOLS_H
#define VOXEL_TOOLS_H
#include "core/color.h"
#include "core/math/rect2.h"
#include "core/math/vector2.h"
#include "core/math/vector3.h"
#include "core/pool_vector.h"
#include "core/reference.h"
#include "core/vector.h"
#include "scene/3d/immediate_geometry.h"
#include "scene/3d/mesh_instance.h"
#include "scene/3d/spatial.h"
#include "scene/main/node.h"
#include "scene/resources/concave_polygon_shape.h"
#include "scene/resources/material.h"
#include "scene/resources/mesh.h"
#include "scene/resources/surface_tool.h"
#include "mesh_utils.h"
#include "../../mesh_data_resource/mesh_data_resource.h"
#include "../library/voxelman_library.h"
const double PI_2 = 3.141592653589793238463 / 2;
const double PI = 3.141592653589793238463;
class VoxelmanLibrary;
class VoxelChunk;
class VoxelMesher : public Reference {
GDCLASS(VoxelMesher, Reference);
public:
Ref<VoxelmanLibrary> get_library();
void set_library(Ref<VoxelmanLibrary> library);
Ref<Material> get_material();
void set_material(Ref<Material> material);
float get_ao_strength() const;
void set_ao_strength(float value);
float get_base_light_value() const;
void set_base_light_value(float value);
float get_voxel_scale() const;
void set_voxel_scale(const float voxel_scale);
int get_lod_size() const;
void set_lod_size(const int lod_size);
Rect2 get_uv_margin() const;
void set_uv_margin(const Rect2 margin);
void reset();
void add_chunk_bind(Node *chunk);
void add_chunk(VoxelChunk *chunk);
void add_chunk_liquid_bind(Node *chunk);
void add_chunk_liquid(VoxelChunk *chunk);
void add_mesh_data_resource(Ref<MeshDataResource> mesh, const Vector3 position = Vector3(0, 0, 0), const Vector3 rotation = Vector3(0, 0, 0), const Vector3 scale = Vector3(1.0, 1.0, 1.0), const Rect2 uv_rect = Rect2(0, 0, 1, 1));
void add_mesh_data_resource_transform(Ref<MeshDataResource> mesh, const Transform transform, const Rect2 uv_rect = Rect2(0, 0, 1, 1));
void bake_colors_bind(Node *chunk);
void bake_colors(VoxelChunk *chunk);
void _bake_colors(Node *p_chunk);
void bake_liquid_colors_bind(Node *chunk);
void bake_liquid_colors(VoxelChunk *chunk);
void _bake_liquid_colors(Node *p_chunk);
void build_collider(RID shape) const;
void bake_lights(MeshInstance *node, Vector<Ref<VoxelLight> > &lights);
void build_mesh(RID mesh);
void initialize_mesh_simplify();
void SimplifyMesh(float quality);
void SimplifyMeshLossless();
void UpdateMesh(int iteration);
void UpdateReferences();
int RemoveVertexPass(int startTrisCount, int targetTrisCount, double threshold, PoolVector<bool> deleted0, PoolVector<bool> deleted1, int deletedTris);
void CompactMesh();
bool AreUVsTheSame(int channel, int indexA, int indexB);
double VertexError(SymmetricMatrix q, double x, double y, double z);
double CalculateError(MUVertex vert0, MUVertex vert1, Vector3 *result);
int UpdateTriangles(int i0, int ia0, MUVertex *v, PoolVector<bool> deleted, int deletedTriangles);
bool Flipped(Vector3 *p, int i0, int i1, MUVertex *v0, PoolVector<bool> &deleted);
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);
static double Min3(double val1, double val2, double val3) {
return (val1 < val2 ? (val1 < val3 ? val1 : val3) : (val2 < val3 ? val2 : val3));
}
PoolVector<Vector3> get_vertices();
void set_vertices(PoolVector<Vector3> values);
int get_vertex_count();
Vector3 get_vertex(int idx);
void remove_vertex(int idx);
void add_vertex(Vector3 vertex);
PoolVector<Vector3> get_normals();
void set_normals(PoolVector<Vector3> values);
int get_normal_count();
Vector3 get_normal(int idx);
void remove_normal(int idx);
void add_normal(Vector3 normal);
PoolVector<Color> get_colors();
void set_colors(PoolVector<Color> values);
int get_color_count();
Color get_color(int idx);
void remove_color(int idx);
void add_color(Color color);
PoolVector<Vector2> get_uvs();
void set_uvs(PoolVector<Vector2> values);
int get_uv_count();
Vector2 get_uv(int idx);
void remove_uv(int idx);
void add_uv(Vector2 vector);
PoolVector<Vector2> get_uv2s();
void set_uv2s(PoolVector<Vector2> values);
int get_uv2_count();
Vector2 get_uv2(int idx);
void remove_uv2(int idx);
void add_uv2(Vector2 vector);
PoolVector<int> get_indices();
void set_indices(PoolVector<int> values);
int get_indices_count();
int get_indice(int idx);
void remove_indices(int idx);
void add_indices(int index);
VoxelMesher(Ref<VoxelmanLibrary> library);
VoxelMesher();
~VoxelMesher();
protected:
static void _bind_methods();
PoolVector<Vector3> _vertices;
PoolVector<Vector3> _normals;
PoolVector<Color> _colors;
PoolVector<Vector2> _uvs;
PoolVector<Vector2> _uv2s;
PoolVector<int> _indices;
PoolVector<int> _bones;
PoolVector<MUTriangle> _mu_triangles;
PoolVector<MUVertex> _mu_vertices;
PoolVector<MURef> _mu_refs;
Ref<VoxelmanLibrary> _library;
Ref<Material> _material;
float _voxel_scale;
int _lod_size;
Ref<SurfaceTool> _surface_tool;
float _ao_strength;
float _base_light_value;
Rect2 _uv_margin;
private:
double vertexLinkDistanceSqr = std::numeric_limits<double>::epsilon();
int maxIterationCount;
double agressiveness;
bool enableSmartLink;
bool preserveBorderEdges;
bool preserveUVSeamEdges;
bool preserveUVFoldoverEdges;
};
#endif