forked from przemyslawzaworski/Unity3D-CG-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_projection.shader
71 lines (66 loc) · 2.37 KB
/
map_projection.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Shader "Map Projection"
{
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vertex_shader
#pragma fragment pixel_shader
#pragma target 4.0
uniform extern float4 pointA, pointB, pointC, pointD, pointE, pointF;
uniform extern sampler2D Projection;
uniform extern float4x4 _matrix;
uniform extern float4 colorIN;
uniform extern float cornerAX,cornerAY,cornerBX,cornerBY;
float2 InverseLerp (float2 x, float2 a, float2 b)
{
return (x-a)/(b-a);
}
float Triangle(float2 p0,float2 p1,float2 p2,float2 p )
{
float2 e0 = p1 - p0;
float2 e1 = p2 - p1;
float2 e2 = p0 - p2;
float2 v0 = p - p0;
float2 v1 = p - p1;
float2 v2 = p - p2;
float2 pq0 = v0 - e0*saturate( dot(v0,e0)/dot(e0,e0));
float2 pq1 = v1 - e1*saturate( dot(v1,e1)/dot(e1,e1));
float2 pq2 = v2 - e2*saturate( dot(v2,e2)/dot(e2,e2));
float s = sign( e0.x*e2.y - e0.y*e2.x );
float2 d = min( min( float2( dot( pq0, pq0 ), s*(v0.x*e0.y-v0.y*e0.x) ),
float2( dot( pq1, pq1 ), s*(v1.x*e1.y-v1.y*e1.x) )),
float2( dot( pq2, pq2 ), s*(v2.x*e2.y-v2.y*e2.x) ));
return -sqrt(d.x)*sign(d.y);
}
void vertex_shader (inout float4 vertex:POSITION,inout float2 uv:TEXCOORD0)
{
vertex = UnityObjectToClipPos( vertex );
}
float4 pixel_shader (float4 vertex:POSITION,float2 uv:TEXCOORD0) : COLOR
{
float2 borderA = float2(cornerAX,cornerAY);
float2 borderB = float2(cornerBX,cornerBY);
float2 v1 = InverseLerp (mul(_matrix,pointA).xz,borderA,borderB);
float2 v2 = InverseLerp (mul(_matrix,pointB).xz,borderA,borderB);
float2 v3 = InverseLerp (mul(_matrix,pointC).xz,borderA,borderB);
float2 v4 = InverseLerp (mul(_matrix,pointD).xz,borderA,borderB);
float2 v5 = InverseLerp (mul(_matrix,pointE).xz,borderA,borderB);
float2 v6 = InverseLerp (mul(_matrix,pointF).xz,borderA,borderB);
float d1 = Triangle( v1, v2, v3, uv );
float d2 = Triangle( v4, v5, v6, uv );
float3 colA = (1.0-smoothstep(0.0,0.005,(d1))) * colorIN;
float3 colB = (1.0-smoothstep(0.0,0.005,(d2))) * colorIN;
float3 color;
if (sign(d1)==0.0 || sign(d2)==0.0)
color = 0;
else
color = max(colA,colB);
color+=tex2D(Projection,uv).rgb;
return float4(color,1.0);
}
ENDCG
}
}
}