-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractShader.js
More file actions
52 lines (51 loc) · 1.9 KB
/
extractShader.js
File metadata and controls
52 lines (51 loc) · 1.9 KB
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
const fs = require('fs');
const path = require('path')
const extractGLSL = (rootFolderName) => {
const rootPath = path.resolve(rootFolderName)
const dirAry = fs.readdirSync(rootPath);
dirAry.map((fileName) => {
const relativePath = path.join(rootFolderName, fileName)
const absolutePath = path.resolve(relativePath)
const stat = fs.statSync(absolutePath)
if (stat.isDirectory()) {
//
return extractGLSL(absolutePath)
} else {
const regExp = /VSHADER_SOURCE[\w\W]*\\n'/g
const content = fs.readFileSync(absolutePath, {
encoding: 'utf8'
})
const matchRes = content.match(regExp)
if (matchRes && matchRes[0] && typeof matchRes[0] === 'string') {
let codeStr = matchRes[0]
const vertRegExp = /VSHADER_SOURCE\s*=([\w\W]*?\})/
const fragRegExp = /FSHADER_SOURCE\s*=([\w\W]*?})/
const vertCodeStrRes = codeStr.match(vertRegExp)
let vertCode = vertCodeStrRes && vertCodeStrRes[1] || ''
const fragCodeStrRes = codeStr.match(fragRegExp)
let fragCode = fragCodeStrRes && fragCodeStrRes[1] || ''
vertCode = vertCode.replace(/\\n/g, '')
vertCode = vertCode.replace(/\'/g, '')
vertCode = vertCode.replace(/\+/g, '')
fragCode = fragCode.replace(/\\n/g, '')
fragCode = fragCode.replace(/\'/g, '')
fragCode = fragCode.replace(/\+/g, '')
// console.log(fragCodeStrRes);
const extName = path.extname(absolutePath)
const vertglslPath = absolutePath.replace(extName, '.vert.glsl')
const fragglslPath = absolutePath.replace(extName, '.frag.glsl')
if (vertCode.trim()) {
fs.writeFileSync(vertglslPath, vertCode, {
encoding: 'utf8'
})
}
if (fragCode.trim()) {
fs.writeFileSync(fragglslPath, fragCode, {
encoding: 'utf8'
})
}
}
}
})
}
extractGLSL('./')