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 11:09:36 +01:00
|
|
|
#include "terrain_2d_chunk_default.h"
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
#include "../../defines.h"
|
|
|
|
|
|
|
|
#include visual_server_h
|
|
|
|
#include physics_server_h
|
|
|
|
|
|
|
|
#include "../../../opensimplex/open_simplex_noise.h"
|
2022-02-22 11:09:36 +01:00
|
|
|
#include "../../meshers/default/terrain_2d_mesher_default.h"
|
|
|
|
#include "../terrain_2d_world.h"
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:09:36 +01:00
|
|
|
#include "../jobs/terrain_2d_job.h"
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:09:36 +01:00
|
|
|
#include "terrain_2d_world_default.h"
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:09:36 +01:00
|
|
|
#include "../jobs/terrain_2d_light_job.h"
|
|
|
|
#include "../jobs/terrain_2d_prop_job.h"
|
|
|
|
#include "../jobs/terrain_2d_terrain_job.h"
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-24 17:04:35 +01:00
|
|
|
const String Terrain2DChunkDefault::BINDING_STRING_BUILD_FLAGS = "Use Lighting,Use AO,Use RAO,Generate AO,Generate RAO,Bake Lights,Create Collider";
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
_FORCE_INLINE_ int Terrain2DChunkDefault::get_build_flags() const {
|
2021-11-22 22:00:45 +01:00
|
|
|
return _build_flags;
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
_FORCE_INLINE_ void Terrain2DChunkDefault::set_build_flags(const int flags) {
|
2021-11-22 22:00:45 +01:00
|
|
|
_build_flags = flags;
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
bool Terrain2DChunkDefault::get_lights_dirty() const {
|
2021-11-22 22:00:45 +01:00
|
|
|
return _lights_dirty;
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::set_lights_dirty(const bool value) {
|
2021-11-22 22:00:45 +01:00
|
|
|
_lights_dirty = value;
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::emit_build_finished() {
|
2021-11-22 22:00:45 +01:00
|
|
|
emit_signal("mesh_generation_finished", this);
|
|
|
|
|
|
|
|
if (_voxel_world != NULL) {
|
|
|
|
_voxel_world->on_chunk_mesh_generation_finished(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Meshes
|
2022-02-22 11:42:28 +01:00
|
|
|
Dictionary Terrain2DChunkDefault::mesh_rids_get() {
|
2021-11-22 22:00:45 +01:00
|
|
|
return _rids;
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::mesh_rids_set(const Dictionary &rids) {
|
2021-11-22 22:00:45 +01:00
|
|
|
_rids = rids;
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
RID Terrain2DChunkDefault::mesh_rid_get(const int mesh_index, const int mesh_type_index) {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return RID();
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
if (!m.has(mesh_type_index))
|
|
|
|
return RID();
|
|
|
|
|
|
|
|
Variant v = m[mesh_type_index];
|
|
|
|
|
|
|
|
#if VERSION_MAJOR > 3
|
|
|
|
if (v.get_type() != Variant::RID)
|
|
|
|
return RID();
|
|
|
|
#else
|
|
|
|
if (v.get_type() != Variant::_RID)
|
|
|
|
return RID();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::mesh_rid_set(const int mesh_index, const int mesh_type_index, RID value) {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
_rids[mesh_index] = Dictionary();
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
if (!m.has(mesh_type_index)) {
|
|
|
|
m[mesh_type_index] = value;
|
|
|
|
_rids[mesh_index] = m;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant v = m[mesh_type_index];
|
|
|
|
|
|
|
|
#if VERSION_MAJOR > 3
|
|
|
|
ERR_FAIL_COND(v.get_type() != Variant::RID);
|
|
|
|
#else
|
|
|
|
ERR_FAIL_COND(v.get_type() != Variant::_RID);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m[mesh_type_index] = value;
|
|
|
|
_rids[mesh_index] = m;
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
RID Terrain2DChunkDefault::mesh_rid_get_index(const int mesh_index, const int mesh_type_index, const int index) {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return RID();
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
if (!m.has(mesh_type_index))
|
|
|
|
return RID();
|
|
|
|
|
|
|
|
Variant v = m[mesh_type_index];
|
|
|
|
|
|
|
|
if (v.get_type() != Variant::ARRAY)
|
|
|
|
return RID();
|
|
|
|
|
|
|
|
Array arr = v;
|
|
|
|
|
|
|
|
ERR_FAIL_INDEX_V(index, arr.size(), RID());
|
|
|
|
|
|
|
|
return arr[index];
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::mesh_rid_set_index(const int mesh_index, const int mesh_type_index, const int index, RID value) {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
_rids[mesh_index] = Dictionary();
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
if (!m.has(mesh_type_index)) {
|
|
|
|
Array arr;
|
|
|
|
arr.resize(index + 1);
|
|
|
|
arr[index] = value;
|
|
|
|
|
|
|
|
m[mesh_type_index] = arr;
|
|
|
|
_rids[mesh_index] = m;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant v = m[mesh_type_index];
|
|
|
|
|
|
|
|
ERR_FAIL_COND(v.get_type() != Variant::ARRAY);
|
|
|
|
|
|
|
|
Array arr = m[mesh_type_index];
|
|
|
|
|
|
|
|
if (arr.size() <= index)
|
|
|
|
arr.resize(index + 1);
|
|
|
|
|
|
|
|
arr[index] = value;
|
|
|
|
|
|
|
|
m[mesh_type_index] = arr;
|
|
|
|
_rids[mesh_index] = m;
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
int Terrain2DChunkDefault::mesh_rid_get_count(const int mesh_index, const int mesh_type_index) {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
if (!m.has(mesh_type_index))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Variant v = m[mesh_type_index];
|
|
|
|
|
|
|
|
if (v.get_type() != Variant::ARRAY)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Array arr = v;
|
|
|
|
|
|
|
|
return arr.size();
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::mesh_rids_clear(const int mesh_index, const int mesh_type_index) {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
if (!m.has(mesh_type_index))
|
|
|
|
return;
|
|
|
|
|
|
|
|
m.erase(mesh_type_index);
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
Array Terrain2DChunkDefault::meshes_get(const int mesh_index, const int mesh_type_index) {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return Array();
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
if (!m.has(mesh_type_index))
|
|
|
|
return Array();
|
|
|
|
|
|
|
|
Variant v = m[mesh_type_index];
|
|
|
|
|
|
|
|
if (v.get_type() != Variant::ARRAY)
|
|
|
|
return Array();
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::meshes_set(const int mesh_index, const int mesh_type_index, const Array &meshes) {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
_rids[mesh_index] = Dictionary();
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
m[mesh_type_index] = meshes;
|
|
|
|
_rids[mesh_index] = m;
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
bool Terrain2DChunkDefault::meshes_has(const int mesh_index, const int mesh_type_index) {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
if (!m.has(mesh_type_index))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::rids_clear() {
|
2021-11-22 22:00:45 +01:00
|
|
|
_rids.clear();
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::rids_free() {
|
2021-11-22 22:00:45 +01:00
|
|
|
List<Variant> keys;
|
|
|
|
|
|
|
|
_rids.get_key_list(&keys);
|
|
|
|
|
|
|
|
for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
|
|
|
|
Variant v = E->get();
|
|
|
|
|
|
|
|
if (v.get_type() != Variant::INT)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
free_index(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::meshes_create(const int mesh_index, const int mesh_count) {
|
2021-11-22 22:00:45 +01:00
|
|
|
ERR_FAIL_COND(_voxel_world == NULL);
|
|
|
|
ERR_FAIL_COND(!get_library().is_valid());
|
|
|
|
|
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
_rids[mesh_index] = Dictionary();
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
ERR_FAIL_COND(m.has(MESH_TYPE_INDEX_MESH));
|
|
|
|
|
|
|
|
Array am;
|
|
|
|
|
|
|
|
for (int i = 0; i < mesh_count; ++i) {
|
|
|
|
RID mesh_rid = VS::get_singleton()->mesh_create();
|
|
|
|
|
|
|
|
am.push_back(mesh_rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
m[MESH_TYPE_INDEX_MESH] = am;
|
|
|
|
|
|
|
|
_rids[mesh_index] = m;
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::meshes_free(const int mesh_index) {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
RID rid;
|
|
|
|
|
|
|
|
if (m.has(MESH_TYPE_INDEX_MESH)) {
|
|
|
|
Array a = m[MESH_TYPE_INDEX_MESH];
|
|
|
|
|
|
|
|
for (int i = 0; i < a.size(); ++i) {
|
|
|
|
RID r = a[i];
|
|
|
|
|
|
|
|
if (r != rid) {
|
|
|
|
VS::get_singleton()->free(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.erase(MESH_TYPE_INDEX_MESH);
|
|
|
|
}
|
|
|
|
|
2022-02-24 09:40:21 +01:00
|
|
|
void Terrain2DChunkDefault::mesh_create(const int mesh_index) {
|
|
|
|
ERR_FAIL_COND(_voxel_world == NULL);
|
|
|
|
ERR_FAIL_COND(!get_library().is_valid());
|
|
|
|
|
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
_rids[mesh_index] = Dictionary();
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
ERR_FAIL_COND(m.has(MESH_TYPE_INDEX_MESH));
|
|
|
|
|
|
|
|
RID mesh_rid = VS::get_singleton()->mesh_create();
|
|
|
|
|
|
|
|
m[MESH_TYPE_INDEX_MESH] = mesh_rid;
|
|
|
|
|
|
|
|
_rids[mesh_index] = m;
|
|
|
|
}
|
|
|
|
void Terrain2DChunkDefault::mesh_free(const int mesh_index) {
|
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
RID rid;
|
|
|
|
|
|
|
|
if (m.has(MESH_TYPE_INDEX_MESH)) {
|
|
|
|
RID r = m[MESH_TYPE_INDEX_MESH];
|
|
|
|
|
|
|
|
if (r != rid) {
|
|
|
|
VS::get_singleton()->free(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.erase(MESH_TYPE_INDEX_MESH);
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::colliders_create(const int mesh_index, const int layer_mask) {
|
2022-02-23 19:38:06 +01:00
|
|
|
/*
|
2021-11-22 22:00:45 +01:00
|
|
|
ERR_FAIL_COND(_voxel_world == NULL);
|
|
|
|
ERR_FAIL_COND(PhysicsServer::get_singleton()->is_flushing_queries());
|
|
|
|
//ERR_FAIL_COND(!get_voxel_world()->is_inside_tree());
|
|
|
|
|
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
_rids[mesh_index] = Dictionary();
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
ERR_FAIL_COND(m.has(MESH_TYPE_INDEX_BODY));
|
|
|
|
ERR_FAIL_COND(m.has(MESH_TYPE_INDEX_SHAPE));
|
|
|
|
|
|
|
|
RID shape_rid = PhysicsServer::get_singleton()->shape_create(PhysicsServer::SHAPE_CONCAVE_POLYGON);
|
|
|
|
RID body_rid = PhysicsServer::get_singleton()->body_create(PhysicsServer::BODY_MODE_STATIC);
|
|
|
|
|
|
|
|
PhysicsServer::get_singleton()->body_set_collision_layer(body_rid, layer_mask);
|
|
|
|
PhysicsServer::get_singleton()->body_set_collision_mask(body_rid, layer_mask);
|
|
|
|
|
|
|
|
PhysicsServer::get_singleton()->body_add_shape(body_rid, shape_rid);
|
|
|
|
|
|
|
|
PhysicsServer::get_singleton()->body_set_state(body_rid, PhysicsServer::BODY_STATE_TRANSFORM, get_transform());
|
|
|
|
|
|
|
|
if (get_voxel_world()->is_inside_tree() && get_voxel_world()->is_inside_world()) {
|
|
|
|
Ref<World> world = get_voxel_world()->GET_WORLD();
|
|
|
|
|
|
|
|
if (world.is_valid() && world->get_space() != RID())
|
|
|
|
PhysicsServer::get_singleton()->body_set_space(body_rid, world->get_space());
|
|
|
|
}
|
|
|
|
|
|
|
|
m[MESH_TYPE_INDEX_BODY] = body_rid;
|
|
|
|
m[MESH_TYPE_INDEX_SHAPE] = shape_rid;
|
|
|
|
|
|
|
|
_rids[mesh_index] = m;
|
2022-02-23 19:38:06 +01:00
|
|
|
*/
|
|
|
|
|
2022-02-24 09:24:41 +01:00
|
|
|
//todo
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::colliders_create_area(const int mesh_index, const int layer_mask) {
|
2022-02-23 19:38:06 +01:00
|
|
|
/*
|
2021-11-22 22:00:45 +01:00
|
|
|
ERR_FAIL_COND(_voxel_world == NULL);
|
|
|
|
ERR_FAIL_COND(PhysicsServer::get_singleton()->is_flushing_queries());
|
|
|
|
|
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
_rids[mesh_index] = Dictionary();
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
ERR_FAIL_COND(m.has(MESH_TYPE_INDEX_AREA));
|
|
|
|
ERR_FAIL_COND(m.has(MESH_TYPE_INDEX_SHAPE));
|
|
|
|
|
|
|
|
RID shape_rid = PhysicsServer::get_singleton()->shape_create(PhysicsServer::SHAPE_CONCAVE_POLYGON);
|
|
|
|
RID area_rid = PhysicsServer::get_singleton()->area_create();
|
|
|
|
|
|
|
|
PhysicsServer::get_singleton()->area_attach_object_instance_id(area_rid, _voxel_world->get_instance_id());
|
|
|
|
PhysicsServer::get_singleton()->area_set_param(area_rid, PhysicsServer::AREA_PARAM_GRAVITY, 9.8);
|
|
|
|
PhysicsServer::get_singleton()->area_set_param(area_rid, PhysicsServer::AREA_PARAM_GRAVITY_VECTOR, Vector3(0, -1, 0));
|
|
|
|
|
|
|
|
//PhysicsServer::get_singleton()->area_set_monitor_callback(area_rid, this, "_body_area_inout");
|
|
|
|
//PhysicsServer::get_singleton()->area_set_area_monitor_callback(area_rid, this, "_body_area_area_inout");
|
|
|
|
//PhysicsServer::get_singleton()->area_set_monitorable(area_rid, true);
|
|
|
|
|
|
|
|
PhysicsServer::get_singleton()->area_set_collision_layer(area_rid, layer_mask);
|
|
|
|
PhysicsServer::get_singleton()->area_set_collision_mask(area_rid, layer_mask);
|
|
|
|
|
|
|
|
if (get_voxel_world()->is_inside_tree() && get_voxel_world()->is_inside_world()) {
|
|
|
|
Ref<World> world = get_voxel_world()->GET_WORLD();
|
|
|
|
|
|
|
|
if (world.is_valid() && world->get_space() != RID())
|
|
|
|
PhysicsServer::get_singleton()->area_set_space(area_rid, world->get_space());
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsServer::get_singleton()->area_add_shape(area_rid, shape_rid, get_transform());
|
|
|
|
|
|
|
|
m[MESH_TYPE_INDEX_AREA] = area_rid;
|
|
|
|
m[MESH_TYPE_INDEX_SHAPE] = shape_rid;
|
|
|
|
|
|
|
|
_rids[mesh_index] = m;
|
2022-02-23 19:38:06 +01:00
|
|
|
*/
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::colliders_free(const int mesh_index) {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
RID rid;
|
|
|
|
|
|
|
|
if (m.has(MESH_TYPE_INDEX_SHAPE)) {
|
|
|
|
RID r = m[MESH_TYPE_INDEX_SHAPE];
|
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
//PhysicsServer::get_singleton()->free(r);
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m.has(MESH_TYPE_INDEX_BODY)) {
|
|
|
|
RID r = m[MESH_TYPE_INDEX_BODY];
|
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
//PhysicsServer::get_singleton()->free(r);
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m.erase(MESH_TYPE_INDEX_SHAPE);
|
|
|
|
m.erase(MESH_TYPE_INDEX_BODY);
|
|
|
|
|
|
|
|
_rids[mesh_index] = m;
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::free_index(const int mesh_index) {
|
2021-11-22 22:00:45 +01:00
|
|
|
meshes_free(mesh_index);
|
2022-02-24 09:40:21 +01:00
|
|
|
mesh_free(mesh_index);
|
2021-11-22 22:00:45 +01:00
|
|
|
colliders_free(mesh_index);
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::update_transforms() {
|
2021-11-22 22:00:45 +01:00
|
|
|
RID empty_rid;
|
2022-02-24 12:44:02 +01:00
|
|
|
//Transform2D t = get_transform();
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
List<Variant> keys;
|
|
|
|
|
|
|
|
_rids.get_key_list(&keys);
|
|
|
|
|
|
|
|
for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
|
|
|
|
Variant v = E->get();
|
|
|
|
|
|
|
|
if (v.get_type() != Variant::INT)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Dictionary d = _rids[v];
|
|
|
|
|
|
|
|
if (d.has(MESH_TYPE_INDEX_BODY)) {
|
|
|
|
RID rid = d[MESH_TYPE_INDEX_BODY];
|
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
if (rid != empty_rid) {
|
|
|
|
//PhysicsServer::get_singleton()->body_set_state(rid, PhysicsServer::BODY_STATE_TRANSFORM, t);
|
|
|
|
}
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (d.has(MESH_TYPE_INDEX_AREA)) {
|
|
|
|
RID rid = d[MESH_TYPE_INDEX_AREA];
|
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
if (rid != empty_rid) {
|
|
|
|
//PhysicsServer::get_singleton()->area_set_shape_transform(rid, 0, t);
|
|
|
|
}
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < collider_get_count(); ++i) {
|
2022-02-23 19:38:06 +01:00
|
|
|
//PhysicsServer::get_singleton()->body_set_state(collider_get_body(i), PhysicsServer::BODY_STATE_TRANSFORM, get_transform() * collider_get_transform(i));
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 20:01:38 +01:00
|
|
|
//if (_debug_mesh_instance != RID()) {
|
|
|
|
// VS::get_singleton()->instance_set_transform(_debug_mesh_instance, get_transform());
|
|
|
|
//}
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Lights
|
2022-02-22 11:42:28 +01:00
|
|
|
Ref<Terrain2DLight> Terrain2DChunkDefault::get_light(const int index) {
|
|
|
|
ERR_FAIL_INDEX_V(index, _lights.size(), Ref<Terrain2DLight>());
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
return _lights.get(index);
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
int Terrain2DChunkDefault::get_light_count() const {
|
2021-11-22 22:00:45 +01:00
|
|
|
return _lights.size();
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::debug_mesh_allocate() {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (_debug_mesh_rid == RID()) {
|
|
|
|
_debug_mesh_rid = VisualServer::get_singleton()->mesh_create();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_debug_mesh_instance == RID()) {
|
|
|
|
_debug_mesh_instance = VisualServer::get_singleton()->instance_create();
|
|
|
|
|
2022-02-23 20:01:38 +01:00
|
|
|
//if (get_voxel_world()->GET_WORLD().is_valid())
|
|
|
|
// VS::get_singleton()->instance_set_scenario(_debug_mesh_instance, get_voxel_world()->GET_WORLD()->get_scenario());
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
VS::get_singleton()->instance_set_base(_debug_mesh_instance, _debug_mesh_rid);
|
2022-02-23 20:01:38 +01:00
|
|
|
//VS::get_singleton()->instance_set_transform(_debug_mesh_instance, get_transform());
|
2021-11-22 22:00:45 +01:00
|
|
|
VS::get_singleton()->instance_set_visible(_debug_mesh_instance, true);
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::debug_mesh_free() {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (_debug_mesh_instance != RID()) {
|
|
|
|
VisualServer::get_singleton()->free(_debug_mesh_instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_debug_mesh_rid != RID()) {
|
|
|
|
VisualServer::get_singleton()->free(_debug_mesh_rid);
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
bool Terrain2DChunkDefault::debug_mesh_has() {
|
2021-11-22 22:00:45 +01:00
|
|
|
return _debug_mesh_rid != RID();
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::debug_mesh_clear() {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (_debug_mesh_rid != RID()) {
|
|
|
|
VisualServer::get_singleton()->mesh_clear(_debug_mesh_rid);
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::debug_mesh_array_clear() {
|
2021-11-22 22:00:45 +01:00
|
|
|
_debug_mesh_array.resize(0);
|
|
|
|
}
|
2022-02-24 16:55:14 +01:00
|
|
|
void Terrain2DChunkDefault::debug_mesh_add_vertices_to(const PoolVector2Array &arr) {
|
2021-11-22 22:00:45 +01:00
|
|
|
_debug_mesh_array.append_array(arr);
|
|
|
|
|
|
|
|
if (_debug_mesh_array.size() % 2 == 1) {
|
|
|
|
_debug_mesh_array.append(_debug_mesh_array[_debug_mesh_array.size() - 1]);
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::debug_mesh_send() {
|
2021-11-22 22:00:45 +01:00
|
|
|
debug_mesh_allocate();
|
|
|
|
debug_mesh_clear();
|
|
|
|
|
|
|
|
if (_debug_mesh_array.size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SceneTree *st = SceneTree::get_singleton();
|
|
|
|
|
|
|
|
Array arr;
|
|
|
|
arr.resize(VisualServer::ARRAY_MAX);
|
|
|
|
arr[VisualServer::ARRAY_VERTEX] = _debug_mesh_array;
|
|
|
|
|
|
|
|
VisualServer::get_singleton()->mesh_add_surface_from_arrays(_debug_mesh_rid, VisualServer::PRIMITIVE_LINES, arr);
|
|
|
|
|
|
|
|
if (st) {
|
|
|
|
VisualServer::get_singleton()->mesh_surface_set_material(_debug_mesh_rid, 0, SceneTree::get_singleton()->get_debug_collision_material()->get_rid());
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_mesh_array_clear();
|
|
|
|
}
|
|
|
|
|
2022-02-24 16:55:14 +01:00
|
|
|
void Terrain2DChunkDefault::draw_cross_voxels(Vector2 pos) {
|
|
|
|
pos.x *= get_cell_size_x();
|
|
|
|
pos.y *= get_cell_size_x();
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
int size = _debug_mesh_array.size();
|
|
|
|
_debug_mesh_array.resize(_debug_mesh_array.size() + 6);
|
|
|
|
|
2022-02-24 16:55:14 +01:00
|
|
|
_debug_mesh_array.set(size, pos + Vector2(0, -0.2 * _cell_size_y));
|
|
|
|
_debug_mesh_array.set(size + 1, pos + Vector2(0, 0.2 * _cell_size_y));
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-24 16:55:14 +01:00
|
|
|
_debug_mesh_array.set(size + 4, pos + Vector2(-0.2 * _cell_size_x, 0));
|
|
|
|
_debug_mesh_array.set(size + 5, pos + Vector2(0.2 * _cell_size_x, 0));
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
2022-02-24 16:55:14 +01:00
|
|
|
void Terrain2DChunkDefault::draw_cross_voxels_fill(Vector2 pos, float fill) {
|
|
|
|
pos.x *= get_cell_size_x();
|
|
|
|
pos.y *= get_cell_size_x();
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
int size = _debug_mesh_array.size();
|
|
|
|
_debug_mesh_array.resize(_debug_mesh_array.size() + 6);
|
|
|
|
|
2022-02-24 16:55:14 +01:00
|
|
|
_debug_mesh_array.set(size, pos + Vector2(0, -0.2 * fill * _cell_size_y));
|
|
|
|
_debug_mesh_array.set(size + 1, pos + Vector2(0, 0.2 * fill * _cell_size_y));
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-24 16:55:14 +01:00
|
|
|
_debug_mesh_array.set(size + 4, pos + Vector2(-0.2 * fill * _cell_size_x, 0));
|
|
|
|
_debug_mesh_array.set(size + 5, pos + Vector2(0.2 * fill * _cell_size_x, 0));
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::draw_debug_voxels(int max, Color color) {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (!debug_mesh_has()) {
|
|
|
|
debug_mesh_allocate();
|
|
|
|
}
|
|
|
|
|
|
|
|
//debug_mesh_array_clear();
|
|
|
|
|
|
|
|
//_debug_drawer->begin(Mesh::PRIMITIVE_LINES);
|
|
|
|
|
|
|
|
int a = 0;
|
|
|
|
|
|
|
|
int64_t sx = static_cast<int64_t>(_size_x);
|
2022-02-24 16:55:14 +01:00
|
|
|
int64_t sy = static_cast<int64_t>(_size_y);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-24 16:55:14 +01:00
|
|
|
for (int y = 0; y < sy; ++y) {
|
2022-02-23 19:38:06 +01:00
|
|
|
for (int x = 0; x < sx; ++x) {
|
2022-02-24 16:55:14 +01:00
|
|
|
int type = get_voxel(x, y, Terrain2DChunkDefault::DEFAULT_CHANNEL_TYPE);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
if (type == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-24 16:55:14 +01:00
|
|
|
draw_cross_voxels_fill(Vector2(x, y), 1);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
++a;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
if (a > max) {
|
|
|
|
break;
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-23 19:38:06 +01:00
|
|
|
}
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
debug_mesh_send();
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::draw_debug_voxel_lights() {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (!debug_mesh_has()) {
|
|
|
|
debug_mesh_allocate();
|
|
|
|
}
|
|
|
|
|
|
|
|
//debug_mesh_array_clear();
|
|
|
|
|
|
|
|
//_debug_drawer->begin(Mesh::PrimitiveType::PRIMITIVE_LINES);
|
|
|
|
|
|
|
|
for (int i = 0; i < _lights.size(); ++i) {
|
2022-02-22 11:42:28 +01:00
|
|
|
Ref<Terrain2DLight> v = _lights[i];
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
int pos_x = v->get_world_position_x() - (_size_x * _position_x);
|
2022-02-24 16:55:14 +01:00
|
|
|
int pos_y = v->get_world_position_y() - (_size_y * _position_y);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-24 16:55:14 +01:00
|
|
|
draw_cross_voxels_fill(Vector2(pos_x, pos_y), 1.0);
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
debug_mesh_send();
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::draw_debug_mdr_colliders() {
|
2021-11-22 22:00:45 +01:00
|
|
|
if (!debug_mesh_has()) {
|
|
|
|
debug_mesh_allocate();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < collider_get_count(); ++i) {
|
2022-02-23 19:38:06 +01:00
|
|
|
Ref<Shape2D> shape = collider_get_shape(i);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
if (!shape.is_valid())
|
|
|
|
continue;
|
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
Transform2D t = collider_get_transform(i);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
//shape->add_vertices_to_array(_debug_mesh_array, t);
|
|
|
|
//draw
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::_exit_tree() {
|
|
|
|
Terrain2DChunk::_exit_tree();
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 10:33:06 +01:00
|
|
|
if (!_is_generating) {
|
|
|
|
rids_free();
|
|
|
|
rids_clear();
|
|
|
|
}
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::_world_transform_changed() {
|
|
|
|
Terrain2DChunk::_world_transform_changed();
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
update_transforms();
|
|
|
|
}
|
|
|
|
|
2022-02-24 09:24:41 +01:00
|
|
|
void Terrain2DChunkDefault::_draw() {
|
|
|
|
if (_is_generating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-24 13:02:01 +01:00
|
|
|
VisualServer::get_singleton()->canvas_item_clear(get_canvas_item());
|
|
|
|
|
2022-02-24 09:24:41 +01:00
|
|
|
Terrain2DWorld *world = get_voxel_world();
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!world);
|
|
|
|
|
|
|
|
RID terrain_mesh_rid = mesh_rid_get(MESH_INDEX_TERRAIN, MESH_TYPE_INDEX_MESH);
|
|
|
|
|
|
|
|
if (terrain_mesh_rid != RID()) {
|
|
|
|
RID terrain_texture_rid = mesh_rid_get(MESH_INDEX_TERRAIN, MESH_TYPE_INDEX_TEXTURE_RID);
|
|
|
|
|
2022-02-24 12:45:21 +01:00
|
|
|
//Note: the transform parameter is not implemented in gles2
|
2022-02-24 13:02:01 +01:00
|
|
|
VisualServer::get_singleton()->canvas_item_add_mesh(get_canvas_item(), terrain_mesh_rid, Transform2D(), Color(1, 1, 1, 1), terrain_texture_rid, RID());
|
2022-02-24 09:24:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RID liquid_mesh_rid = mesh_rid_get(MESH_INDEX_LIQUID, MESH_TYPE_INDEX_MESH);
|
|
|
|
|
|
|
|
if (liquid_mesh_rid != RID()) {
|
|
|
|
RID liquid_texture_rid = mesh_rid_get(MESH_INDEX_LIQUID, MESH_TYPE_INDEX_TEXTURE_RID);
|
|
|
|
|
2022-02-24 12:45:21 +01:00
|
|
|
//Note: the transform parameter is not implemented in gles2
|
2022-02-24 13:02:01 +01:00
|
|
|
VisualServer::get_singleton()->canvas_item_add_mesh(get_canvas_item(), liquid_mesh_rid, Transform2D(), Color(1, 1, 1, 1), liquid_texture_rid, RID());
|
2022-02-24 09:24:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RID prop_mesh_rid = mesh_rid_get(MESH_INDEX_PROP, MESH_TYPE_INDEX_MESH);
|
|
|
|
|
|
|
|
if (prop_mesh_rid != RID()) {
|
|
|
|
RID prop_texture_rid = mesh_rid_get(MESH_INDEX_PROP, MESH_TYPE_INDEX_TEXTURE_RID);
|
|
|
|
|
2022-02-24 12:45:21 +01:00
|
|
|
//Note: the transform parameter is not implemented in gles2
|
2022-02-24 13:02:01 +01:00
|
|
|
VisualServer::get_singleton()->canvas_item_add_mesh(get_canvas_item(), prop_mesh_rid, Transform2D(), Color(1, 1, 1, 1), prop_texture_rid, RID());
|
2022-02-24 09:24:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 22:00:45 +01:00
|
|
|
//Lights
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::_bake_lights() {
|
2021-11-22 22:00:45 +01:00
|
|
|
clear_baked_lights();
|
|
|
|
|
|
|
|
for (int i = 0; i < _lights.size(); ++i) {
|
|
|
|
bake_light(_lights.get(i));
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::_bake_light(Ref<Terrain2DLight> light) {
|
2021-11-22 22:00:45 +01:00
|
|
|
ERR_FAIL_COND(!light.is_valid());
|
|
|
|
|
|
|
|
Color color = light->get_color();
|
|
|
|
int size = light->get_size();
|
|
|
|
|
|
|
|
int local_x = light->get_world_position_x() - (_position_x * _size_x);
|
2022-02-24 16:55:14 +01:00
|
|
|
int local_y = light->get_world_position_y() - (_position_y * _size_y);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(size < 0);
|
|
|
|
|
|
|
|
int64_t dsx = static_cast<int64_t>(_data_size_x);
|
2022-02-24 16:55:14 +01:00
|
|
|
int64_t dsy = static_cast<int64_t>(_data_size_y);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
uint8_t *channel_color_r = channel_get(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R);
|
|
|
|
uint8_t *channel_color_g = channel_get(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G);
|
|
|
|
uint8_t *channel_color_b = channel_get(Terrain2DChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(channel_color_r == NULL || channel_color_g == NULL || channel_color_b == NULL);
|
|
|
|
|
2022-02-24 16:55:14 +01:00
|
|
|
for (int y = local_y - size; y <= local_y + size; ++y) {
|
|
|
|
if (y < 0 || y >= dsy)
|
2021-11-22 22:00:45 +01:00
|
|
|
continue;
|
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
for (int x = local_x - size; x <= local_x + size; ++x) {
|
|
|
|
if (x < 0 || x >= dsx)
|
2021-11-22 22:00:45 +01:00
|
|
|
continue;
|
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
int lx = x - local_x;
|
2022-02-24 16:55:14 +01:00
|
|
|
int ly = y - local_y;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-24 16:55:14 +01:00
|
|
|
float str = size - (((float)lx * lx + ly * ly));
|
2022-02-23 19:38:06 +01:00
|
|
|
str /= size;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
if (str < 0)
|
|
|
|
continue;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-24 16:55:14 +01:00
|
|
|
int index = get_data_index(x, y);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
int r = color.r * str * 255.0;
|
|
|
|
int g = color.g * str * 255.0;
|
|
|
|
int b = color.b * str * 255.0;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
r += channel_color_r[index];
|
|
|
|
g += channel_color_g[index];
|
|
|
|
b += channel_color_b[index];
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
if (r > 255)
|
|
|
|
r = 255;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
if (g > 255)
|
|
|
|
g = 255;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
if (b > 255)
|
|
|
|
b = 255;
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-23 19:38:06 +01:00
|
|
|
channel_color_r[index] = r;
|
|
|
|
channel_color_g[index] = g;
|
|
|
|
channel_color_b[index] = b;
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::_clear_baked_lights() {
|
2021-11-22 22:00:45 +01:00
|
|
|
channel_fill(0, DEFAULT_CHANNEL_LIGHT_COLOR_R);
|
|
|
|
channel_fill(0, DEFAULT_CHANNEL_LIGHT_COLOR_G);
|
|
|
|
channel_fill(0, DEFAULT_CHANNEL_LIGHT_COLOR_B);
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::_world_light_added(const Ref<Terrain2DLight> &light) {
|
2021-11-22 22:00:45 +01:00
|
|
|
_lights.push_back(light);
|
|
|
|
|
|
|
|
set_lights_dirty(true);
|
|
|
|
}
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::_world_light_removed(const Ref<Terrain2DLight> &light) {
|
2021-11-22 22:00:45 +01:00
|
|
|
int index = _lights.find(light);
|
|
|
|
|
|
|
|
if (index != -1) {
|
|
|
|
_lights.remove(index);
|
|
|
|
|
|
|
|
set_lights_dirty(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::free_chunk() {
|
2021-11-22 22:00:45 +01:00
|
|
|
rids_free();
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::_finalize_build() {
|
2021-11-22 22:00:45 +01:00
|
|
|
ERR_FAIL_COND(!_library.is_valid());
|
|
|
|
|
|
|
|
#if TOOLS_ENABLED
|
|
|
|
if (_debug_mesh_array.size() > 0) {
|
|
|
|
debug_mesh_send();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
call_deferred("update_transforms");
|
2022-02-23 22:05:33 +01:00
|
|
|
//call_deferred("update");
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
Terrain2DChunkDefault::Terrain2DChunkDefault() {
|
2021-11-22 22:00:45 +01:00
|
|
|
_abort_build = false;
|
|
|
|
|
|
|
|
_enabled = true;
|
|
|
|
|
2022-02-24 17:04:35 +01:00
|
|
|
_build_flags = BUILD_FLAG_CREATE_COLLIDER;
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
Terrain2DChunkDefault::~Terrain2DChunkDefault() {
|
2021-11-22 22:00:45 +01:00
|
|
|
_abort_build = true;
|
|
|
|
|
|
|
|
_lights.clear();
|
|
|
|
|
|
|
|
debug_mesh_free();
|
2022-02-22 10:33:06 +01:00
|
|
|
|
|
|
|
rids_free();
|
|
|
|
rids_clear();
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::_channel_setup() {
|
2021-11-22 22:00:45 +01:00
|
|
|
channel_set_count(MAX_DEFAULT_CHANNELS);
|
|
|
|
}
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
void Terrain2DChunkDefault::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_build_flags"), &Terrain2DChunkDefault::get_build_flags);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_build_flags", "value"), &Terrain2DChunkDefault::set_build_flags);
|
2021-11-22 22:00:45 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "build_flags", PROPERTY_HINT_FLAGS, BINDING_STRING_BUILD_FLAGS, 0), "set_build_flags", "get_build_flags");
|
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_lights_dirty"), &Terrain2DChunkDefault::get_lights_dirty);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_lights_dirty", "value"), &Terrain2DChunkDefault::set_lights_dirty);
|
2021-11-22 22:00:45 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "lights_dirty", PROPERTY_HINT_NONE, "", 0), "set_lights_dirty", "get_lights_dirty");
|
|
|
|
|
|
|
|
//Meshes
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_mesh_rids"), &Terrain2DChunkDefault::mesh_rids_get);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_mesh_rids", "rids"), &Terrain2DChunkDefault::mesh_rids_set);
|
|
|
|
ClassDB::bind_method(D_METHOD("clear_rids"), &Terrain2DChunkDefault::rids_clear);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("mesh_rid_get", "mesh_index", "mesh_type_index"), &Terrain2DChunkDefault::mesh_rid_get);
|
|
|
|
ClassDB::bind_method(D_METHOD("mesh_rid_set", "mesh_index", "mesh_type_index", "value"), &Terrain2DChunkDefault::mesh_rid_set);
|
|
|
|
ClassDB::bind_method(D_METHOD("mesh_rid_get_index", "mesh_index", "mesh_type_index", "index"), &Terrain2DChunkDefault::mesh_rid_get_index);
|
|
|
|
ClassDB::bind_method(D_METHOD("mesh_rid_set_index", "mesh_index", "mesh_type_index", "index", "value"), &Terrain2DChunkDefault::mesh_rid_set_index);
|
|
|
|
ClassDB::bind_method(D_METHOD("mesh_rid_get_count", "mesh_index", "mesh_type_index"), &Terrain2DChunkDefault::mesh_rid_get_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("mesh_rids_clear", "mesh_index", "mesh_type_index"), &Terrain2DChunkDefault::mesh_rids_clear);
|
|
|
|
ClassDB::bind_method(D_METHOD("meshes_get", "mesh_index", "mesh_type_index"), &Terrain2DChunkDefault::meshes_get);
|
|
|
|
ClassDB::bind_method(D_METHOD("meshes_set", "mesh_index", "mesh_type_index", "meshes"), &Terrain2DChunkDefault::meshes_set);
|
|
|
|
ClassDB::bind_method(D_METHOD("meshes_has", "mesh_index", "mesh_type_index"), &Terrain2DChunkDefault::meshes_has);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("rids_free"), &Terrain2DChunkDefault::rids_free);
|
|
|
|
ClassDB::bind_method(D_METHOD("free_index", "mesh_index"), &Terrain2DChunkDefault::free_index);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("meshes_create", "mesh_index", "mesh_count"), &Terrain2DChunkDefault::meshes_create);
|
|
|
|
ClassDB::bind_method(D_METHOD("meshes_free", "mesh_index"), &Terrain2DChunkDefault::meshes_free);
|
2022-02-24 09:40:21 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("mesh_create", "mesh_index"), &Terrain2DChunkDefault::mesh_create);
|
|
|
|
ClassDB::bind_method(D_METHOD("mesh_free", "mesh_index"), &Terrain2DChunkDefault::mesh_free);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("create_colliders", "mesh_index", "layer_mask"), &Terrain2DChunkDefault::colliders_create, DEFVAL(1));
|
|
|
|
ClassDB::bind_method(D_METHOD("free_colliders", "mesh_index"), &Terrain2DChunkDefault::colliders_free);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
//Lights
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_light", "index"), &Terrain2DChunkDefault::get_light);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_light_count"), &Terrain2DChunkDefault::get_light_count);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
//Debug
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_allocate"), &Terrain2DChunkDefault::debug_mesh_allocate);
|
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_free"), &Terrain2DChunkDefault::debug_mesh_free);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_has"), &Terrain2DChunkDefault::debug_mesh_has);
|
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_clear"), &Terrain2DChunkDefault::debug_mesh_clear);
|
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_array_clear"), &Terrain2DChunkDefault::debug_mesh_array_clear);
|
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_add_vertices_to", "arr"), &Terrain2DChunkDefault::debug_mesh_add_vertices_to);
|
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_send"), &Terrain2DChunkDefault::debug_mesh_send);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("draw_cross_voxels", "max"), &Terrain2DChunkDefault::draw_cross_voxels);
|
|
|
|
ClassDB::bind_method(D_METHOD("draw_cross_voxels_fill", "max", "fill"), &Terrain2DChunkDefault::draw_cross_voxels_fill);
|
|
|
|
ClassDB::bind_method(D_METHOD("draw_debug_voxels", "max", "color"), &Terrain2DChunkDefault::draw_debug_voxels, DEFVAL(Color(1, 1, 1)));
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("draw_debug_voxel_lights"), &Terrain2DChunkDefault::draw_debug_voxel_lights);
|
|
|
|
ClassDB::bind_method(D_METHOD("draw_debug_mdr_colliders"), &Terrain2DChunkDefault::draw_debug_mdr_colliders);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
//Free
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("free_chunk"), &Terrain2DChunkDefault::free_chunk);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
//etc
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("emit_build_finished"), &Terrain2DChunkDefault::emit_build_finished);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
//virtuals
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_channel_setup"), &Terrain2DChunkDefault::_channel_setup);
|
2022-02-24 09:24:41 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_draw"), &Terrain2DChunkDefault::_draw);
|
|
|
|
|
2021-11-22 22:00:45 +01:00
|
|
|
//lights
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_bake_lights"), &Terrain2DChunkDefault::_bake_lights);
|
|
|
|
ClassDB::bind_method(D_METHOD("_bake_light", "light"), &Terrain2DChunkDefault::_bake_light);
|
|
|
|
ClassDB::bind_method(D_METHOD("_clear_baked_lights"), &Terrain2DChunkDefault::_clear_baked_lights);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_world_light_added", "light"), &Terrain2DChunkDefault::_world_light_added);
|
|
|
|
ClassDB::bind_method(D_METHOD("_world_light_removed", "light"), &Terrain2DChunkDefault::_world_light_removed);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2022-02-22 11:42:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_finalize_build"), &Terrain2DChunkDefault::_finalize_build);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(DEFAULT_CHANNEL_TYPE);
|
2022-02-26 14:56:04 +01:00
|
|
|
BIND_ENUM_CONSTANT(DEFAULT_CHANNEL_FLAGS);
|
2022-02-22 10:33:06 +01:00
|
|
|
BIND_ENUM_CONSTANT(DEFAULT_CHANNEL_LIQUID_TYPE);
|
2021-11-22 22:00:45 +01:00
|
|
|
BIND_ENUM_CONSTANT(DEFAULT_CHANNEL_LIGHT_COLOR_R);
|
|
|
|
BIND_ENUM_CONSTANT(DEFAULT_CHANNEL_LIGHT_COLOR_G);
|
|
|
|
BIND_ENUM_CONSTANT(DEFAULT_CHANNEL_LIGHT_COLOR_B);
|
|
|
|
BIND_ENUM_CONSTANT(DEFAULT_CHANNEL_AO);
|
|
|
|
BIND_ENUM_CONSTANT(DEFAULT_CHANNEL_RANDOM_AO);
|
|
|
|
BIND_ENUM_CONSTANT(MAX_DEFAULT_CHANNELS);
|
|
|
|
|
2022-02-26 14:56:04 +01:00
|
|
|
BIND_ENUM_CONSTANT(FLAG_CHANNEL_WALL_NORTH);
|
|
|
|
BIND_ENUM_CONSTANT(FLAG_CHANNEL_WALL_PY);
|
|
|
|
BIND_ENUM_CONSTANT(FLAG_CHANNEL_WALL_SOUTH);
|
|
|
|
BIND_ENUM_CONSTANT(FLAG_CHANNEL_WALL_NY);
|
|
|
|
BIND_ENUM_CONSTANT(FLAG_CHANNEL_WALL_EAST);
|
|
|
|
BIND_ENUM_CONSTANT(FLAG_CHANNEL_WALL_PX);
|
|
|
|
BIND_ENUM_CONSTANT(FLAG_CHANNEL_WALL_WEST);
|
|
|
|
BIND_ENUM_CONSTANT(FLAG_CHANNEL_WALL_NX);
|
2022-02-26 17:24:45 +01:00
|
|
|
BIND_ENUM_CONSTANT(FLAG_CHANNEL_COLLIDER);
|
|
|
|
BIND_ENUM_CONSTANT(FLAG_CHANNEL_FLIP_H);
|
|
|
|
BIND_ENUM_CONSTANT(FLAG_CHANNEL_FLIP_V);
|
|
|
|
BIND_ENUM_CONSTANT(FLAG_CHANNEL_WALL_HOLE);
|
2022-02-26 14:56:04 +01:00
|
|
|
|
2022-02-22 10:33:06 +01:00
|
|
|
BIND_CONSTANT(MESH_INDEX_TERRAIN);
|
2021-11-22 22:00:45 +01:00
|
|
|
BIND_CONSTANT(MESH_INDEX_PROP);
|
|
|
|
BIND_CONSTANT(MESH_INDEX_LIQUID);
|
|
|
|
BIND_CONSTANT(MESH_INDEX_CLUTTER);
|
|
|
|
|
|
|
|
BIND_CONSTANT(MESH_TYPE_INDEX_MESH);
|
|
|
|
BIND_CONSTANT(MESH_TYPE_INDEX_SHAPE);
|
|
|
|
BIND_CONSTANT(MESH_TYPE_INDEX_BODY);
|
2022-02-24 09:24:41 +01:00
|
|
|
BIND_CONSTANT(MESH_TYPE_INDEX_AREA);
|
|
|
|
BIND_CONSTANT(MESH_TYPE_INDEX_TEXTURE_RID);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(BUILD_FLAG_USE_LIGHTING);
|
|
|
|
BIND_ENUM_CONSTANT(BUILD_FLAG_USE_AO);
|
|
|
|
BIND_ENUM_CONSTANT(BUILD_FLAG_USE_RAO);
|
|
|
|
BIND_ENUM_CONSTANT(BUILD_FLAG_GENERATE_AO);
|
|
|
|
BIND_ENUM_CONSTANT(BUILD_FLAG_AUTO_GENERATE_RAO);
|
|
|
|
BIND_ENUM_CONSTANT(BUILD_FLAG_BAKE_LIGHTS);
|
|
|
|
BIND_ENUM_CONSTANT(BUILD_FLAG_CREATE_COLLIDER);
|
|
|
|
}
|