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"

Applying material to multiple meshes in c++

Hello.

I'm creating an actor, which has 256 meshes. Every mesh uses the same material, given as 'uproperty'. Is there any fast, efficient method to apply the material to all the meshes at once?

Product Version: UE 4.11
Tags:
more ▼

asked Apr 25 '16 at 02:30 PM in C++ Programming

avatar image

Cansisti
36 8 10 15

avatar image zimzimdz Apr 25 '16 at 09:57 PM

Create a material instance of your uproperty material then loop over your meshes and SetMaterial(index, pMaterialInstance); What kind of mesh is it ? Static ? Skeletal ? Do you use a UStaticMeshComponent for each ?

avatar image Cansisti Apr 26 '16 at 01:28 PM

I'm using UStaticMeshComponent, and it's static mesh. What is the 'index' argument?

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

You're talking about an efficient way to do it but we are talking about assignation so you're forced to go over every mesh and assign them your material. The only thing I could recommend is basic c++ tricks like using a contiguous memory container (TArray, vector) to prevent cash misses, but even here, you're meshes are pointers so at every iteration your system have to look into the heap. Just do: UMaterialInstanceDynamic* const pDynMat = UMaterialInstanceDynamic::Create(m_MyMaterial, this); for(UStaticMeshComponent* pComponent : m_MyStaticMeshComps) { pComponent->SetMaterial(0, pDynMat ); }

Sorry about code format

more ▼

answered Apr 26 '16 at 01:45 PM

avatar image

zimzimdz
284 10 5 10

avatar image Cansisti Apr 26 '16 at 01:53 PM

I hoped there is a single function for that or so. Gonna go like that. Thanks

avatar image zimzimdz Apr 26 '16 at 01:56 PM
 void AYourActorWithTonsOfMesh::PostInitializeComponents()
 {
     Super::PostInitializeComponents();
     UMaterialInstanceDynamic* const pDynMat = UMaterialInstanceDynamic::Create(m_MyMaterial, this);
     for (UStaticMeshComponent* pComponent : m_MyStaticMeshComps)
     {
         pComponent->SetMaterial(0, pDynMat);
     }
 }
(comments are locked)
10|2000 characters needed characters left
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