Is it possible to load bitmap or JPG files at runtime and use them on assets as textures?

I am looking for functionality that is similar to Unity’s WWW.texture

// Get the latest webcam shot from outside "Friday's" in Times Square
	var url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg";
	function Start () {
		 // Start a download of the given URL
		var www : WWW = new WWW (url);

		// Wait for download to complete
		yield www;

		// assign texture
		renderer.material.mainTexture = www.texture;
	}

Yes you could do that, however you would have to write in the functionality to obtain the image from a webserver, and to parse the BMP/JPG, then you can create a UTexture2D (Using the CreateTransient static function). Lock it and fill it with data, I am doing something similar to this for runtime textures, although I am using DDS and reading from the HDD.

This is great!
Do you mind sharing some of the code you use to read from the drive, use the function and create a texture object? If you could share any part of this code, it would really help. I am very new to C++ but used to program in other languages. Thank you for your time.

I used FDDSLoadHelper to actually parse the file, which is a built in UE4 helper class. The code was too big to paste in here, so below is a link to the snippet.

Thank you so much.

Hi there, I am also interested in this topic. So far I get image data from an URL and save the response as TArray imageDataArray = response->GetContent();

When trying to create a texture from this TArray I fail miserably. I considered using libpng/libjpeg for this purpose but I guess I am not skilled enough.

It would be great if you could share your solution or at least give a hint on how to create a texture to use it on runtime. Thanks!

Cheers

you can also use ImageWrapperModule and load PNG or other supported formats.

example:

include "Developer/ImageWrapper/Public/Interfaces/IImageWrapper.h"
include "Developer/ImageWrapper/Public/Interfaces/IImageWrapperModule.h"
    
    ...
    	FString pngfile = "myimage.png";
    	IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
    	// Note: PNG format.  Other formats are supported
    	IImageWrapperPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);
    
    	if (FFileHelper::LoadFileToArray(RawFileData, *pngfile))
    	{
    		if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
    		{
    			const TArray<uint8>* UncompressedBGRA = NULL;
    			if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
                            {
                                  mytex = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);

thank you very much, this works great. I have one final error. I hope you can assist. When I assign the texture to a StaticMesh, the texture falsely appears white. I got:

...
            // HTTP respone; image
            TArray<uint8> imageDataArray = response->GetContent();
    
            if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(imageDataArray.GetData(), imageDataArray.Num()))
            {
                const TArray<uint8>* uncompressedBGRA = NULL;
                if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, uncompressedBGRA))
                {
                    // TODO get actual image dimensions
                    imageTexture = UTexture2D::CreateTransient(100, 100, PF_B8G8R8A8);
                    imageTexture->MipGenSettings = TMGS_NoMipmaps;

.. 

// change appropriate texture parameter
matInst->SetTextureParameterValue(FName("webTexture"), imageTexture);

UncompressedBGRA correctly holds all the color values. Do I have to call something else after CreateTransient(...)?

oh yes, you need copy the loaded data onto texture:

			void* TextureData = mytex->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
			FMemory::Memcpy(TextureData, UncompressedBGRA->GetTypedData(), UncompressedBGRA->Num());
			mytex->PlatformData->Mips[0].BulkData.Unlock();

			// Update the rendering resource from data.
			mytex->UpdateResource();

perfect! thanks again. I thought I do not have to care about mips because I have a non power of two image and therefore there are no mipmaps. The basic mip has to be created anyways… great help

Great thread, it helped me a lot! I have a question about deleting texture, releasing the memory.

Let’s say I have

UProperty()
Texture2D* mytex;

and created a texture using

mytex = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);

and I don’t need this texture anymore. Will setting mytex to nullptr will be enaught and GarbageCollector will do the job? Do I have to run UTexture::ReleaseResource() or UObject::ConditionalBeginDestroy() ? Simply deleting mytex cause crash, so I thing the engine has to clean it it’s ways.

Hi zompi2,

this thread talks about instantly deleting UObject, dynamic material instance, static mesh actors, etc.

Although I didn’t tested it, the solution seems to be to first check for validity and then call:

mytex->ConditionalBeginDestroy();
mytex = NULL;
GetWorld()->ForceGarbageCollection(true);

Yup, it will be this. Thanks for ensure me it is right :slight_smile:

For future reference, Rama also have a great Blueprintable function for this in the forum : The solution is in this post: (39) Rama's Extra Blueprint Nodes for You as a Plugin, No C++ Required! - Blueprint - Unreal Engine Forums

Hi. I don’t know if any of you are still watching this thread, but in my case, using 4.9 (I don’t use 4.10+ yet) the garbage collector is running amok on this texture and calls UTexture2D::BeginDestroy() after a few seconds. I only use it directly on to the HUD, plotting it at every tick, so it’s not dependent on another object being destroyed. Any thought on what might happen here ?

I want image file with mipmaps. Is there any way to load dynamic texture with mip levels?

I’m trying to set material onto object at run-time from texture, normal map, displacement map etc. located in the hard disk. To achieve this i need to create dynamic material instance and set the parameter to the material by loading the image file at run-time.

I’m creating material from texture & displacement map image file. These image files are loading from folder using Victory Plugin’s “Victory Load Texture 2D from File” Node.

I’m creating the material instance dynamic of the “test material”. Now I’m setting the texture and displacement parameter as shown in figure 4.
After creating material instance dynamic, I’m setting this material to the object.

figure[1] test material:

figure[2] texture:

figure[3] displacement map:

figure[4] Method:

Now here is the problem.

When I directly create material in material editor from texture and displacement map and set it onto object. It doesn’t generate** moiré pattern.**

But when I run-time set the material onto object using figure [4]. It will generate moiré pattern.

Here are the sample screen shots of both.

figure[5]: Material Editor result:

figure[6]: Runtime result: Imgur: The magic of the Internet

I think the “Load Texture 2D from File” nodes is not calculating the Mip Map correctly.

I tried with following nodes but the result is same.
1> “Victory Load Texture 2D from File” (Created by rama)
2> “Load Texture 2D from File by Extension” (Created by you)
3> “Victory Load Texture 2D from File Pixels” (Created by rama)

I hope you will take this request into consideration. I would like to thank you in advance for your time and attention in this matter.
Should you have any further questions or concerns, please do not hesitate to contact me.

Regards,

Hi , have you solved your problem?

SEE THIS :slight_smile:
How do you change the texture on an object with blueprint? - Development / Rendering - Epic Developer Community Forums (unrealengine.com)