You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C++中区分Unreal Geometry Collection的真实粒子与集群

如何在Unreal Engine的Geometry Collection中区分真实粒子与集群?

我在C++中实现了自定义Geometry Collection组件,还基于它制作了对应蓝图。测试用的Geometry Collection在编辑器里包含4个集群(含根节点)和9个真实粒子,但自定义组件的日志却检测到了13个粒子。

相关检测代码

void ACustomGCActor::BeginPlay()
{
    Super::BeginPlay();

    const FGeometryCollectionPhysicsProxy* PhysicsProxy = GeometryCollectionComponent->GetPhysicsProxy();

    for (int32 i = 0; i < GeometryCollectionComponent->GetDynamicCollection()->GetNumTransforms(); ++i)
    {
        if (const Chaos::TPBDRigidParticle<Chaos::FReal, 3>* Particle = PhysicsProxy->GetParticleByIndex_External(i))
        {
            UE_LOG(LogTemp, Warning, TEXT("%s[%d] M: %f"), *GetName(), i, Particle->M());
        }
    }
}

粒子质量日志输出

GeometryCollectionActor_1[0] M: 100000.000000
GeometryCollectionActor_1[1] M: 53194.617188
GeometryCollectionActor_1[2] M: 30795.964844
GeometryCollectionActor_1[3] M: 16009.417969
GeometryCollectionActor_1[4] M: 17966.726562
GeometryCollectionActor_1[5] M: 33580.132812
GeometryCollectionActor_1[6] M: 1647.759644
GeometryCollectionActor_1[7] M: 9634.439453
GeometryCollectionActor_1[8] M: 7064.375000
GeometryCollectionActor_1[9] M: 14097.150391
GeometryCollectionActor_1[10] M: 1927.681030
GeometryCollectionActor_1[11] M: 14055.364258
GeometryCollectionActor_1[12] M: 26.372398

从日志能看出:

  • 索引0的粒子质量为整体总质量,对应编辑器里的根集群节点SM_ChamferCube
  • 索引1、2、3的粒子质量之和等于总质量,对应根节点下的三个子集群SM_ChamferCube_0、SM_ChamferCube_1、SM_ChamferCube_2

现在需要准确区分哪些是真实粒子,哪些是集群节点。


解决方法

要区分集群节点和真实粒子,可通过Geometry Collection的层级结构、粒子集群ID来实现:

  1. 通过集群ID与层级关系判断
    利用Chaos::GeometryCollection的集群数据,结合父子节点关系,直接识别集群根节点和真实粒子:

    void ACustomGCActor::BeginPlay()
    {
        Super::BeginPlay();
    
        const FGeometryCollection* StaticCollection = GeometryCollectionComponent->GetGeometryCollection();
        const FGeometryCollectionPhysicsProxy* PhysicsProxy = GeometryCollectionComponent->GetPhysicsProxy();
        const Chaos::GeometryCollection* ChaosCollection = StaticCollection->GetChaosCollection();
    
        for (int32 i = 0; i < GeometryCollectionComponent->GetDynamicCollection()->GetNumTransforms(); ++i)
        {
            if (const Chaos::TPBDRigidParticle<Chaos::FReal, 3>* Particle = PhysicsProxy->GetParticleByIndex_External(i))
            {
                // 获取当前粒子所属的集群ID
                const int32 ClusterID = ChaosCollection->GetParticleCluster(i);
                // 集群根节点的ID等于自身索引
                bool bIsClusterRoot = (ClusterID == i);
                // 真实粒子没有子节点
                bool bIsIndividualParticle = ChaosCollection->GetChildren(i).Num() == 0;
    
                const FString ParticleType = bIsClusterRoot ? TEXT("集群节点") : (bIsIndividualParticle ? TEXT("真实粒子") : TEXT("集群子粒子"));
                UE_LOG(LogTemp, Warning, TEXT("%s[%d] M: %f | 类型: %s | 集群ID: %d"), 
                    *GetName(), i, Particle->M(), *ParticleType, ClusterID);
            }
        }
    }
    
  2. 质量验证辅助确认
    集群根节点的质量等于其下所有子粒子(含子集群、真实粒子)的质量总和,而真实粒子无后代,质量即为自身物理质量。可以遍历计算子粒子质量总和,与根节点质量对比,验证集群身份。

  3. 利用静态结构属性
    在编辑器创建Geometry Collection时,集群节点会带有特定属性标记。可通过StaticCollection->GetAttribute<int32>("ClusterType", i)获取类型标识(不同UE版本属性名可能有差异,需对应调整),直接区分集群与真实粒子。


内容的提问来源于stack exchange,提问作者Nick Charney Kaye

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 06:50:55