@@ -1,19 +1,14 @@
|
||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||
|
||||
#include "CollisionData.h"
|
||||
#include "Engine/Core/Log.h"
|
||||
#include "Engine/Content/Content.h"
|
||||
#include "Engine/Content/Assets/Model.h"
|
||||
#include "Engine/Content/Factories/BinaryAssetFactory.h"
|
||||
#include "Engine/Physics/Physics.h"
|
||||
#include "Engine/Physics/PhysicsScene.h"
|
||||
#include "Engine/Physics/Utilities.h"
|
||||
#include "Engine/Physics/CollisionData.h"
|
||||
#include "Engine/Physics/PhysicsBackend.h"
|
||||
#include "Engine/Physics/CollisionCooking.h"
|
||||
#include "Engine/Threading/Threading.h"
|
||||
#include <ThirdParty/PhysX/extensions/PxDefaultStreams.h>
|
||||
#include <ThirdParty/PhysX/geometry/PxTriangleMesh.h>
|
||||
#include <ThirdParty/PhysX/geometry/PxConvexMesh.h>
|
||||
#include <ThirdParty/PhysX/PxPhysics.h>
|
||||
|
||||
REGISTER_BINARY_ASSET(CollisionData, "FlaxEngine.CollisionData", true);
|
||||
|
||||
@@ -141,9 +136,11 @@ bool CollisionData::GetModelTriangle(uint32 faceIndex, MeshBase*& mesh, uint32&
|
||||
if (!IsLoaded())
|
||||
return false;
|
||||
ScopeLock lock(Locker);
|
||||
if (_triangleMesh && faceIndex < _triangleMesh->getNbTriangles())
|
||||
if (_triangleMesh)
|
||||
{
|
||||
if (const PxU32* remap = _triangleMesh->getTrianglesRemap())
|
||||
uint32 trianglesCount;
|
||||
const uint32* remap = PhysicsBackend::GetTriangleMeshRemap(_triangleMesh, trianglesCount);
|
||||
if (remap && faceIndex < trianglesCount)
|
||||
{
|
||||
// Get source triangle index from the triangle mesh
|
||||
meshTriangleIndex = remap[faceIndex];
|
||||
@@ -187,98 +184,10 @@ void CollisionData::ExtractGeometry(Array<Vector3>& vertexBuffer, Array<int32>&
|
||||
indexBuffer.Clear();
|
||||
|
||||
ScopeLock lock(Locker);
|
||||
|
||||
uint32 numVertices = 0;
|
||||
uint32 numIndices = 0;
|
||||
|
||||
// Convex Mesh
|
||||
if (_convexMesh)
|
||||
{
|
||||
numVertices = _convexMesh->getNbVertices();
|
||||
|
||||
const uint32 numPolygons = _convexMesh->getNbPolygons();
|
||||
for (uint32 i = 0; i < numPolygons; i++)
|
||||
{
|
||||
PxHullPolygon face;
|
||||
const bool status = _convexMesh->getPolygonData(i, face);
|
||||
ASSERT(status);
|
||||
|
||||
numIndices += (face.mNbVerts - 2) * 3;
|
||||
}
|
||||
}
|
||||
// Triangle Mesh
|
||||
PhysicsBackend::GetConvexMeshTriangles(_convexMesh, vertexBuffer, indexBuffer);
|
||||
else if (_triangleMesh)
|
||||
{
|
||||
numVertices = _triangleMesh->getNbVertices();
|
||||
numIndices = _triangleMesh->getNbTriangles() * 3;
|
||||
}
|
||||
// No collision data
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Prepare vertex and index buffers
|
||||
vertexBuffer.Resize(numVertices);
|
||||
indexBuffer.Resize(numIndices);
|
||||
auto outVertices = vertexBuffer.Get();
|
||||
auto outIndices = indexBuffer.Get();
|
||||
|
||||
if (_convexMesh)
|
||||
{
|
||||
const PxVec3* convexVertices = _convexMesh->getVertices();
|
||||
const byte* convexIndices = _convexMesh->getIndexBuffer();
|
||||
|
||||
for (uint32 i = 0; i < numVertices; i++)
|
||||
*outVertices++ = P2C(convexVertices[i]);
|
||||
|
||||
uint32 numPolygons = _convexMesh->getNbPolygons();
|
||||
for (uint32 i = 0; i < numPolygons; i++)
|
||||
{
|
||||
PxHullPolygon face;
|
||||
bool status = _convexMesh->getPolygonData(i, face);
|
||||
ASSERT(status);
|
||||
|
||||
const PxU8* faceIndices = convexIndices + face.mIndexBase;
|
||||
for (uint32 j = 2; j < face.mNbVerts; j++)
|
||||
{
|
||||
*outIndices++ = faceIndices[0];
|
||||
*outIndices++ = faceIndices[j];
|
||||
*outIndices++ = faceIndices[j - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const PxVec3* vertices = _triangleMesh->getVertices();
|
||||
for (uint32 i = 0; i < numVertices; i++)
|
||||
*outVertices++ = P2C(vertices[i]);
|
||||
|
||||
if (_triangleMesh->getTriangleMeshFlags() & PxTriangleMeshFlag::e16_BIT_INDICES)
|
||||
{
|
||||
const uint16* indices = (const uint16*)_triangleMesh->getTriangles();
|
||||
|
||||
uint32 numTriangles = numIndices / 3;
|
||||
for (uint32 i = 0; i < numTriangles; i++)
|
||||
{
|
||||
outIndices[i * 3 + 0] = (uint32)indices[i * 3 + 0];
|
||||
outIndices[i * 3 + 1] = (uint32)indices[i * 3 + 1];
|
||||
outIndices[i * 3 + 2] = (uint32)indices[i * 3 + 2];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const uint32* indices = (const uint32*)_triangleMesh->getTriangles();
|
||||
|
||||
uint32 numTriangles = numIndices / 3;
|
||||
for (uint32 i = 0; i < numTriangles; i++)
|
||||
{
|
||||
outIndices[i * 3 + 0] = indices[i * 3 + 0];
|
||||
outIndices[i * 3 + 1] = indices[i * 3 + 1];
|
||||
outIndices[i * 3 + 2] = indices[i * 3 + 2];
|
||||
}
|
||||
}
|
||||
}
|
||||
PhysicsBackend::GetTriangleMeshTriangles(_triangleMesh, vertexBuffer, indexBuffer);
|
||||
}
|
||||
|
||||
#if USE_EDITOR
|
||||
@@ -360,16 +269,13 @@ CollisionData::LoadResult CollisionData::load(const SerializedOptions* options,
|
||||
return LoadResult::InvalidData;
|
||||
|
||||
// Create PhysX object
|
||||
PxDefaultMemoryInputData input(dataPtr, dataSize);
|
||||
if (_options.Type == CollisionDataType::ConvexMesh)
|
||||
{
|
||||
_convexMesh = Physics::GetPhysics()->createConvexMesh(input);
|
||||
_options.Box = P2C(_convexMesh->getLocalBounds());
|
||||
_convexMesh = PhysicsBackend::CreateConvexMesh(dataPtr, dataSize, _options.Box);
|
||||
}
|
||||
else if (_options.Type == CollisionDataType::TriangleMesh)
|
||||
{
|
||||
_triangleMesh = Physics::GetPhysics()->createTriangleMesh(input);
|
||||
_options.Box = P2C(_triangleMesh->getLocalBounds());
|
||||
_triangleMesh = PhysicsBackend::CreateTriangleMesh(dataPtr, dataSize, _options.Box);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -385,16 +291,12 @@ void CollisionData::unload(bool isReloading)
|
||||
{
|
||||
if (_convexMesh)
|
||||
{
|
||||
for (auto scene : Physics::Scenes)
|
||||
scene->RemoveObject(_convexMesh);
|
||||
|
||||
PhysicsBackend::DestroyObject(_convexMesh);
|
||||
_convexMesh = nullptr;
|
||||
}
|
||||
if (_triangleMesh)
|
||||
{
|
||||
for (auto scene : Physics::Scenes)
|
||||
scene->RemoveObject(_triangleMesh);
|
||||
|
||||
PhysicsBackend::DestroyObject(_triangleMesh);
|
||||
_triangleMesh = nullptr;
|
||||
}
|
||||
_options = CollisionDataOptions();
|
||||
|
||||
Reference in New Issue
Block a user