1 . 用菲尼尔控制外边缘模糊加上菲尼尔反向后的内边缘模糊,用参数两者的强度控制;
2 . 用uv或者mesh的顶点的轴向消除掉两端的硬度(这里用UV);
思路简单,代码如下:
Shader "Unlit/MeshLight"
{
Properties
{
_BaseColor("Base Color",color) = (1,1,1,1)
_BaseMap("BaseMap", 2D) = "white" {}
_MaskFre("光线范围",Range(0,10)) = 1
_sofLight("边缘柔软",Range(0,10)) = 0.5
_PowLight("光照强弱",Range(1,30)) = 1
_uvSpeed("动画速度",vector ) = (0,0,0,0)
}
SubShader
{
Tags { "RenderPipeline"="UniversalPipeline" "RenderType" = "Transparent" "Queue" = "Transparent" "PreviewType"="Skybox" }
LOD 100
Cull off
ZWrite Off
// Blend SrcAlpha OneMinusSrcAlpha
Blend One One
Pass
{
Name "Unlit"
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library
#pragma prefer_hlslcc gles
#pragma exclude_renderers d3d11_9x
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
float4 normalOS : NORMAL;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float4 uv : TEXCOORD0;
float fogCoord : TEXCOORD1;
float3 normalWS : TEXCOORD2;
float3 positionWS : TEXCOORD3;
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor; half _MaskFre , _sofLight ,_PowLight; half4 _uvSpeed;
float4 _BaseMap_ST;
CBUFFER_END
TEXTURE2D (_BaseMap);SAMPLER(sampler_BaseMap);
//菲尼尔反向模糊边缘
half Fresnel(half3 N , half3 V , half Fres)
{
return pow(abs(1-(dot(N,V))), Fres);
}
Varyings vert(Attributes v)
{
Varyings o = (Varyings)0;
o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
o.normalWS = TransformObjectToWorldNormal(v.normalOS.xyz);
o.positionWS = TransformObjectToWorld(v.positionOS.xyz);
o.uv.xy = TRANSFORM_TEX(v.uv, _BaseMap);
o.uv.zw = v.uv;
o.fogCoord = ComputeFogFactor(o.positionCS.z);
return o;
}
half4 frag(Varyings i) : SV_Target
{
//Mask
float uv_col = i.uv.w*(pow(abs(1-i.uv.w),_MaskFre))*_MaskFre;
half3 N = i.normalWS.xyz;
half3 V = SafeNormalize(_WorldSpaceCameraPos - i.positionWS);
float FresCol01 = 1-Fresnel(N,V,0.5);
float FresCol02 = 1-Fresnel(N,-V,0.5);
float FresCol = (FresCol01 + FresCol02)*_sofLight;
//uv流动
i.uv.xy = float2(i.uv.x+ _uvSpeed.x*_Time.y,i.uv.y+ _uvSpeed.y*_Time.y);
half NoiseTex = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.uv.xy).r;
float Mask = uv_col*NoiseTex* FresCol *_PowLight;
float4 c = Mask * _BaseColor;
c.rgb = MixFog(c.rgb, i.fogCoord);
return c;
}
ENDHLSL
}
}
}