Reputation: 35216
There are some hand wavey answers for this on answers.unrealengine.com, but they seem to lack any detail or examples.
Specifically, and in detail, if you wanted to implement a set of dynamic textured quads which rendered in the 3d game world, how would you do it?
For use case, consider a 2dish side scroller that uses Spriter animations. The 2D animations are loaded from XML easily enough, but how do you then render this 2D set of textured, rotated and scaled quads dynamically on the scene?
Upvotes: 29
Views: 3654
Reputation: 300
Is the problem you are facing concern spawning the mesh or with obtaining the right orientation? (i.e. orthographic projection, facing the camera)
Spawning the mesh is easy enough, can be done either through Blueprints or in code.
In Blueprints, you would set up certain preconditions and then choose to spawn actors based on the conditions. The actual coding solution would look much the same.
If it is regarding orientation, then this answer will be of help to you, found on the UnrealEngine forums:
EDIT:
After much hair pulling and documentation surfing, here's the code that made things work.
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);
}
The headerfile looks like this:
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;
};
The final output looked like this in the editor:
I set it up so that an instance of my class 'DynamicMeshSpawner' would be spawned everytime I hit 'P' on the keyboard. When the instance of this class is created, it calls the constructor, which spawns the cube with the material applied. I did the class instance spawning stuff in BluePrints using the SpawnActor node.
The conditions that you require for spawning stuff would obviously depend on the application.
This method works for normal Materials but NOT Material Instances. I believe you would have to make changes to the type of TheMaterial, the ConstructorHelper call, and the cast from the material reference into TheMaterial in order to make it function.
I'm confident that this would work with Animated materials as well, meaning that the 2D animations would need to be converted into a Material of some sort.
Perhaps the link below would help.
https://forums.unrealengine.com/showthread.php?6744-Flipbook-material-to-recreate-an-animated-gif
EDIT 2:
Below are a very good set of examples on how to procedurally create objects in Unreal. Leaving it here for posterity and in case anyone comes looking.
https://github.com/SiggiG/ProceduralMeshes/
Upvotes: 4