介绍:
基于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,越界还需要手动处理下,楼主偷懒了