轉自:http://blog.csdn.net/pizi0475/article/details/50396277
隨着近來虛幻引擎4的一些變化,渲染多種顏色的輪廓線已經可以實現了!通過自定義模板,類似自定義深度的緩沖區——但允許網格作為整數值來渲染。它提供了一個0~255范圍,可以在網格上定義不同顏色的輪廓線,甚至可以多個功能混用,例如下圖使用不同模板索引的描邊效果。
原始自定義深度
去年制作的原始輪廓線材質是基於在自定義模板可用之前的自定義深度。也就是說,在作為一個單通道深度值繪制到緩沖區后,不能決定網格的類型。原始效果用到的指令很少,所以若你的游戲不需要多種顏色,可以考慮使用舊的特效。
新特效依舊使用自定義深度來決定遮蔽(可選的),通過在后期處理中修改FillAlpha參數來添加一個模糊的覆蓋顏色來調整。可以不勾選材質實例中的FillOcclusion選項來關閉該遮蔽。

開啟自定義模板
自定義模板缺省是不開啟的,可以依次點擊Window> Project Settings > Rendering > Post Process > CustomDepth-stencil Pass ,將其設為Enabled with Stencil來啟用自定義模板。
在本例中,一些網格在自定義模板視圖中不可見,他們的模板值為0(缺省),不包含在緩沖區中。

為了讓它們在視圖中可見,依次查看Lit > Buffer Visualizer > Custom Stencil。
你可以通過渲染目錄下網格的編輯菜單,來啟用自定義深度或改變模板索引。

若你使用C++,為了方便起見,可以在游戲頭文件(比如:survivalGame.h)中對它們進行宏定義。
/* Stencil index mapping to PP_OutlineColored */ #define STENCIL_FRIENDLY_OUTLINE 252; #define STENCIL_NEUTRAL_OUTLINE 253; #define STENCIL_ENEMY_OUTLINE 254; #define STENCIL_ITEMHIGHLIGHT 255;
在C++中直接啟用自定義深度並設置順序。
GetMesh()->SetRenderCustomDepth(true); GetMesh()->CustomDepthStencilValue = STENCIL_ENEMY_OUTLINE; // or simply assign an int32 within 1-255 range.
設置后期處理
你需要放置一個Post ProcessVolume來啟用輪廓線。確保設置為Unbound,這樣就可以不管相機是否在體積內。在選中PostProcess Volume狀態下,點擊Settings > Blendables 並添加PPI_OutlineColored作為第一個入口。
原文鏈接:http://www.tomlooman.com/multi-color-outline-post-process-in-unreal-engine-4/
原文作者:Tom
代碼參考:http://www.tomlooman.com/soft-outlines-in-ue4/


float3 CurColor=0; float2 NewUV = UV; int i=0; float StepSize = Distance / (int) DistanceSteps; float CurDistance=0; float2 CurOffset=0; float SubOffset = 0; float TwoPi = 6.283185; float accumdist=0; if (DistanceSteps < 1) { return Texture2DSample(CustomDepthTexture,CustomDepthTextureSampler,UV); } else { while (i < (int) DistanceSteps) { CurDistance += StepSize; for (int j = 0; j < (int) RadialSteps; j++) { SubOffset +=1; CurOffset.x = cos(TwoPi*(SubOffset / RadialSteps)); CurOffset.y = sin(TwoPi*(SubOffset / RadialSteps)); NewUV.x = UV.x + CurOffset.x * CurDistance; NewUV.y = UV.y + CurOffset.y * CurDistance; float distpow = pow(CurDistance, KernelPower); CurColor += ceil(Texture2DSample(CustomDepthTexture,CustomDepthTextureSampler,NewUV))*distpow; accumdist += distpow; } SubOffset +=RadialOffset; i++; } CurColor = CurColor; CurColor /=accumdist; return CurColor; }