UE4 后处理材质 镜头光晕(Lensflare)效果

介绍:

基于PostProcessMaterial实现镜头光晕(Lensflare)的效果。

不使用UE自带的Lensflare,在ShaderToy上发现一个好看的:
ShaderToy地址: ShaderToyLensflareSample
在这里插入图片描述

将其移植到UE4中,效果还行(去掉了巨亮的太阳光圈效果)↓

在这里插入图片描述
叠加天空盒子
在这里插入图片描述
纯黑背景

步骤:

1.场景摆后处理Volume,设置为Unbound

在这里插入图片描述

2.创建后处理材质,MaterialDomain选Post Process即可

在这里插入图片描述

3.这样连蓝图

红框是两个Cutom节点,里面写了Shader
在这里插入图片描述

要上代码了,点赞收藏一下不过分吧 =w=

4.SunPosUV节点将太阳位置转换为屏幕空间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.Lensflare节点程序计算绘制Lensflare,原ShaderToy里不需要的去掉了

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;

除0,越界还需要手动处理下,楼主偷懒了

可以自行修改参数和Noise贴图,调整表现效果~

需要判断遮挡的话,采一下SceneDepth就行~

猜你喜欢

转载自blog.csdn.net/qq_34813925/article/details/126333329