2021-11-22 22:00:45 +01:00
|
|
|
/*
|
2022-02-22 10:33:06 +01:00
|
|
|
Copyright (c) 2019-2022 Péter Magyar
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-02-22 12:02:42 +01:00
|
|
|
#ifndef TERRAIN_2D_TOOLS_H
|
|
|
|
#define TERRAIN_2D_TOOLS_H
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
#include "core/version.h"
|
|
|
|
|
|
|
|
#if VERSION_MAJOR > 3
|
2022-02-22 10:33:06 +01:00
|
|
|
#include "core/object/ref_counted.h"
|
|
|
|
#ifndef Reference
|
|
|
|
#define Reference RefCounted
|
|
|
|
#endif
|
|
|
|
#include "core/math/color.h"
|
2022-02-24 15:50:47 +01:00
|
|
|
#include "core/templates/vector.h"
|
2021-11-22 22:00:45 +01:00
|
|
|
#else
|
2022-02-24 15:50:47 +01:00
|
|
|
#include "core/color.h"
|
2021-11-22 22:00:45 +01:00
|
|
|
#include "core/reference.h"
|
|
|
|
#include "core/vector.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "../defines.h"
|
|
|
|
|
|
|
|
#include pool_vector_h
|
|
|
|
include_pool_vector
|
|
|
|
|
|
|
|
#include mesh_instance_h
|
|
|
|
|
|
|
|
#include "core/math/rect2.h"
|
|
|
|
#include "core/math/vector2.h"
|
|
|
|
#include "core/math/vector3.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
|
|
|
|
|
2022-02-22 11:09:36 +01:00
|
|
|
#include "../library/terrain_2d_library.h"
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
class Terrain2DLibrary;
|
|
|
|
class Terrain2DChunk;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
class Terrain2DMesher : public Reference {
|
|
|
|
GDCLASS(Terrain2DMesher, Reference);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
const double PI_2 = 3.141592653589793238463 / 2;
|
|
|
|
const double PI = 3.141592653589793238463;
|
|
|
|
|
|
|
|
struct Vertex {
|
2022-02-23 19:38:06 +01:00
|
|
|
Vector2 vertex;
|
2021-11-22 22:00:45 +01:00
|
|
|
Color color;
|
|
|
|
Vector2 uv;
|
|
|
|
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_channel_index_type() const;
|
|
|
|
void set_channel_index_type(const int value);
|
|
|
|
|
|
|
|
int get_mesher_index() const;
|
|
|
|
void set_mesher_index(const int value);
|
|
|
|
|
|
|
|
int get_format() const;
|
|
|
|
void set_format(const int value);
|
|
|
|
|
|
|
|
int get_texture_scale() const;
|
|
|
|
void set_texture_scale(const int value);
|
|
|
|
|
|
|
|
int get_lod_index() const;
|
|
|
|
void set_lod_index(const int value);
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
Ref<Terrain2DLibrary> get_library();
|
|
|
|
void set_library(const Ref<Terrain2DLibrary> &library);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-02-24 15:50:47 +01:00
|
|
|
int get_cell_size_x() const;
|
|
|
|
void set_cell_size_x(const int val);
|
|
|
|
|
|
|
|
int get_cell_size_y() const;
|
|
|
|
void set_cell_size_y(const int val);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
Rect2 get_uv_margin() const;
|
|
|
|
void set_uv_margin(const Rect2 margin);
|
|
|
|
|
|
|
|
void reset();
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void add_chunk(Ref<Terrain2DChunk> chunk);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
#ifdef MESH_DATA_RESOURCE_PRESENT
|
|
|
|
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));
|
2022-02-23 19:38:06 +01:00
|
|
|
void add_mesh_data_resource_transform(Ref<MeshDataResource> mesh, const Transform2D transform, const Rect2 uv_rect = Rect2(0, 0, 1, 1));
|
|
|
|
void add_mesh_data_resource_transform_colored(Ref<MeshDataResource> mesh, const Transform2D transform, const PoolColorArray &colors, const Rect2 uv_rect = Rect2(0, 0, 1, 1));
|
2021-11-22 22:00:45 +01:00
|
|
|
#endif
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void add_mesher(const Ref<Terrain2DMesher> &mesher);
|
|
|
|
void _add_mesher(const Ref<Terrain2DMesher> &mesher);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void bake_colors(Ref<Terrain2DChunk> chunk);
|
|
|
|
void bake_liquid_colors(Ref<Terrain2DChunk> chunk);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
PoolVector<Vector2> build_collider() const;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void bake_lights(MeshInstance *node, Vector<Ref<Terrain2DLight>> &lights);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
Array build_mesh();
|
|
|
|
void build_mesh_into(RID mesh);
|
|
|
|
|
|
|
|
void remove_doubles();
|
|
|
|
void remove_doubles_hashed();
|
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
PoolVector<Vector2> get_vertices() const;
|
|
|
|
void set_vertices(const PoolVector<Vector2> &values);
|
2021-11-22 22:00:45 +01:00
|
|
|
int get_vertex_count() const;
|
2022-02-23 19:38:06 +01:00
|
|
|
Vector2 get_vertex(const int idx) const;
|
2021-11-22 22:00:45 +01:00
|
|
|
void remove_vertex(const int idx);
|
2022-02-23 19:38:06 +01:00
|
|
|
void add_vertex(const Vector2 &vertex);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
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<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);
|
|
|
|
|
2022-02-22 10:33:06 +01:00
|
|
|
#if VERSION_MAJOR >= 4
|
2022-02-22 11:42:28 +01:00
|
|
|
GDVIRTUAL1(_add_chunk, Ref<Terrain2DChunk>);
|
|
|
|
GDVIRTUAL1(_bake_colors, Ref<Terrain2DChunk>);
|
|
|
|
GDVIRTUAL1(_bake_liquid_colors, Ref<Terrain2DChunk>);
|
|
|
|
GDVIRTUAL1(_add_mesher, Ref<Terrain2DChunk>);
|
2022-02-22 10:33:06 +01:00
|
|
|
#endif
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
Terrain2DMesher(const Ref<Terrain2DLibrary> &library);
|
|
|
|
Terrain2DMesher();
|
|
|
|
~Terrain2DMesher();
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
int _channel_index_type;
|
|
|
|
|
|
|
|
int _mesher_index;
|
|
|
|
|
|
|
|
int _format;
|
|
|
|
|
|
|
|
int _texture_scale;
|
|
|
|
|
|
|
|
int _lod_index;
|
|
|
|
|
|
|
|
PoolVector<Vertex> _vertices;
|
|
|
|
PoolVector<int> _indices;
|
|
|
|
|
|
|
|
Color _last_color;
|
|
|
|
Vector2 _last_uv;
|
|
|
|
Vector<int> _last_bones;
|
|
|
|
Vector<float> _last_weights;
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
Ref<Terrain2DLibrary> _library;
|
2021-11-22 22:00:45 +01:00
|
|
|
Ref<Material> _material;
|
|
|
|
|
2022-02-24 15:50:47 +01:00
|
|
|
int _cell_size_x;
|
|
|
|
int _cell_size_y;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
float _ao_strength;
|
|
|
|
float _base_light_value;
|
|
|
|
Rect2 _uv_margin;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|