sandbox /webGLSandbox/webGL Study Page/compile.py

Language Python Lines 61
MD5 Hash 786ff31b2796b8aa24ea1d963379d7be Estimated Cost $1,236 (why?)
Repository https://bitbucket.org/Levantulee/sandbox View Raw File View Project SPDX
 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
from json import dumps
from os.path import splitext, abspath, dirname, join
from os import listdir
import re

if __name__ == '__main__':
    here = dirname(
        abspath(__file__)
    )

    shaders = {}
    sectionre = re.compile('^(\w+):$')
    files = []
            
    directives = '\n'.join([
        '#version 100',
        '#ifdef GL_ES',
        'precision highp int;',
        'precision highp float;',
        'precision highp vec2;',
        'precision highp vec3;',
        'precision highp vec4;',
        'precision highp ivec2;',
        'precision highp ivec3;',
        'precision highp ivec4;',
        '#endif',
    ])

    for name in listdir(here):
        if name.endswith('.shader'):
            fullname = join(here, name)
            shortname = splitext(name)[0]
            source = open(fullname).read()
            lines = source.split('\n')
            type = None
            types = {None: ''}
            filenum = len(files)
            for linenum, line in enumerate(lines):
                line = line.strip()
                if not line:
                    continue
                if line.startswith('//'):
                    continue

                match = sectionre.match(line)
                if match:
                    type = match.group(1)
                    types.setdefault(type, '')
                elif type:
                    types[type] += '#line %i %i\n%s\n'  % (linenum, filenum, line.strip())
            
            files.append({
                'name': shortname, 
                'path': fullname,
                'lines': lines,
                'fragment': '%s\n%s' % (directives, types['fragment']),
                'vertex': '%s\n%s' % (directives, types['vertex']),
            })

    open(join(here, 'shaders.json'), 'w').write(dumps(files))
Back to Top