头发篇【2018.10.12】

带菲尼尔的细贴图

 

 

带菲尼尔的细贴图4pass的

卸了纹理贴图的光圈更明显一些

 

不带菲尼尔的细贴图

 

 

 

 

 

 

 

带菲尼尔的粗贴图

 

不带菲尼尔的粗贴图

/*带透明裁剪
深度写入
阴影

菲尼尔立方体贴图

Kajiaya 光照模型(光圈)
*/


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(0,258)) = 1

			//菲尼尔反射强度
		_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));

			//fixed3 diffuse = _LightColor0.rgb * albedo * sqrt(1. - abs(dot(tangentNormal.xyz, tangentLightDir.xyz)));

			fixed3 diffuse = _LightColor0.rgb * albedo * sqrt(1. - abs(lerp(0.25, 1.0, dot(tangentNormal.xyz, tangentLightDir.xyz))));



			
			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 specular = _LightColor0.rgb*_Specular.rgb*pow(sqrt(1 - abs(dot(tangentNormal, normalize(1 - tangentViewDir - tangentLightDir)))), _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));

			//fixed3 diffuse = _LightColor0.rgb * albedo * sqrt(1. - abs(dot(tangentNormal.xyz, tangentLightDir.xyz)));


			fixed3 diffuse = _LightColor0.rgb * albedo * sqrt(1. - abs(lerp(0.25, 1.0, dot(tangentNormal.xyz, tangentLightDir.xyz))));



			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 specular = _LightColor0.rgb*_Specular.rgb*pow(sqrt(1 - abs(dot(tangentNormal, normalize(1-tangentViewDir -tangentLightDir)))), _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"
}
/*带透明裁剪
深度写入
阴影

Kajiaya 光照模型(光圈)
*/



Shader "Unlit/HearTry"
{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(0,256)) = 20
}



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" 

	fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
fixed _Cutoff;
fixed _AlphaScale;

sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
fixed4 _Specular;
float _Gloss;

struct a2v
{
	float4 vertex : POSITION;
	float3 normal : NORMAL;
	float4 texcoord : TEXCOORD0;

	float4 tangent:TANGENT;
};

struct v2f
{
	float4 pos : SV_POSITION;
	float3 worldNormal : TEXCOORD0;
	float3 worldPos : TEXCOORD1;
	float4 uv : TEXCOORD2;
	SHADOW_COORDS(3)

		float3 lightDir:TEXCOORD4;
	float3 viewDir:TEXCOORD5;
};



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;

	o.worldNormal = UnityObjectToWorldNormal(v.normal);
	o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
	//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;

	TRANSFER_SHADOW(o);

	return o;
}

fixed4 frag(v2f i) : SV_Target
{

	fixed3 tangentLightDir = normalize(i.lightDir);
fixed3 tangentViewDir = normalize(i.viewDir);


//利用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 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.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 * saturate(dot(worldNormal, worldLightDir));

//fixed3 diffuse = _LightColor0.rgb * albedo * sqrt(1. - abs(dot(tangentNormal.xyz, tangentLightDir.xyz)));

fixed3 diffuse = _LightColor0.rgb * albedo * sqrt(1. - abs(lerp(0.25,1.0,dot(tangentNormal.xyz, tangentLightDir.xyz))));




UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);


fixed3 halfDir = normalize(tangentLightDir + tangentViewDir);
//fixed3 specular = _LightColor0.rgb*_Specular.rgb*pow(max(0, dot(tangentNormal, halfDir)), _Gloss);

fixed3 specular = _LightColor0.rgb*_Specular.rgb*pow(sqrt(1 - abs(dot(tangentNormal, normalize(1 - tangentViewDir - tangentLightDir)))), _Gloss);


return fixed4(ambient + (diffuse + 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" 

	fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
fixed _Cutoff;
fixed _AlphaScale;

sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
fixed4 _Specular;
float _Gloss;

struct a2v
{
	float4 vertex : POSITION;
	float3 normal : NORMAL;
	float4 texcoord : TEXCOORD0;

	float4 tangent:TANGENT;
};

struct v2f
{
	float4 pos : SV_POSITION;
	float3 worldNormal : TEXCOORD0;
	float3 worldPos : TEXCOORD1;
	float4 uv : TEXCOORD2;
	SHADOW_COORDS(3)

		float3 lightDir:TEXCOORD4;
	float3 viewDir:TEXCOORD5;
};



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;

	o.worldNormal = UnityObjectToWorldNormal(v.normal);
	o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
	//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;

	TRANSFER_SHADOW(o);

	return o;
}

fixed4 frag(v2f i) : SV_Target
{

	fixed3 tangentLightDir = normalize(i.lightDir);
fixed3 tangentViewDir = normalize(i.viewDir);


//利用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 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.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 * saturate(dot(worldNormal, worldLightDir));

//fixed3 diffuse = _LightColor0.rgb * albedo * sqrt(1. - abs(dot(tangentNormal.xyz, tangentLightDir.xyz)));

fixed3 diffuse = _LightColor0.rgb * albedo * sqrt(1. - abs(lerp(0.25, 1.0, dot(tangentNormal.xyz, tangentLightDir.xyz))));


UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);


fixed3 halfDir = normalize(tangentLightDir + tangentViewDir);
//fixed3 specular = _LightColor0.rgb*_Specular.rgb*pow(max(0, dot(tangentNormal, halfDir)), _Gloss);

fixed3 specular = _LightColor0.rgb*_Specular.rgb*pow(sqrt(1 - abs(dot(tangentNormal, normalize(1 - tangentViewDir - tangentLightDir)))), _Gloss);



return fixed4(ambient + (diffuse + specular)* atten, texColor.a*_AlphaScale);
}
ENDCG
}


}

Fallback "Transparent/Cutout/VertexLit"
}

 

上一天没思路,昨天大姨妈请假,今天才来继续弄,今天找了一个头发高光是光圈的效果的高光公式,修改了一点参数让光圈是我想要的位置,但是整体看来还是不行,没有质感,而且带菲尼尔会过爆,印象高光效果,不带还有点过硬,下周试试把菲尼尔去了,用边缘检测的方法试试,异性就先不搞了,男生头发看不出来,女生板板整整的头发效果才能看出来,

 

 

参考文章:

https://blog.csdn.net/himilong/article/details/78150005

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值