summaryrefslogtreecommitdiff
path: root/themes/src/tools/shader.js
blob: 10889db46a05c0913d2ca68c9357b5c90aa253e2 (plain)
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
/*
  Responsible for generating .h files from GLSL files.
 */


const path = require('path');
const fs = require('fs');
const directory = path.join(__dirname, "..", "_shaders");
const out_directory = path.join(__dirname, "..", "shaders");

if (!fs.existsSync(out_directory)){
    fs.mkdirSync(out_directory);
}

const files = fs.readdirSync(directory);
files.forEach(file => {
  const filePath = path.join(directory, file);
  const text = String(fs.readFileSync(filePath));
  const splitText = text.split('\n');
  const splitName = file.split('.');
  const def = `SHADER_${splitName.join('_').toUpperCase()}`;
  let header_result = `#ifndef ${def} \n`;
  const filename = splitName.join('_');
  const header_name = filename + ".h";
  const cpp_name = filename + ".cpp";
  header_result += `#define ${def} \n`;
  header_result += `extern const char* ${def.toLowerCase()};\n`;
  header_result += '#endif\n';
  fs.writeFileSync(path.join(out_directory, header_name), header_result);
  
  let cpp_result = `#include "${header_name}"\n\n`
  cpp_result += `const char* ${def.toLowerCase()} = `
  splitText.forEach((line, index) => {
    cpp_result += "\"" + line + " \\n\"";
    if (index == splitText.length - 1)
      cpp_result += ";\n";
    else
      cpp_result += "\n";
  });
  fs.writeFileSync(path.join(out_directory,  cpp_name), cpp_result);
});