Streamingle_URP/Assets/NiloToonURP/Shaders/NiloToonCharacterFur.shader
2025-12-15 01:52:59 +09:00

567 lines
24 KiB
Plaintext

// NiloToon Character Fur Shader
// Single-material fur shader with Base + Fur Shell rendering
// Requires NiloToonFurRendererFeature to be added to URP Renderer
//
// Usage:
// 1. Add NiloToonFurRendererFeature to your URP Renderer Asset
// 2. Apply this shader to your mesh material (single material slot)
Shader "Universal Render Pipeline/NiloToon/NiloToon_Character_Fur"
{
Properties
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Base Color
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[Header(Base Color)]
[Space(5)]
[MainTexture] _BaseMap("Base Map", 2D) = "white" {}
[HDR][MainColor] _BaseColor("Base Color", Color) = (1,1,1,1)
_Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Normal Map
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[Header(Normal Map)]
[Space(5)]
[Toggle(_NORMALMAP)] _UseNormalMap("Enable Normal Map", Float) = 0
[Normal] _BumpMap("Normal Map", 2D) = "bump" {}
_BumpScale("Normal Scale", Range(0, 2)) = 1.0
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Fur Settings
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[Header(Fur Shape)]
[Space(5)]
_FurNoiseMask("Fur Noise Mask", 2D) = "white" {}
_FurMask("Fur Mask (where fur appears)", 2D) = "white" {}
_FurLengthMask("Fur Length Mask", 2D) = "white" {}
_FurVector("Fur Direction (XYZ) + Length (W)", Vector) = (0, 0, 1, 0.02)
[Normal] _FurVectorTex("Fur Direction Map (Normal)", 2D) = "bump" {}
_FurVectorScale("Fur Direction Scale", Range(-10, 10)) = 1.0
_FurGravity("Fur Gravity", Range(0, 1)) = 0.25
_FurRandomize("Fur Randomize", Range(0, 1)) = 0.1
_FurAO("Fur Ambient Occlusion", Range(0, 1)) = 0.5
[Header(Shell Layers)]
[Space(5)]
[IntRange] _FurLayerNum("Fur Layer Count", Range(1, 3)) = 2
_FurRootOffset("Fur Root Offset", Range(-1, 0)) = 0
[Header(Fur Rim Light)]
[Space(5)]
[HDR] _FurRimColor("Fur Rim Color", Color) = (1, 1, 1, 1)
_FurRimFresnelPower("Fur Rim Fresnel Power", Range(0.01, 50)) = 3.0
_FurRimAntiLight("Fur Rim Anti-Light", Range(0, 1)) = 0.5
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Toon Shading (NiloToon Style)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[Header(Cel Shading)]
[Space(5)]
_CelShadeMidPoint("Cel Shade Mid Point", Range(-1, 1)) = 0
_CelShadeSoftness("Cel Shade Softness", Range(0, 1)) = 0.1
[Header(Shadow Color)]
[Space(5)]
[Toggle(_SHADOW_COLOR)] _EnableShadowColor("Enable Shadow Color", Float) = 0
[HDR] _ShadowColor("Shadow Tint Color", Color) = (1, 1, 1, 1)
_ShadowBrightness("Shadow Brightness", Range(0, 2)) = 1.0
[Header(HSV Adjustment in Shadow)]
[Space(5)]
_ShadowHueShift("Shadow Hue Shift", Range(-0.5, 0.5)) = 0
_ShadowSaturationBoost("Shadow Saturation Boost", Range(0, 2)) = 1.0
_ShadowValueMultiplier("Shadow Value Multiplier", Range(0, 2)) = 1.0
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MatCap
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[Header(MatCap Additive)]
[Space(5)]
[Toggle(_MATCAP_ADD)] _UseMatCapAdd("Enable MatCap (Add)", Float) = 0
_MatCapAddMap("MatCap Add Map", 2D) = "black" {}
[HDR] _MatCapAddColor("MatCap Add Color", Color) = (1, 1, 1, 1)
_MatCapAddIntensity("MatCap Add Intensity", Range(0, 5)) = 1.0
_MatCapAddMask("MatCap Add Mask", 2D) = "white" {}
[Header(MatCap Multiply)]
[Space(5)]
[Toggle(_MATCAP_MUL)] _UseMatCapMul("Enable MatCap (Multiply)", Float) = 0
_MatCapMulMap("MatCap Multiply Map", 2D) = "white" {}
_MatCapMulIntensity("MatCap Multiply Intensity", Range(0, 2)) = 1.0
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rim Light (General)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[Header(Rim Light)]
[Space(5)]
[Toggle(_RIMLIGHT)] _UseRimLight("Enable Rim Light", Float) = 0
[HDR] _RimLightColor("Rim Light Color", Color) = (1, 1, 1, 1)
_RimLightPower("Rim Light Power", Range(0.01, 20)) = 5.0
_RimLightIntensity("Rim Light Intensity", Range(0, 5)) = 1.0
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Emission
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[Header(Emission)]
[Space(5)]
[Toggle(_EMISSION)] _UseEmission("Enable Emission", Float) = 0
_EmissionMap("Emission Map", 2D) = "white" {}
[HDR] _EmissionColor("Emission Color", Color) = (0, 0, 0, 1)
_EmissionIntensity("Emission Intensity", Range(0, 10)) = 1.0
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Occlusion
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[Header(Occlusion)]
[Space(5)]
[Toggle(_OCCLUSIONMAP)] _UseOcclusion("Enable Occlusion Map", Float) = 0
_OcclusionMap("Occlusion Map", 2D) = "white" {}
_OcclusionStrength("Occlusion Strength", Range(0, 1)) = 1.0
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Outline
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[Header(Outline)]
[Space(5)]
_OutlineWidth("Outline Width", Range(0, 10)) = 1.0
[HDR] _OutlineColor("Outline Color", Color) = (0, 0, 0, 1)
_OutlineWidthMask("Outline Width Mask", 2D) = "white" {}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rendering Options
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[Header(Rendering Options)]
[Space(5)]
[Enum(UnityEngine.Rendering.CullMode)] _Cull("Cull Mode", Float) = 2
[Enum(UnityEngine.Rendering.CullMode)] _FurCull("Fur Cull Mode", Float) = 0
[Enum(Off, 0, On, 1)] _ZWrite("ZWrite", Float) = 1
_FurZWrite("Fur ZWrite", Float) = 0
[Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 4
// Hidden
[HideInInspector] _Surface("__surface", Float) = 0.0
[HideInInspector] _Blend("__blend", Float) = 0.0
[HideInInspector] _QueueOffset("Queue offset", Float) = 0.0
}
SubShader
{
Tags
{
"RenderType" = "Opaque"
"RenderPipeline" = "UniversalPipeline"
"UniversalMaterialType" = "Lit"
"IgnoreProjector" = "True"
"Queue" = "Geometry"
}
LOD 300
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pass 0: ForwardLit - Base Surface (Full NiloToon Style)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Pass
{
Name "ForwardLit"
Tags { "LightMode" = "UniversalForward" }
Cull [_Cull]
ZWrite On
ZTest LEqual
Blend One Zero
HLSLPROGRAM
#pragma target 4.5
// Shader Features
#pragma shader_feature_local _NORMALMAP
#pragma shader_feature_local _SHADOW_COLOR
#pragma shader_feature_local _MATCAP_ADD
#pragma shader_feature_local _MATCAP_MUL
#pragma shader_feature_local _RIMLIGHT
#pragma shader_feature_local _EMISSION
#pragma shader_feature_local _OCCLUSIONMAP
// URP Keywords
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN
#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
#pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
#pragma multi_compile_fragment _ _SHADOWS_SOFT
#pragma multi_compile_fog
// GPU Instancing
#pragma multi_compile_instancing
#pragma instancing_options renderinglayer
#pragma vertex vert
#pragma fragment frag
#define NILOTOON_FUR_BASE_PASS
#include "NiloToonCharacterFur_HLSL/NiloToonCharacterFur_Shared.hlsl"
#include "NiloToonCharacterFur_HLSL/NiloToonCharacterFur_Fragment.hlsl"
ENDHLSL
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pass 1: Fur Shell Pass (Geometry Shader) - Standard single target
// Rendered by NiloToonFurRendererFeature via "NiloToonFurShell" LightMode
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Pass
{
Name "FurShell"
Tags { "LightMode" = "NiloToonFurShell" }
Cull [_FurCull]
ZWrite [_FurZWrite]
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
HLSLPROGRAM
#pragma target 4.5
#pragma require geometry
// Shader Features
#pragma shader_feature_local _NORMALMAP
#pragma shader_feature_local _SHADOW_COLOR
#pragma shader_feature_local _MATCAP_ADD
#pragma shader_feature_local _MATCAP_MUL
#pragma shader_feature_local _RIMLIGHT
#pragma shader_feature_local _EMISSION
#pragma shader_feature_local _OCCLUSIONMAP
// URP Keywords
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN
#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
#pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
#pragma multi_compile_fragment _ _SHADOWS_SOFT
#pragma multi_compile_fog
// GPU Instancing
#pragma multi_compile_instancing
#pragma instancing_options renderinglayer
#pragma vertex vert_fur
#pragma geometry geom_fur
#pragma fragment frag_fur
#define NILOTOON_FUR_SHELL_PASS
#include "NiloToonCharacterFur_HLSL/NiloToonCharacterFur_Shared.hlsl"
#include "NiloToonCharacterFur_HLSL/NiloToonCharacterFur_Geometry.hlsl"
#include "NiloToonCharacterFur_HLSL/NiloToonCharacterFur_Fragment.hlsl"
ENDHLSL
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pass 1.5: Fur Shell Mask Pass (Geometry Shader)
// Renders fur shells to mask buffer only (white where fur is rendered)
// Used for fur area detection in post-processing
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Pass
{
Name "FurShellMask"
Tags { "LightMode" = "NiloToonFurShellMask" }
Cull [_FurCull]
ZWrite Off
ZTest Always // Always pass - we want to mark ALL fur pixels regardless of depth
Blend One Zero // Just overwrite
HLSLPROGRAM
#pragma target 4.5
#pragma require geometry
// GPU Instancing
#pragma multi_compile_instancing
#pragma instancing_options renderinglayer
#pragma vertex vert_fur
#pragma geometry geom_fur
#pragma fragment frag_fur_mask
#define NILOTOON_FUR_SHELL_PASS
#define NILOTOON_FUR_MASK_PASS
#include "NiloToonCharacterFur_HLSL/NiloToonCharacterFur_Shared.hlsl"
#include "NiloToonCharacterFur_HLSL/NiloToonCharacterFur_Geometry.hlsl"
#include "NiloToonCharacterFur_HLSL/NiloToonCharacterFur_Fragment.hlsl"
ENDHLSL
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pass 2: Outline (NiloToon Style)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Pass
{
Name "Outline"
Tags { "LightMode" = "NiloToonOutline" }
Cull Front
ZWrite [_ZWrite]
ZTest [_ZTest]
HLSLPROGRAM
#pragma target 4.5
#pragma multi_compile_instancing
#pragma instancing_options renderinglayer
#pragma multi_compile_fog
#pragma vertex vert_outline
#pragma fragment frag_outline
#define NILOTOON_FUR_OUTLINE_PASS
#include "NiloToonCharacterFur_HLSL/NiloToonCharacterFur_Shared.hlsl"
Varyings vert_outline(Attributes input)
{
Varyings output = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_TRANSFER_INSTANCE_ID(input, output);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
float outlineWidth = _OutlineWidth * 0.001;
float outlineMask = SAMPLE_TEXTURE2D_LOD(_OutlineWidthMask, sampler_OutlineWidthMask, input.uv, 0).r;
outlineWidth *= outlineMask;
// FOV-aware outline width
float fovFactor = unity_OrthoParams.w > 0.5 ? 1.0 : (2.0 * abs(UNITY_MATRIX_P[1][1]));
outlineWidth *= fovFactor;
float3 posOS = input.positionOS.xyz + input.normalOS * outlineWidth;
output.positionCS = TransformObjectToHClip(posOS);
output.uv = input.uv;
output.fogFactor = ComputeFogFactor(output.positionCS.z);
return output;
}
half4 frag_outline(Varyings input) : SV_Target
{
half4 color = _OutlineColor;
color.rgb = MixFog(color.rgb, input.fogFactor);
return color;
}
ENDHLSL
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pass 3: ShadowCaster
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Pass
{
Name "ShadowCaster"
Tags { "LightMode" = "ShadowCaster" }
ZWrite On
ZTest LEqual
ColorMask 0
Cull [_Cull]
HLSLPROGRAM
#pragma target 4.5
#pragma multi_compile_instancing
#pragma instancing_options renderinglayer
#pragma vertex ShadowPassVertex
#pragma fragment ShadowPassFragment
#define NILOTOON_FUR_SHADOW_PASS
#include "NiloToonCharacterFur_HLSL/NiloToonCharacterFur_Shared.hlsl"
float3 _LightDirection;
float3 _LightPosition;
float4 GetShadowPositionHClip(Attributes input)
{
float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
#if _CASTING_PUNCTUAL_LIGHT_SHADOW
float3 lightDirectionWS = normalize(_LightPosition - positionWS);
#else
float3 lightDirectionWS = _LightDirection;
#endif
float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS));
#if UNITY_REVERSED_Z
positionCS.z = min(positionCS.z, UNITY_NEAR_CLIP_VALUE);
#else
positionCS.z = max(positionCS.z, UNITY_NEAR_CLIP_VALUE);
#endif
return positionCS;
}
Varyings ShadowPassVertex(Attributes input)
{
Varyings output = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_TRANSFER_INSTANCE_ID(input, output);
output.uv = input.uv;
output.positionCS = GetShadowPositionHClip(input);
return output;
}
half4 ShadowPassFragment(Varyings input) : SV_TARGET
{
half alpha = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv).a * _BaseColor.a;
clip(alpha - _Cutoff);
return 0;
}
ENDHLSL
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pass 4: DepthOnly
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Pass
{
Name "DepthOnly"
Tags { "LightMode" = "DepthOnly" }
ZWrite On
ColorMask R
Cull [_Cull]
HLSLPROGRAM
#pragma target 4.5
#pragma multi_compile_instancing
#pragma instancing_options renderinglayer
#pragma vertex DepthOnlyVertex
#pragma fragment DepthOnlyFragment
#define NILOTOON_FUR_DEPTH_PASS
#include "NiloToonCharacterFur_HLSL/NiloToonCharacterFur_Shared.hlsl"
Varyings DepthOnlyVertex(Attributes input)
{
Varyings output = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_TRANSFER_INSTANCE_ID(input, output);
output.uv = input.uv;
output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
return output;
}
half DepthOnlyFragment(Varyings input) : SV_TARGET
{
half alpha = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv).a * _BaseColor.a;
clip(alpha - _Cutoff);
return input.positionCS.z;
}
ENDHLSL
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pass 5: DepthNormals
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Pass
{
Name "DepthNormals"
Tags { "LightMode" = "DepthNormals" }
ZWrite On
Cull [_Cull]
HLSLPROGRAM
#pragma target 4.5
#pragma multi_compile_instancing
#pragma instancing_options renderinglayer
#pragma vertex DepthNormalsVertex
#pragma fragment DepthNormalsFragment
#define NILOTOON_FUR_DEPTHNORMALS_PASS
#include "NiloToonCharacterFur_HLSL/NiloToonCharacterFur_Shared.hlsl"
Varyings DepthNormalsVertex(Attributes input)
{
Varyings output = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_TRANSFER_INSTANCE_ID(input, output);
output.uv = input.uv;
output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
output.normalWS = TransformObjectToWorldNormal(input.normalOS);
return output;
}
float4 DepthNormalsFragment(Varyings input) : SV_TARGET
{
half alpha = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv).a * _BaseColor.a;
clip(alpha - _Cutoff);
return float4(normalize(input.normalWS) * 0.5 + 0.5, 0);
}
ENDHLSL
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pass 6: NiloToonPrepassBuffer (Base mesh only)
// For NiloToon Bloom / Tonemapping character area detection - base mesh
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Pass
{
Name "NiloToonPrepassBuffer"
Tags { "LightMode" = "NiloToonPrepassBuffer" }
ZWrite On
ZTest LEqual
Cull [_Cull]
HLSLPROGRAM
#pragma target 4.5
#pragma multi_compile_instancing
#pragma instancing_options renderinglayer
#pragma vertex PrepassBufferVertexBase
#pragma fragment PrepassBufferFragmentBase
#define NILOTOON_FUR_PREPASSBUFFER_PASS
#include "NiloToonCharacterFur_HLSL/NiloToonCharacterFur_Shared.hlsl"
Varyings PrepassBufferVertexBase(Attributes input)
{
Varyings output = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_TRANSFER_INSTANCE_ID(input, output);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
output.uv = input.uv;
output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
output.furLayer = -1; // Base mesh marker
return output;
}
float4 PrepassBufferFragmentBase(Varyings input) : SV_TARGET
{
UNITY_SETUP_INSTANCE_ID(input);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
// Base mesh alpha clip
half alpha = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv).a * _BaseColor.a;
clip(alpha - _Cutoff);
// Output: character visible area = 1
return float4(0, 1, 0, 1);
}
ENDHLSL
}
// Note: Fur shell prepass is handled via MRT in the FurShellMRT pass
// When WriteToPrepassBuffer is enabled, FurShellMRT outputs to both:
// - SV_Target0: Main color buffer
// - SV_Target1: PrepassBuffer (character mask with jagged fur silhouette)
}
FallBack "Universal Render Pipeline/Lit"
CustomEditor "NiloToonURP.ShaderGUI.NiloToonCharacterFurShaderGUI"
}