-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.ts
349 lines (307 loc) · 9.54 KB
/
index.ts
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import _Perlin from "./src/Perlin.glsl";
import _Simplex from "./src/Simplex.glsl";
import _Worley from "./src/Worley.glsl";
import _BlendModes from "./src/BlendModes.glsl";
import _Common from "./src/Common.glsl";
import _GerstnerWave from "./src/GerstnerWave.glsl";
import _Curl from "./src/Curl.glsl";
export const Perlin: string = _Perlin;
export const Simplex: string = _Simplex;
export const Worley: string = _Worley;
export const BlendModes: string = _BlendModes;
export const Common: string = _Common;
export const GerstnerWave: string = _GerstnerWave;
export const Curl: string = _Curl;
const _all = [Perlin, Simplex, Worley, BlendModes, GerstnerWave, Curl];
export const All: string[] = _all;
const isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
//~START~
async function nodeFetch(s: string) {
// @ts-ignore
const fs = await import("fs");
// @ts-ignore
const path = (await import("path")).default;
const f = fs.readFileSync(path.resolve(s));
return {
text: async function () {
return f.toString();
},
};
}
//~END~
function getDeps(chunks: string[]) {
let names: string[] = [];
let deps: string[][] = [];
chunks.forEach((chunk: string) => {
const name = chunk.match(/#name: (.*)\n/);
const dep = chunk.match(/#deps: (.*)\n/);
names.push(name ? name[1] : "");
deps.push(dep ? dep[1].split(" ") : []);
});
return { names, deps };
}
function verifyDeps(chunks: string[]) {
const { names, deps } = getDeps(chunks);
let missing_dependancy: { [key: string]: number }[] = [];
let missing_dependant: string;
deps.forEach((dep, i) => {
dep.forEach((d, j) => {
if (!names.includes(d)) {
missing_dependancy.push({
outter: i,
inner: j,
});
missing_dependant = names[i];
}
});
});
if (missing_dependancy.length !== 0) {
const dependancies = missing_dependancy.map((e) => deps[e.outter][e.inner]);
throw new Error(`glNoise: Missing dependencies "${dependancies.join(", ")}" for "${missing_dependant}"`);
}
}
/**
* Loads Shaders without appeneding any Shader Chunks.
* @deprecated
*
* @async
* @param {string[]} shaders Array of paths to shaders.
* @returns {Promise<string[]>} Array of shaders corresponding to each path.
*
* @example
* const [vert, frag] = await loadShadersRaw(["vert.glsl", "frag.glsl"]);
*/
export async function loadShadersRaw(shaders: string | string[]) {
const _fetch = isNode ? nodeFetch : window.fetch;
const _shaders = Array.isArray(shaders) ? shaders : [shaders];
const output = await Promise.all(
_shaders.map(async (s) => {
return (await _fetch(s)).text();
})
);
return Array.isArray(shaders) ? output : output[0];
}
/**
* Loads shaders with specified Shader Chunks.
* If chunks not specified, all chunks will be appended.
* @deprecated
*
* @async
* @param {string[]} paths Array of Paths to shaders.
* @param {string[][]} chunks Array of chunks to append to each shader
* @param {string[]} headers Array of headers to be appended to each shader. Can be used to provide precision;
* @returns {Promise<string[]>} Array of shaders corresponding to each path with respective chunks applied.
*
* @example
* const head = `
* precision highp float;
* ${Common}
* `;
*
* const chunks = [
* [Perlin, Simplex],
* []
* ];
* const paths = [
* "vert.glsl",
* "frag.glsl",
* ];
* const [vert, frag] = await loadShaders(paths, chunks, head);
*/
export async function loadShaders(paths: string | string[], chunks?: string[][], headers?: string[]) {
if (!paths || paths.length <= 0) throw new Error("glNoise: 'loadShaders' requires atleast one path.");
let _paths: string[];
if (!Array.isArray(paths)) {
_paths = [paths];
} else {
_paths = paths;
}
if (!headers) headers = new Array(_paths.length).fill(Common);
let shaders: string[] = (await loadShadersRaw(_paths)) as string[];
if (chunks) {
shaders = shaders.map((s, i) => {
let c: string[];
if (chunks[i]) c = chunks[i];
else c = _all;
verifyDeps(c);
let h: string;
if (headers[i]) h = headers[i];
else h = Common;
return "\n" + h + "\n" + c.join("\n") + "\n" + s;
});
} else {
shaders = shaders.map((s, i) => {
let h: string;
if (headers[i]) h = headers[i];
else h = Common;
return "\n" + h + "\n" + _all.join("\n") + "\n" + s;
});
}
if (!Array.isArray(paths)) {
return shaders[0];
} else {
return shaders;
}
}
/**
* Patches shaders with specified Shader Chunks.
* If chunks not specified, all chunks will be appended.
*
* @async
* @param {string[]} paths Array of Shaders as strings.
* @param {string[][]} chunks Array of chunks to append to each shader
* @param {string[]} headers Array of headers to be appended to each shader. Can be used to provide precision;
* @returns {string[]} Array of shaders corresponding to each path with respective chunks applied.
*
* @example
* const head = `
* precision highp float;
* ${Common}
* `;
*
* const chunks = [
* [Perlin, Simplex],
* []
* ];
* const shaders = [
* `
* gl_Posiiton = ...
* `,
* gl_FragColor = ...
* `,
* ];
* const [vert, frag] = await loadShaders(shaders, chunks, head);
*/
export function patchShaders(shader: string | string[], chunks?: string[][], headers?: string[]) {
if (!shader || shader.length <= 0) throw new Error("glNoise: 'loadShaders' requires atleast one path.");
let _shader: string[];
if (!Array.isArray(shader)) {
_shader = [shader];
} else {
_shader = shader;
}
if (!headers) headers = new Array(_shader.length).fill(Common);
let output: string[] = _shader;
if (chunks) {
output = output.map((s, i) => {
let c: string[];
if (chunks[i]) c = chunks[i];
else c = _all;
verifyDeps(c);
let h: string;
if (headers[i]) h = headers[i];
else h = Common;
return "\n" + h + "\n" + c.join("\n") + "\n" + s;
});
} else {
output = output.map((s, i) => {
let h: string;
if (headers[i]) h = headers[i];
else h = Common;
return "\n" + h + "\n" + _all.join("\n") + "\n" + s;
});
}
if (!Array.isArray(shader)) {
return output[0];
} else {
return output;
}
}
/**
* Loads shaders with Shader Chunks for use with [THREE-CustomShaderMaterial.]{@link https://github.com/FarazzShaikh/THREE-CustomShaderMaterial}
* If chunks not specified, all chunks will be appended.
* @deprecated
*
* @async
* @param {Object} shaders Paths of shaders.
* * @param {string} shaders.defines Path of definitions shader.
* * @param {string} shaders.header Path of header shader.
* * @param {string} shaders.main Path of main shader.
* @param {string[]} chunks Array of chunks to append into the Header Section.
* @returns {Promise<Object>} CSM friendly shader.
*
* @example
* const chunks = [Perlin, Simplex];
* const paths = [
* defines: "defines.glsl",
* header: "header.glsl",
* main: "main.glsl",
* ];
* const {defines, header, main} = await loadShadersCSM(paths, chunks);
*/
export async function loadShadersCSM(
shaders: {
defines: string;
header: string;
main: string;
},
chunks?: string[]
) {
const _fetch = isNode ? nodeFetch : window.fetch;
let _defines: string = "",
_header: string = "",
_main: string = "";
if (shaders.defines) _defines = await (await _fetch(shaders.defines)).text();
if (shaders.header) _header = await (await _fetch(shaders.header)).text();
if (shaders.main) _main = await (await _fetch(shaders.main)).text();
if (!chunks)
return {
defines: "\n" + _defines + "\n" + Common,
header: "\n" + _all.join("\n") + "\n // ABCD \n" + _header,
main: "\n" + _main,
};
verifyDeps(chunks);
return {
defines: "\n" + _defines + "\n" + Common,
header: "\n" + chunks.join("\n") + "\n" + _header,
main: "\n" + _main,
};
}
/**
* Patches shaders with Shader Chunks for use with [THREE-CustomShaderMaterial.]{@link https://github.com/FarazzShaikh/THREE-CustomShaderMaterial}
* If chunks not specified, all chunks will be appended.
*
* @async
* @param {Object} shaders Paths of shaders.
* * @param {string} shaders.defines Path of definitions shader.
* * @param {string} shaders.header Path of header shader.
* * @param {string} shaders.main Path of main shader.
* @param {string[]} chunks Array of chunks to append into the Header Section.
* @returns {Object} CSM friendly shader.
*
* @example
* const chunks = [Perlin, Simplex];
* const shaders = [
* defines: "...",
* header: "...",
* main: "...",
* ];
* const {defines, header, main} = await loadShadersCSM(shaders, chunks);
*/
export function patchShadersCSM(
shaders: {
defines: string;
header: string;
main: string;
},
chunks?: string[]
) {
let _defines: string = "",
_header: string = "",
_main: string = "";
if (shaders.defines) _defines = shaders.defines;
if (shaders.header) _header = shaders.header;
if (shaders.main) _main = shaders.main;
if (!chunks)
return {
defines: "\n" + _defines + "\n" + Common,
header: "\n" + _all.join("\n") + "\n // ABCD \n" + _header,
main: "\n" + _main,
};
verifyDeps(chunks);
return {
defines: "\n" + _defines + "\n" + Common,
header: "\n" + chunks.join("\n") + "\n" + _header,
main: "\n" + _main,
};
}