99/// you may not use this file except in compliance with the License.
1010/// You may obtain a copy of the License at
1111///
12- /// http://www.apache.org/licenses/LICENSE-2.0
12+ /// http://www.apache.org/licenses/LICENSE-2.0
1313///
1414/// Unless required by applicable law or agreed to in writing, software
1515/// distributed under the License is distributed on an "AS IS" BASIS,
1818/// limitations under the License.
1919/// </copyright>
2020/// <summary>
21- /// Based on Unity's (Pro Only) Image Effect: Fisheye.shader
21+ /// Long ago, based on Unity's (Pro Only) Image Effect: Fisheye.shader
2222/// Author: Greg Aring
2323/// Email: greg@sensics.com
2424/// </summary>
25+
26+ /// Syntax reference: see http://docs.unity3d.com/Manual/SL-Shader.html
27+
2528Shader "Osvr/OsvrDistortion" {
2629Properties
2730{
28- _K1_Red ( "K1 Red" , Range (0.00 ,1.00 )) = 0.5 // sliders
29- _K1_Green ( "Reflection distort" , Range (0.00 ,1.00 )) = 0.5
30- _K1_Blue ( "Reflection distort" , Range (0.00 ,1.00 )) = 0.5
31- _Full_Center ( "FullCenter" , Vector ) = (.5 ,.5 ,0 ,0 )
32- _Left_Center ( "LeftCenter" , Vector ) = (.5 ,.5 ,0 ,0 )
33- _Right_Center ( "RightCenter" , Vector ) = (.5 ,.5 ,0 ,0 )
31+ _K1_Red ( "K1 Red" , Range (0.00 ,1.00 )) = 0.0 // sliders
32+ _K1_Green ( "K1 Green" , Range (0.00 ,1.00 )) = 0.0
33+ _K1_Blue ( "K1 Blue" , Range (0.00 ,1.00 )) = 0.0
34+ _Center ( "Center of Projection" , Vector ) = (.5 ,.5 ,0 ,0 )
3435 _MainTex ( "Base (RGB)" , 2D ) = "" {}
3536}
36- CGINCLUDE
37+ CGINCLUDE
3738 #include "UnityCG.cginc"
38-
39+
3940 struct v2f {
4041 float4 pos : SV_POSITION ;
4142 float2 uv : TEXCOORD0 ;
4243 };
43-
44+
4445 float _K1_Red;
4546 float _K1_Green;
4647 float _K1_Blue;
47- float4 _Full_Center;
48- float4 _Left_Center;
49- float4 _Right_Center;
48+ float4 _Center;
5049 sampler2D _MainTex;
5150
52- v2f vert ( appdata_img v )
51+ v2f vert ( appdata_img v )
5352 {
5453 v2f o;
5554 o.pos = mul (UNITY_MATRIX_MVP , v.vertex);
5655 o.uv = v.texcoord.xy;
5756 return o;
58- }
57+ }
5958 float2 Distort (float2 p, float k1)
6059 {
61-
62-
60+ /// @todo would pow improve performance here? (by using SFU if available?)
6361 float r2 = p.x * p.x + p.y * p.y;
64- float r = sqrt (r2);
6562
66- float newRadius = (1 + k1*r*r );
63+ float newRadius = (1 + k1*r2 );
6764 p.x = p.x * newRadius;
6865 p.y = p.y * newRadius;
6966
7067 return p;
7168 }
72-
73- half4 frag (v2f i) : SV_Target
69+
70+ half4 frag (v2f i) : SV_Target
7471 {
7572 float2 uv_red, uv_green, uv_blue;
7673 float4 color_red, color_green, color_blue;
7774 float2 sectorOrigin;
7875 float4 color;
7976
80- sectorOrigin = _Full_Center .xy;
77+ sectorOrigin = _Center .xy;
8178
8279 uv_red = Distort (i.uv-sectorOrigin, _K1_Red) + sectorOrigin;
8380 uv_green = Distort (i.uv-sectorOrigin, _K1_Green) + sectorOrigin;
@@ -101,13 +98,13 @@ Properties
10198
10299 Subshader {
103100 Pass {
104- ZTest Always Cull Off ZWrite Off
101+ ZTest Always Cull Off ZWrite Off
102+
103+ CGPROGRAM
104+ #pragma vertex vert
105+ #pragma fragment frag
106+ ENDCG
107+ }
105108
106- CGPROGRAM
107- #pragma vertex vert
108- #pragma fragment frag
109- ENDCG
110- }
111-
112109 }
113- }
110+ }
0 commit comments