主要思路:将顶点延法线挤出,一层一层pass往外叠加形成毛发效果。
原文链接:https://blog.csdn.net/qq_24153371/article/details/81742337
cginc部分:
#include "Lighting.cginc"
#include "UnityCG.cginc"
#include "AutoLight.cginc"
float4 _Color;
float4 _Specular;
float _Shininess;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _FurTex;
float4 _FurTex_ST;
float _FurLength;
float _FurDensity;
float _FurThinness;
float _FurShading;
float _RimPower;
struct v2f {
float4 pos:SV_POSITION;
float4 uv:TEXCOORD0;
float3 worldNormal:TEXCOORD1;
float3 worldPos:TEXCOORD2;
};
v2f vert_surface(appdata_base v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
return o;
}
//lambert+blinn-phone
float4 frag_surface(v2f i) :SV_Target{
float3 worldNormal = normalize(i.worldNormal);
float3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
float3 worldView = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
float3 worldHalf = normalize(worldView + worldLight);
float3 albedo = tex2D(_MainTex, i.uv.xy).rgb *_Color;
float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
float3 diffuse = _LightColor0.rgb*albedo*saturate(dot(worldNormal, worldLight));
float3 specular = _LightColor0.rgb*_Specular.rgb*pow(saturate(dot(worldNormal, worldHalf)), _Shininess);
float3 color = ambient + diffuse + specular;
return float4(color, 1.0);
}
v2f vert_base(appdata_base v) {
v2f o;
//每个pass延法线挤出顶点
float3 P = v.vertex.xyz + v.normal*_FurLength*FURSTEP;
o.pos = UnityObjectToClipPos(float4(P, 1.0));
o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv.zw = TRANSFORM_TEX(v.texcoord, _FurTex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
return o;
}
float4 frag_base(v2f i) :SV_Target{
float3 worldNormal = normalize(i.worldNormal);
float3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
float3 worldView = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
float3 worldHalf = normalize(worldView + worldLight);
float3 albedo = tex2D(_MainTex, i.uv.xy).rgb*_Color;
//阴影:靠近根部颜色越暗
albedo -= (pow(1 - FURSTEP, 3)) * _FurShading;
//边缘光:边缘毛发可能受油光影响
float rim = 1.0 - saturate(dot(worldView, worldNormal));
albedo += float3(1, 1, 1)*pow(rim, _RimPower);
float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
float3 diffuse = _LightColor0.rgb*albedo*saturate(dot(worldNormal, worldLight));
float3 specular = _LightColor0.rgb*_Specular.rgb*pow(saturate(dot(worldNormal, worldHalf)), _Shininess);
float3 color = ambient + diffuse + specular;
//修改tile改变毛发细度
float3 noise = tex2D(_FurTex, i.uv.zw*_FurThinness).rgb;
//越到尾部毛发越细越少
float alpha = clamp(noise - (FURSTEP*FURSTEP)*_FurDensity, 0, 1);
return float4(color, alpha);
}
shader部分:
Shader "Unlit/Fur"
{
Properties
{
_Color("Color",Color)=(1,1,1,1)
_Specular("Specular",Color)=(1,1,1,1)
_Shininess("Shininess",Range(0,20))=0.0
_MainTex("MainTex",2D)="white"{}
_FurTex("FurTex",2D)="white"{}
_FurLength("FurLength",float)=0.0
_FurDensity("FurDensity",Range(0,2))=0.11
_FurThinness("FurThinness",Range(0.01,10))=1
_FurShading("FurShading",Range(0.0,1))=0.25
_RimPower("RimPower",Range(0,20))=1
}
SubShader{
Tags {"RenderType" = "Transparent" "Queue" = "Transparent"}
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGPROGRAM
#define FURSTEP 0.00
#pragma vertex vert_surface
#pragma fragment frag_surface
#include "FurCGInclude.cginc"
ENDCG
}
Tags {"RenderType" = "Transparent" "Queue" = "Transparent"}
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGPROGRAM
#define FURSTEP 0.05
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags {"RenderType" = "Transparent" "Queue" = "Transparent"}
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGPROGRAM
#define FURSTEP 0.10
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags {"RenderType" = "Transparent" "Queue" = "Transparent"}
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGPROGRAM
#define FURSTEP 0.15
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags {"RenderType" = "Transparent" "Queue" = "Transparent"}
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGPROGRAM
#define FURSTEP 0.20
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags {"RenderType" = "Transparent" "Queue" = "Transparent"}
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGPROGRAM
#define FURSTEP 0.25
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags {"RenderType" = "Transparent" "Queue" = "Transparent"}
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGPROGRAM
#define FURSTEP 0.30
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags {"RenderType" = "Transparent" "Queue" = "Transparent"}
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGPROGRAM
#define FURSTEP 0.35
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#define FURSTEP 0.40
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#define FURSTEP 0.45
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#define FURSTEP 0.50
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#define FURSTEP 0.55
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#define FURSTEP 0.60
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#define FURSTEP 0.65
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#define FURSTEP 0.70
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#define FURSTEP 0.75
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#define FURSTEP 0.80
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#define FURSTEP 0.85
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#define FURSTEP 0.90
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#define FURSTEP 0.95
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#define FURSTEP 0.1
#pragma vertex vert_base
#pragma fragment frag_base
#include "FurCGInclude.cginc"
ENDCG
}
}
}