下面是shader的代码:
Shader "Forzen/FrozenEffect_myself"
{
Properties
{
[Header(Main infos)]
_MainTex ("Albedo", 2D) = "white" {}
_SpeculatTint("specular color",Color) = (0.5,0.5,0.5)
_Smoothness("smoothness",Range(0,1)) = 0.1
_Metallic("metallic",Range(0,1)) = 0
_BumpMap("Normal map",2D) = "bump"{}
[HDR]_Emission("emission",color)= (0,0,0,0)
_Occlusion("_Occlusion",Range(0,1)) = 1
_FrozenProgress("_FrozenProgress",Range(0,1)) = 1
_ForzenRegionSize("_ForzenRegionSize",Range(0,1))= 0.3
_ForzenDir("_ForzenDir ",vector) = (0,1,0,0)
_PosMinMax("_PosMinMax",vector) = (0,1,0,0)
[Header(Ice Colors)]
[NoScaleOffset] _IceTex(" iceTexture",2D) = "white"{}
[NoScaleOffset]_IceNormal("ice normal",2D) = "bump"{}
_IceTiling(" ice tiling ",Range(0,10)) = 1
_IceSpecularSmoothness("ice specular smoothness",vector) =(0.3,0.3,0.3,0.8)
_IceColorFactor("ice color factor",Range(0,1))=1
_IceDesaturate(" ice desaturate ",Range(0,1))=1
[Header(Fresnal)]
_FresnalPower ("fresnal power",Range(1,30)) = 1
_FresnalScaler ("fresnal Scaler",Range(0,1)) = 1
[HDR]_FresnalColor("fresnal color",color) = (0,0,0,0)
}
SubShader
{
Pass
{
Tags{ "RenderType"="Opaque" "LightMode"="ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#define _FORWADR_BASE
#pragma multi_compile_fwdbase
#pragma multi_compile_fog
#include "UnityCG.cginc"
#include "UnityPBSLighting.cginc" // PBS
#include "AutoLight.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float _Smoothness;
float _Metallic;
float4 _SpeculatTint;
sampler2D _IceTex;
sampler2D _IceNormal;
float4 _IceSpecularSmoothness;
float _IceColorFactor;
float _IceDesaturate;
float _IceTiling;
float _FresnalPower;
float _FresnalScaler;
float4 _FresnalColor;
float _FrozenProgress;
float _ForzenRegionSize;
float4 _ForzenDir;
float4 _PosMinMax;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 tangent : TANGENT;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 normal : TEXCOORD1;
float4 worldPos : TEXCOORD2;
float3 T2WRow0 : TEXCOORD3;
float3 T2WRow1 : TEXCOORD4;
float3 T2WRow2 : TEXCOORD5;
};
float Remap(float oa,float ob,float na,float nb,float val)
{
return (val-oa)/(ob-oa) * (nb-na) + na;
}
v2f vert(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv,_MainTex);
o.worldPos = mul(unity_ObjectToWorld,v.vertex);
float3 normal = UnityObjectToWorldNormal(v.normal);
o.normal = normal;
float3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
float3 worldBionT = cross(worldTangent,normal) * v.tangent.w * unity_WorldTransformParams.w;
o.T2WRow0 = float3(worldTangent.x,worldBionT.x,normal.x);
o.T2WRow1 = float3(worldTangent.y,worldBionT.y,normal.y);
o.T2WRow2 = float3(worldTangent.z,worldBionT.z,normal.z);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float3 worldPos = i.worldPos;
// main normal
fixed3 mainNormal = UnpackNormal(tex2D(_BumpMap,i.uv)).xyz;
float3 mainWorldNormal = fixed3(dot(mainNormal,i.T2WRow0),dot(mainNormal,i.T2WRow1),dot(mainNormal,i.T2WRow2));
// ice normal
float2 iceUV = i.uv * _IceTiling;
fixed3 iceNormal = UnpackNormal(tex2D(_IceNormal,iceUV)).xyz;
float3 IceWorldNormal = fixed3(dot(iceNormal,i.T2WRow0),dot(iceNormal,i.T2WRow1),dot(iceNormal,i.T2WRow2));
// 混合后的法线
float3 worldNormal = BlendNormals(mainWorldNormal,IceWorldNormal * 5);
float progress = _FrozenProgress;
float3 rootPos = mul(unity_ObjectToWorld,float4(0,0,0,1)).xyz;
float3 diffPos = worldPos - rootPos;
float pdDir = dot(diffPos,_ForzenDir);
// Remap映射函数 将0,2 映射到 0,1 的范围内
float forzenModelProgress = Remap(_PosMinMax.x,_PosMinMax.y,0,1,pdDir);
progress = smoothstep(forzenModelProgress - _ForzenRegionSize,forzenModelProgress,_FrozenProgress);
float smoothness = lerp(_Smoothness,_IceSpecularSmoothness.a,progress);
float3 specularTint = lerp(_SpeculatTint.rgb,_IceSpecularSmoothness.rgb,progress);
float3 viewDir = UnityWorldSpaceViewDir(worldPos);
float3 lightDir = UnityWorldSpaceLightDir(worldPos);
// diffuse
float3 diffTerm = saturate(dot(worldNormal,lightDir)) * _LightColor0.rgb;
// specular
float3 halfDir = normalize(viewDir + lightDir);
float3 specTerm = saturate(pow(dot(worldNormal,halfDir),smoothness));
//
float4 albedo = tex2D(_MainTex,i.uv);
float oneMinusReflectivity;
albedo.rgb = DiffuseAndSpecularFromMetallic(albedo,_Metallic,specularTint,oneMinusReflectivity);
float3 directColor = albedo.rgb * diffTerm + specTerm*specularTint;
// fresnal 在用法线计算的时候,dot的时候,结果可能为负数,一定要限制在(0,1)之间
float3 fresnalColor = pow(1 -saturate(dot(worldNormal,viewDir)), _FresnalPower) * _FresnalScaler * _FresnalColor.rgb;
// Ice color
float3 iceColor = tex2D(_IceTex,iceUV).rbg;
// 将颜色提纯
float3 iceDesaturateColor = dot(iceColor,float3(0.299,0.587,0.114));
iceColor = lerp(iceColor,iceDesaturateColor,_IceDesaturate) * _IceColorFactor;
float3 emissionColor = lerp(float3(0,0,0),iceColor + fresnalColor,progress);
//
return float4(emissionColor+ directColor,1);
}
ENDCG
}
}
}
用到的函数:
(1)Remap函数
(2)SmoothStep函数
上面有些属性定义了,但是没有使用,后面再慢慢补充吧。。。
(3)冰冻的起始方向可以调整,有兴趣的自己慢慢试吧,主要就是Remap的参数
最近有点飘,动力不足,不多说了,好好干,结果不会陪你演戏!
打算把pbr流程整理一遍,加油吧。。。。。
百度云链接
链接: https://pan.baidu.com/s/1NZmuZBv2NRtmP2wUS-x_MQ 提取码: bjg7