-
Notifications
You must be signed in to change notification settings - Fork 0
/
VertexStarShader.sl
97 lines (71 loc) · 1.98 KB
/
VertexStarShader.sl
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
#version 300 es
// Inputs
in vec3 star;
in float magnitude;
in vec3 input_color;
// Outputs
out mediump vec4 vs_color;
// Uniforms
uniform vec3 camera_position;
uniform vec4 camera_orientation;
uniform float camera_fov;
// Perspective vars
const float near = 0.000001;
const float far = 1000000.0;
vec4 quat_mult(vec4 q1, vec4 q2)
{
vec4 qr;
qr.w = (q1.w * q2.w) - (q1.x * q2.x) - (q1.y * q2.y) - (q1.z * q2.z);
qr.x = (q1.w * q2.x) + (q1.x * q2.w) + (q1.y * q2.z) - (q1.z * q2.y);
qr.y = (q1.w * q2.y) - (q1.x * q2.z) + (q1.y * q2.w) + (q1.z * q2.x);
qr.z = (q1.w * q2.z) + (q1.x * q2.y) - (q1.y * q2.x) + (q1.z * q2.w);
return qr;
}
vec4 quat_conjugate(vec4 q)
{
vec4 qr;
qr.w = q.w;
qr.xyz = -q.xyz;
return qr;
}
vec3 rotate(vec3 p, vec4 q)
{
vec4 qv;
qv.w = 0.0;
qv.xyz = p.xyz;
qv = quat_mult(quat_conjugate(q), quat_mult(qv, q));
return qv.xyz;
}
vec3 apply_frustum(vec3 position)
{
vec3 result = position;
float a = 2.0 / (far - near);
float b = -1.0 - a * near;
result.z = a * result.z + b;
a = ((far - near) * cos(camera_fov / 2.0));
b = near * cos(camera_fov / 2.0) + a;
result.x = result.x / (a * result.z + b);
result.y = result.y / (a * result.z + b);
return result;
}
float get_apparent_magnitude(float magnitude, vec3 position)
{
return 5.0 * log(distance(position, vec3(0.0)) * 0.30659458) / log(10.0) + magnitude;
}
float get_point_size_from_apparent_magnitude(float magnitude)
{
return max((20.0 - 20.0 * magnitude / 1.0) / 5.0, 0.0);
}
void main(void)
{
vec3 position = star;
position = position - camera_position;
position = rotate(position, camera_orientation);
float apparent_magnitude = get_apparent_magnitude(magnitude, position);
gl_PointSize = 20.0 / (1.0 + exp(1.0 * (apparent_magnitude - 9.0)));
position = apply_frustum(position);
gl_Position.xyz = position.xyz;
gl_Position.w = 1.0;
vs_color.xyz = input_color;
vs_color.w = 1.0;
}