DepthContour.shader
3.02 KB
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
/*========================================================================
Copyright (c) 2017 PTC Inc. All Rights Reserved.
Vuforia is a trademark of PTC Inc., registered in the United States and other
countries.
=========================================================================*/
Shader "Custom/DepthContour" {
Properties{
_ContourColor("Contour Color", Color) = (1,1,1,1)
_SurfaceColor("Surface Color", Color) = (0.5,0.5,0.5,1)
_DepthThreshold("Depth Threshold", Float) = 0.002
}
SubShader {
Tags { "Queue" = "Geometry" "RenderType" = "Transparent" }
Pass {
Cull Back
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _CameraDepthTexture;
uniform float4 _ContourColor;
uniform float4 _SurfaceColor;
uniform float _DepthThreshold;
struct v2f {
float4 pos : SV_POSITION;
float4 screenPos : TEXCOORD0;
float depth : TEXCOORD1;
};
v2f vert(appdata_base v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.screenPos = ComputeScreenPos(o.pos);
COMPUTE_EYEDEPTH(o.depth);
o.depth = (o.depth - _ProjectionParams.y) / (_ProjectionParams.z - _ProjectionParams.y);
return o;
}
half4 frag(v2f i) : COLOR
{
float2 uv = i.screenPos.xy / i.screenPos.w;
float du = 1.0 / _ScreenParams.x;
float dv = 1.0 / _ScreenParams.y;
float2 uv_X1 = uv + float2(du, 0.0);
float2 uv_Y1 = uv + float2(0.0, dv);
float2 uv_X2 = uv + float2(-du, 0.0);
float2 uv_Y2 = uv + float2(0.0, -dv);
float depth0 = Linear01Depth(UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, uv)));
float depthX1 = Linear01Depth(UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, uv_X1)));
float depthY1 = Linear01Depth(UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, uv_Y1)));
float depthX2 = Linear01Depth(UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, uv_X2)));
float depthY2 = Linear01Depth(UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, uv_Y2)));
float farDist = _ProjectionParams.z;
float refDepthStep = _DepthThreshold / farDist;
float depthStepX = max(abs(depth0 - depthX1), abs(depth0 - depthX2));
float depthStepY = max(abs(depth0 - depthY1), abs(depth0 - depthY2));
float maxDepthStep = length(float2(depthStepX, depthStepY));
half contour = (maxDepthStep > refDepthStep) ? 1.0 : 0.0;
return _SurfaceColor * (1.0 - contour) + _ContourColor * contour;
}
ENDCG
}
}
Fallback "Diffuse"
}