冯乐乐的《Unity Shader入门精要》附带项目,其中部分shader已经修改测试
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

100 lines
2.8 KiB

Shader "Unlit/ToonShaderHouse"
{
Properties {
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_MainTex ("Texture", 2D) = "white" {}
_LuminousTex ("Luminous Tex", 2D) = "white" {}
_LuminousColor("Luminous Color",Color) = (1,1,1,1)
_Specular ("Specular", Color) = (1, 1, 1, 1)
_Gloss ("Gloss", Range(8.0, 256)) = 20
_LightEdge ("Light Edge",Float) = 1.0
_LightEdgeColor("Light Edge Color",Color) = (1,1,1,1)
}
SubShader {
Pass {
Tags { "LightMode"="ForwardBase" }
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "AutoLight.cginc"
fixed4 _Color;
fixed4 _Specular;
float _Gloss;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _LuminousTex;
float4 _LuminousTex_ST;
fixed4 _LuminousColor;
float _LightEdge;
fixed4 _LightEdgeColor;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
float2 uv : TEXCOORD1;
};
struct v2f {
float4 pos : SV_POSITION;
float3 worldNormal : TEXCOORD0;
float3 worldPos : TEXCOORD1;
float2 lumUV : TEXCOORD2;
float2 tex : TEXCOORD3;
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.lumUV = TRANSFORM_TEX(v.uv, _LuminousTex);
o.tex = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed4 col = tex2D(_MainTex, i.tex);
fixed4 lum = tex2D(_LuminousTex, i.lumUV);
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
//fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 ambient = ShadeSH9(fixed4(i.worldNormal,1));
// Use the texture to sample the diffuse color
fixed halfLambert = 0.5 * dot(worldNormal, worldLightDir) + 0.5;
//fixed3 diffuse = _LightColor0.rgb * diffuseColor;
fixed3 diffuse = saturate(dot(worldNormal,worldLightDir));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
fixed3 halfDir = normalize(worldLightDir + viewDir);
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(worldNormal, halfDir)), _Gloss);
fixed3 diffuseEdge = ((clamp(diffuse,0,0.5)-0.5) * (floor(diffuse*2)-1)* -1+(floor(diffuse*2)-1)* -1*0.5) *_LightEdgeColor.rgb;
diffuse = (fixed3(floor(diffuse*2)*0.5)+diffuseEdge) * _LightColor0.rgb;
specular = fixed3(floor(specular*2)*0.5);
fixed4 finalColor = fixed4(ambient + diffuse + specular, 1.0)*_LightEdge;
//fixed4 finalColor = fixed4(ambient + diffuse, 1.0);
//finalColor = fixed4 (0.0,0.0,0.0,1.0);
return finalColor*col+lum*_LuminousColor;
}
ENDCG
}
}
FallBack "Specular"
}