support runtime set terrain and mesh (#3767)

This commit is contained in:
jiaxin.lai
2021-07-26 15:30:43 +08:00
committed by GitHub
parent 01c576e735
commit e6009c4a3a
6 changed files with 69 additions and 22 deletions
@@ -37,28 +37,29 @@ void PhysXShape::initialize(scene::Node *node) {
_mSharedBody = ins.getSharedBody(node);
getSharedBody().reference(true);
onComponentSet();
getPxShapeMap().insert(std::pair<uintptr_t, uintptr_t>(reinterpret_cast<uintptr_t>(&getShape()), getImpl()));
insertToShapeMap();
}
void PhysXShape::onEnable() {
_mEnabled = true;
getSharedBody().addShape(*this);
if (_mShape) getSharedBody().addShape(*this);
getSharedBody().enabled(true);
}
void PhysXShape::onDisable() {
_mEnabled = false;
getSharedBody().removeShape(*this);
if (_mShape) getSharedBody().removeShape(*this);
getSharedBody().enabled(false);
}
void PhysXShape::onDestroy() {
getSharedBody().reference(false);
getPxShapeMap().erase(reinterpret_cast<uintptr_t>(&getShape()));
eraseFromShapeMap();
}
void PhysXShape::setMaterial(uint16_t id, float f, float df, float r,
uint8_t m0, uint8_t m1) {
if (!_mShape) return;
auto *mat = reinterpret_cast<physx::PxMaterial *>(getSharedBody().getWorld().createMaterial(id, f, df, r, m0, m1));
getShape().setMaterials(&mat, 1);
}
@@ -71,7 +72,7 @@ void PhysXShape::setAsTrigger(bool v) {
getShape().setFlag(physx::PxShapeFlag::eTRIGGER_SHAPE, v);
getShape().setFlag(physx::PxShapeFlag::eSIMULATION_SHAPE, !v);
}
if (_mIndex >= 0) {
if (_mEnabled) {
getSharedBody().removeShape(*this);
getSharedBody().addShape(*this);
}
@@ -103,15 +104,17 @@ void PhysXShape::updateEventListener(EShapeFilterFlag flag) {
scene::AABB &PhysXShape::getAABB() {
static scene::AABB aabb;
auto bounds = physx::PxShapeExt::getWorldBounds(getShape(), *getSharedBody().getImpl().rigidActor);
pxSetVec3Ext(aabb.getLayout()->center, (bounds.maximum + bounds.minimum) / 2);
pxSetVec3Ext(aabb.getLayout()->halfExtents, (bounds.maximum - bounds.minimum) / 2);
if (_mShape) {
auto bounds = physx::PxShapeExt::getWorldBounds(getShape(), *getSharedBody().getImpl().rigidActor);
pxSetVec3Ext(aabb.getLayout()->center, (bounds.maximum + bounds.minimum) / 2);
pxSetVec3Ext(aabb.getLayout()->halfExtents, (bounds.maximum - bounds.minimum) / 2);
}
return aabb;
}
scene::Sphere &PhysXShape::getBoundingSphere() {
static scene::Sphere sphere;
sphere.define(getAABB());
if (_mShape) sphere.define(getAABB());
return sphere;
}
@@ -119,13 +122,26 @@ void PhysXShape::updateFilterData(const physx::PxFilterData &data) {
}
void PhysXShape::updateCenter() {
auto & sb = getSharedBody();
auto * node = sb.getNode();
if (!_mShape) return;
auto &sb = getSharedBody();
auto *node = sb.getNode();
node->updateWorldTransform();
physx::PxTransform local{_mCenter * node->getWorldScale(), _mRotation};
getShape().setLocalPose(local);
if (_mEnabled && !isTrigger()) sb.updateCenterOfMass();
}
void PhysXShape::insertToShapeMap() {
if (_mShape) {
getPxShapeMap().insert(std::pair<uintptr_t, uintptr_t>(reinterpret_cast<uintptr_t>(&getShape()), getImpl()));
}
}
void PhysXShape::eraseFromShapeMap() {
if (_mShape) {
getPxShapeMap().erase(reinterpret_cast<uintptr_t>(&getShape()));
}
}
} // namespace physics
} // namespace cc
@@ -77,11 +77,12 @@ protected:
physx::PxShape * _mShape{nullptr};
physx::PxVec3 _mCenter;
physx::PxQuat _mRotation;
int8_t _mIndex{-1};
uint8_t _mFlag{0};
bool _mEnabled{false};
virtual void updateCenter();
virtual void onComponentSet() = 0;
virtual void insertToShapeMap();
virtual void eraseFromShapeMap();
};
} // namespace physics
@@ -34,15 +34,22 @@ namespace physics {
PhysXTerrain::PhysXTerrain() : _mTerrain(nullptr),
_mRowScale(1.F),
_mColScale(1.F),
_mHeightScale(1.F){};
_mHeightScale(1.F),
_mIsTrigger(false){};
void PhysXTerrain::setTerrain(uintptr_t handle, float rs, float cs, float hs) {
if (_mShape) return;
if (reinterpret_cast<uintptr_t>(_mTerrain) == handle) return;
_mTerrain = reinterpret_cast<physx::PxHeightField *>(handle);
_mRowScale = rs;
_mColScale = cs;
_mHeightScale = hs;
if (_mShape) {
if (_mSharedBody && _mTerrain) {
onComponentSet();
insertToShapeMap();
if (_mEnabled) getSharedBody().addShape(*this);
setAsTrigger(_mIsTrigger);
updateCenter();
}
}
@@ -62,8 +69,16 @@ void PhysXTerrain::updateScale() {
}
void PhysXTerrain::updateCenter() {
if (!_mShape) return;
getShape().setLocalPose(physx::PxTransform{_mCenter, _mRotation});
}
void PhysXTerrain::setAsTrigger(bool v) {
_mIsTrigger = v;
if (_mShape) {
PhysXShape::setAsTrigger(v);
}
}
} // namespace physics
} // namespace cc
@@ -37,13 +37,15 @@ public:
void setTerrain(uintptr_t handle, float rs, float cs, float hs) override;
void updateScale() override;
void updateCenter() override;
void setAsTrigger(bool v) override;
private:
physx::PxHeightField *_mTerrain;
float _mRowScale;
float _mColScale;
float _mHeightScale;
void onComponentSet() override;
bool _mIsTrigger;
void onComponentSet() override;
};
} // namespace physics
@@ -32,20 +32,24 @@ namespace cc {
namespace physics {
PhysXTrimesh::PhysXTrimesh() : _mMeshHandle(0),
_mConvex(false){};
_mConvex(false),
_mIsTrigger(false){};
void PhysXTrimesh::setMesh(uintptr_t handle) {
if (_mShape) return;
if (_mMeshHandle == handle) return;
_mMeshHandle = handle;
if (_mShape) {
if (_mSharedBody && _mMeshHandle) {
onComponentSet();
insertToShapeMap();
if (_mEnabled) getSharedBody().addShape(*this);
setAsTrigger(_mIsTrigger);
updateCenter();
}
}
void PhysXTrimesh::useConvex(bool v) {
if (_mConvex == v) return;
_mConvex = v;
if (_mShape) {
}
}
void PhysXTrimesh::onComponentSet() {
@@ -74,7 +78,7 @@ void PhysXTrimesh::updateScale() {
void PhysXTrimesh::updateGeometry() {
static physx::PxMeshScale scale;
scale.rotation = physx::PxQuat{physx::PxIdentity};
auto *node = getSharedBody().getNode();
auto *node = getSharedBody().getNode();
node->updateWorldTransform();
pxSetVec3Ext(scale.scale, node->getWorldScale());
const auto &type = _mShape->getGeometryType();
@@ -93,5 +97,12 @@ void PhysXTrimesh::updateGeometry() {
}
}
void PhysXTrimesh::setAsTrigger(bool v) {
_mIsTrigger = v;
if (_mShape) {
PhysXShape::setAsTrigger(v);
}
}
} // namespace physics
} // namespace cc
@@ -37,12 +37,14 @@ public:
void setMesh(uintptr_t handle) override;
void useConvex(bool v) override;
void updateScale() override;
void setAsTrigger(bool v) override;
private:
bool _mConvex;
uintptr_t _mMeshHandle;
bool _mIsTrigger;
void updateGeometry();
void onComponentSet() override;
void onComponentSet() override;
};
} // namespace physics