-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhydra-vec4.js
81 lines (80 loc) · 2.4 KB
/
hydra-vec4.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
{
const getHydra = function () {
const whereami = window.location?.href?.includes("hydra.ojack.xyz")
? "editor"
: window.atom?.packages
? "atom"
: "idk";
if (whereami === "editor") {
return window.hydraSynth;
}
if (whereami === "atom") {
return global.atom.packages.loadedPackages["atom-hydra"]
.mainModule.main.hydra;
}
let _h = [
window.hydraSynth,
window._hydra,
window.hydra,
window.h,
window.H,
window.hy
].find(h => h?.regl);
return _h;
};
window._hydra = getHydra();
window._hydraScope = _hydra.sandbox.makeGlobal ? window : _hydra.synth;
}
{
function filterVecArgs(args) {
let pass = [];
args.forEach((arg) => {
if (typeof arg == "number" || typeof arg == "function")
pass = pass.concat(arg);
else if (arg.constructor.name == "Array") {
if (arg._vec) pass = pass.concat(arg);
else pass = pass.concat([arg]);
}
});
return pass;
}
window.vec4 = function (...args) {
args = filterVecArgs(args);
if (args.length == 1) {
const arg = args[0];
return solid(arg, arg, arg, arg);
} else if (args.length == 4) {
return _hydraScope.solid(...args);
} else {
throw new Error("vec4 should receive 4 elements");
}
};
window.vec3 = function (...args) {
args = filterVecArgs(args);
if (args.length == 1) {
const arg = args[0];
let ret = Array.from({ length: 3 }, () => arg);
ret._vec = true;
return ret;
} else if (args.length == 3) {
args._vec = true;
return args;
} else {
throw new Error("vec3 should receive 3 elements");
}
};
window.vec2 = function (...args) {
args = filterVecArgs(args);
if (args.length == 1) {
const arg = args[0];
let ret = Array.from({ length: 2 }, () => arg);
ret._vec = true;
return ret;
} else if (args.length == 2) {
args._vec = true;
return args;
} else {
throw new Error("vec2 should receive 2 elements");
}
};
}