Skip to content

Instantly share code, notes, and snippets.

@leegoonz
Created January 12, 2023 14:56
Show Gist options
  • Save leegoonz/d23e4356341e6af57e38692b62784123 to your computer and use it in GitHub Desktop.
Save leegoonz/d23e4356341e6af57e38692b62784123 to your computer and use it in GitHub Desktop.
Shader "Skin Clip Alpha" {
Properties {
_MainTex ("Albedo(RGB) , Roughness(A)", 2D) = "white" {}
[HDR]_Color ("BaseColor", Color) = (1,1,1,1)
[HideInInspector]_Specular ("Specular", Range(0, 1)) = 0.28
_Roughness ("Roughness", Range(0, 1)) = 0.5
[NoScaleOffset]_SurfaceTangentMap ("Normal Map", 2D) = "bump" {}
_OcclusionStrength ("Occlusion", Range(0, 1)) = 0.8
_Reflection ("Reflection", Range(0, 5)) = 1
[Header(SKIN BRDF)]
[MaterialToggle(_USESKINBRDF)] _UseSKinBRDF("UseSKinBRDF", Float) = 1
[MaterialToggle(_USEBRDFATTEN)] _UseBrdfAtten("Use Skin Brdf Attenuation", Float) = 1
_AttenuationScale("BRDF Attenuation" , Range(0.25,1)) = 0.25
[NoScaleOffset]_SkinBRDF ("Skin BRDF", 2D) = "black" {}
_SkinToneScale ("Skin Tone Scale", Range(0, 1)) = 0.3
_SkinTransColor("Skin Trans Color", Color) = (0,0,0,1)
[NoScaleOffset]_SkinTransMap ("Skin Trans Map(R,G,B) Alpha(A)", 2D) = "white" {}
_BackLightDir("BackLight Dir", Vector) = (0,0,-1,0)
[Header(SubLighting)]
[MaterialToggle(_USESUBLIGHT)] _UseSubLight("Use Sub lighting",Float) = 0
_SubLightScale("Lighting Intensity", Range(1.0, 1.4)) = 1.25
[Header(TONE MAPPER)]//// Neutral tonemapping (Hable/Hejl/Frostbite)
[Toggle] _toggleToneMapper("Tone Mapper" , FLOAT) = 0
_a("Segment A" , Range(0.1, 1)) = 0.2
_b("Segment B" , Range(0.1, 1)) = 0.29
_c("Segment C" , Range(0.1, 1)) = 0.24
_d("Segment D" , Range(0.1, 1)) = 0.272
_e("Segment E" , Range(0.01, 0.1)) = 0.02
_f("Segment F" , Range(0.1, 1)) = 0.3
_WhiteLevel("ToneMappe White Level" , Range(1.3, 5.3)) = 2.3
[Space(5)]
[Header(Alpha)]
_Cutoff("Alpha Cotoff", Range( 0 , 1)) = 1
}
SubShader {
Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
Cull Back
ColorMask RGBA
Pass {
Name "FORWARD"
Tags {
"LightMode"="ForwardBase"
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define FLOAT half
#define FLOAT2 half2
#define FLOAT3 half3
#define FLOAT4 half4
#define BRDF_TEX 1
#define NONMETAL 1
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "Lighting.cginc"
#pragma multi_compile_fwdbase_fullshadows
#pragma multi_compile_fog
#pragma shader_feature _USESKINBRDF
#pragma shader_feature _USESUBLIGHT
#pragma shader_feature _USEBRDFATTEN
#pragma target 3.0
#pragma multi_compile _ DYNAMIC_POINT_LIGHTS_4
#pragma multi_compile_fwdbase
#include "./Common/JPStandardCoreForward.cginc"
half3 StandardBase( half3 Albedo , half Metallic , half Specular , half Roughness , half3 Emissive , half Occlusion , half Reflection , half3 Normal , float3 WorldPos , half3 ViewDir , half3 LightDir , half3 LightColor , half LightAtten , half3 Ambient , half3 Reflect , float2 LightmapUV , half3 BRDF , half3 BleedAO , half Alpha )
{
FragmentCommonData s;
s.position = WorldPos;
s.viewdir = ViewDir;
s.ambient = Ambient + BleedAO;
s.albedo = JpGammaToLinearSpace(Albedo);
s.alpha = Alpha;
s.metallic = JpGammaToLinearSpace(saturate(Metallic));
s.roughness = JpLinearToGammaSpace(saturate(Roughness));
s.occlusion = JpGammaToLinearSpace(Occlusion);
s.specular = Specular;
s.emissive = JpGammaToLinearSpace(Emissive);
s.normal = Normal;
s.irradiance = Reflection;
s.reflect = Reflect;
s.lightdir = LightDir;
s.lightcolor = LightColor;
s.shadow = LightAtten;
s.lightmapUV.xy = LightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;
#if BRDF_TEX
s.brdf = BRDF;
#endif
return fragForwardBaseInternal(s);
}
uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
uniform sampler2D _SurfaceTangentMap;
uniform half _Roughness;
uniform half _Specular;
uniform half _OcclusionStrength;
uniform half _Reflection;
uniform half4 _Color;
uniform sampler2D _SkinBRDF;
uniform half _SkinToneScale;
uniform half _AttenuationScale;
uniform half _toggleToneMapper;
uniform half _WhiteLevel;
uniform half _a;
uniform half _b;
uniform half _c;
uniform half _d;
uniform half _e;
uniform half _f;
uniform half _Cutoff;
#if _USESKINBRDF
static bool _UseSKinBRDF = true;
#else
static bool _UseSKinBRDF = false;
#endif
#if _USEBRDFATTEN
static bool _UseBrdfAtten = true;
#else
static bool _UseBrdfAtten = false;
#endif
uniform sampler2D _RoughnessMap;
uniform sampler2D _OcclusionMap;
uniform sampler2D _SkinTransMap;
uniform half4 _SkinTransColor;
uniform half4 _BackLightDir;
struct VertexInput {
float4 vertex : POSITION;
float4 tangent : TANGENT;
float3 normal : NORMAL;
float2 texcoord0 : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float2 uv1 : TEXCOORD1;
float4 posWorld : TEXCOORD2;
FLOAT3 normalDir : TEXCOORD3;
FLOAT3 tangentDir : TEXCOORD4;
FLOAT3 bitangentDir : TEXCOORD5;
LIGHTING_COORDS(6,7)
UNITY_FOG_COORDS(8)
#if defined(LIGHTMAP_ON) || defined(UNITY_SHOULD_SAMPLE_SH)
FLOAT4 ambientOrLightmapUV : TEXCOORD9;
#endif
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.uv1 = v.texcoord1;
o.normalDir = UnityObjectToWorldNormal(v.normal);
o.tangentDir = normalize( mul( unity_ObjectToWorld, FLOAT4( v.tangent.xyz, 0.0 ) ).xyz );
o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir)) * (v.tangent.w * unity_WorldTransformParams.w);
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
float3 lightColor = _LightColor0.rgb;
#if UNITY_SHOULD_SAMPLE_SH
FLOAT4 normalWorld = FLOAT4(o.normalDir, 1);
float4 posWorld = o.posWorld;
#ifdef VERTEXLIGHT_ON
// Approximated illumination from non-important point lights
o.ambientOrLightmapUV.rgb = Shade4PointLights(
unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,
unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb,
unity_4LightAtten0, posWorld, normalWorld.xyz);
#endif
#endif
o.pos = UnityObjectToClipPos(v.vertex );
UNITY_TRANSFER_FOG(o,o.pos);
TRANSFER_VERTEX_TO_FRAGMENT(o)
return o;
}
// Neutral tonemapping (Hable/Hejl/Frostbite)
FLOAT3 NeutralCurve(FLOAT3 x, FLOAT a, FLOAT b, FLOAT c, FLOAT d, FLOAT e, FLOAT f)
{
return ((x * (a * x + c * b) + d * e) / (x * (a * x + b) + d * f)) - e / f;
}
FLOAT3 NeutralTonemap(FLOAT3 x)
{
// Tonemap
const FLOAT a = _a;
const FLOAT b = _b;
const FLOAT c = _c;
const FLOAT d = _d;
const FLOAT e = _e;
const FLOAT f = _f;
const FLOAT whiteLevel = _WhiteLevel;
const FLOAT whiteClip = 1;
half3 whiteScale = (1.0).xxx / NeutralCurve(whiteLevel, a, b, c, d, e, f);
x = NeutralCurve(x * whiteScale, a, b, c, d, e, f);
x *= whiteScale;
// Post-curve white point adjustment
x /= whiteClip.xxx;
return x;
}
FLOAT4 frag(VertexOutput i) : COLOR
{
i.normalDir = normalize(i.normalDir);
float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
half3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
half4 _SurfaceTangentTex = tex2D(_SurfaceTangentMap,i.uv0);
half3 _SurfaceTangentMap_Local = JP_UnpackNormalRG(_SurfaceTangentTex.xy);
half3 normalT = normalize(_SurfaceTangentMap_Local);
FLOAT3 normalTDirection = mul( normalT, tangentTransform );// Perturbed normals
FLOAT3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
half2 brdfUV = FLOAT2((dot(lightDirection,normalTDirection)*0.5+0.5),(dot(_LightColor0.rgb,half3(0.22,0.707,0.071))*_SkinToneScale));
half2 blendNormalTex = lerp(_SurfaceTangentTex.zw , _SurfaceTangentTex.xy, brdfUV.x);
half3 normalT_Final = normalize(JP_UnpackNormalRG(blendNormalTex.xy));
FLOAT3 normalDirectionFinal = mul( normalT_Final , tangentTransform);
float normalFinalDirection = dot( normalDirectionFinal, lightDirection );
brdfUV.x = normalFinalDirection * 0.5 + 0.5;
half2 brdfUV_Blur = brdfUV.xy;
half ldotn = max(0,dot(lightDirection, normalTDirection));
half3 lightColor = _LightColor0.rgb;
half4 vertexGIAmbient = 0;
#if UNITY_SHOULD_SAMPLE_SH
vertexGIAmbient = i.ambientOrLightmapUV;
#endif
////// Lighting:
//FLOAT attenuation = LIGHT_ATTENUATION(i);
half4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
half3 MainTex_Color = (_MainTex_var.rgb*_Color.rgb);
half _RoughnessMap_var = _MainTex_var.a;
half4 _SkinBRDF_var = tex2D(_SkinBRDF,brdfUV_Blur);// Clamp Wrap Mode
half4 _SkinTransMap_var = tex2D(_SkinTransMap,i.uv0);//This texture value green channel is occlusion map
half inputAlpha = _SkinTransMap_var.a;
clip(inputAlpha - _Cutoff);
half _OcclusionMap_var = 1;
_OcclusionMap_var = _SkinTransMap_var.g;
half3 strength = _SkinTransColor;// Fake Color-Bleeding for AO
half3 colorBleedAO = (pow(abs(_OcclusionMap_var), 1.0f - strength)) * (1-_OcclusionMap_var);
//attenuation *= pow(_SkinBRDF_var , _AttenuationScale);
half attenuation = _UseBrdfAtten?LIGHT_ATTENUATION(i) * (saturate( ( (normalFinalDirection * 0.5 + 0.5) * 2.0) * 2.0 - 1.0)) * pow(_SkinBRDF_var , _AttenuationScale ):LIGHT_ATTENUATION(i);
half3 finalColor = StandardBase( MainTex_Color , 0.0 , _Specular , (_RoughnessMap_var*_Roughness) , half3(0,0,0) ,
lerp(1.0,_OcclusionMap_var,_OcclusionStrength) , _Reflection , normalDirectionFinal , i.posWorld.rgb , viewDirection ,
lightDirection , _LightColor0.rgb , attenuation , vertexGIAmbient.rgb , reflect((-1*viewDirection),normalDirectionFinal) , i.uv1 ,
(_UseSKinBRDF ? (_SkinBRDF_var.rgb + (_SkinTransColor.rgb * clamp((_SkinTransMap_var.g * dot((-1 * mul( float4(normalize(_BackLightDir.rgb),0), UNITY_MATRIX_V ).xyz.rgb),normalDirectionFinal)),0,1))) : FLOAT3(ldotn,ldotn,ldotn)) , colorBleedAO , inputAlpha);
if (_toggleToneMapper)
{
finalColor = NeutralTonemap(finalColor);
}
half4 finalRGBA = FLOAT4(finalColor,1);
UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
return finalRGBA;
}
ENDCG
}
Pass {
Name "Meta"
Tags {
"LightMode"="Meta"
}
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_META 1
#define FLOAT half
#define FLOAT2 half2
#define FLOAT3 half3
#define FLOAT4 half4
#include "UnityCG.cginc"
#include "UnityMetaPass.cginc"
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_shadowcaster
#pragma multi_compile_fog
#pragma target 3.0
uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
uniform half4 _Color;
struct VertexInput {
float4 vertex : POSITION;
float2 texcoord0 : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
float2 texcoord2 : TEXCOORD2;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.pos = UnityMetaVertexPosition(v.vertex, v.texcoord1.xy, v.texcoord2.xy, unity_LightmapST, unity_DynamicLightmapST );
return o;
}
FLOAT4 frag(VertexOutput i) : SV_Target {
UnityMetaInput o;
UNITY_INITIALIZE_OUTPUT( UnityMetaInput, o );
o.Emission = 0;
half4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
half3 MainTex_Color = (_MainTex_var.rgb*_Color.rgb);
half3 Meta = MainTex_Color;
FLOAT3 diffColor = Meta;
o.Albedo = diffColor;
return UnityMetaFragment( o );
}
ENDCG
}
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment