【问题标题】:How to set a mesh with a material to actor in C++?如何在 C++ 中设置带有材质的网格来执行角色?
【发布时间】:2020-09-13 18:28:35
【问题描述】:

如何为actor AMySphere设置带有材质的网格?

void AMyActor::DrawSpheresAtLevel(int level_)
{
    // 1. Generate parameters...
    if (0 != level_)
    {
        spheres_count += spheres_count / 100 * factor;
        radius += radius / 100 * rad_factor; // radius of a circle with a player placed at the center
    }
    // 2. Generate spheres.
    FVector location;
    GetPlayerLocation(location);
    float x = location.X + radius;
    FVector previous_scenter;
    for (int j = 0; j <= spheres_count; ++j)
    {
        float y = std::sqrt(pow(radius, 2) - pow(x, 2));
        FVector sphere_center = { x, y, 100 };
        
        FRotator rot = FRotator::ZeroRotator;
        if (j != 0) // calculate distance between previous sphere and current sphere 
        {           // centers until it becomes less, or equal to 80
            float deg = 1.f;
            do
            {
                rot.SetComponentForAxis(EAxis::Type::Z, deg++);
                sphere_center = rot.RotateVector(previous_scenter);
            } while (Distance(previous_scenter, sphere_center) < 80.f);
        }
        // Draw a sphere.
        FActorSpawnParameters SpawnParams;
        AMySphere* sphere_ptr = GetWorld()->SpawnActor<AMySphere>(sphere_center, rot, SpawnParams);
        if (nullptr == sphere_ptr)
        {
            std::cout << "nullptr\n";
        }
        else
        {
            sphere_ptr->SetObserver(this);
            sphere_ptr->SetIndex_(spheres.size());
            spheres.push_back(sphere_ptr);
        }
        previous_scenter = sphere_center;
    }
}

【问题讨论】:

    标签: c++ unreal-engine4


    【解决方案1】:

    如果您只需要一个球体,您可以在AMySphere::AMySphere 中添加这样的组件,然后在该组件中添加您想要的任何材料。

    USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
    RootComponent = SphereComponent;
    SphereComponent->InitSphereRadius(40.0f);
    

    如果您想更改网格,您可以创建 UStaticMeshComponent 并将网格资源分配给它的运行时。

    【讨论】:

    • 我想让 AMySphere 对象在我的场景中可见。这需要什么?
    • 确保球体大小合适并在合适的位置生成,可见标志设置为真且材质不是不可见的。您可以在编辑器中播放时调试生成的结果,只需为新演员设置一些名称并在场景树中搜索以查看生成的状态
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 2020-02-04
    • 2015-12-29
    • 1970-01-01
    • 2022-06-26
    • 2018-12-02
    • 2020-10-15
    相关资源
    最近更新 更多