1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-20 15:40:32 +00:00

Merge branch 'navmesh_optimize_get_recast_mesh' into 'master'

Reduce waiting while creating RecastMesh

See merge request OpenMW/openmw!1097
This commit is contained in:
psi29a 2021-08-07 20:05:01 +00:00
commit e89b304fed
3 changed files with 30 additions and 17 deletions

View File

@ -123,7 +123,13 @@ namespace DetourNavigator
tileBounds.mMin /= mSettings.mRecastScaleFactor;
tileBounds.mMax /= mSettings.mRecastScaleFactor;
RecastMeshBuilder builder(tileBounds);
std::vector<RecastMeshObject> objects;
using Object = std::tuple<
osg::ref_ptr<const osg::Referenced>,
std::reference_wrapper<const btCollisionShape>,
btTransform,
AreaType
>;
std::vector<Object> objects;
std::size_t revision;
{
const std::lock_guard lock(mMutex);
@ -133,11 +139,14 @@ namespace DetourNavigator
std::visit(AddHeightfield {v.mCell, builder}, v.mShape);
objects.reserve(mObjects.size());
for (const auto& [k, object] : mObjects)
objects.push_back(object.getImpl());
{
const RecastMeshObject& impl = object.getImpl();
objects.emplace_back(impl.getHolder(), impl.getShape(), impl.getTransform(), impl.getAreaType());
}
revision = mRevision;
}
for (const auto& v : objects)
builder.addObject(v.getShape(), v.getTransform(), v.getAreaType());
for (const auto& [holder, shape, transform, areaType] : objects)
builder.addObject(shape, transform, areaType);
return std::move(builder).create(mGeneration, revision);
}

View File

@ -23,35 +23,35 @@ namespace DetourNavigator
return result;
}
std::vector<RecastMeshObject> makeChildrenObjects(const osg::ref_ptr<const Resource::BulletShapeInstance>& instance,
std::vector<RecastMeshObject> makeChildrenObjects(const osg::ref_ptr<const osg::Referenced>& holder,
const btCompoundShape& shape, const AreaType areaType)
{
std::vector<RecastMeshObject> result;
for (int i = 0, num = shape.getNumChildShapes(); i < num; ++i)
{
const CollisionShape collisionShape {instance, *shape.getChildShape(i)};
const CollisionShape collisionShape {holder, *shape.getChildShape(i)};
result.emplace_back(collisionShape, shape.getChildTransform(i), areaType);
}
return result;
}
std::vector<RecastMeshObject> makeChildrenObjects(const osg::ref_ptr<const Resource::BulletShapeInstance>& instance,
std::vector<RecastMeshObject> makeChildrenObjects(const osg::ref_ptr<const osg::Referenced>& holder,
const btCollisionShape& shape, const AreaType areaType)
{
if (shape.isCompound())
return makeChildrenObjects(std::move(instance), static_cast<const btCompoundShape&>(shape), areaType);
return makeChildrenObjects(holder, static_cast<const btCompoundShape&>(shape), areaType);
return std::vector<RecastMeshObject>();
}
}
RecastMeshObject::RecastMeshObject(const CollisionShape& shape, const btTransform& transform,
const AreaType areaType)
: mShapeInstance(shape.getShapeInstance())
: mHolder(shape.getHolder())
, mShape(shape.getShape())
, mTransform(transform)
, mAreaType(areaType)
, mLocalScaling(mShape.get().getLocalScaling())
, mChildren(makeChildrenObjects(mShapeInstance, mShape.get(), mAreaType))
, mChildren(makeChildrenObjects(mHolder, mShape.get(), mAreaType))
{
}

View File

@ -3,11 +3,10 @@
#include "areatype.hpp"
#include <components/resource/bulletshape.hpp>
#include <LinearMath/btTransform.h>
#include <osg/ref_ptr>
#include <osg/Referenced>
#include <functional>
#include <vector>
@ -20,16 +19,16 @@ namespace DetourNavigator
class CollisionShape
{
public:
CollisionShape(osg::ref_ptr<const Resource::BulletShapeInstance> instance, const btCollisionShape& shape)
: mShapeInstance(std::move(instance))
CollisionShape(osg::ref_ptr<const osg::Referenced> holder, const btCollisionShape& shape)
: mHolder(std::move(holder))
, mShape(shape)
{}
const osg::ref_ptr<const Resource::BulletShapeInstance>& getShapeInstance() const { return mShapeInstance; }
const osg::ref_ptr<const osg::Referenced>& getHolder() const { return mHolder; }
const btCollisionShape& getShape() const { return mShape; }
private:
osg::ref_ptr<const Resource::BulletShapeInstance> mShapeInstance;
osg::ref_ptr<const osg::Referenced> mHolder;
std::reference_wrapper<const btCollisionShape> mShape;
};
@ -40,6 +39,11 @@ namespace DetourNavigator
bool update(const btTransform& transform, const AreaType areaType);
const osg::ref_ptr<const osg::Referenced>& getHolder() const
{
return mHolder;
}
const btCollisionShape& getShape() const
{
return mShape;
@ -56,7 +60,7 @@ namespace DetourNavigator
}
private:
osg::ref_ptr<const Resource::BulletShapeInstance> mShapeInstance;
osg::ref_ptr<const osg::Referenced> mHolder;
std::reference_wrapper<const btCollisionShape> mShape;
btTransform mTransform;
AreaType mAreaType;