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"

How to set a texture parameter in C++?

I'm working on a custom USceneComponent. In the constructor I'm trying to load a material asset, a texture asset and apply the material to an attached static mesh component.

This is what I have so far:

 Mesh = ObjectInitializer.CreateDefaultSubobject<USquareMeshComponent>(this, TEXT("Mesh"));
 Mesh->AttachParent = this;

 static ConstructorHelpers::FObjectFinder<UMaterial> MaterialFinder(
     TEXT("Material'/Game/Materials/DynamicTextureMaterial.DynamicTextureMaterial'"));
 UMaterial* Material = MaterialFinder.Object;
 DynamicMaterial = UMaterialInstanceDynamic::Create(Material, NULL);

 static ConstructorHelpers::FObjectFinder<UTexture> TextureFinder(
     TEXT("Texture2D'/Game/Textures/2DBackground.2DBackground'"));
 UTexture* Texture = TextureFinder.Object;

 DynamicMaterial->SetTextureParameterValue(FName(TEXT("DynamicTexture")), Texture);
 Mesh->SetMaterial(0, DynamicMaterial);

This compiles fine, but crashes the Editor on launch, except if I comment the SetTextureParameterValue call. Without it I can start the Editor, see my component, just with the wrong default texture.

I tried doing the same in the Level BP's Event Play and it worked fine. So my material and texture should be ok.

Am I doing something wrong? Thanks!

Product Version: Not Selected
Tags:
more ▼

asked Feb 01 '15 at 03:53 PM in C++ Programming

avatar image

Bajee
285 16 30 135

avatar image Duncan Dam Feb 02 '15 at 03:06 AM

Don't put SetTextureParameerValue and SetMaterial in your Constructor try to move it to PostInitializeComponents

avatar image Bajee Feb 02 '15 at 10:27 AM

I'm implementing a component, not an actor. As far as I can tell, PostInitializeComponents is a method on AActor. Do you have a suggestion what to do in a component? I tried to move those calls to PostInitProperties with the same results (crash on launch). Thanks!

avatar image Bajee Feb 02 '15 at 01:05 PM

Okay, so setting the texture and material in the PostLoad method of my component seems to work.

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

2 answers: sort voted first

Here is what turned out to work for me.

In my component constructor I load the assets and assign them to my properties:

 struct FConstructorStatics
 {
     ConstructorHelpers::FObjectFinderOptional<UTexture> TextureFinder;
     ConstructorHelpers::FObjectFinderOptional<UMaterial> MaterialFinder;
     FConstructorStatics()
         : TextureFinder(TEXT("Texture2D'/Game/Textures/2DBackground.2DBackground'"))
         , MaterialFinder(TEXT("Material'/Game/Materials/DynamicTextureMaterial.DynamicTextureMaterial'"))
     {
     }
 };
 static FConstructorStatics ConstructorStatics;

 Texture = ConstructorStatics.TextureFinder.Get();
 UMaterial* Material = ConstructorStatics.MaterialFinder.Get();
 DynamicMaterial = UMaterialInstanceDynamic::Create(Material, this);

Then in my component's PostLoad I apply them:

 DynamicMaterial->SetTextureParameterValue(FName("DynamicTexture"), Texture);
 Mesh->SetMaterial(0, DynamicMaterial);

Thanks Duncan and Rajko for the suggestions!

more ▼

answered Feb 02 '15 at 01:08 PM

avatar image

Bajee
285 16 30 135

avatar image Pelangi Jun 09 '16 at 10:05 AM

Hi Bajee,

The SetTextureParameterValue() function does not exist in UMaterialInstanceDynamic class nor UMaterialInstance class. What UE version did you use?

I'm using version 4.11 and all I see is SetTextureParameterValueInternal() function but it is protected. How did you do this in UE 4.11 and later?

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

For me the finder only works inside a AHUD derived class :/

more ▼

answered Mar 24 '15 at 01:12 PM

avatar image

Ciprian At Facerig
6 2 3

(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