• 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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

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

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

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

  • 相关阅读:
    苹果“欠”用户的承诺,何时会兑现?
    C++ Primer Plus第五版笔记(p51-100)
    【redis过期删除】
    AI教我做科研系列——超级对话模型ChatGPT教我一步步如何如何生成知识图谱
    前沿研究|16s+宏基因组binning揭示大型藻类附生微生物群落核心组成
    [LINUX使用] iptables && tcpdump && wireshark && tshark
    ftp服务器搭建部署与C#实现ftp文件的上传
    面试不到10分钟就被赶出来了,问的实在是太变态了...
    OceanBase TableAPI实践案例(Java)
    【C语言】模拟实现strcat
  • 原文地址:https://blog.csdn.net/qq_34813925/article/details/126333329