• 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就行~

  • 相关阅读:
    使用JavaScript计算两点经纬度之间的弧线点经纬度数组
    基于nsct变换特征提取和模糊神经网络的无参考图像质量评价算法matlab仿真
    项目实战接口开发SpringBoot
    软件和硬件之间的数据交互接口
    SpringMVC-拦截器
    分组后再子集再查询
    网络安全入门教程(非常详细)从零基础入门到精通,看完这一篇就够了。
    【CesiumJS-3】加载倾斜模型数据(3DTilest)以及修改位置
    书店管理系统
    如何提升网站图片的加载速度呢?
  • 原文地址:https://blog.csdn.net/qq_34813925/article/details/126333329