Is it possible to assign a UTexture2D to a material at runtime?

来源:互联网 发布:qq输入法mac 编辑:程序博客网 时间:2024/05/17 07:57

https://answers.unrealengine.com/questions/29271/is-it-possible-to-assign-a-utexture2d-to-a-materia.html

动态材质可以实时更改参数。
但是只能在c++代码或BP中创建以及应用到模型上。






Dynamic Material Instance

Step 1:

In editor, make a material that has a Texture as a propety.

a. hold down T and left click in material space to make a new texture sample

b. right click and convert to parameter

c. Give it the name "T2DParam"

d. save this new material as your master material

Step 2:

Copy the reference into a constructor

or create a MaterialInterface* UPROPERTY to set from editor on a class BP (like your player controller BP)

  1. //~~~ BP Referenced Materials ~~~
  2. UPROPERTY(EditDefaultsOnly, Category=Materials)
  3. UMaterialInterface * MasterMaterialRef;
  4.  
  5.  

Step 3:

In code, make a material Instance of this master material. Do NOT do this in the constructor, only after post init.

  1. UMaterialInstanceDynamic* RV_MatInst = UMaterialInstanceDynamic::Create(MasterMaterialRef, this);
  2.  

Step 4:

After you make your custom T2D, assign it as a texture parameter to your material

  1. RV_MatInst->SetTextureParameterValue(FName("T2DParam"),YourCustomT2DRef);
  2.  
  3.  

UMateriaInstanceDynamic.h:

  1. /** Set an MID texture parameter value */
  2. UFUNCTION(BlueprintCallable, Category="Rendering|Material")
  3. ENGINE_API void SetTextureParameterValue(FName ParameterName, class UTexture* Value);
  4.  
0 0
原创粉丝点击