2020-03-12 23:23:38 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2019-2020 Péter Magyar
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "voxel_chunk_default.h"
|
|
|
|
|
2020-05-23 10:34:47 +02:00
|
|
|
#include "../../defines.h"
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-05-23 10:34:47 +02:00
|
|
|
#include visual_server_h
|
|
|
|
#include physics_server_h
|
2020-04-09 12:34:39 +02:00
|
|
|
|
2020-04-06 13:41:45 +02:00
|
|
|
#include "../../../opensimplex/open_simplex_noise.h"
|
2020-04-10 13:40:07 +02:00
|
|
|
#include "../../meshers/default/voxel_mesher_default.h"
|
|
|
|
#include "../voxel_world.h"
|
2020-04-05 01:36:41 +02:00
|
|
|
|
2020-10-01 20:18:43 +02:00
|
|
|
#include "../jobs/voxel_job.h"
|
2020-08-04 09:44:18 +02:00
|
|
|
|
2020-08-16 18:20:16 +02:00
|
|
|
#include "voxel_world_default.h"
|
|
|
|
|
2020-10-02 23:47:39 +02:00
|
|
|
#include "../jobs/voxel_light_job.h"
|
|
|
|
#include "../jobs/voxel_prop_job.h"
|
|
|
|
#include "../jobs/voxel_terrarin_job.h"
|
|
|
|
|
2020-04-06 13:41:45 +02:00
|
|
|
const String VoxelChunkDefault::BINDING_STRING_BUILD_FLAGS = "Use Isolevel,Use Lighting,Use AO,Use RAO,Generate AO,Generate RAO,Bake Lights,Create Collider,Create Lods";
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-04-06 13:41:45 +02:00
|
|
|
_FORCE_INLINE_ int VoxelChunkDefault::get_build_flags() const {
|
|
|
|
return _build_flags;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ void VoxelChunkDefault::set_build_flags(const int flags) {
|
|
|
|
_build_flags = flags;
|
|
|
|
}
|
|
|
|
|
2020-04-15 12:41:52 +02:00
|
|
|
bool VoxelChunkDefault::get_lights_dirty() const {
|
|
|
|
return _lights_dirty;
|
|
|
|
}
|
|
|
|
void VoxelChunkDefault::set_lights_dirty(const bool value) {
|
|
|
|
_lights_dirty = value;
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:46:39 +02:00
|
|
|
int VoxelChunkDefault::get_lod_num() const {
|
|
|
|
return _lod_num;
|
|
|
|
}
|
|
|
|
void VoxelChunkDefault::set_lod_num(const int value) {
|
|
|
|
_lod_num = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VoxelChunkDefault::get_current_lod_level() const {
|
|
|
|
return _current_lod_level;
|
|
|
|
}
|
|
|
|
void VoxelChunkDefault::set_current_lod_level(const int value) {
|
|
|
|
_current_lod_level = value;
|
|
|
|
|
2020-04-06 13:41:45 +02:00
|
|
|
if ((_build_flags & BUILD_FLAG_CREATE_LODS) == 0)
|
2020-03-30 19:46:39 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (_current_lod_level < 0)
|
|
|
|
_current_lod_level = 0;
|
|
|
|
|
|
|
|
if (_current_lod_level > _lod_num)
|
|
|
|
_current_lod_level = _lod_num;
|
|
|
|
|
|
|
|
for (int i = 0; i < _lod_num + 1; ++i) {
|
|
|
|
bool vis = false;
|
|
|
|
|
|
|
|
if (i == _current_lod_level)
|
|
|
|
vis = true;
|
|
|
|
|
|
|
|
RID rid = get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH_INSTANCE, i);
|
|
|
|
|
2020-06-23 19:32:37 +02:00
|
|
|
if (rid != RID())
|
|
|
|
VisualServer::get_singleton()->instance_set_visible(rid, vis);
|
|
|
|
|
|
|
|
rid = get_mesh_rid_index(MESH_INDEX_PROP, MESH_TYPE_INDEX_MESH_INSTANCE, i);
|
|
|
|
|
2020-03-30 19:46:39 +02:00
|
|
|
if (rid != RID())
|
|
|
|
VisualServer::get_singleton()->instance_set_visible(rid, vis);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 18:42:01 +01:00
|
|
|
void VoxelChunkDefault::emit_build_finished() {
|
|
|
|
emit_signal("mesh_generation_finished", this);
|
|
|
|
|
|
|
|
if (_voxel_world != NULL) {
|
|
|
|
_voxel_world->on_chunk_mesh_generation_finished(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
//Meshes
|
|
|
|
Dictionary VoxelChunkDefault::get_mesh_rids() {
|
|
|
|
return _rids;
|
|
|
|
}
|
|
|
|
void VoxelChunkDefault::set_mesh_rids(const Dictionary &rids) {
|
|
|
|
_rids = rids;
|
|
|
|
}
|
|
|
|
void VoxelChunkDefault::clear_rids() {
|
|
|
|
_rids.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
RID VoxelChunkDefault::get_mesh_rid(const int mesh_index, const int mesh_type_index) {
|
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return RID();
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
Dictionary m = _rids[mesh_index];
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (!m.has(mesh_type_index))
|
|
|
|
return RID();
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
Variant v = m[mesh_type_index];
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (v.get_type() != Variant::_RID)
|
|
|
|
return RID();
|
|
|
|
|
|
|
|
return v;
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
2020-03-30 18:32:39 +02:00
|
|
|
void VoxelChunkDefault::set_mesh_rid(const int mesh_index, const int mesh_type_index, RID value) {
|
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
_rids[mesh_index] = Dictionary();
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
Dictionary m = _rids[mesh_index];
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (!m.has(mesh_type_index)) {
|
|
|
|
m[mesh_type_index] = value;
|
|
|
|
_rids[mesh_index] = m;
|
|
|
|
return;
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
2020-03-30 18:32:39 +02:00
|
|
|
|
|
|
|
Variant v = m[mesh_type_index];
|
|
|
|
|
|
|
|
ERR_FAIL_COND(v.get_type() != Variant::_RID);
|
|
|
|
|
|
|
|
m[mesh_type_index] = value;
|
|
|
|
_rids[mesh_index] = m;
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
2020-03-30 18:32:39 +02:00
|
|
|
RID VoxelChunkDefault::get_mesh_rid_index(const int mesh_index, const int mesh_type_index, const int index) {
|
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return RID();
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
Dictionary m = _rids[mesh_index];
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (!m.has(mesh_type_index))
|
|
|
|
return RID();
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
Variant v = m[mesh_type_index];
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (v.get_type() != Variant::ARRAY)
|
|
|
|
return RID();
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
Array arr = v;
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
ERR_FAIL_INDEX_V(index, arr.size(), RID());
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
return arr[index];
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
2020-03-30 18:32:39 +02:00
|
|
|
void VoxelChunkDefault::set_mesh_rid_index(const int mesh_index, const int mesh_type_index, const int index, RID value) {
|
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
_rids[mesh_index] = Dictionary();
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (!m.has(mesh_type_index)) {
|
|
|
|
Array arr;
|
|
|
|
arr.resize(index + 1);
|
|
|
|
arr[index] = value;
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
m[mesh_type_index] = arr;
|
|
|
|
_rids[mesh_index] = m;
|
|
|
|
return;
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
2020-03-30 18:32:39 +02:00
|
|
|
|
|
|
|
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;
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
2020-03-30 18:32:39 +02:00
|
|
|
int VoxelChunkDefault::get_mesh_rid_count(const int mesh_index, const int mesh_type_index) {
|
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return 0;
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
Dictionary m = _rids[mesh_index];
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (!m.has(mesh_type_index))
|
|
|
|
return 0;
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
Variant v = m[mesh_type_index];
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (v.get_type() != Variant::ARRAY)
|
|
|
|
return 0;
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
Array arr = v;
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
return arr.size();
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
2020-03-30 18:32:39 +02:00
|
|
|
void VoxelChunkDefault::clear_mesh_rids(const int mesh_index, const int mesh_type_index) {
|
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return;
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
Dictionary m = _rids[mesh_index];
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (!m.has(mesh_type_index))
|
|
|
|
return;
|
|
|
|
|
|
|
|
m.erase(mesh_type_index);
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
2020-03-30 18:32:39 +02:00
|
|
|
Array VoxelChunkDefault::get_meshes(const int mesh_index, const int mesh_type_index) {
|
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return Array();
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
if (!m.has(mesh_type_index))
|
|
|
|
return Array();
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
Variant v = m[mesh_type_index];
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (v.get_type() != Variant::ARRAY)
|
|
|
|
return Array();
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
void VoxelChunkDefault::set_meshes(const int mesh_index, const int mesh_type_index, const Array &meshes) {
|
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
_rids[mesh_index] = Dictionary();
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
m[mesh_type_index] = meshes;
|
|
|
|
_rids[mesh_index] = m;
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
2020-03-30 18:32:39 +02:00
|
|
|
bool VoxelChunkDefault::has_meshes(const int mesh_index, const int mesh_type_index) {
|
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
if (!m.has(mesh_type_index))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelChunkDefault::free_rids() {
|
|
|
|
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;
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
free_index(v);
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
void VoxelChunkDefault::free_index(const int mesh_index) {
|
|
|
|
free_meshes(mesh_index);
|
|
|
|
free_colliders(mesh_index);
|
|
|
|
}
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
void VoxelChunkDefault::create_meshes(const int mesh_index, const int mesh_count) {
|
|
|
|
ERR_FAIL_COND(_voxel_world == NULL);
|
2020-03-12 23:23:38 +01:00
|
|
|
ERR_FAIL_COND(!get_library().is_valid());
|
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
_rids[mesh_index] = Dictionary();
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
|
|
|
|
ERR_FAIL_COND(m.has(MESH_TYPE_INDEX_MESH));
|
|
|
|
ERR_FAIL_COND(m.has(MESH_TYPE_INDEX_MESH_INSTANCE));
|
|
|
|
|
|
|
|
Array am;
|
|
|
|
Array ami;
|
|
|
|
|
|
|
|
for (int i = 0; i < mesh_count; ++i) {
|
|
|
|
RID mesh_instance_rid = VS::get_singleton()->instance_create();
|
|
|
|
|
2020-05-23 10:34:47 +02:00
|
|
|
if (get_voxel_world()->GET_WORLD().is_valid())
|
|
|
|
VS::get_singleton()->instance_set_scenario(mesh_instance_rid, get_voxel_world()->GET_WORLD()->get_scenario());
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
RID mesh_rid = VS::get_singleton()->mesh_create();
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
VS::get_singleton()->instance_set_base(mesh_instance_rid, mesh_rid);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-04-02 21:28:19 +02:00
|
|
|
VS::get_singleton()->instance_set_transform(mesh_instance_rid, get_transform());
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 19:46:39 +02:00
|
|
|
if (i != 0)
|
|
|
|
VS::get_singleton()->instance_set_visible(mesh_instance_rid, false);
|
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
am.push_back(mesh_rid);
|
|
|
|
ami.push_back(mesh_instance_rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
m[MESH_TYPE_INDEX_MESH] = am;
|
|
|
|
m[MESH_TYPE_INDEX_MESH_INSTANCE] = ami;
|
|
|
|
|
|
|
|
_rids[mesh_index] = m;
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
2020-03-30 18:32:39 +02:00
|
|
|
void VoxelChunkDefault::free_meshes(const int mesh_index) {
|
|
|
|
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];
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
for (int i = 0; i < a.size(); ++i) {
|
|
|
|
RID r = a[i];
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (r != rid) {
|
|
|
|
VS::get_singleton()->free(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m.has(MESH_TYPE_INDEX_MESH_INSTANCE)) {
|
|
|
|
Array a = m[MESH_TYPE_INDEX_MESH_INSTANCE];
|
|
|
|
|
|
|
|
for (int i = 0; i < a.size(); ++i) {
|
|
|
|
RID r = a[i];
|
|
|
|
|
|
|
|
if (r != rid) {
|
|
|
|
VS::get_singleton()->free(r);
|
|
|
|
}
|
|
|
|
}
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
2020-03-30 18:32:39 +02:00
|
|
|
|
|
|
|
m.erase(MESH_TYPE_INDEX_MESH);
|
|
|
|
m.erase(MESH_TYPE_INDEX_MESH_INSTANCE);
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
void VoxelChunkDefault::create_colliders(const int mesh_index, const int layer_mask) {
|
2020-04-20 17:30:22 +02:00
|
|
|
ERR_FAIL_COND(_voxel_world == NULL);
|
|
|
|
ERR_FAIL_COND(PhysicsServer::get_singleton()->is_flushing_queries());
|
2020-04-22 12:33:14 +02:00
|
|
|
//ERR_FAIL_COND(!get_voxel_world()->is_inside_tree());
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
_rids[mesh_index] = Dictionary();
|
|
|
|
|
|
|
|
Dictionary m = _rids[mesh_index];
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
ERR_FAIL_COND(m.has(MESH_TYPE_INDEX_BODY));
|
|
|
|
ERR_FAIL_COND(m.has(MESH_TYPE_INDEX_SHAPE));
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
RID shape_rid = PhysicsServer::get_singleton()->shape_create(PhysicsServer::SHAPE_CONCAVE_POLYGON);
|
|
|
|
RID body_rid = PhysicsServer::get_singleton()->body_create(PhysicsServer::BODY_MODE_STATIC);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
PhysicsServer::get_singleton()->body_set_collision_layer(body_rid, layer_mask);
|
|
|
|
PhysicsServer::get_singleton()->body_set_collision_mask(body_rid, layer_mask);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
PhysicsServer::get_singleton()->body_add_shape(body_rid, shape_rid);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-04-02 21:28:19 +02:00
|
|
|
PhysicsServer::get_singleton()->body_set_state(body_rid, PhysicsServer::BODY_STATE_TRANSFORM, get_transform());
|
2020-04-18 02:15:01 +02:00
|
|
|
|
2020-04-22 12:23:49 +02:00
|
|
|
if (get_voxel_world()->is_inside_tree() && get_voxel_world()->is_inside_world()) {
|
2020-05-23 10:34:47 +02:00
|
|
|
Ref<World> world = get_voxel_world()->GET_WORLD();
|
2020-04-20 13:54:24 +02:00
|
|
|
|
|
|
|
if (world.is_valid() && world->get_space() != RID())
|
|
|
|
PhysicsServer::get_singleton()->body_set_space(body_rid, world->get_space());
|
|
|
|
}
|
2020-03-30 18:32:39 +02:00
|
|
|
|
|
|
|
m[MESH_TYPE_INDEX_BODY] = body_rid;
|
|
|
|
m[MESH_TYPE_INDEX_SHAPE] = shape_rid;
|
|
|
|
|
|
|
|
_rids[mesh_index] = m;
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
2020-04-20 01:01:24 +02:00
|
|
|
void VoxelChunkDefault::create_colliders_area(const int mesh_index, const int layer_mask) {
|
2020-04-20 17:30:22 +02:00
|
|
|
ERR_FAIL_COND(_voxel_world == NULL);
|
|
|
|
ERR_FAIL_COND(PhysicsServer::get_singleton()->is_flushing_queries());
|
2020-04-20 01:01:24 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2020-04-20 17:30:22 +02:00
|
|
|
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);
|
|
|
|
|
2020-04-20 01:01:24 +02:00
|
|
|
PhysicsServer::get_singleton()->area_set_collision_layer(area_rid, layer_mask);
|
|
|
|
PhysicsServer::get_singleton()->area_set_collision_mask(area_rid, layer_mask);
|
|
|
|
|
2020-04-22 12:23:49 +02:00
|
|
|
if (get_voxel_world()->is_inside_tree() && get_voxel_world()->is_inside_world()) {
|
2020-05-23 10:34:47 +02:00
|
|
|
Ref<World> world = get_voxel_world()->GET_WORLD();
|
2020-04-20 13:54:24 +02:00
|
|
|
|
|
|
|
if (world.is_valid() && world->get_space() != RID())
|
|
|
|
PhysicsServer::get_singleton()->area_set_space(area_rid, world->get_space());
|
|
|
|
}
|
2020-04-20 01:01:24 +02:00
|
|
|
|
2020-04-20 17:30:22 +02:00
|
|
|
PhysicsServer::get_singleton()->area_add_shape(area_rid, shape_rid, get_transform());
|
|
|
|
|
2020-04-20 01:01:24 +02:00
|
|
|
m[MESH_TYPE_INDEX_AREA] = area_rid;
|
|
|
|
m[MESH_TYPE_INDEX_SHAPE] = shape_rid;
|
|
|
|
|
|
|
|
_rids[mesh_index] = m;
|
|
|
|
}
|
2020-04-20 17:30:22 +02:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
void VoxelChunkDefault::free_colliders(const int mesh_index) {
|
|
|
|
if (!_rids.has(mesh_index))
|
|
|
|
return;
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
Dictionary m = _rids[mesh_index];
|
|
|
|
RID rid;
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (m.has(MESH_TYPE_INDEX_SHAPE)) {
|
|
|
|
RID r = m[MESH_TYPE_INDEX_SHAPE];
|
|
|
|
|
|
|
|
PhysicsServer::get_singleton()->free(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m.has(MESH_TYPE_INDEX_BODY)) {
|
|
|
|
RID r = m[MESH_TYPE_INDEX_BODY];
|
|
|
|
|
|
|
|
PhysicsServer::get_singleton()->free(r);
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
2020-03-30 18:32:39 +02:00
|
|
|
|
|
|
|
m.erase(MESH_TYPE_INDEX_SHAPE);
|
|
|
|
m.erase(MESH_TYPE_INDEX_BODY);
|
|
|
|
|
|
|
|
_rids[mesh_index] = m;
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 19:33:10 +01:00
|
|
|
void VoxelChunkDefault::update_transforms() {
|
2020-03-30 18:32:39 +02:00
|
|
|
RID empty_rid;
|
|
|
|
Transform t = get_transform();
|
|
|
|
|
|
|
|
List<Variant> keys;
|
|
|
|
|
|
|
|
_rids.get_key_list(&keys);
|
2020-03-15 19:33:10 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
|
|
|
|
Variant v = E->get();
|
2020-03-15 19:33:10 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (v.get_type() != Variant::INT)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Dictionary d = _rids[v];
|
|
|
|
|
|
|
|
if (d.has(MESH_TYPE_INDEX_MESH_INSTANCE)) {
|
|
|
|
Array arr = d[MESH_TYPE_INDEX_MESH_INSTANCE];
|
|
|
|
|
|
|
|
for (int i = 0; i < arr.size(); ++i) {
|
|
|
|
RID rid = arr[i];
|
|
|
|
|
|
|
|
if (rid != empty_rid)
|
|
|
|
VS::get_singleton()->instance_set_transform(rid, get_transform());
|
|
|
|
}
|
|
|
|
}
|
2020-03-15 19:33:10 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
if (d.has(MESH_TYPE_INDEX_BODY)) {
|
|
|
|
RID rid = d[MESH_TYPE_INDEX_BODY];
|
|
|
|
|
|
|
|
if (rid != empty_rid)
|
|
|
|
PhysicsServer::get_singleton()->body_set_state(rid, PhysicsServer::BODY_STATE_TRANSFORM, t);
|
|
|
|
}
|
2020-04-20 01:01:24 +02:00
|
|
|
|
|
|
|
if (d.has(MESH_TYPE_INDEX_AREA)) {
|
|
|
|
RID rid = d[MESH_TYPE_INDEX_AREA];
|
|
|
|
|
|
|
|
if (rid != empty_rid)
|
|
|
|
PhysicsServer::get_singleton()->area_set_shape_transform(rid, 0, t);
|
|
|
|
}
|
2020-03-30 18:32:39 +02:00
|
|
|
}
|
2020-06-28 21:55:00 +02:00
|
|
|
|
2020-06-30 18:55:50 +02:00
|
|
|
for (int i = 0; i < get_collider_count(); ++i) {
|
|
|
|
PhysicsServer::get_singleton()->body_set_state(get_collider_body(i), PhysicsServer::BODY_STATE_TRANSFORM, get_transform() * get_collider_transform(i));
|
2020-06-28 21:55:00 +02:00
|
|
|
}
|
2020-06-29 23:08:52 +02:00
|
|
|
|
|
|
|
if (_debug_mesh_instance != RID()) {
|
|
|
|
VS::get_singleton()->instance_set_transform(_debug_mesh_instance, get_transform());
|
|
|
|
}
|
2020-03-15 19:33:10 +01:00
|
|
|
}
|
|
|
|
|
2020-04-15 12:41:52 +02:00
|
|
|
//Lights
|
|
|
|
Ref<VoxelLight> VoxelChunkDefault::get_light(const int index) {
|
|
|
|
ERR_FAIL_INDEX_V(index, _lights.size(), Ref<VoxelLight>());
|
|
|
|
|
|
|
|
return _lights.get(index);
|
|
|
|
}
|
|
|
|
int VoxelChunkDefault::get_light_count() const {
|
|
|
|
return _lights.size();
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
void VoxelChunkDefault::debug_mesh_allocate() {
|
|
|
|
if (_debug_mesh_rid == RID()) {
|
|
|
|
_debug_mesh_rid = VisualServer::get_singleton()->mesh_create();
|
|
|
|
}
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
if (_debug_mesh_instance == RID()) {
|
|
|
|
_debug_mesh_instance = VisualServer::get_singleton()->instance_create();
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-06-29 23:08:52 +02: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());
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
VS::get_singleton()->instance_set_base(_debug_mesh_instance, _debug_mesh_rid);
|
|
|
|
VS::get_singleton()->instance_set_transform(_debug_mesh_instance, get_transform());
|
|
|
|
VS::get_singleton()->instance_set_visible(_debug_mesh_instance, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void VoxelChunkDefault::debug_mesh_free() {
|
|
|
|
if (_debug_mesh_instance != RID()) {
|
|
|
|
VisualServer::get_singleton()->free(_debug_mesh_instance);
|
|
|
|
}
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
if (_debug_mesh_rid != RID()) {
|
|
|
|
VisualServer::get_singleton()->free(_debug_mesh_rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool VoxelChunkDefault::debug_mesh_has() {
|
|
|
|
return _debug_mesh_rid != RID();
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
2020-06-29 23:08:52 +02:00
|
|
|
void VoxelChunkDefault::debug_mesh_clear() {
|
|
|
|
if (_debug_mesh_rid != RID()) {
|
|
|
|
VisualServer::get_singleton()->mesh_clear(_debug_mesh_rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void VoxelChunkDefault::debug_mesh_array_clear() {
|
|
|
|
_debug_mesh_array.resize(0);
|
|
|
|
}
|
|
|
|
void VoxelChunkDefault::debug_mesh_add_vertices_to(const PoolVector3Array &arr) {
|
|
|
|
_debug_mesh_array.append_array(arr);
|
2020-06-29 23:44:54 +02:00
|
|
|
|
|
|
|
if (_debug_mesh_array.size() % 2 == 1) {
|
|
|
|
_debug_mesh_array.append(_debug_mesh_array[_debug_mesh_array.size() - 1]);
|
|
|
|
}
|
2020-06-29 23:08:52 +02:00
|
|
|
}
|
|
|
|
void VoxelChunkDefault::debug_mesh_send() {
|
|
|
|
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;
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
VisualServer::get_singleton()->mesh_add_surface_from_arrays(_debug_mesh_rid, VisualServer::PRIMITIVE_LINES, arr);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
if (st) {
|
|
|
|
VisualServer::get_singleton()->mesh_surface_set_material(_debug_mesh_rid, 0, SceneTree::get_singleton()->get_debug_collision_material()->get_rid());
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
2020-06-29 23:44:54 +02:00
|
|
|
|
|
|
|
debug_mesh_array_clear();
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelChunkDefault::draw_cross_voxels(Vector3 pos) {
|
|
|
|
pos *= _voxel_scale;
|
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
int size = _debug_mesh_array.size();
|
|
|
|
_debug_mesh_array.resize(_debug_mesh_array.size() + 6);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
_debug_mesh_array.set(size, pos + Vector3(0, 0, -0.2));
|
|
|
|
_debug_mesh_array.set(size + 1, pos + Vector3(0, 0, 0.2));
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
_debug_mesh_array.set(size + 2, pos + Vector3(0, -0.2, 0));
|
|
|
|
_debug_mesh_array.set(size + 3, pos + Vector3(0, 0.2, 0));
|
|
|
|
|
|
|
|
_debug_mesh_array.set(size + 4, pos + Vector3(-0.2, 0, 0));
|
|
|
|
_debug_mesh_array.set(size + 5, pos + Vector3(0.2, 0, 0));
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelChunkDefault::draw_cross_voxels_fill(Vector3 pos, float fill) {
|
|
|
|
pos *= _voxel_scale;
|
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
int size = _debug_mesh_array.size();
|
|
|
|
_debug_mesh_array.resize(_debug_mesh_array.size() + 6);
|
|
|
|
|
|
|
|
_debug_mesh_array.set(size, pos + Vector3(0, 0, -0.2 * fill));
|
|
|
|
_debug_mesh_array.set(size + 1, pos + Vector3(0, 0, 0.2 * fill));
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
_debug_mesh_array.set(size + 2, pos + Vector3(0, -0.2 * fill, 0));
|
|
|
|
_debug_mesh_array.set(size + 3, pos + Vector3(0, 0.2 * fill, 0));
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
_debug_mesh_array.set(size + 4, pos + Vector3(-0.2 * fill, 0, 0));
|
|
|
|
_debug_mesh_array.set(size + 5, pos + Vector3(0.2 * fill, 0, 0));
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelChunkDefault::draw_debug_voxels(int max, Color color) {
|
2020-06-29 23:08:52 +02:00
|
|
|
if (!debug_mesh_has()) {
|
|
|
|
debug_mesh_allocate();
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
//debug_mesh_array_clear();
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
//_debug_drawer->begin(Mesh::PRIMITIVE_LINES);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
|
|
|
int a = 0;
|
|
|
|
|
|
|
|
int64_t sx = static_cast<int64_t>(_size_x);
|
|
|
|
int64_t sy = static_cast<int64_t>(_size_y);
|
|
|
|
int64_t sz = static_cast<int64_t>(_size_y);
|
|
|
|
|
|
|
|
for (int y = 0; y < sy; ++y) {
|
|
|
|
for (int z = 0; z < sz; ++z) {
|
|
|
|
for (int x = 0; x < sx; ++x) {
|
|
|
|
int type = get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_TYPE);
|
|
|
|
|
|
|
|
if (type == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
draw_cross_voxels_fill(Vector3(x, y, z), get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_ISOLEVEL) / 255.0 * get_voxel_scale() * 2.0);
|
|
|
|
|
|
|
|
++a;
|
|
|
|
|
|
|
|
if (a > max) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
debug_mesh_send();
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelChunkDefault::draw_debug_voxel_lights() {
|
2020-06-29 23:08:52 +02:00
|
|
|
if (!debug_mesh_has()) {
|
|
|
|
debug_mesh_allocate();
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
//debug_mesh_array_clear();
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
//_debug_drawer->begin(Mesh::PrimitiveType::PRIMITIVE_LINES);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-04-15 12:41:52 +02:00
|
|
|
for (int i = 0; i < _lights.size(); ++i) {
|
|
|
|
Ref<VoxelLight> v = _lights[i];
|
2020-03-12 23:23:38 +01:00
|
|
|
|
|
|
|
int pos_x = v->get_world_position_x() - (_size_x * _position_x);
|
|
|
|
int pos_y = v->get_world_position_y() - (_size_y * _position_y);
|
|
|
|
int pos_z = v->get_world_position_z() - (_size_z * _position_z);
|
|
|
|
|
|
|
|
draw_cross_voxels_fill(Vector3(pos_x, pos_y, pos_z), 1.0);
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
debug_mesh_send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelChunkDefault::draw_debug_mdr_colliders() {
|
|
|
|
if (!debug_mesh_has()) {
|
|
|
|
debug_mesh_allocate();
|
|
|
|
}
|
|
|
|
|
2020-06-30 20:28:37 +02:00
|
|
|
for (int i = 0; i < get_collider_count(); ++i) {
|
|
|
|
Ref<Shape> shape = get_collider_shape(i);
|
2020-06-29 23:08:52 +02:00
|
|
|
|
2020-06-30 20:28:37 +02:00
|
|
|
if (!shape.is_valid())
|
2020-06-29 23:08:52 +02:00
|
|
|
continue;
|
|
|
|
|
2020-06-30 20:28:37 +02:00
|
|
|
Transform t = get_collider_transform(i);
|
2020-06-29 23:08:52 +02:00
|
|
|
|
2020-06-30 20:28:37 +02:00
|
|
|
shape->add_vertices_to_array(_debug_mesh_array, t);
|
2020-06-29 23:08:52 +02:00
|
|
|
}
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
|
|
|
|
2020-03-29 23:55:40 +02:00
|
|
|
void VoxelChunkDefault::_visibility_changed(bool visible) {
|
2020-03-30 19:46:39 +02:00
|
|
|
if (visible) {
|
|
|
|
set_current_lod_level(_current_lod_level);
|
|
|
|
return;
|
|
|
|
}
|
2020-03-29 23:55:40 +02:00
|
|
|
|
2020-03-30 19:46:39 +02:00
|
|
|
for (int i = 0; i < _lod_num + 1; ++i) {
|
|
|
|
RID rid = get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH_INSTANCE, i);
|
2020-03-29 23:55:40 +02:00
|
|
|
|
2020-04-20 01:01:24 +02:00
|
|
|
if (rid != RID())
|
|
|
|
VisualServer::get_singleton()->instance_set_visible(rid, false);
|
|
|
|
|
|
|
|
rid = get_mesh_rid_index(MESH_INDEX_LIQUID, MESH_TYPE_INDEX_MESH_INSTANCE, i);
|
|
|
|
|
2020-06-23 19:32:37 +02:00
|
|
|
if (rid != RID())
|
|
|
|
VisualServer::get_singleton()->instance_set_visible(rid, false);
|
|
|
|
|
|
|
|
rid = get_mesh_rid_index(MESH_INDEX_PROP, MESH_TYPE_INDEX_MESH_INSTANCE, i);
|
|
|
|
|
2020-03-30 19:46:39 +02:00
|
|
|
if (rid != RID())
|
|
|
|
VisualServer::get_singleton()->instance_set_visible(rid, false);
|
|
|
|
}
|
2020-03-15 19:33:10 +01:00
|
|
|
}
|
|
|
|
|
2020-04-02 21:28:19 +02:00
|
|
|
void VoxelChunkDefault::_exit_tree() {
|
2020-10-02 23:47:39 +02:00
|
|
|
VoxelChunk::_exit_tree();
|
2020-04-02 21:28:19 +02:00
|
|
|
|
|
|
|
free_rids();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelChunkDefault::_world_transform_changed() {
|
|
|
|
VoxelChunk::_world_transform_changed();
|
|
|
|
|
|
|
|
update_transforms();
|
|
|
|
}
|
|
|
|
|
2020-04-15 13:06:45 +02:00
|
|
|
//Lights
|
2020-04-15 12:41:52 +02:00
|
|
|
void VoxelChunkDefault::_bake_lights() {
|
|
|
|
clear_baked_lights();
|
|
|
|
|
|
|
|
for (int i = 0; i < _lights.size(); ++i) {
|
|
|
|
bake_light(_lights.get(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void VoxelChunkDefault::_bake_light(Ref<VoxelLight> light) {
|
|
|
|
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);
|
|
|
|
int local_y = light->get_world_position_y() - (_position_y * _size_y);
|
|
|
|
int local_z = light->get_world_position_z() - (_position_z * _size_z);
|
|
|
|
|
|
|
|
ERR_FAIL_COND(size < 0);
|
|
|
|
|
|
|
|
int64_t dsx = static_cast<int64_t>(_data_size_x);
|
|
|
|
int64_t dsy = static_cast<int64_t>(_data_size_y);
|
|
|
|
int64_t dsz = static_cast<int64_t>(_data_size_z);
|
|
|
|
|
2020-07-07 17:35:44 +02:00
|
|
|
uint8_t *channel_color_r = get_channel(VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R);
|
|
|
|
uint8_t *channel_color_g = get_channel(VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G);
|
|
|
|
uint8_t *channel_color_b = get_channel(VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B);
|
|
|
|
|
|
|
|
ERR_FAIL_COND(channel_color_r == NULL || channel_color_g == NULL || channel_color_b == NULL);
|
|
|
|
|
2020-04-15 12:41:52 +02:00
|
|
|
for (int y = local_y - size; y <= local_y + size; ++y) {
|
|
|
|
if (y < 0 || y >= dsy)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int z = local_z - size; z <= local_z + size; ++z) {
|
|
|
|
if (z < 0 || z >= dsz)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int x = local_x - size; x <= local_x + size; ++x) {
|
|
|
|
if (x < 0 || x >= dsx)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int lx = x - local_x;
|
|
|
|
int ly = y - local_y;
|
|
|
|
int lz = z - local_z;
|
|
|
|
|
|
|
|
float str = size - (((float)lx * lx + ly * ly + lz * lz));
|
|
|
|
str /= size;
|
|
|
|
|
|
|
|
if (str < 0)
|
|
|
|
continue;
|
|
|
|
|
2020-07-07 17:35:44 +02:00
|
|
|
int index = get_data_index(x, y, z);
|
|
|
|
|
2020-04-15 12:41:52 +02:00
|
|
|
int r = color.r * str * 255.0;
|
|
|
|
int g = color.g * str * 255.0;
|
|
|
|
int b = color.b * str * 255.0;
|
|
|
|
|
2020-07-07 17:35:44 +02:00
|
|
|
r += channel_color_r[index];
|
|
|
|
g += channel_color_g[index];
|
|
|
|
b += channel_color_b[index];
|
2020-04-15 12:41:52 +02:00
|
|
|
|
|
|
|
if (r > 255)
|
|
|
|
r = 255;
|
|
|
|
|
|
|
|
if (g > 255)
|
|
|
|
g = 255;
|
|
|
|
|
|
|
|
if (b > 255)
|
|
|
|
b = 255;
|
|
|
|
|
2020-07-07 17:35:44 +02:00
|
|
|
channel_color_r[index] = r;
|
|
|
|
channel_color_g[index] = g;
|
|
|
|
channel_color_b[index] = b;
|
2020-04-15 12:41:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void VoxelChunkDefault::_clear_baked_lights() {
|
|
|
|
fill_channel(0, DEFAULT_CHANNEL_LIGHT_COLOR_R);
|
|
|
|
fill_channel(0, DEFAULT_CHANNEL_LIGHT_COLOR_G);
|
|
|
|
fill_channel(0, DEFAULT_CHANNEL_LIGHT_COLOR_B);
|
|
|
|
}
|
|
|
|
void VoxelChunkDefault::_world_light_added(const Ref<VoxelLight> &light) {
|
|
|
|
_lights.push_back(light);
|
|
|
|
|
|
|
|
set_lights_dirty(true);
|
|
|
|
}
|
|
|
|
void VoxelChunkDefault::_world_light_removed(const Ref<VoxelLight> &light) {
|
|
|
|
int index = _lights.find(light);
|
|
|
|
|
|
|
|
if (index != -1) {
|
|
|
|
_lights.remove(index);
|
|
|
|
|
|
|
|
set_lights_dirty(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:23:38 +01:00
|
|
|
void VoxelChunkDefault::free_chunk() {
|
2020-03-30 18:32:39 +02:00
|
|
|
free_rids();
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
|
|
|
|
2020-10-03 17:18:43 +02:00
|
|
|
void VoxelChunkDefault::_finalize_build() {
|
2020-10-02 23:47:39 +02:00
|
|
|
ERR_FAIL_COND(!_library.is_valid());
|
|
|
|
|
|
|
|
#if TOOLS_ENABLED
|
|
|
|
if (_debug_mesh_array.size() > 0) {
|
|
|
|
debug_mesh_send();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
set_current_lod_level(get_current_lod_level());
|
|
|
|
|
|
|
|
call_deferred("update_transforms");
|
2020-08-04 09:44:18 +02:00
|
|
|
}
|
|
|
|
|
2020-03-12 23:23:38 +01:00
|
|
|
VoxelChunkDefault::VoxelChunkDefault() {
|
|
|
|
_abort_build = false;
|
|
|
|
|
|
|
|
_enabled = true;
|
2020-03-30 19:46:39 +02:00
|
|
|
|
|
|
|
_lod_num = 3;
|
|
|
|
_current_lod_level = 0;
|
2020-04-06 13:41:45 +02:00
|
|
|
|
|
|
|
_build_flags = BUILD_FLAG_CREATE_COLLIDER | BUILD_FLAG_CREATE_LODS;
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
VoxelChunkDefault::~VoxelChunkDefault() {
|
2020-08-04 09:49:40 +02:00
|
|
|
_abort_build = true;
|
2020-04-15 12:41:52 +02:00
|
|
|
|
|
|
|
_lights.clear();
|
2020-06-28 19:02:49 +02:00
|
|
|
|
2020-06-29 23:44:54 +02:00
|
|
|
debug_mesh_free();
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelChunkDefault::_setup_channels() {
|
|
|
|
set_channel_count(MAX_DEFAULT_CHANNELS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelChunkDefault::_bind_methods() {
|
2020-04-06 13:41:45 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_build_flags"), &VoxelChunkDefault::get_build_flags);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_build_flags", "value"), &VoxelChunkDefault::set_build_flags);
|
2020-04-22 12:26:53 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "build_flags", PROPERTY_HINT_FLAGS, BINDING_STRING_BUILD_FLAGS, 0), "set_build_flags", "get_build_flags");
|
2020-04-06 13:41:45 +02:00
|
|
|
|
2020-04-15 12:41:52 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_lights_dirty"), &VoxelChunkDefault::get_lights_dirty);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_lights_dirty", "value"), &VoxelChunkDefault::set_lights_dirty);
|
2020-04-22 12:26:53 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "lights_dirty", PROPERTY_HINT_NONE, "", 0), "set_lights_dirty", "get_lights_dirty");
|
2020-04-15 12:41:52 +02:00
|
|
|
|
2020-03-30 19:46:39 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_lod_num"), &VoxelChunkDefault::get_lod_num);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_lod_num"), &VoxelChunkDefault::set_lod_num);
|
2020-04-22 12:26:53 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "lod_num", PROPERTY_HINT_NONE, "", 0), "set_lod_num", "get_lod_num");
|
2020-03-30 19:46:39 +02:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_current_lod_level"), &VoxelChunkDefault::get_current_lod_level);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_current_lod_level"), &VoxelChunkDefault::set_current_lod_level);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_lod_level"), "set_current_lod_level", "get_current_lod_level");
|
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
//Meshes
|
|
|
|
ClassDB::bind_method(D_METHOD("get_mesh_rids"), &VoxelChunkDefault::get_mesh_rids);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_mesh_rids", "rids"), &VoxelChunkDefault::set_mesh_rids);
|
|
|
|
ClassDB::bind_method(D_METHOD("clear_rids"), &VoxelChunkDefault::clear_rids);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_mesh_rid", "mesh_index", "mesh_type_index"), &VoxelChunkDefault::get_mesh_rid);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_mesh_rid", "mesh_index", "mesh_type_index", "value"), &VoxelChunkDefault::set_mesh_rid);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_mesh_rid_index", "mesh_index", "mesh_type_index", "index"), &VoxelChunkDefault::get_mesh_rid_index);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_mesh_rid_index", "mesh_index", "mesh_type_index", "index", "value"), &VoxelChunkDefault::set_mesh_rid_index);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_mesh_rid_count", "mesh_index", "mesh_type_index"), &VoxelChunkDefault::get_mesh_rid_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("clear_mesh_rids", "mesh_index", "mesh_type_index"), &VoxelChunkDefault::clear_mesh_rids);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_meshes", "mesh_index", "mesh_type_index"), &VoxelChunkDefault::get_meshes);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_meshes", "mesh_index", "mesh_type_index", "meshes"), &VoxelChunkDefault::set_meshes);
|
|
|
|
ClassDB::bind_method(D_METHOD("has_meshes", "mesh_index", "mesh_type_index"), &VoxelChunkDefault::has_meshes);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("free_rids"), &VoxelChunkDefault::free_rids);
|
|
|
|
ClassDB::bind_method(D_METHOD("free_index", "mesh_index"), &VoxelChunkDefault::free_index);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("create_meshes", "mesh_index", "mesh_count"), &VoxelChunkDefault::create_meshes);
|
|
|
|
ClassDB::bind_method(D_METHOD("free_meshes", "mesh_index"), &VoxelChunkDefault::free_meshes);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("create_colliders", "mesh_index", "layer_mask"), &VoxelChunkDefault::create_colliders, DEFVAL(1));
|
|
|
|
ClassDB::bind_method(D_METHOD("free_colliders", "mesh_index"), &VoxelChunkDefault::free_colliders);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-04-15 12:41:52 +02:00
|
|
|
//Lights
|
|
|
|
ClassDB::bind_method(D_METHOD("get_light", "index"), &VoxelChunkDefault::get_light);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_light_count"), &VoxelChunkDefault::get_light_count);
|
|
|
|
|
|
|
|
//Debug
|
2020-06-29 23:08:52 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_allocate"), &VoxelChunkDefault::debug_mesh_allocate);
|
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_free"), &VoxelChunkDefault::debug_mesh_free);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-06-29 23:08:52 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_has"), &VoxelChunkDefault::debug_mesh_has);
|
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_clear"), &VoxelChunkDefault::debug_mesh_clear);
|
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_array_clear"), &VoxelChunkDefault::debug_mesh_array_clear);
|
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_add_vertices_to", "arr"), &VoxelChunkDefault::debug_mesh_add_vertices_to);
|
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_send"), &VoxelChunkDefault::debug_mesh_send);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("draw_cross_voxels", "max"), &VoxelChunkDefault::draw_cross_voxels);
|
|
|
|
ClassDB::bind_method(D_METHOD("draw_cross_voxels_fill", "max", "fill"), &VoxelChunkDefault::draw_cross_voxels_fill);
|
|
|
|
ClassDB::bind_method(D_METHOD("draw_debug_voxels", "max", "color"), &VoxelChunkDefault::draw_debug_voxels, DEFVAL(Color(1, 1, 1)));
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("draw_debug_voxel_lights"), &VoxelChunkDefault::draw_debug_voxel_lights);
|
2020-06-29 23:08:52 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("draw_debug_mdr_colliders"), &VoxelChunkDefault::draw_debug_mdr_colliders);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-04-15 12:41:52 +02:00
|
|
|
//Free
|
|
|
|
ClassDB::bind_method(D_METHOD("free_chunk"), &VoxelChunkDefault::free_chunk);
|
|
|
|
|
|
|
|
//etc
|
|
|
|
ClassDB::bind_method(D_METHOD("emit_build_finished"), &VoxelChunkDefault::emit_build_finished);
|
|
|
|
|
|
|
|
//virtuals
|
2020-03-12 23:23:38 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_setup_channels"), &VoxelChunkDefault::_setup_channels);
|
|
|
|
|
2020-04-15 12:41:52 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_visibility_changed", "visible"), &VoxelChunkDefault::_visibility_changed);
|
2020-03-12 23:23:38 +01:00
|
|
|
|
2020-04-15 12:41:52 +02:00
|
|
|
//lights
|
|
|
|
ClassDB::bind_method(D_METHOD("_bake_lights"), &VoxelChunkDefault::_bake_lights);
|
|
|
|
ClassDB::bind_method(D_METHOD("_bake_light", "light"), &VoxelChunkDefault::_bake_light);
|
|
|
|
ClassDB::bind_method(D_METHOD("_clear_baked_lights"), &VoxelChunkDefault::_clear_baked_lights);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("_world_light_added", "light"), &VoxelChunkDefault::_world_light_added);
|
|
|
|
ClassDB::bind_method(D_METHOD("_world_light_removed", "light"), &VoxelChunkDefault::_world_light_removed);
|
2020-03-15 19:33:10 +01:00
|
|
|
|
2020-10-03 17:18:43 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("_finalize_build"), &VoxelChunkDefault::_finalize_build);
|
2020-08-04 09:44:18 +02:00
|
|
|
|
2020-03-12 23:23:38 +01:00
|
|
|
BIND_ENUM_CONSTANT(DEFAULT_CHANNEL_TYPE);
|
|
|
|
BIND_ENUM_CONSTANT(DEFAULT_CHANNEL_ISOLEVEL);
|
2020-08-21 20:02:47 +02:00
|
|
|
BIND_ENUM_CONSTANT(DEFAULT_CHANNEL_ALT_TYPE);
|
|
|
|
BIND_ENUM_CONSTANT(DEFAULT_CHANNEL_ALT_ISOLEVEL);
|
2020-03-12 23:23:38 +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(DEFAULT_CHANNEL_LIQUID_FLOW);
|
|
|
|
BIND_ENUM_CONSTANT(MAX_DEFAULT_CHANNELS);
|
|
|
|
|
2020-03-30 18:32:39 +02:00
|
|
|
BIND_CONSTANT(MESH_INDEX_TERRARIN);
|
|
|
|
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_MESH_INSTANCE);
|
|
|
|
BIND_CONSTANT(MESH_TYPE_INDEX_SHAPE);
|
|
|
|
BIND_CONSTANT(MESH_TYPE_INDEX_BODY);
|
2020-04-06 13:41:45 +02:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(BUILD_FLAG_USE_ISOLEVEL);
|
|
|
|
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);
|
|
|
|
BIND_ENUM_CONSTANT(BUILD_FLAG_CREATE_LODS);
|
2020-03-12 23:23:38 +01:00
|
|
|
}
|