Prototype implementation, that makes walls actually work with godot's y sort. It does need tweaks.

This commit is contained in:
Relintai 2022-03-03 13:22:52 +01:00
parent 6f3eb79f32
commit 324611fc46
3 changed files with 53 additions and 19 deletions

View File

@ -682,7 +682,11 @@ void Terrain2DChunkDefault::_draw() {
return;
}
VisualServer::get_singleton()->canvas_item_clear(get_canvas_item());
setup_canvas_items_size(mesh_rid_get_count(MESH_INDEX_TERRAIN, MESH_TYPE_INDEX_MESH));
for (int i = 0; i < get_canvas_item_count(); ++i) {
VisualServer::get_singleton()->canvas_item_clear(get_canvas_item(i));
}
Terrain2DWorld *world = get_voxel_world();
@ -694,10 +698,12 @@ void Terrain2DChunkDefault::_draw() {
if (terrain_mesh_rid != RID()) {
RID terrain_texture_rid = mesh_rid_get(MESH_INDEX_TERRAIN, MESH_TYPE_INDEX_TEXTURE_RID);
VisualServer::get_singleton()->canvas_item_add_set_transform(get_canvas_item(), _mesh_transforms[i]);
VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(i), get_transform() * _mesh_transforms[i]);
VisualServer::get_singleton()->canvas_item_clear(get_canvas_item(i));
//Note: the transform parameter is not implemented in gles2
VisualServer::get_singleton()->canvas_item_add_mesh(get_canvas_item(), terrain_mesh_rid, Transform2D(), Color(1, 1, 1, 1), terrain_texture_rid, RID());
VisualServer::get_singleton()->canvas_item_add_mesh(get_canvas_item(i), terrain_mesh_rid, Transform2D(), Color(1, 1, 1, 1), terrain_texture_rid, RID());
}
}
@ -707,7 +713,7 @@ void Terrain2DChunkDefault::_draw() {
RID liquid_texture_rid = mesh_rid_get(MESH_INDEX_LIQUID, MESH_TYPE_INDEX_TEXTURE_RID);
//Note: the transform parameter is not implemented in gles2
VisualServer::get_singleton()->canvas_item_add_mesh(get_canvas_item(), liquid_mesh_rid, Transform2D(), Color(1, 1, 1, 1), liquid_texture_rid, RID());
VisualServer::get_singleton()->canvas_item_add_mesh(get_canvas_item(0), liquid_mesh_rid, Transform2D(), Color(1, 1, 1, 1), liquid_texture_rid, RID());
}
RID prop_mesh_rid = mesh_rid_get(MESH_INDEX_PROP, MESH_TYPE_INDEX_MESH);
@ -716,7 +722,7 @@ void Terrain2DChunkDefault::_draw() {
RID prop_texture_rid = mesh_rid_get(MESH_INDEX_PROP, MESH_TYPE_INDEX_TEXTURE_RID);
//Note: the transform parameter is not implemented in gles2
VisualServer::get_singleton()->canvas_item_add_mesh(get_canvas_item(), prop_mesh_rid, Transform2D(), Color(1, 1, 1, 1), prop_texture_rid, RID());
VisualServer::get_singleton()->canvas_item_add_mesh(get_canvas_item(0), prop_mesh_rid, Transform2D(), Color(1, 1, 1, 1), prop_texture_rid, RID());
}
}

View File

@ -218,14 +218,14 @@ void Terrain2DChunk::set_library(const Ref<Terrain2DLibrary> &value) {
_library = value;
}
int Terrain2DChunk::get_cell_size_x() const{
int Terrain2DChunk::get_cell_size_x() const {
return _cell_size_x;
}
void Terrain2DChunk::set_cell_size_x(const int value){
void Terrain2DChunk::set_cell_size_x(const int value) {
_cell_size_x = value;
}
int Terrain2DChunk::get_cell_size_y() const{
int Terrain2DChunk::get_cell_size_y() const {
return _cell_size_y;
}
void Terrain2DChunk::set_cell_size_y(const int value) {
@ -242,7 +242,6 @@ void Terrain2DChunk::set_voxel_world_bind(Node *world) {
set_voxel_world(Object::cast_to<Terrain2DWorld>(world));
}
Transform2D Terrain2DChunk::mesh_transform_terrain_get() {
return _mesh_transform_terrain;
}
@ -1112,6 +1111,30 @@ bool Terrain2DChunk::is_safe_to_delete() {
#endif
}
void Terrain2DChunk::setup_canvas_items_size(const int amount) {
if (amount > _canvas_items.size()) {
while (amount > _canvas_items.size()) {
RID ci = VisualServer::get_singleton()->canvas_item_create();
if (_voxel_world) {
VisualServer::get_singleton()->canvas_item_set_parent(ci, get_voxel_world()->get_canvas_item());
}
_canvas_items.push_back(ci);
}
return;
} else {
while (amount != _canvas_items.size()) {
RID ci = _canvas_items[_canvas_items.size() - 1];
VisualServer::get_singleton()->free(ci);
_canvas_items.resize(_canvas_items.size() - 1);
}
}
}
Terrain2DChunk::Terrain2DChunk() {
_is_processing = false;
_is_phisics_processing = false;
@ -1152,12 +1175,10 @@ Terrain2DChunk::Terrain2DChunk() {
_current_job = -1;
_queued_generation = false;
_canvas_item = VisualServer::get_singleton()->canvas_item_create();
}
Terrain2DChunk::~Terrain2DChunk() {
VisualServer::get_singleton()->free(_canvas_item);
setup_canvas_items_size(0);
if (_library.is_valid()) {
_library.unref();
@ -1190,7 +1211,9 @@ Terrain2DChunk::~Terrain2DChunk() {
void Terrain2DChunk::_enter_tree() {
if (_voxel_world) {
VisualServer::get_singleton()->canvas_item_set_parent(get_canvas_item(), get_voxel_world()->get_canvas_item());
for (int i = 0; i < _canvas_items.size(); ++i) {
VisualServer::get_singleton()->canvas_item_set_parent(_canvas_items[i], get_voxel_world()->get_canvas_item());
}
}
for (int i = 0; i < _jobs.size(); ++i) {
@ -1284,7 +1307,7 @@ void Terrain2DChunk::_generation_physics_process(const float delta) {
void Terrain2DChunk::_world_transform_changed() {
Transform2D t;
Vector2 pos = Vector2(_position_x * static_cast<int>(_size_x) * _cell_size_x, _position_y * static_cast<int>(_size_y) * _cell_size_y);
pos = _mesh_transform_terrain.xform(pos);
//t *= _custom_transform;
@ -1292,7 +1315,9 @@ void Terrain2DChunk::_world_transform_changed() {
set_transform(t);
VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(), t);
if (get_canvas_item_count() > 0) {
VisualServer::get_singleton()->canvas_item_set_transform(get_canvas_item(0), t);
}
}
/*
@ -1526,7 +1551,6 @@ void Terrain2DChunk::_bind_methods() {
ClassDB::bind_method(D_METHOD("mesh_transform_wall_west_set", "player"), &Terrain2DChunk::mesh_transform_wall_west_set);
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "mesh_transform_wall_west"), "mesh_transform_wall_west_set", "mesh_transform_wall_west_get");
//Terra Data
ClassDB::bind_method(D_METHOD("channel_setup"), &Terrain2DChunk::channel_setup);

View File

@ -350,9 +350,13 @@ public:
bool is_safe_to_delete();
_FORCE_INLINE_ RID get_canvas_item() const {
return _canvas_item;
_FORCE_INLINE_ RID get_canvas_item(const int index) const {
return _canvas_items[index];
}
_FORCE_INLINE_ int get_canvas_item_count() {
return _canvas_items.size();
}
void setup_canvas_items_size(const int amount);
Terrain2DChunk();
~Terrain2DChunk();
@ -460,7 +464,7 @@ protected:
bool _abort_build;
bool _queued_generation;
RID _canvas_item;
Vector<RID> _canvas_items;
Transform2D _mesh_transform_terrain;
Transform2D _mesh_transform_wall_north;