diff --git a/prop_instance_merger.cpp b/prop_instance_merger.cpp index 655e854..9b49f8f 100644 --- a/prop_instance_merger.cpp +++ b/prop_instance_merger.cpp @@ -49,8 +49,8 @@ typedef class RenderingServer VS; #include "./props/prop_data_prop.h" #include "./props/prop_data_scene.h" #include "jobs/prop_mesher_job_step.h" -#include "material_cache/prop_material_cache.h" #include "lights/prop_light.h" +#include "material_cache/prop_material_cache.h" #if TEXTURE_PACKER_PRESENT #include "./singleton/prop_cache.h" @@ -70,6 +70,33 @@ void PropInstanceMerger::set_building(const bool value) { set_process_internal(_building); } +int PropInstanceMerger::get_lod_level() { + return _lod_level; +} +void PropInstanceMerger::set_lod_level(const int value) { + _lod_level = value; + + if (_lod_level < 0) { + _lod_level = 0; + } + + apply_lod_level(); +} + +float PropInstanceMerger::get_first_lod_distance_squared() { + return _first_lod_distance_squared; +} +void PropInstanceMerger::set_first_lod_distance_squared(const float dist) { + _first_lod_distance_squared = dist; +} + +float PropInstanceMerger::get_lod_reduction_distance_squared() { + return _lod_reduction_distance_squared; +} +void PropInstanceMerger::set_lod_reduction_distance_squared(const float dist) { + _lod_reduction_distance_squared = dist; +} + Ref PropInstanceMerger::get_job() { return _job; } @@ -156,8 +183,7 @@ void PropInstanceMerger::meshes_create(const int num) { VS::get_singleton()->instance_set_transform(mesh_instance_rid, get_transform()); - if (i != 0) - VS::get_singleton()->instance_set_visible(mesh_instance_rid, false); + VS::get_singleton()->instance_set_visible(mesh_instance_rid, false); MeshEntry e; e.mesh = mesh_rid; @@ -165,6 +191,8 @@ void PropInstanceMerger::meshes_create(const int num) { _meshes.push_back(e); } + + apply_lod_level(); } Vector PropInstanceMerger::meshes_get() { @@ -267,6 +295,46 @@ void PropInstanceMerger::colliders_set(const Vector &colliders) { } } +void PropInstanceMerger::apply_lod_level() { + if (_meshes.size() == 0) { + return; + } + + VisualServer *vs = VisualServer::get_singleton(); + + for (int i = 0; i < _meshes.size(); ++i) { + RID mi = _meshes[i].mesh_instance; + + if (mi == RID()) { + continue; + } + + vs->instance_set_visible(mi, false); + } + + if (!is_inside_tree()) { + return; + } + + if (!is_visible_in_tree()) { + return; + } + + int indx = _lod_level; + + if (_meshes.size() <= _lod_level) { + indx = _meshes.size() - 1; + } + + RID mi = _meshes[indx].mesh_instance; + + if (mi == RID()) { + return; + } + + vs->instance_set_visible(mi, true); +} + void PropInstanceMerger::debug_mesh_allocate() { if (_debug_mesh_rid == RID()) { _debug_mesh_rid = VisualServer::get_singleton()->mesh_create(); @@ -351,20 +419,6 @@ void PropInstanceMerger::draw_debug_mdr_colliders() { debug_mesh_send(); } -float PropInstanceMerger::get_first_lod_distance_squared() { - return _first_lod_distance_squared; -} -void PropInstanceMerger::set_first_lod_distance_squared(const float dist) { - _first_lod_distance_squared = dist; -} - -float PropInstanceMerger::get_lod_reduction_distance_squared() { - return _lod_reduction_distance_squared; -} -void PropInstanceMerger::set_lod_reduction_distance_squared(const float dist) { - _lod_reduction_distance_squared = dist; -} - void PropInstanceMerger::free_meshes() { RID rid; @@ -468,6 +522,8 @@ Don't submit here, as it starts in physics process mode void PropInstanceMerger::_build_finished() { set_building(false); + apply_lod_level(); + if (_build_queued) { call_deferred("build"); } @@ -559,6 +615,7 @@ void PropInstanceMerger::_prop_preprocess(Transform transform, const Ref get_job(); void set_job(const Ref &job); @@ -85,6 +94,8 @@ public: Vector colliders_get(); void colliders_set(const Vector &colliders); + void apply_lod_level(); + //Debug void debug_mesh_allocate(); void debug_mesh_free(); @@ -95,12 +106,6 @@ public: void debug_mesh_send(); void draw_debug_mdr_colliders(); - float get_first_lod_distance_squared(); - void set_first_lod_distance_squared(const float dist); - - float get_lod_reduction_distance_squared(); - void set_lod_reduction_distance_squared(const float dist); - void free_meshes(); void free_colliders(); @@ -135,9 +140,11 @@ private: bool _build_queued; bool _building; + int _lod_level; + Ref _job; - Vector > _materials; + Vector> _materials; Vector _meshes; Vector _colliders;