(1)先看效果: 流光的图片是unity自带的,吃藕==
不想多说什么了,很简单的效果。。。。。。用到的知识点,之前的效果里都用到过
此处说一下: rim 自发光 和 菲涅尔公式, 两者都是 1- dot(NdotV), 与法线和视线的夹角相关
流光: 确定流光方向,即 uv偏移的方向
住注意点:
Flow贴图的设置这里要特别说明三个地方
–(1) Alpha from Grayscale 从图片的灰度生成alpha通道,直接制作alpha通道不直观,灰度图很直观。
这个选项就是让你用一张直观的灰度图生成通道。
你可以不用这个选项,而是自己提供一个有alpha通道的图片
-- (2)wrap Mode当uv超出0 1 范围是的取值方法,我们需要让这张图循环往复,就是利用wrap mode repeat来实现的。
必须使用这个选项
Shader "Unlit/Scan_Code"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_RimMin("RimMin",Range(-1,1)) = 0
_RimMax("RimMax",Range(0,2)) = 1
_TinnerColor(" tinner color",Color) = (0,0,0,0)
_RimColor("Rimcolor",Color) = (1,1,1,1)
_RimTntensity(" Rim Intersity",float) =1.0
_FlowTilling("_FlowTilling",vector) = (1,1,0,0)
_FlowSpeed("_FlowSpeed",vector) = (1,1,0,0)
_FlowIntensity("flow intensity",float) =1
_FlowTex("flow tex",2D) = "white"{}
_InnerAlpha("Innner Alpha",Range(0,1)) = 1
}
SubShader
{
// Queue = Transparent ,而不是这是 RenderType
Tags { "Queue"="Transparent" }
Pass{
ZWrite On
}
Pass
{
ZWrite Off
Blend SrcAlpha One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 pos : SV_POSITION;
float3 pos_world : TEXCOORD1;
float3 normal_world : TEXCOORD2;
float3 pivot_world : TEXCOORD3;
};
sampler2D _MainTex, _FlowTex;
float4 _MainTex_ST;
float _RimMin,_RimMax;
float4 _TinnerColor;
float4 _RimColor;
float _RimTntensity, _InnerAlpha;
float4 _FlowTilling, _FlowSpeed;
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
float3 normal_world =UnityObjectToWorldNormal(v.normal);
float3 worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
o.pos_world = worldPos;
o.normal_world = normalize(normal_world);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.pivot_world = mul(unity_ObjectToWorld,float4(0.0,0.0,0.0,1.0)).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// 自发光
half3 normal_world = normalize(i.normal_world);
half3 view_world = normalize(_WorldSpaceCameraPos.xyz - i.pos_world);
half NdotV = saturate(dot(normal_world,view_world));
half fresnel = 1.0 - NdotV;
fresnel = smoothstep(_RimMin,_RimMax,fresnel);
half emiss = tex2D(_MainTex, i.uv).r;
emiss = pow(emiss,5.0);
half final_fresnel = saturate(fresnel + emiss);
half3 final_rim_color = lerp(_TinnerColor,_RimColor * _RimTntensity, final_fresnel);
half final_rim_alpha = final_fresnel;
//流光(1)
half2 uv_flow = (i.pos_world.xy - i.pivot_world.xy) * _FlowTilling.xy;
//流光(2) 此处 + fresnel 是为了让流光有立体感 有深度, 因为fresnel本身就是 从两边向中间过渡的灰度图
//half2 uv_flow = ((i.pos_world.xy - i.pivot_world.xy)+ saturate(fresnel)) * _FlowTilling.xy;
uv_flow = uv_flow + _FlowSpeed.xy * _Time.y;
float4 flow_rgba = tex2D(_FlowTex,uv_flow);
// 最后合并输出
half3 final_color = final_rim_color + flow_rgba.rgb;
half final_Alpha = saturate(final_rim_alpha + flow_rgba.a + _InnerAlpha);
//return float4(flow_rgba);
return float4(final_color,final_Alpha);
}
ENDCG
}
}
}
流光二:
Shader "Unlit/Hologram_code"
{
Properties
{
_MainTex ("_MainTex", 2D) = "white" {}
_FlowMap ("_FlowMap", 2D) = "white" {}
_Tilling("_Tilling",float) = 1
_Speed ("_Speed",float) = 1
_ScanflineOffset("_ScanflineOffset 控制宽度",float) = 1
_ScanflinePower("_ScanflinePower 控制软硬程度",float) = 1
_ScanfColor("_ScanfColor",color) = (0,0,0,0)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float3 worldPos : TEXCOORD1;
float3 PivotPos : TEXCOORD2;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex,_FlowMap;
float4 _MainTex_ST;
float _Speed,_Tilling;
float _ScanflineOffset,_ScanflinePower;
float4 _ScanfColor;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.PivotPos = mul(unity_ObjectToWorld,float4(0,0,0,1));
o.worldPos = mul(unity_ObjectToWorld,v.vertex);
return o;
}
// 在传入参数的时候,请注意 是否是需要 U和V 都在变换???
// 当只需要Y方向流动时 tilling.x 和 speed.x 都传入 0
float4 GetFlowCol (float3 worldPos,float3 pivotPos,float2 tilling,float2 speed)
{
float flowDataY = (worldPos.y- pivotPos.y) * tilling.y + _Time.y * speed.y;
float flowDataX = (worldPos.x- pivotPos.x) * tilling.x + _Time.y * speed.x;
float2 flowUV = float2(flowDataX,flowDataY);
float4 flowCol = tex2D(_FlowMap,flowUV);
return flowCol;
}
fixed4 frag (v2f i) : SV_Target
{
// float flowDataY = (i.worldPos.y- i.PivotPos.y) * _Tilling + _Time.y * _Speed;
// flowDataY = flowDataY/30.0;
// float2 flowUV = float2(0.5,flowDataY);
// float4 flowCol = tex2D(_FlowMap,flowUV);
// fixed4 col = tex2D(_MainTex, i.uv);
float4 dd = GetFlowCol(i.worldPos,i.PivotPos,float2(0,_Tilling),float2(0,_Speed));
float data = (dd.r - _ScanflineOffset) * _ScanflinePower;
data = _ScanfColor * data;
return data;
}
ENDCG
}
}
}