载入中...
搜索中...
未找到
JoltMesh.cpp
浏览该文件的文档.
1// eve-hot-path
2// Jolt requires its platform header before any other native header.
3#include <Jolt/Jolt.h>
4
5// Provider implementation dependencies.
6#include <Jolt/Geometry/Indexify.h>
7#include <Jolt/Physics/Collision/Shape/MeshShape.h>
9namespace eve::physics::jolt {
12 JPH::MeshShapeSettings settings;
13 settings.mUserData = userData;
14 settings.mPerTriangleUserData = true;
15 if (g.kind == ShapeKind3D::TriangleMesh) {
16 if (g.meshVertices.size() < 9 || g.meshVertices.size() % 3 || g.meshIndices.empty() ||
17 g.meshIndices.size() % 3 || g.meshVertices.size() / 3 > 1000000 || g.meshIndices.size() / 3 > 2000000 ||
18 !std::isfinite(g.meshWeldTolerance) || g.meshWeldTolerance < 0 ||
19 (!g.meshMaterialIndices.empty() && g.meshMaterialIndices.size() != g.meshIndices.size() / 3))
20 return rejected<JPH::RefConst<JPH::Shape>>("Invalid mesh source", eve::DiagnosticCode::InvalidArgument);
21 for (size_t i = 0; i < g.meshVertices.size(); i += 3) {
22 if (!isFinite(g.meshVertices[i], g.meshVertices[i + 1], g.meshVertices[i + 2]))
23 return rejected<JPH::RefConst<JPH::Shape>>("Non-finite vertex", eve::DiagnosticCode::InvalidArgument);
24 settings.mTriangleVertices.emplace_back(g.meshVertices[i], g.meshVertices[i + 1], g.meshVertices[i + 2]);
25 }
26 for (size_t i = 0; i < g.meshIndices.size(); i += 3) {
27 for (size_t j = 0; j < 3; ++j)
28 if (g.meshIndices[i + j] < 0 || size_t(g.meshIndices[i + j]) >= settings.mTriangleVertices.size())
29 return rejected<JPH::RefConst<JPH::Shape>>("Invalid triangle index",
31 JPH::IndexedTriangle triangle(g.meshIndices[i], g.meshIndices[i + 1], g.meshIndices[i + 2], 0,
32 static_cast<JPH::uint32>(i / 3));
33 if (triangle.IsDegenerate(settings.mTriangleVertices))
34 return rejected<JPH::RefConst<JPH::Shape>>("Degenerate triangle", eve::DiagnosticCode::InvalidArgument);
35 settings.mIndexedTriangles.push_back(triangle);
36 }
37 if (g.meshWeldVertices) {
38 JPH::TriangleList triangles;
39 for (const auto& t : settings.mIndexedTriangles)
40 triangles.emplace_back(settings.mTriangleVertices[t.mIdx[0]], settings.mTriangleVertices[t.mIdx[1]],
41 settings.mTriangleVertices[t.mIdx[2]], 0, t.mUserData);
42 settings.mTriangleVertices.clear();
43 settings.mIndexedTriangles.clear();
44 JPH::Indexify(triangles, settings.mTriangleVertices, settings.mIndexedTriangles, g.meshWeldTolerance);
45 if (settings.mIndexedTriangles.size() != triangles.size())
46 return rejected<JPH::RefConst<JPH::Shape>>("Welding removed degenerate triangles",
48 for (const auto& t : settings.mIndexedTriangles)
49 if (t.IsDegenerate(settings.mTriangleVertices))
50 return rejected<JPH::RefConst<JPH::Shape>>("Welding collapsed a triangle",
52 }
53 settings.mActiveEdgeCosThresholdAngle = g.meshIdentifyEdges ? 0.996195f : -1.f;
54 settings.mBuildQuality = g.meshUseMedianSplit ? JPH::MeshShapeSettings::EBuildQuality::FavorBuildSpeed
55 : JPH::MeshShapeSettings::EBuildQuality::FavorRuntimePerformance;
56 } else {
57 const size_t count = size_t(g.heightCountX) * size_t(g.heightCountZ);
58 if (g.heightCountX < 2 || g.heightCountZ < 2 || count > 16000000 || g.heightValues.size() != count ||
59 !isFinite(g.heightCellSizeX, g.heightCellSizeZ, g.heightGlobalMin) || !std::isfinite(g.heightGlobalMax) ||
60 g.heightCellSizeX <= 0 || g.heightCellSizeZ <= 0 || g.heightGlobalMin > g.heightGlobalMax)
61 return rejected<JPH::RefConst<JPH::Shape>>("Invalid height field", eve::DiagnosticCode::InvalidArgument);
62 // Preserve rectangular grids, the fixed cell diagonal and winding of the portable contract.
63 // Jolt HeightFieldShape requires a square grid; a native mesh preserves all accepted source grids.
64 for (int z = 0; z < g.heightCountZ; ++z)
65 for (int x = 0; x < g.heightCountX; ++x) {
66 const float h = g.heightValues[size_t(z) * g.heightCountX + x];
67 const float px = x * g.heightCellSizeX, pz = z * g.heightCellSizeZ;
68 if (!isFinite(px, h, pz) || h < g.heightGlobalMin || h > g.heightGlobalMax)
69 return rejected<JPH::RefConst<JPH::Shape>>("Invalid height sample",
71 settings.mTriangleVertices.emplace_back(px, h, pz);
72 }
73 for (int z = 0; z < g.heightCountZ - 1; ++z)
74 for (int x = 0; x < g.heightCountX - 1; ++x) {
75 const auto a = static_cast<JPH::uint32>(z * g.heightCountX + x), b = a + 1, c = a + g.heightCountX,
76 d = c + 1;
77 auto index = static_cast<JPH::uint32>(settings.mIndexedTriangles.size());
78 settings.mIndexedTriangles.emplace_back(a, g.heightClockwise ? b : c, g.heightClockwise ? c : b, 0,
79 index);
80 settings.mIndexedTriangles.emplace_back(d, g.heightClockwise ? c : b, g.heightClockwise ? b : c, 0,
81 index + 1);
82 }
83 }
84 auto result = settings.Create();
85 if (result.HasError())
86 return rejected<JPH::RefConst<JPH::Shape>>(result.GetError().c_str(), eve::DiagnosticCode::InvalidArgument);
87 return R::success(result.Get());
88}
89int triangleIndex(const JPH::Body& body, const JPH::SubShapeID& id) {
90 JPH::SubShapeID remainder;
91 const auto* leaf = body.GetShape()->GetLeafShape(id, remainder);
92 return leaf && leaf->GetSubType() == JPH::EShapeSubType::Mesh
93 ? static_cast<int>(static_cast<const JPH::MeshShape*>(leaf)->GetTriangleUserData(remainder))
94 : -1;
95}
96SurfaceMaterial3D contactMaterial(const JPH::Body& body, const JPH::SubShapeID& id, const ShapeState& shape) {
97 const int index = triangleIndex(body, id);
98 if (index >= 0 && !shape.meshMaterials.empty()) {
99 const auto& indices = shape.geometry.meshMaterialIndices;
100 const size_t slot = indices.empty() ? 0 : indices.at(size_t(index));
101 return shape.meshMaterials.at(slot);
102 }
103 return shape.material;
104}
105} // namespace eve::physics::jolt
double z
double x
int triangle
Definition FluidSdf.cpp:554
tensor::Graph g
Definition GpuGraph.cpp:7
ShaderImageInput shape
std::vector< std::uint32_t > indices
std::int32_t c
int h
std::vector< Colorf > px
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::vector< TriangleRef > triangles
OceanWakeSettings settings
std::string body
float d
float t
std::string slot
int count
uint32_t index
Move-only operation result carrying either a value or Status.
Definition Result.h:176
int triangleIndex(const JPH::Body &, const JPH::SubShapeID &)
Definition JoltMesh.cpp:89
eve::Result< T > rejected(const char *operation, eve::DiagnosticCode code=eve::DiagnosticCode::Unsupported)
eve::Result< JPH::RefConst< JPH::Shape > > cookMesh(const ShapeGeometry3D &, JPH::uint64 userData)
Definition JoltMesh.cpp:10
SurfaceMaterial3D contactMaterial(const JPH::Body &, const JPH::SubShapeID &, const ShapeState &)
Definition JoltMesh.cpp:96
bool isFinite(float x, float y, float z)
Owning, backend-independent shape source; metres, local quaternion XYZW.
Owning surface values; userMaterialId contains the engine material ID and combine policy encoding.