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/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 +++++++ 4 files changed, 72 insertions(+), 2 deletions(-) (limited to 'source/gameengine/Ketsji') 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