| 14 | #include "Engine/Core/Log.h" |
| 15 | |
| 16 | bool CollisionCooking::CookCollision(const Argument& arg, CollisionData::SerializedOptions& outputOptions, BytesContainer& outputData) |
| 17 | { |
| 18 | PROFILE_CPU(); |
| 19 | PROFILE_MEM(Physics); |
| 20 | int32 convexVertexLimit = Math::Clamp(arg.ConvexVertexLimit, CONVEX_VERTEX_MIN, CONVEX_VERTEX_MAX); |
| 21 | if (arg.ConvexVertexLimit == 0) |
| 22 | convexVertexLimit = CONVEX_VERTEX_MAX; |
| 23 | |
| 24 | DataContainer<Float3> finalVertexData; |
| 25 | DataContainer<uint32> finalIndexData; |
| 26 | const bool needIndexBuffer = arg.Type == CollisionDataType::TriangleMesh; |
| 27 | |
| 28 | // Check if use custom model (specified in the argument, used for fast internal collision cooking by e.g. CSGBuilder) |
| 29 | if (arg.OverrideModelData) |
| 30 | { |
| 31 | // Validate model data |
| 32 | if (arg.OverrideModelData->LODs.IsEmpty()) |
| 33 | { |
| 34 | LOG(Warning, "Missing model data."); |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | // Pick a proper model LOD |
| 39 | const int32 lodIndex = Math::Clamp(arg.ModelLodIndex, 0, arg.OverrideModelData->LODs.Count()); |
| 40 | auto lod = &arg.OverrideModelData->LODs[lodIndex]; |
| 41 | const int32 meshesCount = lod->Meshes.Count(); |
| 42 | |
| 43 | // Count vertex/index buffer sizes |
| 44 | int32 vCount = 0; |
| 45 | int32 iCount = 0; |
| 46 | for (int32 i = 0; i < meshesCount; i++) |
| 47 | { |
| 48 | const auto mesh = lod->Meshes[i]; |
| 49 | if ((arg.MaterialSlotsMask & (1 << mesh->MaterialSlotIndex)) == 0) |
| 50 | continue; |
| 51 | vCount += mesh->Positions.Count(); |
| 52 | if (needIndexBuffer) |
| 53 | iCount += mesh->Indices.Count() * 3; |
| 54 | } |
| 55 | |
| 56 | if (meshesCount == 1 && vCount != 0) |
| 57 | { |
| 58 | // Link a single mesh |
| 59 | const auto mesh = lod->Meshes[0]; |
| 60 | finalVertexData.Link(mesh->Positions); |
| 61 | if (needIndexBuffer) |
| 62 | { |
| 63 | finalIndexData.Link(mesh->Indices); |
| 64 | } |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | // Combine meshes into one |
| 69 | finalVertexData.Allocate(vCount); |
| 70 | finalIndexData.Allocate(iCount); |
| 71 | int32 vertexCounter = 0, indexCounter = 0; |
| 72 | for (int32 i = 0; i < meshesCount; i++) |
| 73 | { |
nothing calls this directly
no test coverage detected