Shader "Unlit/NewUnlitShader"
{
Properties
{
_Color("Color Tint", Color) = (1,1,1,1)
_MainTex("MainTex", 2D) = "White" {}
_Cutoff("Alpha Cutoff", Range(0,1)) = 0.5
//凹凸贴图
_BumpMap("Normal Map",2D) = "bump"{}
//整体透明度强弱
_AlphaScale("Alpha Scale",Range(0,1)) = 1
//法线纹理凹凸程度
_BumpScale("Bump Scale",Float) = 1.0
_Specular("Specular",Color) = (1,1,1,1)
_Gloss("Gloss",Range(8.0,256)) = 20
//菲尼尔反射强度
_FresnelScale("Fresnel Scale",Range(0,1)) = 0.5
//反射颜色
_ReflectColor("Reflect Color",Color)=(1,1,1,1)
//模拟反射的环境映射纹理
_Cubemap("Reflection Cubemap",Cube) = "_Skybox"{}
}
SubShader
{
Tags{ "Queue" = "AlphaTest" "IgnoreProject" = "True" "RenderType" = "TransparentCutout" }
Pass
{
Tags{ "LightMode" = "ForwardBase" }
//剔除前面
Cull Front
//ZWrite off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "LIghting.cginc"
#include "AutoLight.cginc"
#include "UnityCG.cginc"
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
fixed _Cutoff;
fixed _AlphaScale;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
fixed4 _Specular;
float _Gloss;
fixed4 _ReflectColor;
float _FresnelScale;
samplerCUBE _Cubemap;
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
//获取切线
float4 tangent:TANGENT;
};
struct v2f
{
float4 pos : SV_POSITION;
float4 uv : TEXCOORD2;
SHADOW_COORDS(3)
float3 lightDir:TEXCOORD4;
float3 viewDir:TEXCOORD5;
float4 TtoW0 : TEXCOORD6;
float4 TtoW1 : TEXCOORD7;
float4 TtoW2 : TEXCOORD8;
fixed3 worldRefl : TEXCOORD9;
fixed3 worldViewDir : TEXCOORD1;
};
v2f vert (a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv.xy = v.texcoord.xy*_MainTex_ST.xy + _MainTex_ST.zw;
o.uv.zw = v.texcoord.xy*_BumpMap_ST.xy + _BumpMap_ST.zw;
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
//切线
fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
//次法线
fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
//o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
TANGENT_SPACE_ROTATION;
o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
o.worldViewDir = UnityWorldSpaceViewDir(worldPos);
o.worldRefl = reflect(-o.worldViewDir, worldNormal);
//切线,次法线,法线,位置
o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
TRANSFER_SHADOW(o);
return o;
}
//片元着色器
fixed4 frag (v2f i) : SV_Target
{
float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);
fixed3 tangentLightDir = normalize(i.lightDir);
fixed3 tangentViewDir = normalize(i.viewDir);
//凹凸贴图
fixed3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw));
//凹凸
bump = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump)));
//利用tex2D对法线纹理_BumpMap进行采样
fixed4 packedNormal = tex2D(_BumpMap, i.uv.zw);
fixed3 tangentNormal;
//or mark the texture as "Normal map",ang use the built-in funciton(或者将纹理标记为“法线贴图”,使用内置函数)
tangentNormal = UnpackNormal(packedNormal);
tangentNormal.xy *= _BumpScale;
fixed3 worldLightDir = normalize(UnityWorldSpaceViewDir(worldPos));
fixed4 texColor = tex2D(_MainTex, i.uv);
//透明度测试
clip(texColor.a - _Cutoff);
fixed3 albedo = tex2D(_MainTex, i.uv).rgb*_Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(bump, worldLightDir));
UNITY_LIGHT_ATTENUATION(atten, i, worldPos);
fixed3 halfDir = normalize(tangentLightDir + tangentViewDir);
fixed3 specular = _LightColor0.rgb*_Specular.rgb*pow(max(0, dot(tangentNormal, halfDir)), _Gloss);
//在世界空间中使用折射方向访问立方体贴图
fixed3 reflection = texCUBE(_Cubemap, i.worldRefl).rgb*_ReflectColor.rgb;
//获取归一化的视角方向
//fixed3 worldViewDir = normalize(i.worldViewDir);
i.worldViewDir = normalize(_WorldSpaceCameraPos.xyz - worldPos.xyz);
//使用Schlick菲尼尔近似式计算fresnel变量
fixed fresnel = _FresnelScale + (1 - _FresnelScale)*pow(1 - dot(i.worldViewDir, bump), 5);
return fixed4(ambient + lerp(diffuse, reflection, saturate(fresnel) + specular)*atten, texColor.a*_AlphaScale);
//return fixed4(ambient + (lerp(diffuse, reflection, saturate(fresnel) + specular )* atten, texColor.a*_AlphaScale));
}
ENDCG
}
Pass
{
Tags{ "LightMode" = "ForwardBase" }
//剔除背面
Cull Back
//ZWrite off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "LIghting.cginc"
#include "AutoLight.cginc"
#include "UnityCG.cginc"
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
fixed _Cutoff;
fixed _AlphaScale;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
fixed4 _Specular;
float _Gloss;
fixed4 _ReflectColor;
float _FresnelScale;
samplerCUBE _Cubemap;
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
//获取切线
float4 tangent:TANGENT;
};
struct v2f
{
float4 pos : SV_POSITION;
float4 uv : TEXCOORD2;
SHADOW_COORDS(3)
float3 lightDir:TEXCOORD4;
float3 viewDir:TEXCOORD5;
float4 TtoW0 : TEXCOORD6;
float4 TtoW1 : TEXCOORD7;
float4 TtoW2 : TEXCOORD8;
fixed3 worldRefl : TEXCOORD9;
fixed3 worldViewDir : TEXCOORD1;
};
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv.xy = v.texcoord.xy*_MainTex_ST.xy + _MainTex_ST.zw;
o.uv.zw = v.texcoord.xy*_BumpMap_ST.xy + _BumpMap_ST.zw;
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
//切线
fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
//次法线
fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
//o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
TANGENT_SPACE_ROTATION;
o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
o.worldViewDir = UnityWorldSpaceViewDir(worldPos);
o.worldRefl = reflect(-o.worldViewDir, worldNormal);
//切线,次法线,法线,位置
o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
TRANSFER_SHADOW(o);
return o;
}
//片元着色器
fixed4 frag(v2f i) : SV_Target
{
float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);
fixed3 tangentLightDir = normalize(i.lightDir);
fixed3 tangentViewDir = normalize(i.viewDir);
//凹凸贴图
fixed3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw));
//凹凸
bump = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump)));
//利用tex2D对法线纹理_BumpMap进行采样
fixed4 packedNormal = tex2D(_BumpMap, i.uv.zw);
fixed3 tangentNormal;
//or mark the texture as "Normal map",ang use the built-in funciton(或者将纹理标记为“法线贴图”,使用内置函数)
tangentNormal = UnpackNormal(packedNormal);
tangentNormal.xy *= _BumpScale;
fixed3 worldLightDir = normalize(UnityWorldSpaceViewDir(worldPos));
fixed4 texColor = tex2D(_MainTex, i.uv);
//透明度测试
clip(texColor.a - _Cutoff);
fixed3 albedo = tex2D(_MainTex, i.uv).rgb*_Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(bump, worldLightDir));
UNITY_LIGHT_ATTENUATION(atten, i, worldPos);
fixed3 halfDir = normalize(tangentLightDir + tangentViewDir);
fixed3 specular = _LightColor0.rgb*_Specular.rgb*pow(max(0, dot(tangentNormal, halfDir)), _Gloss);
//在世界空间中使用折射方向访问立方体贴图
fixed3 reflection = texCUBE(_Cubemap, i.worldRefl).rgb*_ReflectColor.rgb;
//获取归一化的视角方向
//fixed3 worldViewDir = normalize(i.worldViewDir);
i.worldViewDir = normalize(_WorldSpaceCameraPos.xyz - worldPos.xyz);
//使用Schlick菲尼尔近似式计算fresnel变量
fixed fresnel = _FresnelScale + (1 - _FresnelScale)*pow(1 - dot(i.worldViewDir, bump), 5);
return fixed4(ambient + lerp(diffuse, reflection, saturate(fresnel) + specular)*atten, texColor.a*_AlphaScale);
//return fixed4(ambient + (lerp(diffuse, reflection, saturate(fresnel) + specular )* atten, texColor.a*_AlphaScale));
}
ENDCG
}
}
Fallback "Transparent/Cutout/VertexLit"
}
今天的 战果,有了一点点想要的效果,但是还是不堪入目,不过全部都是自己写的,还是比较开心滴
其实也没加什么,而且。。。。代码量有点多,优化是以后的事,现在连实现都费劲呢,加入了菲尼尔反射,昨天刚学的,还改了一点赋值结构,运算时还加入了副法线和切线,让整体看起来没有那么闷。加入菲尼尔是为了这种效果
现实是。。颜色,上还需要处理,而且菲尼尔会影响高光的颜色。。范围还不完全是自己想要的,明天继续吧,今天先下班喽。