Added a new MeshMerger class. It's based on VoxelMesher.

This commit is contained in:
Relintai 2020-05-30 14:01:17 +02:00
parent 060f0723d0
commit a841b0ece0
5 changed files with 1329 additions and 3 deletions

6
SCsub
View File

@ -4,14 +4,14 @@ Import('env')
module_env = env.Clone()
#Should work with just arrays
#if os.path.isdir('../mesh_data_resource'):
# module_env.Append(CPPDEFINES=['MESH_DATA_RESOURCE_PRESENT'])
if os.path.isdir('../mesh_data_resource'):
module_env.Append(CPPDEFINES=['MESH_DATA_RESOURCE_PRESENT'])
sources = [
"register_types.cpp",
"mesh_utils.cpp",
"mesh_merger.cpp",
"fast_quadratic_mesh_simplifier.cpp",
]

120
defines.h Normal file
View File

@ -0,0 +1,120 @@
#ifndef MESH_UTILS_DEFINES_H
#define MESH_UTILS_DEFINES_H
#include "core/version.h"
#if VERSION_MAJOR >= 4
#define GODOT4 true
#endif
//includes
#if GODOT4
#define core_input_h "core/input/input_event.h"
#define spatial_editor_plugin_h "editor/plugins/node_3d_editor_plugin.h"
#define camera_h "scene/3d/camera_3d.h"
#define spatial_h "scene/3d/node_3d.h"
#define navigation_h "scene/3d/navigation_3d.h"
#define light_h "scene/3d/light_3d.h"
#define visual_server_h "servers/rendering_server.h"
#define mesh_instance_h "scene/3d/mesh_instance_3d.h"
#define pool_vector_h "core/vector.h"
#define physics_server_h "servers/physics_server_3d.h"
#define immediate_geometry_h "scene/3d/immediate_geometry_3d.h"
#define include_pool_vector \
template <class N> \
class Vector; \
template <class N> \
using PoolVector = Vector<N>;
#else
#define core_input_h "core/os/input.h"
#define spatial_editor_plugin_h "editor/plugins/spatial_editor_plugin.h"
#define camera_h "scene/3d/camera.h"
#define spatial_h "scene/3d/spatial.h"
#define navigation_h "scene/3d/navigation.h"
#define light_h "scene/3d/light.h"
#define visual_server_h "servers/visual_server.h"
#define mesh_instance_h "scene/3d/mesh_instance.h"
#define pool_vector_h "core/pool_vector.h"
#define physics_server_h "servers/physics_server.h"
#define immediate_geometry_h "scene/3d/immediate_geometry.h"
#define include_pool_vector
#endif
//Type Defines
#if GODOT4
#define PhysicsDirectSpaceState PhysicsDirectSpaceState3D
#define SpatialEditor Node3DEditor
#define SpatialEditorPlugin Node3DEditorPlugin
#define SpatialEditorViewport Node3DEditorViewport
#define PoolStringArray PackedStringArray
#define REAL FLOAT
#define POOL_STRING_ARRAY PACKED_STRING_ARRAY
#define POOL_BYTE_ARRAY PACKED_BYTE_ARRAY
#define Spatial Node3D
#define SpatialMaterial StandardMaterial3D
#define PoolVector3Array PackedVector3Array
#define PoolVector2Array PackedVector2Array
#define PoolColorArray PackedColorArray
#define PoolIntArray PackedInt64Array
#define PoolRealArray PackedFloat32Array
#define PoolByteArray PackedByteArray
#define MeshInstance MeshInstance3D
#define Camera Camera3D
typedef class World3D World;
#define DirectionalLight DirectionalLight3D
typedef class ImmediateGeometry3D ImmediateGeometry;
typedef class Navigation3D Navigation;
typedef class PhysicsServer3D PhysicsServer;
typedef class RenderingServer VisualServer;
typedef class RenderingServer VS;
//typedef class StandardMaterial3D SpatialMaterial;
//toodo figure out a way to have this
//#define Variant::CallError Callable::CallError
#endif
#if GODOT4
#define VARIANT_ARRAY_GET(arr) \
Vector<Variant> r; \
for (int i = 0; i < arr.size(); i++) { \
r.push_back(arr[i]); \
} \
return r;
#else
#define VARIANT_ARRAY_GET(arr) \
Vector<Variant> r; \
for (int i = 0; i < arr.size(); i++) { \
r.push_back(arr[i].get_ref_ptr()); \
} \
return r;
#endif
#define VARIANT_ARRAY_SET(arr, arr_into, type) \
arr_into.clear(); \
for (int i = 0; i < arr.size(); i++) { \
Ref<type> e = Ref<type>(arr[i]); \
arr_into.push_back(e); \
}
#if GODOT4
//TODO do this properly
#define INSTANCE_VALIDATE(var) var
#define CONNECT(sig, obj, target_method_class, method) connect(sig, callable_mp(obj, &target_method_class::method))
#define DISCONNECT(sig, obj, target_method_class, method) disconnect(sig, callable_mp(obj, &target_method_class::method))
#define GET_WORLD get_world_3d
#else
#define INSTANCE_VALIDATE(var) ObjectDB::instance_validate(var)
#define CONNECT(sig, obj, target_method_class, method) connect(sig, obj, #method)
#define DISCONNECT(sig, obj, target_method_class, method) disconnect(sig, obj, #method)
#define GET_WORLD get_world
#endif
#endif

1007
mesh_merger.cpp Normal file

File diff suppressed because it is too large Load Diff

197
mesh_merger.h Normal file
View File

@ -0,0 +1,197 @@
/*
Copyright (c) 2020 Péter Magyar
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.
*/
#ifndef MESH_MERGER_H
#define MESH_MERGER_H
#include "core/reference.h"
#include "defines.h"
#include pool_vector_h
include_pool_vector
#include mesh_instance_h
#include "core/color.h"
#include "core/math/rect2.h"
#include "core/math/vector2.h"
#include "core/math/vector3.h"
#include "core/vector.h"
#include "scene/main/node.h"
#include "scene/resources/material.h"
#include "scene/resources/mesh.h"
#ifdef MESH_DATA_RESOURCE_PRESENT
#include "../mesh_data_resource/mesh_data_resource.h"
#endif
class MeshMerger : public Reference {
GDCLASS(MeshMerger, Reference);
public:
struct Vertex {
Vector3 vertex;
Color color;
Vector3 normal; // normal, binormal, tangent
Vector3 binormal;
Vector3 tangent;
Vector2 uv;
Vector2 uv2;
Vector<int> bones;
Vector<float> weights;
bool operator==(const Vertex &p_vertex) const;
Vertex() {}
};
struct VertexHasher {
static _FORCE_INLINE_ uint32_t hash(const Vertex &p_vtx);
};
struct WeightSort {
int index;
float weight;
bool operator<(const WeightSort &p_right) const {
return weight < p_right.weight;
}
};
public:
int get_mesher_index() const;
void set_mesher_index(const int value);
int get_format() const;
void set_format(const int value);
Ref<Material> get_material();
void set_material(const Ref<Material> &material);
float get_ao_strength() const;
void set_ao_strength(const float value);
float get_base_light_value() const;
void set_base_light_value(const 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();
#ifdef MESH_DATA_RESOURCE_PRESENT
void add_mesh_data_resource(Ref<MeshDataResource> mesh, const Transform transform = Transform(), const Rect2 uv_rect = Rect2(0, 0, 1, 1));
void add_mesh_data_resource_bone(Ref<MeshDataResource> mesh, const Vector<int> &bones, const Vector<float> &weights, const Transform transform = Transform(), const Rect2 uv_rect = Rect2(0, 0, 1, 1));
#endif
void add_mesher(const Ref<MeshMerger> &mesher);
PoolVector<Vector3> build_collider() const;
Array build_mesh();
void build_mesh_into(RID mesh);
void generate_normals(bool p_flip = false);
void remove_doubles();
void remove_doubles_hashed();
PoolVector<Vector3> get_vertices() const;
void set_vertices(const PoolVector<Vector3> &values);
int get_vertex_count() const;
Vector3 get_vertex(const int idx) const;
void remove_vertex(const int idx);
void add_vertex(const Vector3 &vertex);
PoolVector<Vector3> get_normals() const;
void set_normals(const PoolVector<Vector3> &values);
Vector3 get_normal(const int idx) const;
void add_normal(const Vector3 &normal);
PoolVector<Color> get_colors() const;
void set_colors(const PoolVector<Color> &values);
Color get_color(const int idx) const;
void add_color(const Color &color);
PoolVector<Vector2> get_uvs() const;
void set_uvs(const PoolVector<Vector2> &values);
Vector2 get_uv(const int idx) const;
void add_uv(const Vector2 &vector);
PoolVector<Vector2> get_uv2s() const;
void set_uv2s(const PoolVector<Vector2> &values);
Vector2 get_uv2(const int idx) const;
void add_uv2(const Vector2 &vector);
Vector<int> get_bones(const int idx) const;
void add_bones(const Vector<int> &arr);
Vector<float> get_bone_weights(const int idx) const;
void add_bone_weights(const Vector<float> &arr);
PoolVector<int> get_indices() const;
void set_indices(const PoolVector<int> &values);
int get_indices_count() const;
int get_index(const int idx) const;
void remove_index(const int idx);
void add_indices(const int index);
MeshMerger();
~MeshMerger();
protected:
static void _bind_methods();
int _mesher_index;
int _format;
PoolVector<Vertex> _vertices;
PoolVector<int> _indices;
Color _last_color;
Vector3 _last_normal;
Vector2 _last_uv;
Vector2 _last_uv2;
Vector<int> _last_bones;
Vector<float> _last_weights;
Plane _last_tangent;
Ref<Material> _material;
float _voxel_scale;
int _lod_size;
float _ao_strength;
float _base_light_value;
Rect2 _uv_margin;
};
#endif

View File

@ -26,12 +26,14 @@ SOFTWARE.
#include "core/engine.h"
#include "fast_quadratic_mesh_simplifier.h"
#include "mesh_merger.h"
#include "mesh_utils.h"
static MeshUtils *mesh_utils = NULL;
void register_mesh_utils_types() {
ClassDB::register_class<FastQuadraticMeshSimplifier>();
ClassDB::register_class<MeshMerger>();
mesh_utils = memnew(MeshUtils);
ClassDB::register_class<MeshUtils>();