2020-11-28 16:54:41 +02:00

160 lines
5.2 KiB
Plaintext

Shader "DBV/Kristo/UnlitClip"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color ("Main color", Color) = (1,1,1,1)
_BackFaceColor ("Inside color", Color) = (1,1,1,1)
_FadeColor ("Rim color", Color) = (1,1,1,1)
_FadeDistance ("Fade distance", Float) = 10
_FadeSmooth ("Fade smooth distance", Float) = 1
_DitherTex ("Texture", 2D) = "grey" {}
}
SubShader {
Tags {
"RenderType"="Opaque"
}
LOD 100
Pass {
Cull Back
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog// make fog work
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 screenPos : TEXCOORD1;
};
struct v2f {
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
float4 worldSpace : TEXCOORD1;
float4 screenPos : TEXCOORD2;
};
sampler2D _MainTex, _DitherTex;
float4 _DitherTex_TexelSize;
float4 _MainTex_ST;
float4 _FadeColor, _BackFaceColor, _Color;
float _FadeDistance, _FadeSmooth;
v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.worldSpace = mul(unity_ObjectToWorld, v.vertex);
o.screenPos = ComputeScreenPos(o.vertex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target {
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv) * _Color;
fixed3 vec = i.worldSpace.xyz - _WorldSpaceCameraPos;
float dis = vec.x*vec.x + vec.y*vec.y + vec.z*vec.z;
float fadeDis = _FadeDistance * _FadeDistance;
float fade = _FadeSmooth * _FadeSmooth;
float fadeSmooth = fadeDis + fade;
if(dis < fadeDis) {
col = 0;
clip(-1);
}
else if (dis < fadeSmooth) {
float v = (1-(fadeSmooth-dis)/fade);
//Do dither fade
//https://www.ronja-tutorials.com/2019/05/11/dithering.html
float2 screenPos = i.screenPos.xy / i.screenPos.w;
float2 ditherCoordinate = screenPos * _ScreenParams.xy * _DitherTex_TexelSize.xy;
float ditherValue = tex2D(_DitherTex, ditherCoordinate).r;
clip(v - ditherValue);
col = col * v + (1-v) * _FadeColor;
}
else{
//col.rgb *= i.worldSpace.xyz;
}
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
Pass {
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog// make fog work
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 screenPos : TEXCOORD1;
};
struct v2f {
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
float4 worldSpace : TEXCOORD1;
float4 screenPos : TEXCOORD2;
};
float4 _BackFaceColor;
float _FadeDistance, _FadeSmooth;
sampler2D _DitherTex;
float4 _DitherTex_TexelSize;
v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.worldSpace = mul(unity_ObjectToWorld, v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target {
fixed3 vec = i.worldSpace.xyz - _WorldSpaceCameraPos;
float dis = vec.x*vec.x + vec.y*vec.y + vec.z*vec.z;
float fadeDis = _FadeDistance * _FadeDistance;
float fade = _FadeSmooth * _FadeSmooth;
float fadeSmooth = fadeDis + fade;
if(dis < fadeDis) {
clip(-1);
}
else if (dis < fadeSmooth) {
float v = (1-(fadeSmooth-dis)/fade);
//Do dither fade
//https://www.ronja-tutorials.com/2019/05/11/dithering.html
float2 screenPos = i.screenPos.xy / i.screenPos.w;
float2 ditherCoordinate = screenPos * _ScreenParams.xy * _DitherTex_TexelSize.xy;
float ditherValue = tex2D(_DitherTex, ditherCoordinate).r;
clip(v - ditherValue);
}
return _BackFaceColor;
}
ENDCG
}
}
}