-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhydra-glsl.js
170 lines (167 loc) · 6.63 KB
/
hydra-glsl.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
//hydra-gsls
//code glsl on the fly inside Hydra code
{
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;
}
{
const gS = _hydraScope.osc().constructor.prototype;
const _glslExtension = {
inputArray: () =>
new Array(10)
.fill("i")
.map((x, i) => ({ name: x + i, type: "float", default: 1 })),
nodeCount: 0,
checkCode: function (code) {
code = code.trim();
let lines = code.split(";");
let ll = lines.length - 1;
if (!lines[ll]) {
lines.pop();
ll--;
}
lines[ll] = lines[ll].trim();
lines[ll] =
"\n" +
(lines[ll].substring(0, 6) != "return"
? "return " + lines[ll]
: lines[ll]) +
";";
code = lines.join(";");
return code;
},
getObjAndArgs: function (type, args) {
let inputArray = false;
if (args[0] instanceof Array && args[0][0].constructor === String) {
inputArray = this.inputArray().map((x, i) => {
x.name = args[i] ? args[i][0] : x.name;
return x;
});
inputArray = inputArray.slice(0, args.length);
args = args.map((x) => x[1]);
}
let obj = {
name: "_glsl_ext_NODE_" + this.nodeCount,
type: type,
inputs: inputArray || this.inputArray(),
};
this.nodeCount++;
return [obj, args];
},
glslSource: function (code, ...args) {
let prefix = [
!code.includes("vec2 uv") ? "vec2 uv = _st;\n" : "",
!code.includes("vec2 st") ? "vec2 st = _st;\n" : "",
!code.includes("vec2 xy") ? "vec2 xy = _st;\n" : "",
].join("");
let data = this.getObjAndArgs("src", args);
let obj = data[0];
args = data[1];
obj.glsl = prefix + this.checkCode(code);
_hydra.synth.setFunction(obj);
return globalThis[obj.name](...args);
},
glslColor: function (self, code, ...args) {
let prefix = [
!code.includes("vec4 c0") ? "vec4 c0 = _c0;\n" : "",
!code.includes("vec4 color") ? "vec4 color = _c0;\n" : "",
].join("");
let data = this.getObjAndArgs("color", args);
let obj = data[0];
args = data[1];
obj.glsl = prefix + this.checkCode(code);
_hydra.synth.setFunction(obj);
return gS[obj.name].bind(self)(...args);
},
glslHsv: function (self, code, ...args) {
code = "vec3 hsv = _rgbToHsv(c0.rgb);\n" + code;
code = code + "\nreturn vec4(_hsvToRgb(hsv),c0.a);";
return this.glslColor(self, code, ...args);
},
glslCoord: function (self, code, ...args) {
let prefix = [
!code.includes("vec2 uv") ? "vec2 uv = _st;\n" : "",
!code.includes("vec2 st") ? "vec2 st = _st;\n" : "",
!code.includes("vec2 xy") ? "vec2 xy = _st;\n" : "",
].join("");
let data = this.getObjAndArgs("coord", args);
let obj = data[0];
args = data[1];
obj.glsl = prefix + this.checkCode(code);
_hydra.synth.setFunction(obj);
return gS[obj.name].bind(self)(...args);
},
glslCombine: function (self, code, texture, ...args) {
let prefix = [
!code.includes("vec4 c0") ? "vec4 c0 = _c0;\n" : "",
!code.includes("vec4 color0") ? "vec4 color0 = _c0;\n" : "",
!code.includes("vec4 c1") ? "vec4 c1 = _c1;\n" : "",
!code.includes("vec4 color1") ? "vec4 color1 = _c1;\n" : "",
].join("");
let data = this.getObjAndArgs("combine", args);
let obj = data[0];
args = data[1];
args.unshift(texture);
obj.glsl = prefix + this.checkCode(code);
_hydra.synth.setFunction(obj);
return gS[obj.name].bind(self)(...args);
},
glslCombineCoord: function (self, code, texture, ...args) {
let prefix = [
!code.includes("vec4 c0") ? "vec4 c0 = _c0;\n" : "",
!code.includes("vec4 color") ? "vec4 color = _c0;\n" : "",
!code.includes("vec2 uv") ? "vec2 uv = _st;\n" : "",
!code.includes("vec2 st") ? "vec2 st = _st;\n" : "",
!code.includes("vec2 xy") ? "vec2 xy = _st;\n" : "",
].join("");
let data = this.getObjAndArgs("combineCoord", args);
let obj = data[0];
args = data[1];
args.unshift(texture);
obj.glsl = prefix + this.checkCode(code);
_hydra.synth.setFunction(obj);
return gS[obj.name].bind(self)(...args);
},
};
_hydraScope.glsl = _glslExtension.glslSource.bind(_glslExtension);
gS.glslColor = function (code, ...args) {
return _glslExtension.glslColor(this, code, ...args);
};
gS.glslHsv = function (code, ...args) {
return _glslExtension.glslHsv(this, code, ...args);
};
gS.glslCoord = function (code, ...args) {
return _glslExtension.glslCoord(this, code, ...args);
};
gS.glslCombine = function (code, texture, ...args) {
return _glslExtension.glslCombine(this, code, texture, ...args);
};
gS.glslBlend = gS.glslCombine;
gS.glslCombineCoord = function (code, texture, ...args) {
return _glslExtension.glslCombineCoord(this, code, texture, ...args);
};
gS.glslModulate = gS.glslCombineCoord;
}