UE4 post-processing material lens flare (Lensflare) effect

introduce:

Realize the effect of lens flare (Lensflare) based on PostProcessMaterial.

Instead of using the Lensflare that comes with UE, I found a nice one on ShaderToy:
ShaderToy address: ShaderToyLensflareSample
insert image description here

Transplanted it into UE4, the effect is not bad (remove the huge bright sun aperture effect) ↓

insert image description here
Superimposed sky box
insert image description here
pure black background

step:

1. After the scene is placed, process the Volume and set it to Unbound

insert image description here

2. Create a post-processing material, and select Post Process for MaterialDomain

insert image description here

3. Connect the blueprint like this

The red box is two Cutom nodes, which contain Shader
insert image description here

It’s time to upload the code, it’s not too much to like and bookmark =w=

4. The SunPosUV node converts the sun position to screen space UV

// An highlighted block
	half3 CameratoWorldVectorX = mul(half3(1.0, 0.0, 0.0), (half3x3)(View.CameraViewToTranslatedWorld));
	half3 CameratoWorldVectorY = mul(half3(0.0, 1.0, 0.0), (half3x3)(View.CameraViewToTranslatedWorld));

	half3 LocalCameraX = half3(1.0, 0.0, 0.0) * dot(SunLightVector, CameratoWorldVectorX);
	half3 LocalCameraY = half3(0.0, 1.0, 0.0) * dot(SunLightVector, CameratoWorldVectorY);
	
	
	//Divide xy to find perspective projection
	half2 PerspectiveProjection = (LocalCameraX + LocalCameraY).rg; 

	//unpack 0-1
	half2 ScreenUV = PerspectiveProjection * half2(0.5, -0.5) + half2(0.5, 0.5);
    return ScreenUV;

5. The Lensflare node program calculates and draws Lensflare, and the unnecessary ones in the original ShaderToy are removed

float2 uv = fragCoord.xy - 0.5;
uv.x *= iResolution.x/iResolution.y; //fix aspect ratio

float2 mouse = iMouse - 0.5;
mouse.x *= iResolution.x/iResolution.y; //fix aspect ratio
float2 pos = mouse.xy;


float2 main = uv-pos;
float2 uvd = uv*(length(uv));

// 太阳光晕,去掉
//float ang = atan(main.x/main.y);
// float dist=length(main); dist = pow(dist,.1);
// float n = Texturesample(View.PerlinNoiseGradientTexture, float2(ang*16.0,dist*32.0));

// float f0 = 1.0/(length(uv-pos)*16.0+1.0);

// f0 = f0 + f0*(sin(noise(sin(ang*2.+pos.x)*4.0 - cos(ang*3.+pos.y))*16.)*.1 + dist*.1 + .8);


//float f1 = max(0.01-pow(length(uv+1.2*pos),1.9),.0)*7.0;

float f2 = max(1.0/(1.0+32.0*pow(length(uvd+0.8*pos),2.0)),.0)*00.25;
float f22 = max(1.0/(1.0+32.0*pow(length(uvd+0.85*pos),2.0)),.0)*00.23;
float f23 = max(1.0/(1.0+32.0*pow(length(uvd+0.9*pos),2.0)),.0)*00.21;

float2 uvx = lerp(uv,uvd,-0.5);

float f4 = max(0.01-pow(length(uvx+0.4*pos),2.4),.0)*6.0;
float f42 = max(0.01-pow(length(uvx+0.45*pos),2.4),.0)*5.0;
float f43 = max(0.01-pow(length(uvx+0.5*pos),2.4),.0)*3.0;

uvx = lerp(uv,uvd,-.4);

float f5 = max(0.01-pow(length(uvx+0.2*pos),5.5),.0)*2.0;
float f52 = max(0.01-pow(length(uvx+0.4*pos),5.5),.0)*2.0;
float f53 = max(0.01-pow(length(uvx+0.6*pos),5.5),.0)*2.0;

uvx = lerp(uv,uvd,-0.5);

float f6 = max(0.01-pow(length(uvx-0.3*pos),1.6),.0)*6.0;
float f62 = max(0.01-pow(length(uvx-0.325*pos),1.6),.0)*3.0;
float f63 = max(0.01-pow(length(uvx-0.35*pos),1.6),.0)*5.0;

float3 c = float3(0.0, 0.0, 0.0);

c.r+=f2+f4+f5+f6; c.g+=f22+f42+f52+f62; c.b+=f23+f43+f53+f63;
c = c*1.3 - float3(length(uvd), length(uvd), length(uvd))*.05;
// c+=float3(f0, f0, f0);

float3 color = float3(1.4,1.2,1.0)*c;
color -= Noise*.015;

float w = color.x+color.y+color.z;
color =  lerp(color,float3(w, w, w)*0.5,w*0.1);
//color = cc(color,.5,.1);
float4 OutColor = float4(color,1.0);
return OutColor;

In addition to 0, the cross-border needs to be handled manually, the landlord is lazy

You can modify the parameters and Noise textures by yourself to adjust the performance~

If you need to judge the occlusion, just use SceneDepth~

Guess you like

Origin blog.csdn.net/qq_34813925/article/details/126333329