-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglsl_loader.js
79 lines (63 loc) · 1.88 KB
/
glsl_loader.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
module.exports = function (source){
let str = parseGLSL(source);
str = removeWhitespace(str);
let ans = "const myGLSL = GLSL`"+ mangle_underscore_functions_in_the_code(str)+"`;"
console.log(ans);
return ans;
}
function parseGLSL(str) {
let firstvariable = "`";
let secondvariable = "`";
let result = removeComments(str);
result = result.replace(/\n/g, '');
result = result.match(new RegExp(firstvariable + "(.*)" + secondvariable));
return result[1];
}
// Convert a string to a string hash.
function hashString(str) {
let hashInt = 0;
for (let i = 0; i < str.length; i++) {
hashInt += Math.pow(str.charCodeAt(i) * 31, str.length - i);
hashInt = hashInt & hashInt; // Convert to 32bit integer
}
// Now convert to a string.
const hashchars = ("" + hashInt).split('');
/* console.log(hashchars) */
let hash = "";
for(let ch of hashchars){
hash += String.fromCharCode(97+Number.parseInt(ch))
}
return hash;
}
function removeComments(str){
return str.replace(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm," ");
}
//This funcirton
function mangle_underscore_functions_in_the_code(str){
let words = str.split(" ");
let newStr = str;
const underscoreWords = new Set();
let SymbolMapping ={};
words.forEach(function(word) {
if(word.startsWith("_")){
word = word.split(".")[0];
word = word.split(";")[0];
word = word.split("(")[0];
word = word.split(")")[0];
underscoreWords.add(word);
}
});
underscoreWords.forEach(function(val) {
/* console.log(hashString(val)); */
SymbolMapping[val] = hashString(val);
});
for (let key in SymbolMapping) {
let val = SymbolMapping[key];
newStr = newStr.replace(new RegExp(key, 'g'), val);
}
return newStr;
}
//This function removes the whitespaces.
function removeWhitespace (str){
return str.replace(/\s\s+/g, ' ');
}