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" #include "prop_2d_light.h"
Vector3 Prop2DLight::get_position() { Vector2 Prop2DLight::get_position() {
return _position; return _position;
} }
void Prop2DLight::set_position(const Vector3 &pos) { void Prop2DLight::set_position(const Vector2 &pos) {
_position = pos; _position = pos;
} }
@ -53,7 +53,7 @@ Prop2DLight::~Prop2DLight() {
void Prop2DLight::_bind_methods() { void Prop2DLight::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_position"), &Prop2DLight::get_position); ClassDB::bind_method(D_METHOD("get_position"), &Prop2DLight::get_position);
ClassDB::bind_method(D_METHOD("set_position"), &Prop2DLight::set_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("get_color"), &Prop2DLight::get_color);
ClassDB::bind_method(D_METHOD("set_color"), &Prop2DLight::set_color); ClassDB::bind_method(D_METHOD("set_color"), &Prop2DLight::set_color);

View File

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

View File

@ -183,6 +183,10 @@ Rect2 Prop2DMaterialCache::texture_get_uv_rect(const Ref<Texture> &texture) {
return Rect2(0, 0, 1, 1); return Rect2(0, 0, 1, 1);
} }
Ref<Texture> Prop2DMaterialCache::texture_get_merged() {
return Ref<Texture>();
}
void Prop2DMaterialCache::prop_add_textures(const Ref<Prop2DData> &prop) { void Prop2DMaterialCache::prop_add_textures(const Ref<Prop2DData> &prop) {
if (!prop.is_valid()) { if (!prop.is_valid()) {
return; 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", "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_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_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_add_textures", "prop"), &Prop2DMaterialCache::prop_add_textures);
ClassDB::bind_method(D_METHOD("prop_remove_textures", "prop"), &Prop2DMaterialCache::prop_remove_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 Ref<AtlasTexture> texture_get_atlas_tex(const Ref<Texture> &texture);
virtual Rect2 texture_get_uv_rect(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_add_textures(const Ref<Prop2DData> &prop);
void prop_remove_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; return region;
} }
Ref<Texture> Prop2DMaterialCachePCM::texture_get_merged() {
return _merged_texture;
}
void Prop2DMaterialCachePCM::refresh_rects() { void Prop2DMaterialCachePCM::refresh_rects() {
bool texture_added = false; bool texture_added = false;
@ -121,9 +125,9 @@ void Prop2DMaterialCachePCM::refresh_rects() {
ERR_FAIL_COND(_packer->get_texture_count() == 0); 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; _initialized = true;

View File

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

View File

@ -671,7 +671,7 @@ void Prop2DInstanceMerger::_prop_preprocess(Transform transform, const Ref<Prop2
Vector3 v = t.xform(Vector3()); Vector3 v = t.xform(Vector3());
light->set_position(v); //light->set_position(v);
light->set_color(light_data->get_light_color()); light->set_color(light_data->get_light_color());
light->set_size(light_data->get_light_size()); 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 = pdtw->get_transform();
Transform t = e.base_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) { 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()); array.resize(_vertices.size());
#if !GODOT4 #if !GODOT4
PoolVector<Vector3>::Write w = array.write(); PoolVector<Vector2>::Write w = array.write();
#endif #endif
for (int i = 0; i < _vertices.size(); ++i) { 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 v1 = _vertices.get(i1);
Vertex v2 = _vertices.get(i2); Vertex v2 = _vertices.get(i2);
/*
Vector3 normal; Vector3 normal;
if (!p_flip) if (!p_flip)
normal = Plane(v0.vertex, v1.vertex, v2.vertex).normal; normal = Plane(v0.vertex, v1.vertex, v2.vertex).normal;
else else
normal = Plane(v2.vertex, v1.vertex, v0.vertex).normal; normal = Plane(v2.vertex, v1.vertex, v0.vertex).normal;
*/
v0.normal = normal; // v0.normal = normal;
v1.normal = normal; // v1.normal = normal;
v2.normal = normal; // v2.normal = normal;
_vertices.set(i0, v0); _vertices.set(i0, v0);
_vertices.set(i1, v1); _vertices.set(i1, v1);
@ -466,7 +467,7 @@ void Prop2DMesher::reset() {
_last_tangent = Plane(); _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(!tiled_wall_data.is_valid());
ERR_FAIL_COND(!cache.is_valid()); ERR_FAIL_COND(!cache.is_valid());
ERR_FAIL_COND(width < 0); 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(); int vc = get_vertex_count();
//x + 1, y //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_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 //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_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 //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_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 //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_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 + 2);
add_indices(vc + 1); add_indices(vc + 1);
@ -665,7 +666,7 @@ void Prop2DMesher::add_mesh_data_resource_transform(Ref<MeshDataResource> mesh,
const Array &arr = mesh->get_array(); const Array &arr = mesh->get_array();
PoolVector3Array vertices = arr[Mesh::ARRAY_VERTEX]; PoolVector2Array vertices = arr[Mesh::ARRAY_VERTEX];
PoolVector3Array normals = arr[Mesh::ARRAY_NORMAL]; PoolVector3Array normals = arr[Mesh::ARRAY_NORMAL];
PoolVector2Array uvs = arr[Mesh::ARRAY_TEX_UV]; PoolVector2Array uvs = arr[Mesh::ARRAY_TEX_UV];
PoolColorArray colors = arr[Mesh::ARRAY_COLOR]; PoolColorArray colors = arr[Mesh::ARRAY_COLOR];
@ -692,7 +693,7 @@ void Prop2DMesher::add_mesh_data_resource_transform(Ref<MeshDataResource> mesh,
if (colors.size() > 0) if (colors.size() > 0)
add_color(colors[i]); add_color(colors[i]);
add_vertex(transform.xform(vertices[i])); //add_vertex(transform.xform(vertices[i]));
} }
int orig_indices_count = _indices.size(); 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(); const Array &arr = mesh->get_array();
PoolVector3Array vertices = arr[Mesh::ARRAY_VERTEX]; PoolVector2Array vertices = arr[Mesh::ARRAY_VERTEX];
PoolVector3Array normals = arr[Mesh::ARRAY_NORMAL]; PoolVector3Array normals = arr[Mesh::ARRAY_NORMAL];
PoolVector2Array uvs = arr[Mesh::ARRAY_TEX_UV]; PoolVector2Array uvs = arr[Mesh::ARRAY_TEX_UV];
PoolIntArray indices = arr[Mesh::ARRAY_INDEX]; PoolIntArray indices = arr[Mesh::ARRAY_INDEX];
@ -735,7 +736,7 @@ void Prop2DMesher::add_mesh_data_resource_transform_colored(Ref<MeshDataResource
if (colors.size() > 0) if (colors.size() > 0)
add_color(colors[i]); add_color(colors[i]);
add_vertex(transform.xform(vertices[i])); //add_vertex(transform.xform(vertices[i]));
} }
int orig_indices_count = _indices.size(); int orig_indices_count = _indices.size();
@ -787,8 +788,8 @@ void Prop2DMesher::generate_ao() {
}*/ }*/
} }
float Prop2DMesher::get_random_ao(const Vector3 &position) { float Prop2DMesher::get_random_ao(const Vector2 &position) {
float val = _noise->get_noise_3d(position.x, position.y, position.z); float val = _noise->get_noise_2d(position.x, position.y);
val *= _rao_scale_factor; val *= _rao_scale_factor;
@ -801,20 +802,20 @@ float Prop2DMesher::get_random_ao(const Vector3 &position) {
return val; 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; Vector3 v_lightDiffuse;
//calculate the lights value //calculate the lights value
for (int i = 0; i < _lights.size(); ++i) { for (int i = 0; i < _lights.size(); ++i) {
Ref<Prop2DLight> light = _lights.get(i); Ref<Prop2DLight> light = _lights.get(i);
Vector3 lightDir = light->get_position() - position; Vector2 lightDir = light->get_position() - position;
float dist2 = lightDir.dot(lightDir); float dist2 = lightDir.dot(lightDir);
//inverse sqrt //inverse sqrt
lightDir *= (1.0 / sqrt(dist2)); lightDir *= (1.0 / sqrt(dist2));
float NdotL = normal.dot(lightDir); float NdotL = 1.0;//normal.dot(lightDir);
if (NdotL > 1.0) { if (NdotL > 1.0) {
NdotL = 1.0; NdotL = 1.0;
@ -871,8 +872,8 @@ void Prop2DMesher::clear_lights() {
_lights.clear(); _lights.clear();
} }
PoolVector<Vector3> Prop2DMesher::build_collider() const { PoolVector<Vector2> Prop2DMesher::build_collider() const {
PoolVector<Vector3> face_points; PoolVector<Vector2> face_points;
if (_vertices.size() == 0) if (_vertices.size() == 0)
return face_points; return face_points;
@ -928,7 +929,7 @@ void Prop2DMesher::bake_colors() {
void Prop2DMesher::bake_colors_rao() { void Prop2DMesher::bake_colors_rao() {
for (int i = 0; i < _vertices.size(); ++i) { for (int i = 0; i < _vertices.size(); ++i) {
Vertex vertex = _vertices[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); 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() { void Prop2DMesher::bake_colors_lights_rao() {
for (int i = 0; i < _vertices.size(); ++i) { for (int i = 0; i < _vertices.size(); ++i) {
Vertex vertex = _vertices[i]; Vertex vertex = _vertices[i];
Vector3 vert = vertex.vertex; Vector2 vert = vertex.vertex;
Color light = get_light_color_at(vert, vertex.normal); Color light = get_light_color_at(vert, vertex.normal);
@ -980,7 +981,7 @@ void Prop2DMesher::bake_colors_lights_rao() {
void Prop2DMesher::bake_colors_lights() { void Prop2DMesher::bake_colors_lights() {
for (int i = 0; i < _vertices.size(); ++i) { for (int i = 0; i < _vertices.size(); ++i) {
Vertex vertex = _vertices[i]; Vertex vertex = _vertices[i];
Vector3 vert = vertex.vertex; Vector2 vert = vertex.vertex;
Color light = get_light_color_at(vert, vertex.normal); 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) { for (int v = 0; v < _vertices.size(); ++v) {
Vertex vertexv = _vertices.get(v); Vertex vertexv = _vertices.get(v);
Vector3 vet = vertexv.vertex; Vector3 vet = Vector3()//vertexv.vertex;
Vector3 vertex = node->to_global(vet); Vector3 vertex = node->to_global(vet);
//grab normal //grab normal
@ -1089,8 +1090,8 @@ void Prop2DMesher::bake_lights(MeshInstance *node, Vector<Ref<TerrainLight>> &li
} }
#endif #endif
PoolVector<Vector3> Prop2DMesher::get_vertices() const { PoolVector<Vector2> Prop2DMesher::get_vertices() const {
PoolVector<Vector3> arr; PoolVector<Vector2> arr;
arr.resize(_vertices.size()); arr.resize(_vertices.size());
for (int i = 0; i < _vertices.size(); ++i) { for (int i = 0; i < _vertices.size(); ++i) {
@ -1100,13 +1101,13 @@ PoolVector<Vector3> Prop2DMesher::get_vertices() const {
return arr; 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()); ERR_FAIL_COND(values.size() != _vertices.size());
for (int i = 0; i < _vertices.size(); ++i) { for (int i = 0; i < _vertices.size(); ++i) {
Vertex v = _vertices[i]; Vertex v = _vertices[i];
v.normal = values[i]; v.vertex = values[i];
_vertices.set(i, v); _vertices.set(i, v);
} }
@ -1116,7 +1117,7 @@ int Prop2DMesher::get_vertex_count() const {
return _vertices.size(); return _vertices.size();
} }
void Prop2DMesher::add_vertex(const Vector3 &vertex) { void Prop2DMesher::add_vertex(const Vector2 &vertex) {
Vertex vtx; Vertex vtx;
vtx.vertex = vertex; vtx.vertex = vertex;
vtx.color = _last_color; vtx.color = _last_color;
@ -1132,7 +1133,7 @@ void Prop2DMesher::add_vertex(const Vector3 &vertex) {
_vertices.push_back(vtx); _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; return _vertices.get(idx).vertex;
} }

View File

@ -82,7 +82,7 @@ public:
}; };
struct Vertex { struct Vertex {
Vector3 vertex; Vector2 vertex;
Color color; Color color;
Vector3 normal; // normal, binormal, tangent Vector3 normal; // normal, binormal, tangent
Vector3 binormal; Vector3 binormal;
@ -145,8 +145,8 @@ public:
void reset(); 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_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 Transform &transform, const Rect2 &texture_rect); 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; Vector2 transform_uv(const Vector2 &uv, const Rect2 &rect) const;
#ifdef MESH_DATA_RESOURCE_PRESENT #ifdef MESH_DATA_RESOURCE_PRESENT
@ -156,8 +156,8 @@ public:
#endif #endif
void generate_ao(); void generate_ao();
float get_random_ao(const Vector3 &position); float get_random_ao(const Vector2 &position);
Color get_light_color_at(const Vector3 &position, const Vector3 &normal); 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);
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 add_light(const Ref<Prop2DLight> &light);
void clear_lights(); void clear_lights();
PoolVector<Vector3> build_collider() const; PoolVector<Vector2> build_collider() const;
void bake_colors(); void bake_colors();
void bake_colors_rao(); void bake_colors_rao();
@ -183,12 +183,12 @@ public:
void remove_doubles(); void remove_doubles();
void remove_doubles_hashed(); void remove_doubles_hashed();
PoolVector<Vector3> get_vertices() const; PoolVector<Vector2> get_vertices() const;
void set_vertices(const PoolVector<Vector3> &values); void set_vertices(const PoolVector<Vector2> &values);
int get_vertex_count() const; 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 remove_vertex(const int idx);
void add_vertex(const Vector3 &vertex); void add_vertex(const Vector2 &vertex);
PoolVector<Vector3> get_normals() const; PoolVector<Vector3> get_normals() const;
void set_normals(const PoolVector<Vector3> &values); void set_normals(const PoolVector<Vector3> &values);

View File

@ -137,6 +137,14 @@ void TiledWall2D::refresh() {
_cache->mutex_unlock(); _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(); generate_mesh();
} }
@ -151,7 +159,7 @@ void TiledWall2D::generate_mesh() {
return; 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(); _mesh_array = _mesher->build_mesh();
@ -160,7 +168,7 @@ void TiledWall2D::generate_mesh() {
return; return;
} }
PoolVector<Vector3> vertices = _mesh_array[Mesh::ARRAY_VERTEX]; PoolVector<Vector2> vertices = _mesh_array[Mesh::ARRAY_VERTEX];
if (vertices.size() == 0) { if (vertices.size() == 0) {
update(); update();
@ -169,11 +177,11 @@ void TiledWall2D::generate_mesh() {
VisualServer::get_singleton()->mesh_add_surface_from_arrays(_mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, _mesh_array); 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()); // VisualServer::get_singleton()->mesh_surface_set_material(_mesh_rid, 0, material->get_rid());
} //}
_aabb.size = Vector3(_width, _height, 0); _aabb.size = Vector3(_width, _height, 0);
@ -203,9 +211,11 @@ void TiledWall2D::free_mesh() {
} }
void TiledWall2D::draw() { void TiledWall2D::draw() {
if (!is_inside_tree()) { if (_mesh_rid == RID()) {
return; 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() { TiledWall2D::TiledWall2D() {
@ -253,9 +263,9 @@ void TiledWall2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_data", "value"), &TiledWall2D::set_data); 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_PROPERTY(PropertyInfo(Variant::OBJECT, "data", PROPERTY_HINT_RESOURCE_TYPE, "TiledWall2DData"), "set_data", "get_data");
ADD_GROUP("Collision", "collision_"); //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_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_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("refresh"), &TiledWall2D::refresh);
ClassDB::bind_method(D_METHOD("generate_mesh"), &TiledWall2D::generate_mesh); ClassDB::bind_method(D_METHOD("generate_mesh"), &TiledWall2D::generate_mesh);

View File

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