-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreplaceShaders.js
39 lines (30 loc) · 1.29 KB
/
replaceShaders.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
const path = require("path");
const fs = require("fs");
// replace shader imports with strings in typescript output
const lib = path.resolve(__dirname, "lib/src");
function replaceShaders(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
if (fs.lstatSync(path.resolve(dir, file)).isDirectory()) {
replaceShaders(path.resolve(dir, file));
} else if (file.endsWith(".js")) {
let text = fs.readFileSync(path.resolve(dir, file), "utf-8");
const glslImportRegex = /import(.*)from ("(.*).glsl")/g;
const matches = text.match(glslImportRegex);
if (!matches) continue;
matches.forEach((m) => {
const p = m.match(/"(.*)"/)[0];
let file = path.resolve(dir, p.substring(1, p.length - 1));
file = file.replace("/lib", "");
const shaderSource = fs.readFileSync(file, "utf-8");
let shaderName = m.match(/import (.*) from/)[0];
shaderName = shaderName.substring("import ".length, shaderName.length - " from".length);
const variable = `const ${shaderName} = \`${shaderSource}\``;
text = text.replace(m, variable);
console.log(`Replaced shader import of ${shaderName} in ${file} with source.`);
});
fs.writeFileSync(path.resolve(dir, file), text);
}
}
}
replaceShaders(lib);