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"

Loading textures at runtime

Not sure what the best approach for this would be. I currently load a bunch of textures in a constructor:

 ConstructorHelpers::FObjectFinder<UTexture2D> MyObj(*(pathName));
 TextureArr[i] = MyObj.Object;

and even though this method works, I have found that it is getting bloated and it would also be loading a bunch of other textures I wouldn't actually be using at that point in the game. Now what I need to do is have the same approach to loading really, but in another function, such as a LoadTextures( int TextureSetToLoad ) and then when that is called, it would load up and set some textures.

So is there any way to do this, or is there any way to create another constructor somehow, which I could pass my TextureSetToLoad into, as ConstructorHelpers doesn't seem to work outside of the per-generated constructors.

Product Version: Not Selected
Tags:
more ▼

asked Apr 02 '14 at 12:49 PM in C++ Programming

avatar image

Dune
1.9k 52 40 200

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

4 answers: sort voted first

After having a fresh mind and another look around this morning, I found this, which I had a play around with and got working in a rather simple fashion. I am now able to load textures from path locations at run time by concatenating some strings with variables to create my paths. Only problem is, my textures have to be in a certain folder structure but it's not a huge issue. Basic code is:

 UTexture2D* MyTextureLoader::LoadTextureFromPath(const FString& Path)
 {
     if (Path.IsEmpty()) return NULL;
 
     return Cast<UTexture2D>(StaticLoadObject(UTexture2D::StaticClass(), NULL, *(Path)));
 }

 FString PathToLoad = "/Game/Textures/YourStructureHere";
 UTexture2D* tmpTexture = LoadTextureFromPath(PathToLoad);


more ▼

answered Apr 04 '14 at 11:22 AM

avatar image

Dune
1.9k 52 40 200

avatar image cmartel Nov 14 '14 at 06:54 PM

Important caveat to anyone looking to use this approach:

Loading an asset from code in this fashion will not create a reference to it when dependencies are gathered at cook time. Meaning that once you cook your project, assets that are exclusively loaded in this fashion will not be included in your cooked build and will fail to load.

As described in the question, using ConstructorHelpers::FObjectFinder to assign to a hard pointer property will create a reference and the asset will be gathered. However, as mentioned, it has the downside that the asset will always be loaded along the object.

As per Ben's answer, TAssetPtr is really what you want to use. It will create an assignable property and will be properly referenced, but is not loaded automatically.

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

I would recommend using a TAssetPtr, and then loading them on demand at runtime. Here's a doc explaining the basics of how you would do this:

https://docs.unrealengine.com/latest/INT/Programming/Assets/AsyncLoading/index.html

more ▼

answered Apr 03 '14 at 08:45 PM

avatar image

[Epic]Ben Zeigler STAFF
1.3k 29 131 75

avatar image Dune Apr 04 '14 at 08:51 AM

Is this the correct approach I need to use then to just load in textures and nothing else? I made an attempt with that documentation but found I kept running into errors and such, esepctially when I needed to declare what class the objects where and such, as they are only textures (Assuming UTexture2D would be the class?).

avatar image [Epic]Ben Zeigler STAFF Apr 04 '14 at 04:25 PM

Yeah, it's a UTexture2D.

avatar image AusPaco Jul 06 '14 at 07:43 AM

I also tried using TAssetPtr to try and get from a path to an asset to an instance of it as UTexture2D* but failed miserably. Ben, can you post some example code?

For now I'm just going with what Dune has.

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

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

more ▼

answered May 08 '15 at 03:29 PM

avatar image

SRombauts
1.1k 22 17 97

avatar image manuaganu May 23 '16 at 01:02 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

I am recommend you to reference your asset with

     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Item) TAssetPtr<UTexture> Thumbnail;

in your class.
And after return it as

 UTexture* UCommonLibrary::ReturnThumbnailFunction()
 {
         auto thumbnail = Thumbnail.LoadSynchronous();
                  return thumbnail ;
 }

more ▼

answered Jun 26 '16 at 07:33 AM

avatar image

lab-one
111 3 5 10

(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