diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2020-12-18 16:19:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-18 16:19:12 +0100 |
commit | 7ad29ed64e850fd43ba7ceb1cfaab4e015ef7b97 (patch) | |
tree | f1d46d8194f01e56cd3a4d042b9b33ba728c6f62 /scene | |
parent | 36b4e035dc7c410c29cda4446f8daa2e00a31da0 (diff) | |
parent | d2302f53d6ad80943e7f4245ac572003f1681d00 (diff) |
Merge pull request #44468 from reduz/implement-lod
Implement automatic LOD (Level of Detail)
Diffstat (limited to 'scene')
-rw-r--r-- | scene/3d/reflection_probe.cpp | 14 | ||||
-rw-r--r-- | scene/3d/reflection_probe.h | 4 | ||||
-rw-r--r-- | scene/3d/visual_instance_3d.cpp | 16 | ||||
-rw-r--r-- | scene/3d/visual_instance_3d.h | 5 | ||||
-rw-r--r-- | scene/main/scene_tree.cpp | 4 | ||||
-rw-r--r-- | scene/main/viewport.cpp | 15 | ||||
-rw-r--r-- | scene/main/viewport.h | 6 | ||||
-rw-r--r-- | scene/resources/surface_tool.cpp | 3 | ||||
-rw-r--r-- | scene/resources/surface_tool.h | 2 |
9 files changed, 67 insertions, 2 deletions
diff --git a/scene/3d/reflection_probe.cpp b/scene/3d/reflection_probe.cpp index c7948395d3..c82ed423a7 100644 --- a/scene/3d/reflection_probe.cpp +++ b/scene/3d/reflection_probe.cpp @@ -76,6 +76,15 @@ float ReflectionProbe::get_max_distance() const { return max_distance; } +void ReflectionProbe::set_lod_threshold(float p_pixels) { + lod_threshold = p_pixels; + RS::get_singleton()->reflection_probe_set_lod_threshold(probe, p_pixels); +} + +float ReflectionProbe::get_lod_threshold() const { + return lod_threshold; +} + void ReflectionProbe::set_extents(const Vector3 &p_extents) { extents = p_extents; @@ -199,6 +208,9 @@ void ReflectionProbe::_bind_methods() { ClassDB::bind_method(D_METHOD("set_max_distance", "max_distance"), &ReflectionProbe::set_max_distance); ClassDB::bind_method(D_METHOD("get_max_distance"), &ReflectionProbe::get_max_distance); + ClassDB::bind_method(D_METHOD("set_lod_threshold", "ratio"), &ReflectionProbe::set_lod_threshold); + ClassDB::bind_method(D_METHOD("get_lod_threshold"), &ReflectionProbe::get_lod_threshold); + ClassDB::bind_method(D_METHOD("set_extents", "extents"), &ReflectionProbe::set_extents); ClassDB::bind_method(D_METHOD("get_extents"), &ReflectionProbe::get_extents); @@ -229,6 +241,7 @@ void ReflectionProbe::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior"), "set_as_interior", "is_set_as_interior"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enable_shadows"), "set_enable_shadows", "are_shadows_enabled"); ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lod_threshold", PROPERTY_HINT_RANGE, "0,1024,0.1"), "set_lod_threshold", "get_lod_threshold"); ADD_GROUP("Ambient", "ambient_"); ADD_PROPERTY(PropertyInfo(Variant::INT, "ambient_mode", PROPERTY_HINT_ENUM, "Disabled,Environment,ConstantColor"), "set_ambient_mode", "get_ambient_mode"); @@ -256,6 +269,7 @@ ReflectionProbe::ReflectionProbe() { enable_shadows = false; cull_mask = (1 << 20) - 1; update_mode = UPDATE_ONCE; + lod_threshold = 1.0; probe = RenderingServer::get_singleton()->reflection_probe_create(); RS::get_singleton()->instance_set_base(get_instance(), probe); diff --git a/scene/3d/reflection_probe.h b/scene/3d/reflection_probe.h index 56177d0f95..4bff2f8bf9 100644 --- a/scene/3d/reflection_probe.h +++ b/scene/3d/reflection_probe.h @@ -63,6 +63,7 @@ private: AmbientMode ambient_mode; Color ambient_color; float ambient_color_energy; + float lod_threshold; uint32_t cull_mask; UpdateMode update_mode; @@ -90,6 +91,9 @@ public: void set_max_distance(float p_distance); float get_max_distance() const; + void set_lod_threshold(float p_pixels); + float get_lod_threshold() const; + void set_extents(const Vector3 &p_extents); Vector3 get_extents() const; diff --git a/scene/3d/visual_instance_3d.cpp b/scene/3d/visual_instance_3d.cpp index a1c498e8ab..0b70b0f920 100644 --- a/scene/3d/visual_instance_3d.cpp +++ b/scene/3d/visual_instance_3d.cpp @@ -278,6 +278,16 @@ float GeometryInstance3D::get_extra_cull_margin() const { return extra_cull_margin; } +void GeometryInstance3D::set_lod_bias(float p_bias) { + ERR_FAIL_COND(p_bias < 0.0); + lod_bias = p_bias; + RS::get_singleton()->instance_geometry_set_lod_bias(get_instance(), lod_bias); +} + +float GeometryInstance3D::get_lod_bias() const { + return lod_bias; +} + void GeometryInstance3D::set_shader_instance_uniform(const StringName &p_uniform, const Variant &p_value) { if (p_value.get_type() == Variant::NIL) { Variant def_value = RS::get_singleton()->instance_geometry_get_shader_parameter_default_value(get_instance(), p_uniform); @@ -361,6 +371,9 @@ void GeometryInstance3D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_gi_mode", "mode"), &GeometryInstance3D::set_gi_mode); ClassDB::bind_method(D_METHOD("get_gi_mode"), &GeometryInstance3D::get_gi_mode); + ClassDB::bind_method(D_METHOD("set_lod_bias", "p_bias"), &GeometryInstance3D::set_lod_bias); + ClassDB::bind_method(D_METHOD("get_lod_bias"), &GeometryInstance3D::get_lod_bias); + ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &GeometryInstance3D::set_custom_aabb); ClassDB::bind_method(D_METHOD("get_aabb"), &GeometryInstance3D::get_aabb); @@ -369,6 +382,7 @@ void GeometryInstance3D::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material_override", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,StandardMaterial3D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DEFERRED_SET_RESOURCE), "set_material_override", "get_material_override"); ADD_PROPERTY(PropertyInfo(Variant::INT, "cast_shadow", PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"), "set_cast_shadows_setting", "get_cast_shadows_setting"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "extra_cull_margin", PROPERTY_HINT_RANGE, "0,16384,0.01"), "set_extra_cull_margin", "get_extra_cull_margin"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lod_bias", PROPERTY_HINT_RANGE, "0.001,128,0.001"), "set_lod_bias", "get_lod_bias"); ADD_GROUP("Global Illumination", "gi_"); ADD_PROPERTY(PropertyInfo(Variant::INT, "gi_mode", PROPERTY_HINT_ENUM, "Disabled,Baked,Dynamic"), "set_gi_mode", "get_gi_mode"); ADD_PROPERTY(PropertyInfo(Variant::INT, "gi_lightmap_scale", PROPERTY_HINT_ENUM, "1x,2x,4x,8x"), "set_lightmap_scale", "get_lightmap_scale"); @@ -403,6 +417,8 @@ GeometryInstance3D::GeometryInstance3D() { lod_min_hysteresis = 0; lod_max_hysteresis = 0; + lod_bias = 1.0; + gi_mode = GI_MODE_DISABLED; lightmap_scale = LIGHTMAP_SCALE_1X; diff --git a/scene/3d/visual_instance_3d.h b/scene/3d/visual_instance_3d.h index 51bcb411da..0810b7b4ce 100644 --- a/scene/3d/visual_instance_3d.h +++ b/scene/3d/visual_instance_3d.h @@ -112,6 +112,8 @@ private: float lod_min_hysteresis; float lod_max_hysteresis; + float lod_bias; + mutable HashMap<StringName, Variant> instance_uniforms; mutable HashMap<StringName, StringName> instance_uniform_property_remap; @@ -151,6 +153,9 @@ public: void set_extra_cull_margin(float p_margin); float get_extra_cull_margin() const; + void set_lod_bias(float p_bias); + float get_lod_bias() const; + void set_gi_mode(GIMode p_mode); GIMode get_gi_mode() const; diff --git a/scene/main/scene_tree.cpp b/scene/main/scene_tree.cpp index 9e396d4030..1df61daa2c 100644 --- a/scene/main/scene_tree.cpp +++ b/scene/main/scene_tree.cpp @@ -1391,6 +1391,10 @@ SceneTree::SceneTree() { const bool use_debanding = GLOBAL_DEF("rendering/quality/screen_filters/use_debanding", false); root->set_use_debanding(use_debanding); + float lod_threshold = GLOBAL_DEF("rendering/quality/mesh_lod/threshold_pixels", 1.0); + ProjectSettings::get_singleton()->set_custom_property_info("rendering/quality/mesh_lod/threshold_pixels", PropertyInfo(Variant::FLOAT, "rendering/quality/mesh_lod/threshold_pixels", PROPERTY_HINT_RANGE, "0,1024,0.1")); + root->set_lod_threshold(lod_threshold); + bool snap_2d_transforms = GLOBAL_DEF("rendering/quality/2d/snap_2d_transforms_to_pixel", false); root->set_snap_2d_transforms_to_pixel(snap_2d_transforms); diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index 9f50b34e21..c96dd4ad35 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -3192,6 +3192,14 @@ bool Viewport::is_using_debanding() const { return use_debanding; } +void Viewport::set_lod_threshold(float p_pixels) { + lod_threshold = p_pixels; + RS::get_singleton()->viewport_set_lod_threshold(viewport, lod_threshold); +} +float Viewport::get_lod_threshold() const { + return lod_threshold; +} + void Viewport::set_debug_draw(DebugDraw p_debug_draw) { debug_draw = p_debug_draw; RS::get_singleton()->viewport_set_debug_draw(viewport, RS::ViewportDebugDraw(p_debug_draw)); @@ -3505,6 +3513,9 @@ void Viewport::_bind_methods() { ClassDB::bind_method(D_METHOD("set_sdf_scale", "scale"), &Viewport::set_sdf_scale); ClassDB::bind_method(D_METHOD("get_sdf_scale"), &Viewport::get_sdf_scale); + ClassDB::bind_method(D_METHOD("set_lod_threshold", "pixels"), &Viewport::set_lod_threshold); + ClassDB::bind_method(D_METHOD("get_lod_threshold"), &Viewport::get_lod_threshold); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "own_world_3d"), "set_use_own_world_3d", "is_using_own_world_3d"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world_3d", PROPERTY_HINT_RESOURCE_TYPE, "World3D"), "set_world_3d", "get_world_3d"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "world_2d", PROPERTY_HINT_RESOURCE_TYPE, "World2D", 0), "set_world_2d", "get_world_2d"); @@ -3516,6 +3527,7 @@ void Viewport::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::INT, "msaa", PROPERTY_HINT_ENUM, "Disabled,2x,4x,8x,16x,AndroidVR 2x,AndroidVR 4x"), "set_msaa", "get_msaa"); ADD_PROPERTY(PropertyInfo(Variant::INT, "screen_space_aa", PROPERTY_HINT_ENUM, "Disabled,FXAA"), "set_screen_space_aa", "get_screen_space_aa"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_debanding"), "set_use_debanding", "is_using_debanding"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lod_threshold", PROPERTY_HINT_RANGE, "0,1024,0.1"), "set_lod_threshold", "get_lod_threshold"); ADD_PROPERTY(PropertyInfo(Variant::INT, "debug_draw", PROPERTY_HINT_ENUM, "Disabled,Unshaded,Overdraw,Wireframe"), "set_debug_draw", "get_debug_draw"); ADD_GROUP("Canvas Items", "canvas_item_"); ADD_PROPERTY(PropertyInfo(Variant::INT, "canvas_item_default_texture_filter", PROPERTY_HINT_ENUM, "Nearest,Linear,MipmapLinear,MipmapNearest"), "set_default_canvas_item_texture_filter", "get_default_canvas_item_texture_filter"); @@ -3590,6 +3602,7 @@ void Viewport::_bind_methods() { BIND_ENUM_CONSTANT(DEBUG_DRAW_SDFGI); BIND_ENUM_CONSTANT(DEBUG_DRAW_SDFGI_PROBES); BIND_ENUM_CONSTANT(DEBUG_DRAW_GI_BUFFER); + BIND_ENUM_CONSTANT(DEBUG_DRAW_DISABLE_LOD); BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST); BIND_ENUM_CONSTANT(DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_LINEAR); @@ -3652,6 +3665,8 @@ Viewport::Viewport() { set_shadow_atlas_quadrant_subdiv(2, SHADOW_ATLAS_QUADRANT_SUBDIV_16); set_shadow_atlas_quadrant_subdiv(3, SHADOW_ATLAS_QUADRANT_SUBDIV_64); + set_lod_threshold(lod_threshold); + String id = itos(get_instance_id()); input_group = "_vp_input" + id; gui_input_group = "_vp_gui_input" + id; diff --git a/scene/main/viewport.h b/scene/main/viewport.h index f08f255dde..ffbc3c782a 100644 --- a/scene/main/viewport.h +++ b/scene/main/viewport.h @@ -142,6 +142,7 @@ public: DEBUG_DRAW_SDFGI, DEBUG_DRAW_SDFGI_PROBES, DEBUG_DRAW_GI_BUFFER, + DEBUG_DRAW_DISABLE_LOD, }; enum DefaultCanvasItemTextureFilter { @@ -297,6 +298,8 @@ private: MSAA msaa; ScreenSpaceAA screen_space_aa; bool use_debanding = false; + float lod_threshold = 1.0; + Ref<ViewportTexture> default_texture; Set<ViewportTexture *> viewport_textures; @@ -542,6 +545,9 @@ public: void set_use_debanding(bool p_use_debanding); bool is_using_debanding() const; + void set_lod_threshold(float p_pixels); + float get_lod_threshold() const; + Vector2 get_camera_coords(const Vector2 &p_viewport_coords) const; Vector2 get_camera_rect_size() const; diff --git a/scene/resources/surface_tool.cpp b/scene/resources/surface_tool.cpp index 772b54bc53..50308d641a 100644 --- a/scene/resources/surface_tool.cpp +++ b/scene/resources/surface_tool.cpp @@ -1128,7 +1128,8 @@ Vector<int> SurfaceTool::generate_lod(float p_threshold, int p_target_index_coun vertices[i * 3 + 2] = vertex_array[i].vertex.z; } - uint32_t index_count = simplify_func((unsigned int *)lod.ptrw(), (unsigned int *)index_array.ptr(), index_array.size(), vertices.ptr(), vertex_array.size(), sizeof(float) * 3, p_target_index_count, p_threshold); + float error; + uint32_t index_count = simplify_func((unsigned int *)lod.ptrw(), (unsigned int *)index_array.ptr(), index_array.size(), vertices.ptr(), vertex_array.size(), sizeof(float) * 3, p_target_index_count, p_threshold, &error); ERR_FAIL_COND_V(index_count == 0, lod); lod.resize(index_count); diff --git a/scene/resources/surface_tool.h b/scene/resources/surface_tool.h index e80a5339a9..0e60bfe389 100644 --- a/scene/resources/surface_tool.h +++ b/scene/resources/surface_tool.h @@ -76,7 +76,7 @@ public: typedef void (*OptimizeVertexCacheFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, size_t vertex_count); static OptimizeVertexCacheFunc optimize_vertex_cache_func; - typedef size_t (*SimplifyFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, const float *vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error); + typedef size_t (*SimplifyFunc)(unsigned int *destination, const unsigned int *indices, size_t index_count, const float *vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, float *r_error); static SimplifyFunc simplify_func; private: |