2020-01-31 19:52:37 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2019-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.
|
|
|
|
*/
|
|
|
|
|
2019-05-31 22:54:31 +02:00
|
|
|
#ifndef VOXEL_TOOLS_H
|
|
|
|
#define VOXEL_TOOLS_H
|
|
|
|
|
|
|
|
#include "core/color.h"
|
2020-01-09 04:29:05 +01:00
|
|
|
#include "core/math/rect2.h"
|
2019-05-31 22:54:31 +02:00
|
|
|
#include "core/math/vector2.h"
|
|
|
|
#include "core/math/vector3.h"
|
2020-01-19 18:25:03 +01:00
|
|
|
#include "core/pool_vector.h"
|
2020-01-09 04:29:05 +01:00
|
|
|
#include "core/reference.h"
|
2019-05-31 22:54:31 +02:00
|
|
|
#include "core/vector.h"
|
|
|
|
#include "scene/3d/immediate_geometry.h"
|
2020-01-09 04:29:05 +01:00
|
|
|
#include "scene/3d/mesh_instance.h"
|
2019-05-31 22:54:31 +02:00
|
|
|
#include "scene/3d/spatial.h"
|
|
|
|
#include "scene/main/node.h"
|
|
|
|
#include "scene/resources/concave_polygon_shape.h"
|
2020-01-09 04:29:05 +01:00
|
|
|
#include "scene/resources/material.h"
|
|
|
|
#include "scene/resources/mesh.h"
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2019-12-24 03:22:17 +01:00
|
|
|
#include "../../mesh_data_resource/mesh_data_resource.h"
|
2020-01-09 04:29:05 +01:00
|
|
|
#include "../library/voxelman_library.h"
|
2019-05-31 22:54:31 +02:00
|
|
|
|
|
|
|
const double PI_2 = 3.141592653589793238463 / 2;
|
|
|
|
const double PI = 3.141592653589793238463;
|
|
|
|
|
|
|
|
class VoxelmanLibrary;
|
2019-11-18 20:03:40 +01:00
|
|
|
class VoxelChunk;
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2019-06-08 19:47:17 +02:00
|
|
|
class VoxelMesher : public Reference {
|
|
|
|
GDCLASS(VoxelMesher, Reference);
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2020-03-08 22:36:58 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-31 22:54:31 +02:00
|
|
|
public:
|
2020-02-13 14:16:57 +01:00
|
|
|
int get_mesher_index() const;
|
|
|
|
void set_mesher_index(const int value);
|
|
|
|
|
2020-03-08 22:36:58 +01:00
|
|
|
int get_format() const;
|
|
|
|
void set_format(const int value);
|
|
|
|
|
2019-07-24 02:14:02 +02:00
|
|
|
Ref<VoxelmanLibrary> get_library();
|
2020-02-13 14:16:57 +01:00
|
|
|
void set_library(const Ref<VoxelmanLibrary> &library);
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2019-11-05 21:22:39 +01:00
|
|
|
Ref<Material> get_material();
|
2020-02-10 23:39:57 +01:00
|
|
|
void set_material(const Ref<Material> &material);
|
2019-11-05 21:22:39 +01:00
|
|
|
|
2019-07-24 02:14:02 +02:00
|
|
|
float get_ao_strength() const;
|
2020-02-13 14:16:57 +01:00
|
|
|
void set_ao_strength(const float value);
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2019-07-24 02:14:02 +02:00
|
|
|
float get_base_light_value() const;
|
2020-02-13 14:16:57 +01:00
|
|
|
void set_base_light_value(const float value);
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2019-07-17 17:01:12 +02:00
|
|
|
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);
|
|
|
|
|
2019-11-05 21:22:39 +01:00
|
|
|
Rect2 get_uv_margin() const;
|
|
|
|
void set_uv_margin(const Rect2 margin);
|
|
|
|
|
2019-07-24 02:14:02 +02:00
|
|
|
void reset();
|
|
|
|
|
2020-04-02 21:28:19 +02:00
|
|
|
void add_chunk(Ref<VoxelChunk> chunk);
|
2019-11-18 20:03:40 +01:00
|
|
|
|
2020-04-02 21:28:19 +02:00
|
|
|
void add_chunk_liquid(Ref<VoxelChunk> chunk);
|
2019-11-18 20:03:40 +01:00
|
|
|
|
2019-11-05 21:22:39 +01:00
|
|
|
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));
|
2019-07-24 02:14:02 +02:00
|
|
|
|
2020-02-14 19:02:31 +01:00
|
|
|
void add_mesher(const Ref<VoxelMesher> &mesher);
|
|
|
|
void _add_mesher(const Ref<VoxelMesher> &mesher);
|
|
|
|
|
2020-04-02 21:28:19 +02:00
|
|
|
void bake_colors(Ref<VoxelChunk> chunk);
|
|
|
|
void bake_liquid_colors(Ref<VoxelChunk> chunk);
|
2019-11-10 13:42:59 +01:00
|
|
|
|
2020-03-06 15:39:15 +01:00
|
|
|
PoolVector<Vector3> build_collider() const;
|
2019-07-18 01:43:58 +02:00
|
|
|
|
2019-06-07 01:33:41 +02:00
|
|
|
void bake_lights(MeshInstance *node, Vector<Ref<VoxelLight> > &lights);
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2020-02-20 13:19:54 +01:00
|
|
|
Array build_mesh();
|
2020-02-20 13:44:38 +01:00
|
|
|
void build_mesh_into(RID mesh);
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2020-03-04 23:06:30 +01:00
|
|
|
void generate_normals(bool p_flip = false);
|
2020-03-09 00:17:50 +01:00
|
|
|
void remove_doubles();
|
|
|
|
void remove_doubles_hashed();
|
2020-03-04 23:06:30 +01:00
|
|
|
|
2020-03-08 22:36:58 +01:00
|
|
|
PoolVector<Vector3> get_vertices() const;
|
2020-01-20 15:10:30 +01:00
|
|
|
void set_vertices(const PoolVector<Vector3> &values);
|
2020-03-08 22:36:58 +01:00
|
|
|
int get_vertex_count() const;
|
2020-03-09 00:17:50 +01:00
|
|
|
Vector3 get_vertex(const int idx) const;
|
2020-03-08 22:36:58 +01:00
|
|
|
void remove_vertex(const int idx);
|
|
|
|
void add_vertex(const Vector3 &vertex);
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2020-03-08 22:36:58 +01:00
|
|
|
PoolVector<Vector3> get_normals() const;
|
2020-01-20 15:10:30 +01:00
|
|
|
void set_normals(const PoolVector<Vector3> &values);
|
2020-03-08 22:36:58 +01:00
|
|
|
Vector3 get_normal(const int idx) const;
|
|
|
|
void add_normal(const Vector3 &normal);
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2020-03-08 22:36:58 +01:00
|
|
|
PoolVector<Color> get_colors() const;
|
2020-01-20 15:10:30 +01:00
|
|
|
void set_colors(const PoolVector<Color> &values);
|
2020-03-08 22:36:58 +01:00
|
|
|
Color get_color(const int idx) const;
|
|
|
|
void add_color(const Color &color);
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2020-03-08 22:36:58 +01:00
|
|
|
PoolVector<Vector2> get_uvs() const;
|
2020-01-20 15:10:30 +01:00
|
|
|
void set_uvs(const PoolVector<Vector2> &values);
|
2020-03-08 22:36:58 +01:00
|
|
|
Vector2 get_uv(const int idx) const;
|
|
|
|
void add_uv(const Vector2 &vector);
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2020-03-08 22:36:58 +01:00
|
|
|
PoolVector<Vector2> get_uv2s() const;
|
2020-01-20 15:10:30 +01:00
|
|
|
void set_uv2s(const PoolVector<Vector2> &values);
|
2020-03-08 22:36:58 +01:00
|
|
|
Vector2 get_uv2(const int idx) const;
|
|
|
|
void add_uv2(const Vector2 &vector);
|
2020-01-19 18:25:03 +01:00
|
|
|
|
2020-03-08 22:36:58 +01:00
|
|
|
PoolVector<int> get_indices() const;
|
2020-02-13 14:16:57 +01:00
|
|
|
void set_indices(const PoolVector<int> &values);
|
2020-03-08 22:36:58 +01:00
|
|
|
int get_indices_count() const;
|
|
|
|
int get_index(const int idx) const;
|
|
|
|
void remove_index(const int idx);
|
|
|
|
void add_indices(const int index);
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2020-02-13 14:16:57 +01:00
|
|
|
VoxelMesher(const Ref<VoxelmanLibrary> &library);
|
2019-05-31 22:54:31 +02:00
|
|
|
VoxelMesher();
|
|
|
|
~VoxelMesher();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
2020-02-13 14:16:57 +01:00
|
|
|
int _mesher_index;
|
|
|
|
|
2020-03-08 22:36:58 +01:00
|
|
|
int _format;
|
|
|
|
|
|
|
|
PoolVector<Vertex> _vertices;
|
2020-01-19 18:25:03 +01:00
|
|
|
PoolVector<int> _indices;
|
2020-03-08 22:36:58 +01:00
|
|
|
|
|
|
|
Color _last_color;
|
|
|
|
Vector3 _last_normal;
|
|
|
|
Vector2 _last_uv;
|
|
|
|
Vector2 _last_uv2;
|
|
|
|
Vector<int> _last_bones;
|
|
|
|
Vector<float> _last_weights;
|
|
|
|
Plane _last_tangent;
|
2019-05-31 22:54:31 +02:00
|
|
|
|
|
|
|
Ref<VoxelmanLibrary> _library;
|
2019-11-05 21:22:39 +01:00
|
|
|
Ref<Material> _material;
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2019-07-17 17:01:12 +02:00
|
|
|
float _voxel_scale;
|
|
|
|
int _lod_size;
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2019-07-24 02:14:02 +02:00
|
|
|
float _ao_strength;
|
|
|
|
float _base_light_value;
|
2019-11-05 21:22:39 +01:00
|
|
|
Rect2 _uv_margin;
|
2019-05-31 22:54:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|