From e7d051043dc82c2991eb491e0630d9b1065934c3 Mon Sep 17 00:00:00 2001 From: Jorge Bernal Date: Sun, 22 Mar 2015 18:13:53 +0100 Subject: BGE: New hysteresis offset to improve LOD level transitions This change introduces a new hysteresis parameter that it will be added or subtracted to/from the LOD distance to avoid popping when a LOD object moves close to the LOD transition continuously. Then, we have the following: - a new LOD Hysteresis setting per scene (default 10%) which is located in Scene context --> Level of Detail panel. This scene parameter also will active/deactive the scene hysteresis. - and a new LOD Hysteresis setting per object (default 10%) which is located in Object context --> Levels of Detail panel. The LOD hysteresis setting per object (if active) will overwrite the hysteresis setting per scene value. For the new blends: the hysteresis setting per scene would be active by default and the per object would be inactive by default. For the old blends: both hysteresis settings (per scene and per object) would be inactive by default. A quick way to take advantage of this feature for old blends would be to activate the hysteresis parameter in the scene context -> Level of Detail panel Reviewers: campbellbarton, kupoman, moguri Reviewed By: kupoman, moguri Subscribers: nonamejuju, lordodin Differential Revision: https://developer.blender.org/D957 --- source/blender/blenkernel/intern/object.c | 2 ++ source/blender/blenkernel/intern/scene.c | 3 ++ source/blender/blenloader/intern/versioning_270.c | 18 ++++++++++ .../blenloader/intern/versioning_defaults.c | 3 ++ source/blender/makesdna/DNA_object_types.h | 4 ++- source/blender/makesdna/DNA_scene_types.h | 10 +++++- source/blender/makesrna/intern/rna_object.c | 13 +++++++ source/blender/makesrna/intern/rna_scene.c | 14 ++++++++ .../Converter/BL_BlenderDataConversion.cpp | 4 +++ source/gameengine/Ketsji/KX_GameObject.cpp | 42 +++++++++++++++++++++- source/gameengine/Ketsji/KX_GameObject.h | 10 ++++++ source/gameengine/Ketsji/KX_Scene.cpp | 13 ++++++- source/gameengine/Ketsji/KX_Scene.h | 9 +++++ 13 files changed, 141 insertions(+), 4 deletions(-) (limited to 'source') diff --git a/source/blender/blenkernel/intern/object.c b/source/blender/blenkernel/intern/object.c index 08a74d0c6cd..174fb38aada 100644 --- a/source/blender/blenkernel/intern/object.c +++ b/source/blender/blenkernel/intern/object.c @@ -1098,10 +1098,12 @@ void BKE_object_lod_add(Object *ob) BLI_addtail(&ob->lodlevels, base); base->flags = OB_LOD_USE_MESH | OB_LOD_USE_MAT; base->source = ob; + base->obhysteresis = 10; last = ob->currentlod = base; } lod->distance = last->distance + 25.0f; + lod->obhysteresis = 10; lod->flags = OB_LOD_USE_MESH | OB_LOD_USE_MAT; BLI_addtail(&ob->lodlevels, lod); diff --git a/source/blender/blenkernel/intern/scene.c b/source/blender/blenkernel/intern/scene.c index c28d741d7ec..d2c3f473e11 100644 --- a/source/blender/blenkernel/intern/scene.c +++ b/source/blender/blenkernel/intern/scene.c @@ -677,6 +677,9 @@ Scene *BKE_scene_add(Main *bmain, const char *name) sce->gm.recastData.detailsampledist = 6.0f; sce->gm.recastData.detailsamplemaxerror = 1.0f; + sce->gm.lodflag = SCE_LOD_USE_HYST; + sce->gm.scehysteresis = 10; + sce->gm.exitkey = 218; // Blender key code for ESC sound_create_scene(sce); diff --git a/source/blender/blenloader/intern/versioning_270.c b/source/blender/blenloader/intern/versioning_270.c index adf5f93ae16..0853c93750c 100644 --- a/source/blender/blenloader/intern/versioning_270.c +++ b/source/blender/blenloader/intern/versioning_270.c @@ -654,5 +654,23 @@ void blo_do_versions_270(FileData *fd, Library *UNUSED(lib), Main *main) } } } + + /* hysteresis setted to 10% but not actived */ + if (!DNA_struct_elem_find(fd->filesdna, "LodLevel", "int", "obhysteresis")) { + Object* ob; + for (ob = main->object.first; ob; ob = ob->id.next) { + LodLevel *level; + for (level = ob->lodlevels.first; level; level = level->next) { + level->obhysteresis = 10; + } + } + } + + if (!DNA_struct_elem_find(fd->filesdna, "GameData", "int", "scehysteresis")) { + Scene *scene; + for (scene = main->scene.first; scene; scene = scene->id.next) { + scene->gm.scehysteresis = 10; + } + } } } diff --git a/source/blender/blenloader/intern/versioning_defaults.c b/source/blender/blenloader/intern/versioning_defaults.c index 4c7b011097b..d320b305c06 100644 --- a/source/blender/blenloader/intern/versioning_defaults.c +++ b/source/blender/blenloader/intern/versioning_defaults.c @@ -92,6 +92,9 @@ void BLO_update_defaults_startup_blend(Main *bmain) sculpt->detail_size = 12; } } + + scene->gm.lodflag |= SCE_LOD_USE_HYST; + scene->gm.scehysteresis = 10; } for (linestyle = bmain->linestyle.first; linestyle; linestyle = linestyle->id.next) { diff --git a/source/blender/makesdna/DNA_object_types.h b/source/blender/makesdna/DNA_object_types.h index 2699bb5f22f..4cb979c57b7 100644 --- a/source/blender/makesdna/DNA_object_types.h +++ b/source/blender/makesdna/DNA_object_types.h @@ -109,7 +109,8 @@ typedef struct LodLevel { struct LodLevel *next, *prev; struct Object *source; int flags; - float distance; + float distance, pad; + int obhysteresis; } LodLevel; typedef struct Object { @@ -484,6 +485,7 @@ enum { enum { OB_LOD_USE_MESH = 1 << 0, OB_LOD_USE_MAT = 1 << 1, + OB_LOD_USE_HYST = 1 << 2, }; diff --git a/source/blender/makesdna/DNA_scene_types.h b/source/blender/makesdna/DNA_scene_types.h index 0eae50a2562..ac0803d494c 100644 --- a/source/blender/makesdna/DNA_scene_types.h +++ b/source/blender/makesdna/DNA_scene_types.h @@ -718,7 +718,12 @@ typedef struct GameData { short obstacleSimulation; short raster_storage; float levelHeight; - float deactivationtime, lineardeactthreshold, angulardeactthreshold, pad2; + float deactivationtime, lineardeactthreshold, angulardeactthreshold; + + /* Scene LoD */ + short lodflag, pad2; + int scehysteresis, pad5; + } GameData; #define STEREO_NOSTEREO 1 @@ -791,6 +796,9 @@ enum { #pragma GCC poison GAME_MAT_TEXFACE #endif +/* GameData.lodflag */ +#define SCE_LOD_USE_HYST (1 << 0) + /* UV Paint */ #define UV_SCULPT_LOCK_BORDERS 1 #define UV_SCULPT_ALL_ISLANDS 2 diff --git a/source/blender/makesrna/intern/rna_object.c b/source/blender/makesrna/intern/rna_object.c index d8aa659b68e..a49388866a6 100644 --- a/source/blender/makesrna/intern/rna_object.c +++ b/source/blender/makesrna/intern/rna_object.c @@ -2089,6 +2089,14 @@ static void rna_def_object_lodlevel(BlenderRNA *brna) RNA_def_property_ui_text(prop, "Distance", "Distance to begin using this level of detail"); RNA_def_property_update(prop, NC_OBJECT | ND_LOD, "rna_Object_lod_distance_update"); + prop = RNA_def_property(srna, "object_hysteresis_percentage", PROP_INT, PROP_PERCENTAGE); + RNA_def_property_int_sdna(prop, NULL, "obhysteresis"); + RNA_def_property_range(prop, 0, 100); + RNA_def_property_ui_range(prop, 0, 100, 10, 1); + RNA_def_property_ui_text(prop, "Hysteresis %", "Minimum distance change required to transition to the previous" + " level of detail"); + RNA_def_property_update(prop, NC_OBJECT | ND_LOD, NULL); + prop = RNA_def_property(srna, "object", PROP_POINTER, PROP_NONE); RNA_def_property_pointer_sdna(prop, NULL, "source"); RNA_def_property_struct_type(prop, "Object"); @@ -2107,6 +2115,11 @@ static void rna_def_object_lodlevel(BlenderRNA *brna) RNA_def_property_ui_text(prop, "Use Material", "Use the material from this object at this level of detail"); RNA_def_property_ui_icon(prop, ICON_MATERIAL, 0); RNA_def_property_update(prop, NC_OBJECT | ND_LOD, NULL); + + prop = RNA_def_property(srna, "use_object_hysteresis", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_boolean_sdna(prop, NULL, "flags", OB_LOD_USE_HYST); + RNA_def_property_ui_text(prop, "Hysteresis Override", "Override LoD Hysteresis scene setting for this Lod Level"); + RNA_def_property_update(prop, NC_OBJECT | ND_LOD, NULL); } diff --git a/source/blender/makesrna/intern/rna_scene.c b/source/blender/makesrna/intern/rna_scene.c index 2e941487e98..a0a0eedcfed 100644 --- a/source/blender/makesrna/intern/rna_scene.c +++ b/source/blender/makesrna/intern/rna_scene.c @@ -3886,6 +3886,20 @@ static void rna_def_scene_game_data(BlenderRNA *brna) /* Nestled Data */ rna_def_scene_game_recast_data(brna); + + /* LoD */ + prop = RNA_def_property(srna, "use_scene_hysteresis", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_boolean_sdna(prop, NULL, "lodflag", SCE_LOD_USE_HYST); + RNA_def_property_ui_text(prop, "Hysteresis", "Use LoD Hysteresis setting for the scene"); + RNA_def_property_update(prop, NC_SCENE, NULL); + + prop = RNA_def_property(srna, "scene_hysteresis_percentage", PROP_INT, PROP_PERCENTAGE); + RNA_def_property_int_sdna(prop, NULL, "scehysteresis"); + RNA_def_property_range(prop, 0, 100); + RNA_def_property_ui_range(prop, 0, 100, 10, 1); + RNA_def_property_ui_text(prop, "Hysteresis %", "Minimum distance change required to transition to the previous" + " level of detail"); + RNA_def_property_update(prop, NC_SCENE, NULL); } static void rna_def_gpu_dof_fx(BlenderRNA *brna) diff --git a/source/gameengine/Converter/BL_BlenderDataConversion.cpp b/source/gameengine/Converter/BL_BlenderDataConversion.cpp index cd2e2151034..7b5cc457ee6 100644 --- a/source/gameengine/Converter/BL_BlenderDataConversion.cpp +++ b/source/gameengine/Converter/BL_BlenderDataConversion.cpp @@ -1554,6 +1554,10 @@ static KX_GameObject *gameobject_from_blenderobject( } gameobj->AddLodMesh(BL_ConvertMesh(lodmesh, lodmatob, kxscene, converter, libloading)); } + if (blenderscene->gm.lodflag & SCE_LOD_USE_HYST) { + kxscene->SetLodHysteresis(true); + gameobj->SetLodHysteresisValue(blenderscene->gm.scehysteresis); + } } // for all objects: check whether they want to diff --git a/source/gameengine/Ketsji/KX_GameObject.cpp b/source/gameengine/Ketsji/KX_GameObject.cpp index c8fc64f4679..16dfe5bd9de 100644 --- a/source/gameengine/Ketsji/KX_GameObject.cpp +++ b/source/gameengine/Ketsji/KX_GameObject.cpp @@ -92,6 +92,8 @@ KX_GameObject::KX_GameObject( m_bDyna(false), m_layer(0), m_currentLodLevel(0), + m_previousLodLevel(0), + m_lodHysteresis(0), m_pBlenderObject(NULL), m_pBlenderGroupObject(NULL), m_bSuspendDynamics(false), @@ -784,6 +786,11 @@ void KX_GameObject::AddLodMesh(RAS_MeshObject* mesh) m_lodmeshes.push_back(mesh); } +void KX_GameObject::SetLodHysteresisValue(int hysteresis) +{ + m_lodHysteresis = hysteresis; +} + void KX_GameObject::UpdateLod(MT_Vector3 &cam_pos) { // Handle dupligroups @@ -804,14 +811,47 @@ void KX_GameObject::UpdateLod(MT_Vector3 &cam_pos) int level = 0; Object *bob = this->GetBlenderObject(); LodLevel *lod = (LodLevel*) bob->lodlevels.first; + KX_Scene *sce = this->GetScene(); + for (; lod; lod = lod->next, level++) { if (!lod->source || lod->source->type != OB_MESH) level--; - if (!lod->next || lod->next->distance * lod->next->distance > distance2) break; + if (!lod->next) break; + if (level == (this->m_previousLodLevel) || (level == (this->m_previousLodLevel + 1))) { + short hysteresis = 0; + if (sce->IsActivedLodHysteresis()) { + // if exists, LoD level hysteresis will override scene hysteresis + if (lod->next->flags & OB_LOD_USE_HYST) { + hysteresis = lod->next->obhysteresis; + } + else if (this->m_lodHysteresis != 0) { + hysteresis = m_lodHysteresis; + } + } + float hystvariance = MT_abs(lod->next->distance - lod->distance) * hysteresis / 100; + if ((lod->next->distance + hystvariance) * (lod->next->distance + hystvariance) > distance2) + break; + } + else if (level == (this->m_previousLodLevel - 1)) { + short hysteresis = 0; + if (sce->IsActivedLodHysteresis()) { + // if exists, LoD level hysteresis will override scene hysteresis + if (lod->next->flags & OB_LOD_USE_HYST) { + hysteresis = lod->next->obhysteresis; + } + else if (this->m_lodHysteresis != 0) { + hysteresis = m_lodHysteresis; + } + } + float hystvariance = MT_abs(lod->next->distance - lod->distance) * hysteresis / 100; + if ((lod->next->distance - hystvariance) * (lod->next->distance - hystvariance) > distance2) + break; + } } RAS_MeshObject *mesh = this->m_lodmeshes[level]; this->m_currentLodLevel = level; if (mesh != this->m_meshes[0]) { + this->m_previousLodLevel = level; this->GetScene()->ReplaceMesh(this, mesh, true, false); } } diff --git a/source/gameengine/Ketsji/KX_GameObject.h b/source/gameengine/Ketsji/KX_GameObject.h index 132e72c45e2..d9810b89c90 100644 --- a/source/gameengine/Ketsji/KX_GameObject.h +++ b/source/gameengine/Ketsji/KX_GameObject.h @@ -90,6 +90,8 @@ protected: std::vector m_meshes; std::vector m_lodmeshes; int m_currentLodLevel; + short m_previousLodLevel; + int m_lodHysteresis; SG_QList m_meshSlots; // head of mesh slots of this struct Object* m_pBlenderObject; struct Object* m_pBlenderGroupObject; @@ -804,6 +806,14 @@ public: RAS_MeshObject* mesh ); + /** + * Set lod hysteresis value + */ + void + SetLodHysteresisValue( + int hysteresis + ); + /** * Updates the current lod level based on distance from camera. */ diff --git a/source/gameengine/Ketsji/KX_Scene.cpp b/source/gameengine/Ketsji/KX_Scene.cpp index 47510baa436..d9d07e78c17 100644 --- a/source/gameengine/Ketsji/KX_Scene.cpp +++ b/source/gameengine/Ketsji/KX_Scene.cpp @@ -156,7 +156,8 @@ KX_Scene::KX_Scene(class SCA_IInputDevice* keyboarddevice, m_networkDeviceInterface(ndi), m_active_camera(NULL), m_ueberExecutionPriority(0), - m_blenderScene(scene) + m_blenderScene(scene), + m_isActivedHysteresis(false) { m_suspendedtime = 0.0; m_suspendeddelta = 0.0; @@ -1763,6 +1764,16 @@ void KX_Scene::UpdateObjectLods(void) } } +void KX_Scene::SetLodHysteresis(bool active) +{ + m_isActivedHysteresis = active; +} + +bool KX_Scene::IsActivedLodHysteresis(void) +{ + return m_isActivedHysteresis; +} + void KX_Scene::UpdateObjectActivity(void) { if (m_activity_culling) { diff --git a/source/gameengine/Ketsji/KX_Scene.h b/source/gameengine/Ketsji/KX_Scene.h index 2e1ee9f101d..19873daabb3 100644 --- a/source/gameengine/Ketsji/KX_Scene.h +++ b/source/gameengine/Ketsji/KX_Scene.h @@ -295,6 +295,11 @@ protected: KX_ObstacleSimulation* m_obstacleSimulation; + /** + * Does this scene active the LoD Hysteresis? + */ + bool m_isActivedHysteresis; + public: KX_Scene(class SCA_IInputDevice* keyboarddevice, class SCA_IInputDevice* mousedevice, @@ -546,6 +551,10 @@ public: // Update the mesh for objects based on level of detail settings void UpdateObjectLods(void); + + // Enable/disable LoD Hysteresis + void SetLodHysteresis(bool active); + bool IsActivedLodHysteresis(); // Update the activity box settings for objects in this scene, if needed. void UpdateObjectActivity(void); -- cgit v1.2.3