-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
190 lines (152 loc) · 4.93 KB
/
lib.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
let lerp = (a, b, t) => (1 - t) * a + t * b;
var toHalf = (function() {
var floatView = new Float32Array(1);
var int32View = new Int32Array(floatView.buffer);
/* This method is faster than the OpenEXR implementation (very often
* used, eg. in Ogre), with the additional benefit of rounding, inspired
* by James Tursa?s half-precision code. */
return function toHalf(val) {
floatView[0] = val;
var x = int32View[0];
var bits = (x >> 16) & 0x8000; /* Get the sign */
var m = (x >> 12) & 0x07ff; /* Keep one extra bit for rounding */
var e = (x >> 23) & 0xff; /* Using int is faster here */
/* If zero, or denormal, or exponent underflows too much for a denormal
* half, return signed zero. */
if (e < 103) {
return bits;
}
/* If NaN, return NaN. If Inf or exponent overflow, return Inf. */
if (e > 142) {
bits |= 0x7c00;
/* If exponent was 0xff and one mantissa bit was set, it means NaN,
* not Inf, so make sure we set one mantissa bit too. */
bits |= ((e == 255) ? 0 : 1) && (x & 0x007fffff);
return bits;
}
/* If exponent underflows but not too much, return a denormal */
if (e < 113) {
m |= 0x0800;
/* Extra rounding may overflow and set mantissa to 0 and exponent
* to 1, which is OK. */
bits |= (m >> (114 - e)) + ((m >> (113 - e)) & 1);
return bits;
}
bits |= ((e - 112) << 10) | (m >> 1);
/* Extra rounding. An overflow will set mantissa to 0 and increment
* the exponent, which is OK. */
bits += m & 1;
return bits;
};
}());
// Vector3
class Vec3 {
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
add(b) {
return new Vec3(this.x + b.x, this.y + b.y, this.z + b.z);
}
sub(b) {
return new Vec3(this.x - b.x, this.y - b.y, this.z - b.z);
}
mulv(b) {
return new Vec3(this.x * b.x, this.y * b.y, this.z * b.z);
}
muls(b) {
return new Vec3(this.x * b, this.y * b, this.z * b);
}
divs(b) {
return this.muls(1 / b);
}
get inv() {
return this.muls(-1);
}
get length() {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
get norm() {
return this.divs(this.length);
}
cross(b) {
return new Vec3(
this.y * b.z - this.z * b.y,
this.z * b.x - this.x * b.z,
this.x * b.y - this.y * b.x)
}
}
Vec3.lerp = (a, b, t) => new Vec3(
lerp(a.x, b.x, t),
lerp(a.y, b.y, t),
lerp(a.z, b.z, t));
Vec3.distance = (a, b) => Math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z));
Vec3.right = new Vec3(1, 0, 0);
Vec3.up = new Vec3(0, 1, 0);
Vec3.forward = new Vec3(0, 0, 1);
// Coroutine
const co = (f) => {
let g = f();
const next = () => {
let result = g.next();
if (!result.done) {
setTimeout(next, result.value * 1000);
}
};
next();
};
// Randoms
const randnum = (v = 1) => Math.random() * v;
const randint = (v) => Math.round(randnum(v));
// Audio
const PLAY_AUDIO = true;
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioContext = new AudioContext();
if (audioContext.state === 'suspended') {
audioContext.resume();
}
// Audio stuff
const audio_player = [new Audio(), new Audio(), new Audio(), new Audio(), new Audio()];
let audio_index = 0;
const playaudio = (a) => {
if (PLAY_AUDIO) {
audio_index = (audio_index + 1) % audio_player.length;
audio_player[audio_index].pause();
audio_player[audio_index].src = a;
audio_player[audio_index].play();
}
};
const playaudiorand = (l) => {
if (PLAY_AUDIO) {
playaudio(l[randint(l.length - 1)]);
}
};
class BGM {
constructor(files) {
this.tracks = {};
for (let i = 0; i < files.length; i++) {
let audioElement = document.getElementById(files[i])
let track = audioContext.createMediaElementSource(audioElement);
track.connect(audioContext.destination);
track.mediaElement.loop = true;
this.tracks[files[i]] = track;
}
}
play(song) {
if (!PLAY_AUDIO) return;
if (audioContext.state === 'suspended') {
audioContext.resume();
}
console.dir(this.tracks);
this.tracks[song].mediaElement.play();
}
stop(song) {
if (!PLAY_AUDIO) return;
if (audioContext.state === 'suspended') {
audioContext.resume();
}
console.dir(this.tracks);
this.tracks[song].mediaElement.pause();
}
}