(original_data, data)
return data
Do some simple shader compression. This mostly just looks for variables
    to rewrite and doesn't do much else. There is better compression
    available and I use that for the final product, but this gets 95% of
    the way there.
Do some simple shader compression. This mostly just looks for variables
    to rewrite and doesn't do much else. There is better compression
    available and I use that for the final product, but this gets 95% of
    the way there.
Do some simple shader compression . This mostly just looks for variables to rewrite and doesn t do much else . There is better compression available and I use that for the final product but this gets 95% of the way there .
def compress_webgl_variables(original_data, data):
    """
    Do some simple shader compression. This mostly just looks for variables
    to rewrite and doesn't do much else. There is better compression
    available and I use that for the final product, but this gets 95% of
    the way there.
    """
    shaders = []
    is_in_shader = False

    # 1. Scan the file to find all of the shaders.
    for line in original_data.split("\n"):
        if "#version 300 es" in line or "//SHADER" in line:
            is_in_shader = True
        elif is_in_shader:
            if '`' in line:
                is_in_shader = False
            else:
                #print("Add", line.split("//")[0])
                shaders.append(line.split("//")[0])

    # 2. Look for the variables in the shader.
    shader_variables = []
    keywords = "bool vec2 vec3 vec4 mat3 mat4 sampler2D float int".split()
    all_new_words = []
    for line in shaders:
        new_words = []
        for kw in keywords:
            if kw in line:
                options = re.findall("^ [a-zA-Z_0-9,]*",line.split(kw)[1])
                if len(options) > 0:
                    new_words += [x.strip() for x in options[0].split(",")]
        new_words = [x for x in new_words if x not in ["gl_Position"] and len(x) > 1]
        all_new_words.extend(new_words)

    # 3. Rewrite the variables to use ascii letters (except i/j)
    letters = string.ascii_letters.replace("i","").replace("j","")
    letters = [x for x in letters]
    letters = letters + [a+b for a in letters for b in letters]
    for i,var_name in enumerate(sorted(set(all_new_words),key=len)[::-1]):
        data = data.replace(var_name, letters[i])
    data = data.replace(r'"\n"', "$SLASH_N$")
    data = data.replace(r"\n"," ")

    # 4. Remove all duplicate whitespace
    for _ in range(10):
        data = data.replace("  ", " ")

    # 5. Remove whitespace around symbols
    for symbol in "(){}<>=-+*/,&|;?:":
        for _ in range(5):
            data = data.replace(" "+symbol, symbol)
            data = data.replace(symbol+" ", symbol)
    data = data.replace("$SLASH_N$",r'"\n"')
    data = data.replace("#version 300 es","#version 300 es\\n")
    data = data.replace("//SHADER", "")
    
    return data
def compress_webgl_variables ( original_data , data ) : shaders = [ ] is_in_shader = False # 1. Scan the file to find all of the shaders. for line in original_data . split ( "\n" ) : if "#version 300 es" in line or "//SHADER" in line : is_in_shader = True elif is_in_shader : if '`' in line : is_in_shader = False else : #print("Add", line.split("//")[0]) shaders . append ( line . split ( "//" ) [ 0 ] ) # 2. Look for the variables in the shader. shader_variables = [ ] keywords = "bool vec2 vec3 vec4 mat3 mat4 sampler2D float int" . split ( ) all_new_words = [ ] for line in shaders : new_words = [ ] for kw in keywords : if kw in line : options = re . findall ( "^ [a-zA-Z_0-9,]*" , line . split ( kw ) [ 1 ] ) if len ( options ) > 0 : new_words += [ x . strip ( ) for x in options [ 0 ] . split ( "," ) ] new_words = [ x for x in new_words if x not in [ "gl_Position" ] and len ( x ) > 1 ] all_new_words . extend ( new_words ) # 3. Rewrite the variables to use ascii letters (except i/j) letters = string . ascii_letters . replace ( "i" , "" ) . replace ( "j" , "" ) letters = [ x for x in letters ] letters = letters + [ a + b for a in letters for b in letters ] for i , var_name in enumerate ( sorted ( set ( all_new_words ) , key = len ) [ : : - 1 ] ) : data = data . replace ( var_name , letters [ i ] ) data = data . replace ( r'"\n"' , "$SLASH_N$" ) data = data . replace ( r"\n" , " " ) # 4. Remove all duplicate whitespace for _ in range ( 10 ) : data = data . replace ( "  " , " " ) # 5. Remove whitespace around symbols for symbol in "(){}<>=-+*/,&|;?:" : for _ in range ( 5 ) : data = data . replace ( " " + symbol , symbol ) data = data . replace ( symbol + " " , symbol ) data = data . replace ( "$SLASH_N$" , r'"\n"' ) data = data . replace ( "#version 300 es" , "#version 300 es\\n" ) data = data . replace ( "//SHADER" , "" ) return data