如何在UE4中动态渲染四边形?

Dou*_*oug 29 unreal-engine4

在answers.unrealengine.com上有一些手动的答案,但他们似乎缺乏任何细节或例子.

具体而言,详细地说,如果你想实现一组在3D游戏世界中渲染的动态纹理四边形,你会怎么做?

对于用例,请考虑使用Spriter动画的2dish侧卷轴.2D动画很容易从XML加载,但是如何在场景中动态渲染这组2D纹理,旋转和缩放四边形?

Sai*_*yan 4

您面临的问题是生成网格还是获得正确的方向?(即正投影,面向相机)

生成网格体非常简单,可以通过蓝图或代码来完成。

在蓝图中,您将设置某些先决条件,然后根据条件选择生成参与者。实际的编码解决方案看起来非常相似。

如果是关于方向,那么这个答案将对您有帮助,可以在虚幻引擎论坛上找到:

https://answers.unrealengine.com/questions/62126/how-do-i-render-a-dynamic-mesh-with-orthographic-p.html

编辑:

经过大量的拉扯和文档冲浪之后,这里是使事情正常工作的代码。

ADynamicMeshSpawner::ADynamicMeshSpawner()
{
     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
     PrimaryActorTick.bCanEverTick = true;

    // Using a SphereComponent is not particularly necessary or relevant, but the cube refused to spawn without a root component to attach to, or so I surmise. Yay Unreal. =/
    USphereComponent* CubeComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
    RootComponent = CubeComponent;
    CubeComponent->InitSphereRadius(40.0f);
    CubeComponent->SetCollisionProfileName(TEXT("Pawn"));

    // Create and position a mesh component so we can see where our cube is
    UStaticMeshComponent* CubeVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
    CubeVisual->AttachTo(RootComponent);
    static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube'"));
    if (SphereVisualAsset.Succeeded())
    {
        CubeVisual->SetStaticMesh(SphereVisualAsset.Object);
        CubeVisual->SetRelativeLocation(FVector(-200.0f, 0.0f, 100.0f));
        CubeVisual->SetWorldScale3D(FVector(2.0f));
    }
    // Create a material to be applied on the StaticMeshComponent
    static ConstructorHelpers::FObjectFinder<UMaterial> Material(TEXT("Material'/Game/StarterContent/Materials/M_Tech_Hex_Tile_Pulse.M_Tech_Hex_Tile_Pulse'"));

    if (Material.Object != NULL)
    {
        TheMaterial = (UMaterial*)Material.Object;
    }

    CubeVisual->SetMaterial(0, TheMaterial);
}
Run Code Online (Sandbox Code Playgroud)

头文件如下所示:

UCLASS()
class MYPROJECT_API ADynamicMeshSpawner : public AActor
{
    GENERATED_BODY()

public: 
    // Sets default values for this actor's properties
    ADynamicMeshSpawner();

    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

    // Called every frame
    virtual void Tick( float DeltaSeconds ) override;
    // Pointer to the material that needs to be used
    UMaterial* TheMaterial;
};
Run Code Online (Sandbox Code Playgroud)

最终输出在编辑器中如下所示:

代码生成了一个辉煌的立方体!

我将其设置为每次在键盘上按“P”时都会生成“DynamicMeshSpawner”类的一个实例。创建此类的实例时,它会调用构造函数,该构造函数会生成应用了材质的立方体。我使用 SpawnActor 节点在 BluePrints 中生成了类实例。

在关卡蓝图中实现

生成东西所需的条件显然取决于应用程序。

此方法适用于普通材质,但不适用于材质实例。我相信您必须对 TheMaterial 的类型、ConstructorHelper 调用以及从材质引用到 TheMaterial 的转换进行更改才能使其发挥作用。

我相信这也适用于动画材质,这意味着 2D 动画需要转换为某种材质。

也许下面的链接会有所帮助。

https://forums.unrealengine.com/showthread.php?6744-Flipbook-material-to-recreate-an-animated-gif

编辑2:

下面是关于如何在虚幻中按程序创建对象的一组非常好的示例。把它留在这里供后代使用,以防万一有人来看。

https://github.com/SiggiG/ProceduralMeshes/

  • 好吧,狗屎。=/ 我相信你说得很对。但只要有一点坚持和一些努力,我相信我可以实现它!我承认现在它已经成为一种痴迷。我需要解决这个问题!道格,我要么解决这个问题,要么就死掉。祝我好运。 (2认同)