1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
| Shader "Unlit/CelShader" { Properties { _MainTex ("Texture", 2D) = "white" {} _MainColor ("Main Color", Color) = (1,1,1,1)
[HDR] _SpecularColor("Specular Color", Color) = (1,1,1,1) _Glossiness("Glossiness", Range(8,255)) = 32
[Toggle(_NORMALMAP)] _EnableNormalMap("Enable Normal Map", Int) = 0 [NoScaleOffset]_BumpMap("Normal Map", 2D) = "bump" {} _BumpScale("Bump Scale", Float) = 1.0
[HDR] _RimColor("Rim Color", Color) = (1,1,1,1)
_RimAmount("Rim Amount", Range(0, 1)) = 0.716 _RimThreshold("Rim Threshold", Range(0, 1)) = 0.1
_OutLine("OutLine",Float) = 1 _OutLineColor("Color",Color) = (1,1,1,1)
[Toggle(_CLIP)] _EnableClip("Enable Clip", Int) = 0 _ClipLimit("Clip Limit",Range(0,1)) = 0.2 } SubShader { Tags { "RenderType"="Opaque" } LOD 100
HLSLINCLUDE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" #include "Packages/com.unity.render-pipelines.universal/Shaders/ShadowCasterPass.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl"
struct appdata { float4 vertex : POSITION; float4 normal : NORMAL; float4 tangent : TANGENT; float2 uv : TEXCOORD0; };
struct v2f { float4 vertex : SV_POSITION; float2 uv : TEXCOORD0; };
struct celV2F { float4 vertex : SV_POSITION; float2 uv : TEXCOORD0; float4 positionWS : TEXCOORD1; float4 tangentWS : TEXCOORD2; float4 normalWS : TEXCOORD3; };
CBUFFER_START(UnityPerMaterial) sampler2D _MainTex; float4 _MainTex_ST;
half4 _MainColor;
float4 _SpecularColor; float _Glossiness;
sampler2D _BumpMap; float _BumpScale;
float4 _RimColor; half _RimAmount; half _RimThreshold;
float _OutLine; float4 _OutLineColor;
half _ClipLimit; CBUFFER_END
v2f outLineVert (appdata v) { v2f o; float3 worldPos = TransformObjectToWorld(v.vertex.xyz); float3 viewPos = TransformWorldToView(worldPos); float3 viewNormal = TransformWorldToViewNormal(TransformObjectToWorldNormal(v.normal.xyz)); viewNormal.z = -0.5; worldPos = TransformViewToWorld(viewPos + normalize(viewNormal) * _OutLine); o.vertex = TransformWorldToHClip(worldPos); o.uv = v.uv; return o; }
half4 outLineFrag (v2f i) : SV_Target { half4 col = tex2D(_MainTex, i.uv); #ifdef _CLIP clip(col.a - _ClipLimit); #endif return _OutLineColor; }
v2f vert (appdata v) { v2f o; o.vertex = TransformObjectToHClip(v.vertex.xyz); o.uv = TRANSFORM_TEX(v.uv, _MainTex); return o; }
half4 frag (v2f i) : SV_Target { // sample the texture half4 col = tex2D(_MainTex, i.uv); return col; }
half4 shadow(v2f i) : SV_Target { return 0; }
celV2F celVert(appdata v) { celV2F o; o.vertex = TransformObjectToHClip(v.vertex.xyz); o.uv = TRANSFORM_TEX(v.uv, _MainTex); VertexNormalInputs nInput = GetVertexNormalInputs(v.normal, v.tangent); o.positionWS.xyz = TransformObjectToWorld(v.vertex.xyz); o.positionWS.w = 1; real sign = v.tangent.w * GetOddNegativeScale(); o.tangentWS = half4(nInput.tangentWS,sign); o.normalWS.xyz = nInput.normalWS; return o; }
half4 celFrag(celV2F i) : SV_TARGET { half4 col = tex2D(_MainTex, i.uv); #ifdef _CLIP clip(col.a - _ClipLimit); #endif
float3 normal;
#ifdef _NORMALMAP half4 normalCol = tex2D(_BumpMap,i.uv); #if BUMP_SCALE_NOT_SUPPORTED normal = UnpackNormal(normalCol); #else normal = UnpackNormalScale(normalCol, _BumpScale); #endif float3 bitangent = i.tangentWS.w * cross(i.normalWS.xyz, i.tangentWS.xyz); half3x3 tangentToWorld = half3x3(i.tangentWS.xyz, bitangent.xyz, i.normalWS.xyz); normal = TransformTangentToWorld(normal, tangentToWorld); #else normal = normalize(i.normalWS).xyz; #endif
Light mainLight = GetMainLight(TransformWorldToShadowCoord(i.positionWS));
float NdotL = saturate(dot(mainLight.direction, normal)) * 0.5 + 0.5;
float shadow = smoothstep(0,0.1,mainLight.shadowAttenuation * mainLight.distanceAttenuation);
// 漫反射 //half4 diffuseCol = col * smoothstep(0,1,NdotL) * _MainColor; half4 diffuseCol = col * smoothstep(0.3,0.5,NdotL) * _MainColor; diffuseCol.a = 1;
// 高光 float3 viewDir = GetWorldSpaceNormalizeViewDir(i.positionWS.xyz); float3 halfVector = normalize(mainLight.direction + viewDir); float NdotH = dot(normal, halfVector); float specularIntensity = pow(NdotH, _Glossiness); float specularIntensitySmooth = smoothstep(0.005, 0.01, specularIntensity); float3 specular = specularIntensitySmooth * _SpecularColor.rgb;
// 菲涅尔(边缘光) float rimDot = 1 - saturate(dot(viewDir, normal)); float rimIntensity = rimDot * pow(NdotL, _RimThreshold); rimIntensity = smoothstep(_RimAmount - 0.01, _RimAmount + 0.01, rimIntensity); float3 rim = rimIntensity * _RimColor.rgb;
diffuseCol.rgb = (diffuseCol.rgb + specular);
diffuseCol.rgb *= saturate(mainLight.color.rgb); diffuseCol.rgb *= smoothstep(0,0.2,mainLight.shadowAttenuation); diffuseCol.rgb += rim;
return diffuseCol; }
ENDHLSL
Pass { Tags{"LightMode"="OutLine"}
Cull Front HLSLPROGRAM #pragma vertex outLineVert #pragma fragment outLineFrag
#pragma shader_feature_local _CLIP
ENDHLSL }
Pass { Tags{"LightMode"="UniversalForward"}
Blend SrcAlpha OneMinusSrcAlpha
HLSLPROGRAM #pragma vertex celVert #pragma fragment celFrag
#pragma shader_feature_local _NORMALMAP #pragma shader_feature_local _CLIP
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE
#pragma multi_compile_fragment _ _SHADOWS_SOFT #pragma multi_compile_instancing
ENDHLSL }
pass { Tags{"LightMode"="ShadowCaster"}
ZWrite On ZTest LEqual ColorMask 0
HLSLPROGRAM #pragma vertex vert #pragma fragment shadow #pragma multi_compile_fragment _ LOD_FADE_CROSSFADE #pragma multi_compile_vertex _ _CASTING_PUNCTUAL_LIGHT_SHADOW
ENDHLSL } } }
|