您面临的问题是生成网格还是获得正确的方向? (即正交投影,面向相机)
生成网格很容易,可以通过蓝图或代码来完成。
在蓝图中,您将设置某些先决条件,然后根据条件选择生成 Actor。
实际的编码解决方案看起来差不多。
如果是关于方向,那么这个答案将对你有所帮助,在 UnrealEngine 论坛上找到:
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);
}
头文件如下所示:
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;
};
编辑器中的最终输出如下所示:
我对其进行了设置,以便每次我在键盘上按“P”时都会生成我的类“DynamicMeshSpawner”的实例。创建此类的实例时,它会调用构造函数,该构造函数会生成应用了材质的立方体。我使用 SpawnActor 节点在 BluePrints 中生成了类实例。
生成东西所需的条件显然取决于应用程序。
此方法适用于普通材质,但不适用于材质实例。我相信您必须更改 TheMaterial 的类型、ConstructorHelper 调用以及从材质引用转换为 TheMaterial 才能使其正常工作。
我相信这也适用于动画材质,这意味着需要将 2D 动画转换为某种材质。
也许下面的链接会有所帮助。
https://forums.unrealengine.com/showthread.php?6744-Flipbook-material-to-recreate-an-animated-gif
编辑 2:
以下是一组非常好的示例,介绍如何在 Unreal 中以程序方式创建对象。留在这里以备后人,以防有人来看。
https://github.com/SiggiG/ProceduralMeshes/