Initial working prototype for TiledWall2D.

This commit is contained in:
Relintai 2022-02-22 23:38:45 +01:00
parent da8922e123
commit 3cb79bfffb
12 changed files with 92 additions and 66 deletions

View File

@ -22,10 +22,10 @@ SOFTWARE.
#include "prop_2d_light.h"
Vector3 Prop2DLight::get_position() {
Vector2 Prop2DLight::get_position() {
return _position;
}
void Prop2DLight::set_position(const Vector3 &pos) {
void Prop2DLight::set_position(const Vector2 &pos) {
_position = pos;
}
@ -53,7 +53,7 @@ Prop2DLight::~Prop2DLight() {
void Prop2DLight::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_position"), &Prop2DLight::get_position);
ClassDB::bind_method(D_METHOD("set_position"), &Prop2DLight::set_position);
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "world_position"), "set_position", "get_position");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "world_position"), "set_position", "get_position");
ClassDB::bind_method(D_METHOD("get_color"), &Prop2DLight::get_color);
ClassDB::bind_method(D_METHOD("set_color"), &Prop2DLight::set_color);

View File

@ -39,8 +39,8 @@ class Prop2DLight : public Reference {
GDCLASS(Prop2DLight, Reference);
public:
Vector3 get_position();
void set_position(const Vector3 &pos);
Vector2 get_position();
void set_position(const Vector2 &pos);
Color get_color() const;
void set_color(const Color &color);
@ -55,7 +55,7 @@ private:
static void _bind_methods();
private:
Vector3 _position;
Vector2 _position;
Color _color;
int _size;

View File

@ -183,6 +183,10 @@ Rect2 Prop2DMaterialCache::texture_get_uv_rect(const Ref<Texture> &texture) {
return Rect2(0, 0, 1, 1);
}
Ref<Texture> Prop2DMaterialCache::texture_get_merged() {
return Ref<Texture>();
}
void Prop2DMaterialCache::prop_add_textures(const Ref<Prop2DData> &prop) {
if (!prop.is_valid()) {
return;
@ -354,6 +358,7 @@ void Prop2DMaterialCache::_bind_methods() {
ClassDB::bind_method(D_METHOD("texture_get_atlas", "index"), &Prop2DMaterialCache::texture_get_atlas);
ClassDB::bind_method(D_METHOD("texture_get_atlas_tex", "index"), &Prop2DMaterialCache::texture_get_atlas_tex);
ClassDB::bind_method(D_METHOD("texture_get_uv_rect", "texture"), &Prop2DMaterialCache::texture_get_uv_rect);
ClassDB::bind_method(D_METHOD("texture_get_merged"), &Prop2DMaterialCache::texture_get_merged);
ClassDB::bind_method(D_METHOD("prop_add_textures", "prop"), &Prop2DMaterialCache::prop_add_textures);
ClassDB::bind_method(D_METHOD("prop_remove_textures", "prop"), &Prop2DMaterialCache::prop_remove_textures);

View File

@ -78,6 +78,8 @@ public:
virtual Ref<AtlasTexture> texture_get_atlas_tex(const Ref<Texture> &texture);
virtual Rect2 texture_get_uv_rect(const Ref<Texture> &texture);
virtual Ref<Texture> texture_get_merged();
void prop_add_textures(const Ref<Prop2DData> &prop);
void prop_remove_textures(const Ref<Prop2DData> &prop);

View File

@ -102,6 +102,10 @@ Rect2 Prop2DMaterialCachePCM::texture_get_uv_rect(const Ref<Texture> &texture) {
return region;
}
Ref<Texture> Prop2DMaterialCachePCM::texture_get_merged() {
return _merged_texture;
}
void Prop2DMaterialCachePCM::refresh_rects() {
bool texture_added = false;
@ -121,9 +125,9 @@ void Prop2DMaterialCachePCM::refresh_rects() {
ERR_FAIL_COND(_packer->get_texture_count() == 0);
Ref<Texture> tex = _packer->get_generated_texture(0);
_merged_texture = _packer->get_generated_texture(0);
setup_material_albedo(tex);
setup_material_albedo(_merged_texture);
}
_initialized = true;

View File

@ -65,6 +65,8 @@ public:
Ref<AtlasTexture> texture_get_atlas_tex(const Ref<Texture> &texture);
Rect2 texture_get_uv_rect(const Ref<Texture> &texture);
Ref<Texture> texture_get_merged();
void refresh_rects();
void initial_setup_default();
@ -78,6 +80,7 @@ protected:
static void _bind_methods();
Ref<TexturePacker> _packer;
Ref<Texture> _merged_texture;
};
#endif

View File

@ -671,7 +671,7 @@ void Prop2DInstanceMerger::_prop_preprocess(Transform transform, const Ref<Prop2
Vector3 v = t.xform(Vector3());
light->set_position(v);
//light->set_position(v);
light->set_color(light_data->get_light_color());
light->set_size(light_data->get_light_size());

View File

@ -299,7 +299,7 @@ void Prop2DInstanceProp2DJob::phase_prop() {
//Transform t = pdtw->get_transform();
Transform t = e.base_transform;
_prop_mesher->add_tiled_wall_simple(pdtw->get_width(), pdtw->get_heigth(), t, pdtw->get_data(), _material_cache);
//_prop_mesher->add_tiled_wall_simple(pdtw->get_width(), pdtw->get_heigth(), t, pdtw->get_data(), _material_cache);
}
if (_prop_mesher->get_vertex_count() == 0) {

View File

@ -171,10 +171,10 @@ Array Prop2DMesher::build_mesh() {
}
{
PoolVector<Vector3> array;
PoolVector<Vector2> array;
array.resize(_vertices.size());
#if !GODOT4
PoolVector<Vector3>::Write w = array.write();
PoolVector<Vector2>::Write w = array.write();
#endif
for (int i = 0; i < _vertices.size(); ++i) {
@ -339,15 +339,16 @@ void Prop2DMesher::generate_normals(bool p_flip) {
Vertex v1 = _vertices.get(i1);
Vertex v2 = _vertices.get(i2);
/*
Vector3 normal;
if (!p_flip)
normal = Plane(v0.vertex, v1.vertex, v2.vertex).normal;
else
normal = Plane(v2.vertex, v1.vertex, v0.vertex).normal;
v0.normal = normal;
v1.normal = normal;
v2.normal = normal;
*/
// v0.normal = normal;
// v1.normal = normal;
// v2.normal = normal;
_vertices.set(i0, v0);
_vertices.set(i1, v1);
@ -466,7 +467,7 @@ void Prop2DMesher::reset() {
_last_tangent = Plane();
}
void Prop2DMesher::add_tiled_wall_simple(const int width, const int height, const Transform &transform, const Ref<TiledWall2DData> &tiled_wall_data, Ref<Prop2DMaterialCache> cache) {
void Prop2DMesher::add_tiled_wall_simple(const int width, const int height, const Transform2D &transform, const Ref<TiledWall2DData> &tiled_wall_data, Ref<Prop2DMaterialCache> cache) {
ERR_FAIL_COND(!tiled_wall_data.is_valid());
ERR_FAIL_COND(!cache.is_valid());
ERR_FAIL_COND(width < 0);
@ -610,28 +611,28 @@ void Prop2DMesher::add_tiled_wall_simple(const int width, const int height, cons
}
}
void Prop2DMesher::add_tiled_wall_mesh_rect_simple(const int x, const int y, const Transform &transform, const Rect2 &texture_rect) {
void Prop2DMesher::add_tiled_wall_mesh_rect_simple(const int x, const int y, const Transform2D &transform, const Rect2 &texture_rect) {
int vc = get_vertex_count();
//x + 1, y
add_normal(transform.basis.xform(Vector3(0, 0, -1)));
//add_normal(transform.basis.xform(Vector3(0, 0, -1)));
add_uv(transform_uv(Vector2(1, 1), texture_rect));
add_vertex(transform.xform(Vector3(x + 1, y, 0)));
add_vertex(transform.xform(Vector2(x + 1, y)));
//x, y
add_normal(transform.basis.xform(Vector3(0, 0, -1)));
//add_normal(transform.basis.xform(Vector3(0, 0, -1)));
add_uv(transform_uv(Vector2(0, 1), texture_rect));
add_vertex(transform.xform(Vector3(x, y, 0)));
add_vertex(transform.xform(Vector2(x, y)));
//x, y + 1
add_normal(transform.basis.xform(Vector3(0, 0, -1)));
//add_normal(transform.basis.xform(Vector3(0, 0, -1)));
add_uv(transform_uv(Vector2(0, 0), texture_rect));
add_vertex(transform.xform(Vector3(x, y + 1, 0)));
add_vertex(transform.xform(Vector2(x, y + 1)));
//x + 1, y + 1
add_normal(transform.basis.xform(Vector3(0, 0, -1)));
//add_normal(transform.basis.xform(Vector3(0, 0, -1)));
add_uv(transform_uv(Vector2(1, 0), texture_rect));
add_vertex(transform.xform(Vector3(x + 1, y + 1, 0)));
add_vertex(transform.xform(Vector2(x + 1, y + 1)));
add_indices(vc + 2);
add_indices(vc + 1);
@ -665,7 +666,7 @@ void Prop2DMesher::add_mesh_data_resource_transform(Ref<MeshDataResource> mesh,
const Array &arr = mesh->get_array();
PoolVector3Array vertices = arr[Mesh::ARRAY_VERTEX];
PoolVector2Array vertices = arr[Mesh::ARRAY_VERTEX];
PoolVector3Array normals = arr[Mesh::ARRAY_NORMAL];
PoolVector2Array uvs = arr[Mesh::ARRAY_TEX_UV];
PoolColorArray colors = arr[Mesh::ARRAY_COLOR];
@ -692,7 +693,7 @@ void Prop2DMesher::add_mesh_data_resource_transform(Ref<MeshDataResource> mesh,
if (colors.size() > 0)
add_color(colors[i]);
add_vertex(transform.xform(vertices[i]));
//add_vertex(transform.xform(vertices[i]));
}
int orig_indices_count = _indices.size();
@ -709,7 +710,7 @@ void Prop2DMesher::add_mesh_data_resource_transform_colored(Ref<MeshDataResource
const Array &arr = mesh->get_array();
PoolVector3Array vertices = arr[Mesh::ARRAY_VERTEX];
PoolVector2Array vertices = arr[Mesh::ARRAY_VERTEX];
PoolVector3Array normals = arr[Mesh::ARRAY_NORMAL];
PoolVector2Array uvs = arr[Mesh::ARRAY_TEX_UV];
PoolIntArray indices = arr[Mesh::ARRAY_INDEX];
@ -735,7 +736,7 @@ void Prop2DMesher::add_mesh_data_resource_transform_colored(Ref<MeshDataResource
if (colors.size() > 0)
add_color(colors[i]);
add_vertex(transform.xform(vertices[i]));
//add_vertex(transform.xform(vertices[i]));
}
int orig_indices_count = _indices.size();
@ -787,8 +788,8 @@ void Prop2DMesher::generate_ao() {
}*/
}
float Prop2DMesher::get_random_ao(const Vector3 &position) {
float val = _noise->get_noise_3d(position.x, position.y, position.z);
float Prop2DMesher::get_random_ao(const Vector2 &position) {
float val = _noise->get_noise_2d(position.x, position.y);
val *= _rao_scale_factor;
@ -801,20 +802,20 @@ float Prop2DMesher::get_random_ao(const Vector3 &position) {
return val;
}
Color Prop2DMesher::get_light_color_at(const Vector3 &position, const Vector3 &normal) {
Color Prop2DMesher::get_light_color_at(const Vector2 &position, const Vector3 &normal) {
Vector3 v_lightDiffuse;
//calculate the lights value
for (int i = 0; i < _lights.size(); ++i) {
Ref<Prop2DLight> light = _lights.get(i);
Vector3 lightDir = light->get_position() - position;
Vector2 lightDir = light->get_position() - position;
float dist2 = lightDir.dot(lightDir);
//inverse sqrt
lightDir *= (1.0 / sqrt(dist2));
float NdotL = normal.dot(lightDir);
float NdotL = 1.0;//normal.dot(lightDir);
if (NdotL > 1.0) {
NdotL = 1.0;
@ -871,8 +872,8 @@ void Prop2DMesher::clear_lights() {
_lights.clear();
}
PoolVector<Vector3> Prop2DMesher::build_collider() const {
PoolVector<Vector3> face_points;
PoolVector<Vector2> Prop2DMesher::build_collider() const {
PoolVector<Vector2> face_points;
if (_vertices.size() == 0)
return face_points;
@ -928,7 +929,7 @@ void Prop2DMesher::bake_colors() {
void Prop2DMesher::bake_colors_rao() {
for (int i = 0; i < _vertices.size(); ++i) {
Vertex vertex = _vertices[i];
Vector3 vert = vertex.vertex;
Vector2 vert = vertex.vertex;
Color light = Color(_base_light_value, _base_light_value, _base_light_value);
@ -952,7 +953,7 @@ void Prop2DMesher::bake_colors_rao() {
void Prop2DMesher::bake_colors_lights_rao() {
for (int i = 0; i < _vertices.size(); ++i) {
Vertex vertex = _vertices[i];
Vector3 vert = vertex.vertex;
Vector2 vert = vertex.vertex;
Color light = get_light_color_at(vert, vertex.normal);
@ -980,7 +981,7 @@ void Prop2DMesher::bake_colors_lights_rao() {
void Prop2DMesher::bake_colors_lights() {
for (int i = 0; i < _vertices.size(); ++i) {
Vertex vertex = _vertices[i];
Vector3 vert = vertex.vertex;
Vector2 vert = vertex.vertex;
Color light = get_light_color_at(vert, vertex.normal);
@ -1008,7 +1009,7 @@ void Prop2DMesher::bake_lights(MeshInstance *node, Vector<Ref<TerrainLight>> &li
for (int v = 0; v < _vertices.size(); ++v) {
Vertex vertexv = _vertices.get(v);
Vector3 vet = vertexv.vertex;
Vector3 vet = Vector3()//vertexv.vertex;
Vector3 vertex = node->to_global(vet);
//grab normal
@ -1089,8 +1090,8 @@ void Prop2DMesher::bake_lights(MeshInstance *node, Vector<Ref<TerrainLight>> &li
}
#endif
PoolVector<Vector3> Prop2DMesher::get_vertices() const {
PoolVector<Vector3> arr;
PoolVector<Vector2> Prop2DMesher::get_vertices() const {
PoolVector<Vector2> arr;
arr.resize(_vertices.size());
for (int i = 0; i < _vertices.size(); ++i) {
@ -1100,13 +1101,13 @@ PoolVector<Vector3> Prop2DMesher::get_vertices() const {
return arr;
}
void Prop2DMesher::set_vertices(const PoolVector<Vector3> &values) {
void Prop2DMesher::set_vertices(const PoolVector<Vector2> &values) {
ERR_FAIL_COND(values.size() != _vertices.size());
for (int i = 0; i < _vertices.size(); ++i) {
Vertex v = _vertices[i];
v.normal = values[i];
v.vertex = values[i];
_vertices.set(i, v);
}
@ -1116,7 +1117,7 @@ int Prop2DMesher::get_vertex_count() const {
return _vertices.size();
}
void Prop2DMesher::add_vertex(const Vector3 &vertex) {
void Prop2DMesher::add_vertex(const Vector2 &vertex) {
Vertex vtx;
vtx.vertex = vertex;
vtx.color = _last_color;
@ -1132,7 +1133,7 @@ void Prop2DMesher::add_vertex(const Vector3 &vertex) {
_vertices.push_back(vtx);
}
Vector3 Prop2DMesher::get_vertex(const int idx) const {
Vector2 Prop2DMesher::get_vertex(const int idx) const {
return _vertices.get(idx).vertex;
}

View File

@ -82,7 +82,7 @@ public:
};
struct Vertex {
Vector3 vertex;
Vector2 vertex;
Color color;
Vector3 normal; // normal, binormal, tangent
Vector3 binormal;
@ -145,8 +145,8 @@ public:
void reset();
void add_tiled_wall_simple(const int width, const int height, const Transform &transform, const Ref<TiledWall2DData> &tiled_wall_data, Ref<Prop2DMaterialCache> cache);
void add_tiled_wall_mesh_rect_simple(const int x, const int y, const Transform &transform, const Rect2 &texture_rect);
void add_tiled_wall_simple(const int width, const int height, const Transform2D &transform, const Ref<TiledWall2DData> &tiled_wall_data, Ref<Prop2DMaterialCache> cache);
void add_tiled_wall_mesh_rect_simple(const int x, const int y, const Transform2D &transform, const Rect2 &texture_rect);
Vector2 transform_uv(const Vector2 &uv, const Rect2 &rect) const;
#ifdef MESH_DATA_RESOURCE_PRESENT
@ -156,8 +156,8 @@ public:
#endif
void generate_ao();
float get_random_ao(const Vector3 &position);
Color get_light_color_at(const Vector3 &position, const Vector3 &normal);
float get_random_ao(const Vector2 &position);
Color get_light_color_at(const Vector2 &position, const Vector3 &normal);
void add_mesher(const Ref<Prop2DMesher> &mesher);
void _add_mesher(const Ref<Prop2DMesher> &mesher);
@ -165,7 +165,7 @@ public:
void add_light(const Ref<Prop2DLight> &light);
void clear_lights();
PoolVector<Vector3> build_collider() const;
PoolVector<Vector2> build_collider() const;
void bake_colors();
void bake_colors_rao();
@ -183,12 +183,12 @@ public:
void remove_doubles();
void remove_doubles_hashed();
PoolVector<Vector3> get_vertices() const;
void set_vertices(const PoolVector<Vector3> &values);
PoolVector<Vector2> get_vertices() const;
void set_vertices(const PoolVector<Vector2> &values);
int get_vertex_count() const;
Vector3 get_vertex(const int idx) const;
Vector2 get_vertex(const int idx) const;
void remove_vertex(const int idx);
void add_vertex(const Vector3 &vertex);
void add_vertex(const Vector2 &vertex);
PoolVector<Vector3> get_normals() const;
void set_normals(const PoolVector<Vector3> &values);

View File

@ -137,6 +137,14 @@ void TiledWall2D::refresh() {
_cache->mutex_unlock();
}
Ref<Texture> tex = _cache->texture_get_merged();
if (tex.is_valid()) {
_texture_rid = tex->get_rid();
} else {
_texture_rid = RID();
}
generate_mesh();
}
@ -151,7 +159,7 @@ void TiledWall2D::generate_mesh() {
return;
}
_mesher->add_tiled_wall_simple(_width, _height, Transform(), _data, _cache);
_mesher->add_tiled_wall_simple(_width, _height, Transform2D(), _data, _cache);
_mesh_array = _mesher->build_mesh();
@ -160,7 +168,7 @@ void TiledWall2D::generate_mesh() {
return;
}
PoolVector<Vector3> vertices = _mesh_array[Mesh::ARRAY_VERTEX];
PoolVector<Vector2> vertices = _mesh_array[Mesh::ARRAY_VERTEX];
if (vertices.size() == 0) {
update();
@ -169,11 +177,11 @@ void TiledWall2D::generate_mesh() {
VisualServer::get_singleton()->mesh_add_surface_from_arrays(_mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, _mesh_array);
Ref<Material> material = _cache->material_lod_get(0);
//Ref<Material> material = _cache->material_lod_get(0);
if (material.is_valid()) {
//if (material.is_valid()) {
// VisualServer::get_singleton()->mesh_surface_set_material(_mesh_rid, 0, material->get_rid());
}
//}
_aabb.size = Vector3(_width, _height, 0);
@ -203,9 +211,11 @@ void TiledWall2D::free_mesh() {
}
void TiledWall2D::draw() {
if (!is_inside_tree()) {
if (_mesh_rid == RID()) {
return;
}
VisualServer::get_singleton()->canvas_item_add_mesh(get_canvas_item(), _mesh_rid, get_transform(), Color(1, 1, 1, 1), _texture_rid, RID());
}
TiledWall2D::TiledWall2D() {
@ -253,9 +263,9 @@ void TiledWall2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_data", "value"), &TiledWall2D::set_data);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "data", PROPERTY_HINT_RESOURCE_TYPE, "TiledWall2DData"), "set_data", "get_data");
ADD_GROUP("Collision", "collision_");
ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
//ADD_GROUP("Collision", "collision_");
//ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
//ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
ClassDB::bind_method(D_METHOD("refresh"), &TiledWall2D::refresh);
ClassDB::bind_method(D_METHOD("generate_mesh"), &TiledWall2D::generate_mesh);

View File

@ -78,6 +78,7 @@ private:
AABB _aabb;
RID _mesh_rid;
RID _texture_rid;
Array _mesh_array;
};