-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest3.py
140 lines (113 loc) · 6.05 KB
/
test3.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
kv = '''
<ShaderTest>:
canvas:
Color:
rgb: 0.1, 0.2, 0.3
Rectangle:
size: self.size
pos: self.pos
Button:
text: 'foobar'
size_hint: 0.5, 0.5
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
'''
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.graphics import RenderContext
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.app import App
from kivy.graphics import Fbo, Color, Rectangle
class FboTest(Widget):
def __init__(self, **kwargs):
super(FboTest, self).__init__(**kwargs)
# first step is to create the fbo and use the fbo texture on other
# rectangle
with self.canvas:
# create the fbo
self.fbo = Fbo(size=self.size)
# show our fbo on the widget in different size
Color(1, 1, 1)
Rectangle(size=(32, 32), texture=self.fbo.texture)
Rectangle(pos=(32, 0), size=(64, 64), texture=self.fbo.texture)
Rectangle(pos=(96, 0), size=(128, 128), texture=self.fbo.texture)
# in the second step, you can draw whatever you want on the fbo
with self.fbo:
Color(1, 0, 0, .8)
Rectangle(size=(256, 64))
Color(0, 1, 0, .8)
Rectangle(size=(64, 256))
class ShaderTest(Widget):
_vs = StringProperty('''
#ifdef GL_ES
precision highp float;
#endif
varying vec2 vTexCoord;
void main(void){
gl_Position = ftransform();;
// Clean up inaccuracies
vec2 Pos;
Pos = sign(gl_Vertex.xy);
gl_Position = vec4(Pos, 0.0, 1.0);
// Image-space
vTexCoord = Pos * 0.5 + 0.5;
}''')
vfs = StringProperty('''
#ifdef GL_ES
precision highp float;
#endif
uniform sampler2D RTScene;
varying vec2 vTexCoord;
const float blurSize = 1.0/510.0;
void main(void){
vec4 sum = vec4(0.0);
// blur in y (vertical)
// take nine samples, with the distance blurSize between them
sum += texture2D(RTScene, vec2(vTexCoord.x - 4.0*blurSize, vTexCoord.y)) * 0.05;
sum += texture2D(RTScene, vec2(vTexCoord.x - 3.0*blurSize, vTexCoord.y)) * 0.09;
sum += texture2D(RTScene, vec2(vTexCoord.x - 2.0*blurSize, vTexCoord.y)) * 0.12;
sum += texture2D(RTScene, vec2(vTexCoord.x - blurSize, vTexCoord.y)) * 0.15;
sum += texture2D(RTScene, vec2(vTexCoord.x, vTexCoord.y)) * 0.16;
sum += texture2D(RTScene, vec2(vTexCoord.x + blurSize, vTexCoord.y)) * 0.15;
sum += texture2D(RTScene, vec2(vTexCoord.x + 2.0*blurSize, vTexCoord.y)) * 0.12;
sum += texture2D(RTScene, vec2(vTexCoord.x + 3.0*blurSize, vTexCoord.y)) * 0.09;
sum += texture2D(RTScene, vec2(vTexCoord.x + 4.0*blurSize, vTexCoord.y)) * 0.05;
gl_FragColor = sum;
}''')
hfs = StringProperty('''
#ifdef GL_ES
precision highp float;
#endif
uniform sampler2D RTBlurH; // this should hold the texture rendered by the horizontal blur pass
varying vec2 vTexCoord;
const float blurSize = 1.0/512.0;
void main(void){
vec4 sum = vec4(0.0);
// blur in y (vertical)
// take nine samples, with the distance blurSize between them
sum += texture2D(RTBlurH, vec2(vTexCoord.x, vTexCoord.y - 4.0*blurSize)) * 0.05;
sum += texture2D(RTBlurH, vec2(vTexCoord.x, vTexCoord.y - 3.0*blurSize)) * 0.09;
sum += texture2D(RTBlurH, vec2(vTexCoord.x, vTexCoord.y - 2.0*blurSize)) * 0.12;
sum += texture2D(RTBlurH, vec2(vTexCoord.x, vTexCoord.y - blurSize)) * 0.15;
sum += texture2D(RTBlurH, vec2(vTexCoord.x, vTexCoord.y)) * 0.16;
sum += texture2D(RTBlurH, vec2(vTexCoord.x, vTexCoord.y + blurSize)) * 0.15;
sum += texture2D(RTBlurH, vec2(vTexCoord.x, vTexCoord.y + 2.0*blurSize)) * 0.12;
sum += texture2D(RTBlurH, vec2(vTexCoord.x, vTexCoord.y + 3.0*blurSize)) * 0.09;
sum += texture2D(RTBlurH, vec2(vTexCoord.x, vTexCoord.y + 4.0*blurSize)) * 0.05;
gl_FragColor = sum;
}''')
def __init__(self, **kwargs):
super(ShaderTest, self).__init__(**kwargs)
self.canvas = RenderContext()
#canvas = ObjectProperty(RenderContext(shader='blur.glsl'))
# We'll update our glsl variables in a clock
Clock.schedule_interval(self.update_glsl, 1 / 60.)
with self.canvas:
# create the fbo
self.fbo = Fbo(size=self.size)
class ShaderTestApp(App):
def build(self):
return FboTest()
if __name__ == '__main__':
#Builder.load_string(kv)
ShaderTestApp().run()