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

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.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值