HLSL Blur, anybody?

Hello folks,

i came to a point where i need to blur a map, but within the shader, so i can keep it dynamic. I found some code in the web for doing this here

http://www.dhpoware.com/demos/xnaGaussianBlur.html

and i found a ue4 forums post about a radial blur post effect here

unfortunately i wasnt even able to change the code of the second link to work on a texture sample - so complete rewriting of the gaussian blur is a thing i simply not able to do.
So maybe somebody allready did it yet? :slight_smile: - Box blur would be fine too, i guess, because it doesnt have to be absolutely accurate.

Your GPU blurs your texture already. Just read from a lower mipmap.

Hm thanks, that might work - if i can force the shader to allways use a defined mipmap? Independent from screen size / distance?

anyways, as captured maps doesnt have mip maps, i really need the blur tho :frowning:

Sorry for late reply. You can find the solution in the epic docs.

https://docs.unrealengine.com/latest/INT/Engine/Rendering/Materials/ExpressionReference/Custom/index.html

I also go into some more radial blur prototypes here at the beginning of the thread (it goes off into post process methods after that):

https://forums.unrealengine.com/showthread.php?70143-So-Blurred-glass-material-is-impossible-in-Unreal-Engine-4

By using temporal AA to offset the steps you can get a decent result with surprisingly few steps. Ie a 4 distance steps with 2 radial steps doesn’t look so bad with the TempAA setting tweaked just right.

thank you Samaritan for that hint - allready tweeked the box blur to give a decent Result - if somebody is interesed:


Begin Object Class=MaterialGraphNode_Root Name="MaterialGraphNode_Root_1"
    Begin Object Class=EdGraphPin Name="EdGraphPin_8163"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8162"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8161"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8160"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8159"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8158"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8157"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8156"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8155"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8154"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8153"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8152"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8151"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8150"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8149"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8148"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8147"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8146"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8145"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8144"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8143"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8142"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8141"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8140"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8139"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8138"
    End Object
    Begin Object Name="EdGraphPin_8163"
       PinName="Pixel Depth Offset"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8162"
       PinName="Material Attributes"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8161"
       PinName="Customized UV7"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8160"
       PinName="Customized UV6"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8159"
       PinName="Customized UV5"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8158"
       PinName="Customized UV4"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8157"
       PinName="Customized UV3"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8156"
       PinName="Customized UV2"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8155"
       PinName="Customized UV1"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8154"
       PinName="Customized UV0"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8153"
       PinName="Refraction"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8152"
       PinName="Ambient Occlusion"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8151"
       PinName="Clear Coat Roughness"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8150"
       PinName="Clear Coat"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8149"
       PinName="Subsurface Color"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8148"
       PinName="Tessellation Multiplier"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8147"
       PinName="World Displacement"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8146"
       PinName="World Position Offset"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8145"
       PinName="Normal"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8144"
       PinName="Opacity Mask"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8143"
       PinName="Opacity"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8142"
       PinName="Emissive Color"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8141"
       PinName="Roughness"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8140"
       PinName="Specular"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8139"
       PinName="Metallic"
       PinType=(PinCategory="materialinput")
    End Object
    Begin Object Name="EdGraphPin_8138"
       PinName="Base Color"
       PinType=(PinCategory="materialinput")
       LinkedTo(0)=EdGraphPin'MaterialGraphNode_8.EdGraphPin_8247'
    End Object
    Material=PreviewMaterial'/Engine/Transient.PreviewMaterial_0'
    Pins(0)=EdGraphPin'EdGraphPin_8138'
    Pins(1)=EdGraphPin'EdGraphPin_8139'
    Pins(2)=EdGraphPin'EdGraphPin_8140'
    Pins(3)=EdGraphPin'EdGraphPin_8141'
    Pins(4)=EdGraphPin'EdGraphPin_8142'
    Pins(5)=EdGraphPin'EdGraphPin_8143'
    Pins(6)=EdGraphPin'EdGraphPin_8144'
    Pins(7)=EdGraphPin'EdGraphPin_8145'
    Pins(8)=EdGraphPin'EdGraphPin_8146'
    Pins(9)=EdGraphPin'EdGraphPin_8147'
    Pins(10)=EdGraphPin'EdGraphPin_8148'
    Pins(11)=EdGraphPin'EdGraphPin_8149'
    Pins(12)=EdGraphPin'EdGraphPin_8150'
    Pins(13)=EdGraphPin'EdGraphPin_8151'
    Pins(14)=EdGraphPin'EdGraphPin_8152'
    Pins(15)=EdGraphPin'EdGraphPin_8153'
    Pins(16)=EdGraphPin'EdGraphPin_8154'
    Pins(17)=EdGraphPin'EdGraphPin_8155'
    Pins(18)=EdGraphPin'EdGraphPin_8156'
    Pins(19)=EdGraphPin'EdGraphPin_8157'
    Pins(20)=EdGraphPin'EdGraphPin_8158'
    Pins(21)=EdGraphPin'EdGraphPin_8159'
    Pins(22)=EdGraphPin'EdGraphPin_8160'
    Pins(23)=EdGraphPin'EdGraphPin_8161'
    Pins(24)=EdGraphPin'EdGraphPin_8162'
    Pins(25)=EdGraphPin'EdGraphPin_8163'
    NodeGuid=096675D040422786C21B44A6C29F59A6
End Object
Begin Object Class=MaterialGraphNode Name="MaterialGraphNode_8"
    Begin Object Class=MaterialExpressionCustom
Name="MaterialExpressionCustom_0"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8247"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8246"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8245"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8244"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8243"
    End Object
    Begin Object Name="MaterialExpressionCustom_0"
       Code="float3 blur = Texture2DSample(Tex, TexSampler,
UV);

dist = dist / r;

for (int j = 0; j < r;
j++)
{
  for (int i = 0; i < r; i++)
  {
    blur +=
Texture2DSample(Tex, TexSampler, UV + float2(dist*j, dist*i));
   
blur += Texture2DSample(Tex, TexSampler, UV + float2(-dist*j,
dist*i));
    blur += Texture2DSample(Tex, TexSampler, UV +
float2(dist*j, -dist*i));
    blur += Texture2DSample(Tex,
TexSampler, UV + float2(-dist*j, -dist*i));
  }
}

blur /=
((r*r*4)+1);
return blur;"
Inputs(0)=(InputName="Tex",Input=(Expression=MaterialExpressionTextureObjectParameter'MaterialGraphNode_16.MaterialExpressionTextureObjectParameter_1'))
Inputs(1)=(InputName="UV",Input=(Expression=MaterialExpressionTextureCoordinate'MaterialGraphNode_10.MaterialExpressionTextureCoordinate_0'))
Inputs(2)=(InputName="r",Input=(Expression=MaterialExpressionScalarParameter'MaterialGraphNode_15.MaterialExpressionScalarParameter_3'))
Inputs(3)=(InputName="dist",Input=(Expression=MaterialExpressionScalarParameter'MaterialGraphNode_14.MaterialExpressionScalarParameter_2'))
       MaterialExpressionEditorX=-370
       MaterialExpressionEditorY=92
       MaterialExpressionGuid=7C27B04045C60B02EE5372914BDDB2A1
       Material=PreviewMaterial'/Engine/Transient.PreviewMaterial_0'
    End Object
    Begin Object Name="EdGraphPin_8247"
       PinName="Output"
       PinFriendlyName=" "
       Direction=EGPD_Output
LinkedTo(0)=EdGraphPin'MaterialGraphNode_Root_1.EdGraphPin_8138'
    End Object
    Begin Object Name="EdGraphPin_8246"
       PinName="dist"
       PinType=(PinCategory="required")
       LinkedTo(0)=EdGraphPin'MaterialGraphNode_14.EdGraphPin_8179'
    End Object
    Begin Object Name="EdGraphPin_8245"
       PinName="r"
       PinType=(PinCategory="required")
       LinkedTo(0)=EdGraphPin'MaterialGraphNode_15.EdGraphPin_8180'
    End Object
    Begin Object Name="EdGraphPin_8244"
       PinName="UV"
       PinType=(PinCategory="required")
       LinkedTo(0)=EdGraphPin'MaterialGraphNode_10.EdGraphPin_8175'
    End Object
    Begin Object Name="EdGraphPin_8243"
       PinName="Tex"
       PinType=(PinCategory="required")
       LinkedTo(0)=EdGraphPin'MaterialGraphNode_16.EdGraphPin_8181'
    End Object
MaterialExpression=MaterialExpressionCustom'MaterialExpressionCustom_0'
    Pins(0)=EdGraphPin'EdGraphPin_8243'
    Pins(1)=EdGraphPin'EdGraphPin_8244'
    Pins(2)=EdGraphPin'EdGraphPin_8245'
    Pins(3)=EdGraphPin'EdGraphPin_8246'
    Pins(4)=EdGraphPin'EdGraphPin_8247'
    NodePosX=-370
    NodePosY=92
    ErrorType=1
    ErrorMsg="Custom material Custom missing input 5 (temp)"
    NodeGuid=75A713AC4D126C1D741359A9AD9B502F
End Object
Begin Object Class=MaterialGraphNode Name="MaterialGraphNode_10"
    Begin Object Class=MaterialExpressionTextureCoordinate
Name="MaterialExpressionTextureCoordinate_0"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8175"
    End Object
    Begin Object Name="MaterialExpressionTextureCoordinate_0"
       MaterialExpressionEditorX=-623
       MaterialExpressionEditorY=-108
       MaterialExpressionGuid=8C83D66D497870EDB63CC69C09C95C57
       Material=PreviewMaterial'/Engine/Transient.PreviewMaterial_0'
    End Object
    Begin Object Name="EdGraphPin_8175"
       PinName="Output"
       PinFriendlyName=" "
       Direction=EGPD_Output
       LinkedTo(0)=EdGraphPin'MaterialGraphNode_8.EdGraphPin_8244'
    End Object
MaterialExpression=MaterialExpressionTextureCoordinate'MaterialExpressionTextureCoordinate_0'
    Pins(0)=EdGraphPin'EdGraphPin_8175'
    NodePosX=-623
    NodePosY=-108
    NodeGuid=D2A53C314C55186372E48C934A92877D
End Object
Begin Object Class=MaterialGraphNode Name="MaterialGraphNode_14"
    Begin Object Class=MaterialExpressionScalarParameter
Name="MaterialExpressionScalarParameter_2"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8179"
    End Object
    Begin Object Name="MaterialExpressionScalarParameter_2"
       DefaultValue=0.100000
       ParameterName="dist"
       ExpressionGUID=FBD0DCBA4CF55F2864E1CCB3E735DA18
       MaterialExpressionEditorX=-688
       MaterialExpressionEditorY=160
       MaterialExpressionGuid=43F81AE74CE02F6736466F9EA551E594
       Material=PreviewMaterial'/Engine/Transient.PreviewMaterial_0'
    End Object
    Begin Object Name="EdGraphPin_8179"
       PinName="Output"
       PinFriendlyName=" "
       Direction=EGPD_Output
       LinkedTo(0)=EdGraphPin'MaterialGraphNode_8.EdGraphPin_8246'
    End Object
MaterialExpression=MaterialExpressionScalarParameter'MaterialExpressionScalarParameter_2'
    Pins(0)=EdGraphPin'EdGraphPin_8179'
    NodePosX=-688
    NodePosY=160
    bCanRenameNode=True
    NodeGuid=C5DE917349A1DE762F29738EDF68AD2C
End Object
Begin Object Class=MaterialGraphNode Name="MaterialGraphNode_15"
    Begin Object Class=MaterialExpressionScalarParameter
Name="MaterialExpressionScalarParameter_3"
    End Object
    Begin Object Class=EdGraphPin Name="EdGraphPin_8180"
    End Object
    Begin Object Name="MaterialExpressionScalarParameter_3"
       DefaultValue=4.000000
       ParameterName="r"
       ExpressionGUID=B088E58E4B4C299EAB6D2F99F8BC2906
       MaterialExpressionEditorX=-688
       MaterialExpressionEditorY=80
       MaterialExpressionGuid=9B30C19E4AC274443394ACB010E0CB52
       Material=PreviewMaterial'/Engine/Transient.PreviewMaterial_0'
    End Object
    Begin Object Name="EdGraphPin_8180"
       PinName="Output"
       PinFriendlyName=" "
       Direction=EGPD_Output
       LinkedTo(0)=EdGraphPin'MaterialGraphNode_8.EdGraphPin_8245'
    End Object
MaterialExpression=MaterialExpressionScalarParameter'MaterialExpressionScalarParameter_3'
    Pins(0)=EdGraphPin'EdGraphPin_8180'
    NodePosX=-688
    NodePosY=80
    bCanRenameNode=True
    NodeGuid=BC5FFFBC4B11CF60772D9784F7A63686
End Object
Begin Object Class=MaterialGraphNode Name="MaterialGraphNode_16"
    Begin Object Class=EdGraphPin Name="EdGraphPin_8181"
    End Object
    Begin Object Class=MaterialExpressionTextureObjectParameter
Name="MaterialExpressionTextureObjectParameter_1"
    End Object
    Begin Object Name="EdGraphPin_8181"
       PinName="Output"
       PinFriendlyName=" "
       Direction=EGPD_Output
       LinkedTo(0)=EdGraphPin'MaterialGraphNode_8.EdGraphPin_8243'
    End Object
    Begin Object Name="MaterialExpressionTextureObjectParameter_1"
       ParameterName="Tex"
       ExpressionGUID=363C0C694AE8132D341A1691F07F156F
       Texture=Texture2D'/Game/Materials/mouseWheel.mouseWheel'
       MaterialExpressionEditorX=-912
       MaterialExpressionEditorY=-160
       MaterialExpressionGuid=EC1108C64AF5BB80853CDEA587F4CCE3
       Material=PreviewMaterial'/Engine/Transient.PreviewMaterial_0'
    End Object
MaterialExpression=MaterialExpressionTextureObjectParameter'MaterialExpressionTextureObjectParameter_1'
    Pins(0)=EdGraphPin'EdGraphPin_8181'
    NodePosX=-912
    NodePosY=-160
    bCanRenameNode=True
    NodeGuid=D912A74B4B9A606217224DBBA6D9CABB
End Object



Thanks RyanB - that looks amazing, especially cause i might need frosted glass too :slight_smile: but most important for me would be bluring cubemaps, which does not work with my actual code, listed above

@RyanB - i managed to get your temporalAA Code work in Texture2d (TextureObject) - wich gives nice results - but how would i do that with cubemaps then? There are no functions to sample them in custom nodes, are there?

It would have to be re-written a bit in order to work as a cubemap. Currently the math takes an angle and creates a vector based on it, but to do that for a starting 3d vector requires using math like rotate about axis, or possibly a random number generator that respects unit length normals.

There may be an alternate way to do it actually. i made a function that can be used to sample a cubemap using regular UVs. You would need to reimport the texture as a non-cubemap but it can still be an HDR image. Then we can try performing the blur on it by first converting the cameravector from Longlat to UV. I need to dig around to find that function. I’ll post back later.

sounds good, thanks for your help - but i fear it have to be a regular cubemap in order to accept scenecapturecube targets
what i want to do, is capturing a small cubemap and then blur it just a little bit to use it 4 highlights in different materials, thats why i want to blur it by shader, so each material can have its unique glossness