Today I've made this simple post process material, that applies a radial blur to screen. That was made for a sniper rifle mode in game, when player is looking through a optical sight. And we've got this raw effect:
To create this shader I use a custom node expression in material editor. You can see it's code here:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
*UV in screen space (in range from 0 to 1). | |
*Radius is a size of radial blur center area. Inside of this area no blur will be applied. | |
*CenterPosition is a point, to which all blur effect vectors are pointing. | |
*MaxPasses is max number of blur samples. | |
*Darkness is using to reduce brightness of area where blur effect was applied. | |
*/ | |
float mask = 1.0; | |
//this function is to get pixels from the Screen | |
//14 is an index of PostProcessChannel. 14 means that we want to get PostProcessInput0 channel. | |
float4 blur = SceneTextureLookup(UV,14,false); | |
float4 viewSize = Frame.ViewSizeAndInvSize; | |
float4 bufferSize = Frame.BufferSizeAndInvSize; | |
//the next 5 lines are used to get proper uv coords, radius and center position | |
//in relation to screen aspect and viewSize | |
//without this, everything will be stretched and placed in wrong position if you are not in fullscreen | |
//------------------------------------------------------------------------- | |
float2 ratio = float2(bufferSize.x / viewSize.x, bufferSize.y / viewSize.y); | |
float2 aspect = float2 (1.0,bufferSize.y / bufferSize.x); | |
float radius = Radius * viewSize.x/bufferSize.x; | |
float2 centerPosition = CenterPosition * aspect / ratio; | |
float2 uv = UV * aspect; | |
//------------------------------------------------------------------------- | |
float2 BlurVector= centerPosition - uv; | |
float dist = length(BlurVector); | |
//passes are calculated progressively. the more closer we are to center, the less passes we need | |
//it provieds us smooth fade between blured and clean areas. | |
float passes = floor(MaxPasses * (dist/radius-1)); | |
if (passes < 0) passes = 0; | |
if (passes > MaxPasses) passes = MaxPasses; | |
if (dist < radius) | |
{ | |
mask = 1.0 ; | |
} | |
else | |
{ | |
mask = (1.0 - Darkness) * (radius/dist); | |
} | |
[unroll (100)] | |
for (float i = 0; i< passes; i++) | |
{ | |
blur += SceneTextureLookup ( UV + BlurVector*i*SampleSize,14,false); | |
} | |
//calculating average pixel color; | |
return blur / (passes+1)*mask ; |
Just copy the source and paste it into your custom node.
Modify as you need.
Комментариев нет:
Отправить комментарий