-
Notifications
You must be signed in to change notification settings - Fork 0
/
BonzomaticToShadertoy.py
84 lines (65 loc) · 2.39 KB
/
BonzomaticToShadertoy.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Importing re module
import re
import sys
import os.path
filename = "shader"
if len(sys.argv)>1:
filename=sys.argv[1]
filename = filename + ".webgl"
if not os.path.exists(filename):
print("Could not find file "+filename)
quit()
def replaceword(text, searchword, replaceword):
return re.sub("\\b"+searchword+"\\b", replaceword, text)
def replacebegin(text, searchword, replaceword):
return re.sub("\\b"+searchword, replaceword, text)
def removeline(text, search):
return re.sub(search+".*\\n", "", text)
def mround(match):
return str(round(float(match.group()),4))
def roundfloats(file):
floatmatch = re.compile(r"\d*\.\d+")
file = re.sub(floatmatch, mround, file)
return file
texnames = ["texFFT", "texFFTSmoothed", "texFFTIntegrated", "texPreviousFrame", "texChecker", "texNoise", "texTex1", "texTex2", "texTex3", "texTex4"]
def replacetextures(file):
texrepindex = 0
reftext=""
for t in texnames:
channame = "iChannel"+str(texrepindex%4)
result = re.subn("\\b"+t+"\\b", channame, file)
if result[1] > 0:
file = result[0]
reftext += "// " + t + " -> " + channame + "\n"
texrepindex += 1
if texrepindex > 4:
reftext += "// Warning, more than 4 textures used\n"
file = reftext + file
return file
with open(filename,'r+') as f:
# Reading the file data and store
# it in a file variable
file = f.read()
# Replacing the pattern with the string
# in the file data
file = replacebegin(file, "main\(\)", "mainImage( out vec4 out_color, in vec2 fragCoord )")
file = replaceword(file, "gl_FragCoord", "fragCoord")
file = replaceword(file, "fGlobalTime", "iTime")
file = replaceword(file, "fFrameTime", "iTimeDelta")
file = replaceword(file, "v2Resolution", "iResolution.xy")
file = removeline(file, "\\blayout\\(")
file = removeline(file, "#version")
file = removeline(file, "\\bprecision mediump float")
file = removeline(file, "\\bprecision highp int")
file = replaceword(file, "highp ", "")
# rounding float numbers
file = roundfloats(file)
file = replacetextures(file)
# Setting the position to the top
# of the page to insert data
f.seek(0)
# Writing replaced data in the file
f.write(file)
# Truncating the file size
f.truncate()
print("Output file: "+filename)