-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstarfield.js
73 lines (66 loc) · 2.34 KB
/
starfield.js
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
var getStars;
(function () {
// -- settings, immutable -- //
var BRIGHTNESS_FACTOR = 15; // Higher = brighter
var STAR_RANGE_INDICES = 10; // Higher = more random looking, comp expensive
var LEVEL_DEPTH = 5; // Higher = more (faint) stars, comp expensive
var MAX_INT = -1 >>> 1;
var cache = new RRCache(100000);
function cachedHash(to_hash) {
var cached = cache.get(to_hash);
if (cached) return cached; else {
var digest = [
hashFnv32a(to_hash + 'a'),
hashFnv32a(to_hash + 'b'),
hashFnv32a(to_hash + 'c')
];
cache.set(to_hash, digest);
return digest;
}
}
function _getStars(worldOffsetX, worldOffsetY, worldWidth, worldHeight) {
// The level at which you would expect one star in current viewport.
var levelForCurrentScale = - Math.log(worldWidth * worldHeight) / 2
var startLevel = Math.floor(levelForCurrentScale);
var stars = [];
for (level = startLevel; level < startLevel + LEVEL_DEPTH; level++) {
var spacing = Math.exp(-level);
for (
xIndex = Math.floor(worldOffsetX / spacing) - STAR_RANGE_INDICES;
xIndex <= Math.ceil((worldOffsetX + worldWidth) / spacing)
+ STAR_RANGE_INDICES;
xIndex++
) {
for (
yIndex = Math.floor(worldOffsetY / spacing) - STAR_RANGE_INDICES;
yIndex <= Math.ceil((worldOffsetY + worldHeight) / spacing)
+ STAR_RANGE_INDICES;
yIndex++
) {
var hash = cachedHash(xIndex + ':' + yIndex + ':' + level);
stars.push(new Star(
xIndex * spacing
+ (hash[0] / MAX_INT) * spacing * STAR_RANGE_INDICES,
yIndex * spacing
+ (hash[1] / MAX_INT) * spacing * STAR_RANGE_INDICES,
Math.max(0,
Math.atan(
(
Math.exp(
levelForCurrentScale - level - Math.abs(hash[2] / MAX_INT)
) - Math.exp(
// This is to prevent the lowest level of stars from
// suddenly popping in and out with high brightness.
levelForCurrentScale - (startLevel + LEVEL_DEPTH)
)
) * BRIGHTNESS_FACTOR
) * 2 / Math.PI
)
));
}
}
}
return stars;
}
getStars = _getStars;
})();