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"

Creating material with texture in c++ at runtime

I need to load a skeletal mesh at runtime.
My code is based on the ACharacter class and creates a skeletal mesh in the constructor from a custom model format.
The mesh has N materials, each with at least a diffuse texture.
I can create materials and load textures (UTexture2D) at runtime without problems.
My problem is to assign the texture to the material.
In the editor I end up with a skeletal mesh and all the materials, but no textures assigned to them.

What is the best method to do that? Or at least a working one?

PS : I heard that assigning a texture is done in PostLoad or on the components post-init, but these methods aren't called in the editor. I need the solution to work both for the editor and ingame.

Product Version: UE 4.12
more ▼

asked Jun 22 '16 at 01:13 PM in C++ Programming

avatar image

11 1 3 5

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

1 answer: sort voted first

The easiest way to do this is with Material Instances.

Create a UMaterial with a Texture Parameter to serve as the base material. At runtime, create a UMaterialInstanceDynamic and call SetTextureParameterValue to provide a value to your parameter.

If you set your base UMaterial as the material for the mesh section, you can even use the convenience method UPrimitiveComponent::CreateAndSetMaterialInstanceDynamic to create the MID.

more ▼

answered Jun 22 '16 at 11:27 PM

avatar image

Digi Labs Dan
561 16 10 34

avatar image Stalker474 Jun 23 '16 at 01:57 PM

Thanks, I'll look into that. So far I don't know when CreateAndSetMaterialInstanceDynamic is called, but it may suit me. By the way would you know how to debug a "FRenderResource was deleted without being released first!" fatal error?

avatar image Digi Labs Dan Jun 23 '16 at 06:06 PM

I don't know what the issue with FRenderResource is. I would start by looking at the stack trace to find which subclass of FRenderResource is throwing the error, and ideally attach a debugger. I suspect that you have a mistake in how you're creating the skeletal mesh, and you forgot to call InitResources after you construct it.

(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