UnitySahder效果01–边缘光晕效果
UnityShader–边缘光晕效果
边缘光晕效果:
1.发光物体本身的render,当产生光晕效果时,其发光物体也应该是可见的,而不是被光晕遮罩;
2.发光物体周边散发的光晕,且如同渐变消散一般,中心到周边减弱;
原理:
1.发光物体的实现可以参考边缘发光原理,即视线方向与物体表面法线方向的点积,边缘法线与视线方向点积为非正数,因此边缘发光效果的核心公式: pow(1-saturate(dot(N,V)),power)
2.光晕实现,将发光物体进行沿法线方向进行顶点偏移,从而进行渲染,其光晕应是透明效果
效果图
Shader "GHAO/Surface_GlowLight" {Properties{_MainColor("MainColor", Color) = (1,1,1,1)//光晕强度_OuterIntensity("OuterIntensity",Range(0.1,8)) = 2 //光晕范围系数_OuterRangeCoef("OuterCoef",Range(0,1)) = 0.2 }SubShader{Tags { "RenderType" = "Opaque" "Queue" = "Transparent" } LOD 200//=====================================光晕渲染通道Pass{Blend Srcalpha one Zwrite OffCGPROGRAM#pragma vertex vert#pragma fragment frag#include "UnityCG.cginc"fixed4 _MainColor;float _OuterIntensity;float _OuterRangeCoef;struct v2f{float4 pos:SV_POSITION;float3 normal:TEXCOORD0;float4 vertex:TEXCOORD1;};v2f vert(appdata_base v){v2f o;//光晕顶点沿法线方向的系数偏移v.vertex.xyz += v.normal*_OuterRangeCoef; o.pos = UnityObjectToClipPos(v.vertex);o.normal = v.normal;o.vertex = v.vertex;return o;}fixed4 frag(v2f i):SV_TARGET{float3 N = UnityObjectToWorldNormal(i.normal);float3 V = normalize(WorldSpaceViewDir(i.vertex));float bright = pow(saturate(dot(N, V)), _OuterIntensity);_MainColor.a *= bright;return _MainColor;}ENDCG}//=====================================发光物体本身边缘发光Pass{Tags{"LightMode" = "ForwardBase"}Blend one oneZWrite OffCGPROGRAM#pragma vertex vert#pragma fragment frag#include "UnityCG.cginc"fixed4 _MainColor;float _OuterIntensity;struct v2f{float4 pos:SV_POSITION;float3 normal:TEXCOORD0;float4 vertex:TEXCOORD1;};v2f vert(appdata_base v){v2f o;o.pos = UnityObjectToClipPos(v.vertex);o.vertex = v.vertex;o.normal = v.normal;return o;}fixed4 frag(v2f i):COLOR{float3 N = UnityObjectToWorldNormal(i.normal);float3 V = normalize(WorldSpaceViewDir(i.vertex));//边缘发光//float bright = pow(1.0 - saturate(dot(N, V)), _OuterIntensity);//整体发光float bright = pow(saturate(dot(N, V)), _OuterIntensity);return _MainColor*bright;}ENDCG}}FallBack "Diffuse"}
如果觉得《UnityShader效果01--边缘光晕效果》对你有帮助,请点赞、收藏,并留下你的观点哦!