UE4 tutorial : Materials 筆記

本章大綱:
  1. 使用Blueprint編輯Material,拖拉點選一些原本要在shader上時做的光照內容
  2. Material Instance概念
  3. 利用Material Instance來動態變更Material外觀
  4. [Bonus] 查詢追加Event的方法


一些基本的圖學概念就跳過不講了,反正就顏色要疊起來的話是用乘的類似這些最基本概念,還有0~255可以mapping成0~1這樣,unreal的material應該是直接啦shader的數值出來秀,記得OpenGL的shader也是mapping成0~1來秀顏色XD


UE4有Material Instance的概念,有點像class。

一般在製作遊戲材質時,會由程式人員製作一個粗略的原型Material,然後交付給美術人員。
緊接著美術人員產生該材質的Material Instance,並修改該材質已完成客製化 (也不會弄壞原本的material)

這邊開了兩個功能參數出來,可以讓後來接手的人在外部修改,或於Material Instance作客製化。

像這樣,外部修改參數客製化

此外,
因為你新作一個material就相當於是製作了幾個新的shader內容並且需要去重新compile它。

因此
  1. 當你想要快速修改部分內容 (因為已經編譯好了)
  2. 快速創造material變體,抽換一些顏色、texture、specular數值之類的話
可以使用Material Instance

因此才說要在material中創建一些參數可以給外部人員使用,增加可修改性與泛用性。



這裡範例用HueShift這個blueprint Node來示範,HueShift算是一個色相調整器。

如此一來就會有一個參數是在管理亮度(乘很大),一個在做色相調整。 然後輸出到Base Color中。


Create Material Instance

在完成了Material後,在material上按右鍵就可以找到建立Material Instance的選項。

之後點開來大概是這樣

比較重要的大概是可以找到一些來自parent的資料以及可以修改

修改了Preview Mesh把模型放進去,打開客製參數開關就可以開始對instance做些不一樣的數值調整。



之後就可以修改assign給場景中的模型instance的material


動態修改Material Instance

這部分可以透過Blueprint或C++來實現。


直接在prefab修改而非material


Eveny BeginPlay =>相當於Unity的start( )

我們設定這個物件再生成的一開始,就動態生成一個material Instance,然後set到Cube Material


如此一來它就會在Unreal生成BP_Player這個物件之後,建立一個動態Material Instance並組件到StaticMesh之中,然後將Material的數據資料存在一個叫CubeMaterial的參數中。


更新動態Material

Here is the order of execution:

  1. On Component Begin Overlap (StaticMesh): Executes when the cube mesh overlaps with another actor
  2. Cast to BP_Banana: Checks if the overlapping actor is a banana
  3. DestroyActor: If the overlapping actor is a banana, destroy it so it disappears
  4. IncrementFloat: Increments the BananaCounter variable by one
  5. float / float: Divides the counter by a specified number to normalize it
  6. Clamp (float): Clamps the result of the division so that you don’t get a value higher than 1(限制數值大小範圍)
  7. Set Scalar Parameter Value: Sets the ColorAlpha parameter of the cube material to the supplied value. In this case, the value is the normalized and clamped version of BananaCounter


    還不是很懂為什麼cast to可以做到像if的事就是了,明天再去問同事好了,累

留言

這個網誌中的熱門文章

UE4 C++ Delegate

Clean Code隨筆筆記整理

UE4 tutorial : Getting Started