DepthContour.shader
2.76 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*========================================================================
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
{
_SilhouetteSize ("Size", Float) = 1
_SilhouetteColor ("Color", Color) = (1,1,1,1)
}
CGINCLUDE
#include "UnityCG.cginc"
struct v2f
{
float4 position : POSITION;
float4 color : COLOR;
UNITY_VERTEX_OUTPUT_STEREO
};
struct vertIn
{
float4 position : POSITION;
float3 normal : NORMAL;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
uniform float _SilhouetteSize;
uniform float4 _SilhouetteColor;
ENDCG
SubShader
{
Tags { "Queue" = "Geometry" }
Pass
{
Cull Back
Blend Zero One
}
Pass
{
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
v2f vert(vertIn input)
{
v2f output;
UNITY_SETUP_INSTANCE_ID(input); //Insert
UNITY_INITIALIZE_OUTPUT(v2f, output); //Insert
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output); //Insert
// unmodified projected position of the vertex
output.position = UnityObjectToClipPos(input.position);
output.color = _SilhouetteColor;
// calculate silhouette in image space
float3 normal = mul((float3x3)UNITY_MATRIX_IT_MV, input.normal);
float2 normalScreen = TransformViewToProjection(normal.xy);
float2 screenOffset = _SilhouetteSize * normalize(normalScreen);
float2 xyOffset;
xyOffset.x = screenOffset.x / (_ScreenParams.x * 0.5);
xyOffset.y = screenOffset.y / (_ScreenParams.y * 0.5);
// denormalize the screenspace offset, so it is correct after projective division by w
// dividing output.position by w here would interfer with culling
xyOffset *= output.position.w;
output.position.xy += xyOffset;
return output;
}
half4 frag(v2f input) :COLOR
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); //Insert
return input.color;
}
ENDCG
}
}
Fallback "Diffuse"
}