Paper2D C++ Dynamic Sprites + UV

Hi all,

so I’ve been attempting to get my Tiled map data into Unreal Engine and rendering through the use of dynamically selected Tilesets and Tilemaps (not UE flavor, the json exported from Tiled).

Well, I’ve gotten the JSON properly loaded, and the textures properly parsing and the tiles properly being created and added to a

UPaperGroupedSpriteComponent.

However, when my sprite is set with my transient textures, it renders completely as pink tiles, though you can see the tileset is properly loaded into memory (and not doing any UV transformations on it works fine)

263632-pink.png

When I load an imported texture through it properly sets the UVs and displays the various tiles:

263633-proper.png

I know it has something to do with the textures being transient. My current thought there’s some metadata missing in my texture loading function that UPaperSprite::ReloadData() needs to properly calculate things.

but I haven’t been able to track down the error in the last ~4-5 hour; so I figured I’d ask the broader knowledge base if anyone has leads.

For the actual code:

My image loader is mostly this code: A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums
With modifications made to function on the newer engine. I’m only using the LoadImageFromDisk function, not async.

This is the code I’m using to set the new texture on the sprite(ignore the material parameter, it was a shot in the dark I havent cleaned up yet):

void UDynamicPaperSprite::SetTexture(UTexture2D* Texture, FVector2D UV, FVector2D size, UMaterialInstanceDynamic* material)
{
	if (Texture)
	{

		SourceUV.Set(UV.X, UV.Y);
		SourceDimension.Set(size.X, size.Y);
		SourceTexture = Texture;
		
		RebuildData();
	}
}

And this is the code I’m looping through to build the map:

// Loop through an initialize all the sprites use
for (int i = 0; i < FloorTiles.Num(); i++) {
	UDynamicPaperSprite *newTile = NewObject<UDynamicPaperSprite>();
	// Get tile in map
	int32 tileNum = FloorTiles[i];

	// Calculate the UV
	int32 sourceY = (int32)(tileNum / tilesetColumns) * tileWidth * 5;
	int32 sourceX = (int32)(tileNum % tilesetColumns) * tileWidth * 5;

	int32 destY = ((int32)i / TilemapSizeX) * tileWidth;
	int32 destX = ((int32)i % TilemapSizeX) * tileWidth;
	
	// TEXTURE IMPORTED THROUGH EDITOR HERE - WORKS
	UTexture2D* newtex = LoadObject<UTexture2D>(this, TEXT("Texture2D'/Game/textures/cartoon-city-skyline.cartoon-city-skyline'"));

	// DYNAMIC TEXTURE HERE - DOESNT WORK
	//UTexture2D* newtex = UImageLoader::LoadImageFromDisk(this, imagePath);

	newTile->SetTexture(newtex, FVector2D(sourceX, sourceY), FVector2D(tileWidth, tileWidth), nullptr);

	((UPaperGroupedSpriteComponent*)RootComponent)->AddInstanceWithMaterial(
		FTransform(FVector(destX, 0, destY)),
		newTile,
		nullptr
	);
	

	tiles.Add(newTile);
}

Thanks for any and all help in advance,
Paul