-
Notifications
You must be signed in to change notification settings - Fork 5
/
Distort.shader
136 lines (110 loc) · 3.25 KB
/
Distort.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "NewChromantics/Distort" {
Properties {
_MainTex ("_MainTex", 2D) = "green" {}
Invert("Invert",Int) = 0
ZoomUv("ZoomUv", Range(-1,2) ) = 0
Debug("Debug",Int) = 0
LensOffsetX("LensOffsetX", Range(-1,1)) = 0
LensOffsetY("LensOffsetY", Range(-1,1)) = 0
RadialDistortionX("RadialDistortionX", Range(-1.57,1.57)) = 0
RadialDistortionY("RadialDistortionY", Range(-1.57,1.57)) = 0
TangentialDistortionX("TangentialDistortionX", Range(-1.57,1.57)) = 0
TangentialDistortionY("TangentialDistortionY", Range(-1.57,1.57)) = 0
K5Distortion("K5Distortion", Range(-1.57,1.57)) = 0
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
int Debug;
float RadialDistortionX;
float RadialDistortionY;
float TangentialDistortionX;
float TangentialDistortionY;
float K5Distortion;
float LensOffsetX;
float LensOffsetY;
int Invert;
float ZoomUv;
struct VertexInput {
float4 Position : POSITION;
float2 uv_MainTex : TEXCOORD0;
};
struct FragInput {
float4 Position : SV_POSITION;
float2 uv_MainTex : TEXCOORD0;
};
FragInput vert(VertexInput In) {
FragInput Out;
Out.Position = UnityObjectToClipPos (In.Position );
Out.uv_MainTex = In.uv_MainTex;
return Out;
}
// http://stackoverflow.com/questions/21615298/opencv-distort-back
float2 DistortPixel(float2 Point)
{
float Inverse = Invert?-1:1;
float cx = LensOffsetX;
float cy = LensOffsetY;
float k1 = RadialDistortionX * Inverse;
float k2 = RadialDistortionY * Inverse;
float p1 = TangentialDistortionX * Inverse;
float p2 = TangentialDistortionY * Inverse;
float k3 = K5Distortion * Inverse;
float x = Point.x - cx;
float y = Point.y - cy;
float r2 = x*x + y*y;
// Radial distorsion
float xDistort = x * (1 + k1 * r2 + k2 * r2 * r2 + k3 * r2 * r2 * r2);
float yDistort = y * (1 + k1 * r2 + k2 * r2 * r2 + k3 * r2 * r2 * r2);
// Tangential distorsion
xDistort = xDistort + (2 * p1 * x * y + p2 * (r2 + 2 * x * x));
yDistort = yDistort + (p1 * (r2 + 2 * y * y) + 2 * p2 * x * y);
// Back to absolute coordinates.
xDistort = xDistort + cx;
yDistort = yDistort + cy;
return float2( xDistort, yDistort);
}
// 0..1 to -1..1
float2 CenterUv(float2 uv)
{
uv = uv*float2(2,2) - float2(1,1);
return uv;
}
float2 UncenterUv(float2 uv)
{
uv = (uv+float2(1,1)) / float2(2,2);
return uv;
}
fixed4 frag(FragInput In) : SV_Target {
float2 uv = In.uv_MainTex;
uv = CenterUv(uv);
uv *= 1.0f / ZoomUv;
uv = DistortPixel( uv );
uv = UncenterUv(uv);
if ( uv.x > 1 )
return float4(1,0,0,1);
if ( uv.y > 1 )
return float4(0,1,0,1);
if ( uv.x < 0 )
return float4(0,0,1,1);
if ( uv.y < 0 )
return float4(1,1,0,1);
uv = CenterUv(uv);
uv *= 1.0f / ZoomUv;
uv = UncenterUv(uv);
float4 Sample = tex2D( _MainTex, uv );
if ( Debug )
return float4( uv.x, uv.y, Sample.x, 1 );
return Sample;
}
ENDCG
}
}
}