summaryrefslogtreecommitdiff
path: root/src/shaders/stars.lua
blob: 111bef8aa8fa5f302dedd179d4989edf2c37e6a3 (plain)
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
local win = require("window")
local color = require("color")
local world = require("world")
local numstars = 400 -- we might have as many as 4 over
local genned_stars = 0
local period_x = 3
local period_y = 3
local stars = {}
while genned_stars < numstars do
	local rngx = math.random()
	local xpos = rngx * win.width --* (period_x - 1)
	local rngy = math.random()
	local ypos = rngy * win.height --* (period_y - 1)
	local blinks = math.random() > 0.3 and (math.random() * 2 * math.pi) or 0
	stars[#stars+1] = vec3(xpos, ypos, blinks)
	genned_stars = genned_stars + 1
	if xpos < win.width then
		-- duplicate on the last screen
		stars[#stars+1] = vec3(xpos + (win.width * (period_x-2)), ypos, blinks)
		genned_stars = genned_stars + 1
	end
	if ypos < win.height then
		stars[#stars+1] = vec3(xpos, ypos + (win.height * (period_y-2)), blinks)
		genned_stars = genned_stars + 1
	end
	if xpos < win.width and ypos < win.height then
		stars[#stars+1] = vec3(xpos + (win.width * (period_x-2)), ypos+(win.height * (period_y-2)),blinks)
		genned_stars = genned_stars + 1
	end
end
local node = am.use_program(am.program([[
	precision highp float;
	attribute vec3 stars;
	uniform float time;
	uniform float world_x;
	uniform float world_y;
	uniform float world_x_period;
	uniform float world_y_period;
	uniform mat4 MV;
	uniform mat4 P;
	void main() {
		float world_x_off = mod(world_x, world_x_period);
		float world_y_off = mod(world_y, world_y_period); 
		gl_Position = P * MV * vec4(stars.x - world_x_off, stars.y - world_y_off, 0.0, 1.0);
		float intensity = sin(stars.z + time) * cos(time) + 1.;
		gl_PointSize = pow(intensity, 2.) * stars.z * 0.3;
	}
	]],[[
	precision mediump float;
	uniform vec4 color;
	void main() {
		gl_FragColor = color;
	}
]]))
^ am.bind({
	MV = mat4(
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		(-win.width / 2), (-win.height/2), 0, 1
	),
	color = color.am_color.highlight,
	stars = am.vec3_array(stars),
	world_x = am.current_time(),
	world_x_period = (period_x - 2) * win.width,
	world_y = am.current_time(),
	world_y_period = (period_y - 2) * win.height,
	time = am.current_time(),
})
^ am.draw("points")
node:action(function(self)
	self("bind").time = am.current_time()
	self("bind").world_x = world.world_x
	self("bind").world_y = world.world_y
end)
return node