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.

247 lines
5.9 KiB

Shader "Unlit/PrettySkin 2.0"
{
Properties
{
_Bilateral("Texture", 2D) = "white" {}
_Canny("Texture", 2D) = "white" {}
_MainTex ("Texture", 2D) = "white" {}
_smoothDegree("smoothDegree" ,Range(0,1)) = 0.5
_Log("Log",Range(0,3)) = 0
_BSIGMA("BSIGMA",Range(0,1)) = 0.1
_SIGMA("SIGMA",Range(3,15)) = 7
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_TexelSize;
float4 _MainTex_ST;
float _SIGMA;
float _BSIGMA;
#define MSIZE 15
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
float normpdf(in float x, in float sigma)
{
return 0.39894*exp(-0.5*x*x / (sigma*sigma)) / sigma;
}
float3 normpdf3v3(in float3 origin, in float sigma)
{
float x = 0.39894*exp(-0.5*origin.x*origin.x / (sigma*sigma)) / sigma;
float y = 0.39894*exp(-0.5*origin.y*origin.y / (sigma*sigma)) / sigma;
float z = 0.39894*exp(-0.5*origin.z*origin.z / (sigma*sigma)) / sigma;
return float3(x, y, z);
}
float normpdf3(in float3 v, in float sigma)
{
return 0.39894*exp(-0.5*dot(v, v) / (sigma*sigma)) / sigma;
}
fixed4 frag(v2f i) : SV_Target
{
#if UNITY_UV_STARTS_AT_TOP
if (_MainTex_TexelSize.y < 0)
i.uv.y = 1 - i.uv.y;
#endif
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
float3 final_colour = float3(0.0, 0.0, 0.0);
int kSize = (MSIZE - 1) / 2;
float kernel[MSIZE];
float Z = 0.0;
for (int j = 0; j <= kSize; ++j)
{
kernel[kSize + j] = kernel[kSize - j] = normpdf(float(j), _SIGMA);
}
float3 cc;
float factor;
float2 UV = i.uv;
for (int i = -kSize; i <= kSize; ++i)
{
for (int j = -kSize; j <= kSize; ++j)
{
float2 newUV = (float2(UV)) + (float2(float(i), float(j)) / _MainTex_TexelSize.zw);
cc = tex2D(_MainTex, newUV).rgb;
factor = normpdf3(cc - col, _BSIGMA) * kernel[kSize + j] * kernel[kSize + i];
Z += factor;
final_colour += factor*cc;
}
}
// apply fog
//UNITY_APPLY_FOG(i.fogCoord, col);
return float4(final_colour / (Z), 1.0);
}
ENDCG
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
#define iResolution _ScreenParams
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _MainTex_TexelSize;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
#if UNITY_UV_STARTS_AT_TOP
if (_MainTex_TexelSize.y < 0)
i.uv.y = 1 - i.uv.y;
#endif
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
float2 uv = i.uv;
float4 a = tex2D(_MainTex, float2(uv.x - 1.0 / iResolution.x, uv.y + 1.0 / iResolution.y));
float4 b = tex2D(_MainTex, float2(uv.x, uv.y + 1.0 / iResolution.y));
float4 c = tex2D(_MainTex, float2(uv.x + 1.0 / iResolution.x, uv.y + 1.0 / iResolution.y));
float4 d = tex2D(_MainTex, float2(uv.x - 1.0 / iResolution.x, uv.y));
float4 e = tex2D(_MainTex, float2(uv.x + 1.0 / iResolution.x, uv.y));
float4 f = tex2D(_MainTex, float2(uv.x - 1.0 / iResolution.x, uv.y - 1.0 / iResolution.y));
float4 g = tex2D(_MainTex, float2(uv.x, uv.y - 1.0 / iResolution.y));
float4 h = tex2D(_MainTex, float2(uv.x + 1.0 / iResolution.x, uv.y - 1.0 / iResolution.y));
float4 sobelX = a*(-1.0) + c*(1.0) + d*(-2.0) + e*(2.0) + f*(-1.0) + h*(1.0);
float4 sobelY = a*(-1.0) + b*(-2.0) + c*(-1.0) + f*(1.0) + g*(2.0) + h*(1.0);
col = sobelX + sobelY;
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
sampler2D _Bilateral;
sampler2D _Canny;
float4 _MainTex_ST;
float _smoothDegree;
float _Log;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 source = tex2D(_MainTex, i.uv);
fixed4 bilateral = tex2D(_Bilateral, i.uv);
fixed4 canny = tex2D(_Canny, i.uv);
float4 smooth;
float r = source.r;
float g = source.g;
float b = source.b;
//if (canny.r < 0.2 && r> 0.3725 && g > 0.1568 && b > 0.0784 && r > b && (max(max(r, g), b) - min(min(r, g), b) > 0.0588 && abs(r - g) > 0.0588))
if(canny.r < 0.2 && r> 0.3725 && g > 0.1568 && b > 0.0784 && r > b && (max(max(r, g), b) - min(min(r, g), b) > 0.0588 && abs(r - g) > 0.0588))
{
smooth = (1.0 - _smoothDegree) * (source - bilateral) + bilateral;
}
else
{
smooth = source;
}
smooth.r = log(1.0 + (0.2+ _Log) * smooth.r) / log(1.2+ _Log);
smooth.g = log(1.0 + (0.2+ _Log) * smooth.g) / log(1.2+ _Log);
smooth.b = log(1.0 + (0.2+ _Log) * smooth.b) / log(1.2+ _Log);
//smooth = (1.0 - _smoothDegree) * (source - bilateral) + bilateral;
// apply fog
//UNITY_APPLY_FOG(i.fogCoord, smooth);
return smooth;
}
ENDCG
}
}
}