forked from przemyslawzaworski/Unity3D-CG-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distance.shader
50 lines (43 loc) · 905 Bytes
/
distance.shader
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
Shader "Distance"
{
Properties
{
_range ("Maximum distance",Float) = 10
_color ("Color", Color) = (1.0,1.0,1.0,1.0)
}
Subshader
{
Pass
{
CGPROGRAM
#pragma vertex vertex_shader
#pragma fragment pixel_shader
#pragma target 3.0
struct structure
{
float4 vertex : SV_POSITION;
float depth : TEXCOORD0;
};
float _range;
float4 _color;
float inverse_lerp (float a, float b, float t)
{
return (t - a)/(b - a);
}
structure vertex_shader (float4 vertex:POSITION)
{
structure vs;
vs.vertex = UnityObjectToClipPos (vertex);
float d = distance(mul (unity_ObjectToWorld, vertex).xyz,_WorldSpaceCameraPos.xyz);
vs.depth = inverse_lerp(0.0,_range,d);
return vs;
}
float4 pixel_shader (structure ps ) : SV_TARGET
{
float t = ps.depth;
return float4(t*_color.rgb,_color.a);
}
ENDCG
}
}
}