x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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;
     }
 

Product Version: Not Selected
Tags:
more ▼

asked Jul 29 '14 at 12:53 AM in C++ Programming

avatar image

Rammstein
83 7 7 12

avatar image trutty Nov 06 '14 at 01:40 AM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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);

more ▼

answered Nov 06 '14 at 08:19 AM

avatar image

BigaC
91 3 71 13

avatar image trutty Nov 07 '14 at 12:33 AM

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(...)?

avatar image BigaC Nov 07 '14 at 09:30 AM

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();
avatar image trutty Nov 07 '14 at 09:51 AM

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

avatar image DrHobo Feb 26 '16 at 03:19 AM

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 ?

avatar image zompi2 Dec 01 '14 at 08:40 PM

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.

avatar image trutty Dec 02 '14 at 10:30 AM

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);

avatar image zompi2 Dec 03 '14 at 05:54 PM

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

avatar image SRombauts May 08 '15 at 03:29 PM

For future reference, Rama also have a great Blueprintable function for this in the forum : The solution is in this post: https://forums.unrealengine.com/showthread.php?3851-(39)-Rama-s-Extra-Blueprint-Nodes-for-You-as-a-Plugin-No-C-Required!&p=210419&viewfull=1#post210419

avatar image manuaganu May 23 '16 at 01:03 PM

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: http://imgur.com/PHlU0TA

figure[2] texture: http://imgur.com/6YmZ4ea

figure[3] displacement map: http://imgur.com/iGDYsrS

figure[4] Method: http://imgur.com/PQZ9C3a

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: http://imgur.com/nKan9eK

figure[6]: Runtime result: http://imgur.com/ao2XnDk

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)

http://imgur.com/MOZQLSu

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, manuganu

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Jul 29 '14 at 03:22 AM

avatar image

Ehamloptiran ♦
1.4k 49 7 58

avatar image Rammstein Jul 30 '14 at 11:41 PM

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.

avatar image Ehamloptiran ♦ Jul 31 '14 at 01:36 AM

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.

https://drive.google.com/file/d/0BzqpBc4MrAMXLXN1ZGNzUEJZc1k/edit?usp=sharing

avatar image Rammstein Aug 21 '14 at 11:03 PM

Thank you so much.

avatar image manuaganu May 13 '16 at 04:01 AM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question