1
2/*
3Copyright (c) 2019 The Khronos Group Inc.
4Use of this source code is governed by an MIT-style license that can be
5found in the LICENSE.txt file.
6*/
7
8
9#ifdef GL_ES
10#ifdef GL_FRAGMENT_PRECISION_HIGH
11precision highp float;
12#else
13precision mediump float;
14#endif
15#endif
16varying vec4 color;
17
18void main (void)
19{
20	const float M_PI = 3.14159265358979323846;
21	float x = 2.0 * (color.g - 0.5);
22	float y = 2.0 * (color.b - 0.5);
23	float atan_c = 0.0;
24	float scale = 1.0;
25	float sign = 1.0;
26	vec4 result = vec4(0.0, 0.0, 0.0, 1.0);
27	const float epsilon = 1.0e-4;
28
29	// Avoid evaluating atan(0, x) for x < epsilon because it's implementation-dependent
30	if(x > epsilon || abs(y) > epsilon)
31	{
32		if(x < 0.0 ^^ y < 0.0)
33		{
34			sign = -1.0;
35		}
36
37		if(abs(y) <= abs(x))
38		{
39			float c = abs(y / x);
40
41			// Taylors series expansion for atan
42			for(int i = 1; i < 12; i += 2)
43			{
44				atan_c += scale * pow(c, float(i)) / float(i);
45				scale *= -1.0;
46			}
47
48			result = vec4(sign * atan_c / (2.0 * M_PI) + 0.5, 0.0, 0.0, 1.0);
49		}
50		else
51		{
52			float c = abs(x / y);
53
54			// Taylors series expansion for atan
55			for(int i = 1; i < 12; i += 2)
56			{
57				atan_c += scale * pow(c, float(i)) / float(i);
58				scale *= -1.0;
59			}
60
61			result = vec4(sign * (M_PI / 2.0 - atan_c) / (2.0 * M_PI) + 0.5, 0.0, 0.0, 1.0);
62		}
63
64		if(x < 0.0)
65			if(y < 0.0) result.r -= 0.5;
66			else if(y > 0.0) result.r += 0.5;
67	}
68
69	gl_FragColor = result;
70}
71
72