mirror of https://github.com/kivy/kivy.git
Added new effects and improved example
This commit is contained in:
parent
c7960cbe8e
commit
8b113323dc
|
@ -20,6 +20,10 @@ from kivy.uix.effectwidget import (effect_monochrome,
|
||||||
effect_blue,
|
effect_blue,
|
||||||
effect_green,
|
effect_green,
|
||||||
effect_invert,
|
effect_invert,
|
||||||
|
effect_mix,
|
||||||
|
effect_flash,
|
||||||
|
effect_blur_h,
|
||||||
|
effect_blur_v,
|
||||||
effect_plasma)
|
effect_plasma)
|
||||||
|
|
||||||
class EffectSpinner(Spinner):
|
class EffectSpinner(Spinner):
|
||||||
|
@ -43,40 +47,115 @@ class SpinnerRow(BoxLayout):
|
||||||
effects.append(effect_green)
|
effects.append(effect_green)
|
||||||
if text == 'invert':
|
if text == 'invert':
|
||||||
effects.append(effect_invert)
|
effects.append(effect_invert)
|
||||||
|
if text == 'mix':
|
||||||
|
effects.append(effect_mix)
|
||||||
|
if text == 'flash':
|
||||||
|
effects.append(effect_flash)
|
||||||
|
if text == 'blur_h':
|
||||||
|
effects.append(effect_blur_h)
|
||||||
|
if text == 'blur_v':
|
||||||
|
effects.append(effect_blur_v)
|
||||||
if text == 'plasma':
|
if text == 'plasma':
|
||||||
effects.append(effect_plasma)
|
effects.append(effect_plasma)
|
||||||
if self.effectwidget:
|
if self.effectwidget:
|
||||||
self.effectwidget.effects = effects
|
self.effectwidget.effects = effects
|
||||||
|
|
||||||
example = Builder.load_string('''
|
example = Builder.load_string('''
|
||||||
|
#:import Vector kivy.vector.Vector
|
||||||
BoxLayout:
|
BoxLayout:
|
||||||
orientation: 'vertical'
|
orientation: 'vertical'
|
||||||
BoxLayout:
|
BoxLayout:
|
||||||
EffectWidget:
|
EffectWidget:
|
||||||
id: effect1
|
id: effect1
|
||||||
Image:
|
Widget:
|
||||||
size_hint: 1, 1
|
canvas:
|
||||||
pos_hint: {'x': 0, 'y': 0}
|
Color:
|
||||||
source: 'colours.png'
|
rgba: 1, 0, 0, 1
|
||||||
allow_stretch: True
|
Ellipse:
|
||||||
keep_ratio: False
|
pos: Vector(self.pos) + 0.5*Vector(self.size)
|
||||||
Button:
|
size: 0.4*Vector(self.size)
|
||||||
text: 'Hello world'
|
Color:
|
||||||
size_hint: None, None
|
rgba: 0, 1, 0.3, 1
|
||||||
size: 150, 100
|
Ellipse:
|
||||||
pos_hint: {'center_x': 0.25, 'center_y': 0.5}
|
pos: Vector(self.pos) + 0.1*Vector(self.size)
|
||||||
on_press: print('Button pressed!')
|
size: 0.6*Vector(self.size)
|
||||||
|
Color:
|
||||||
|
rgba: 0.5, 0.5, 0.2, 1
|
||||||
|
Ellipse:
|
||||||
|
pos: Vector(self.pos) + Vector([0, 0.6])*Vector(self.size)
|
||||||
|
size: 0.4*Vector(self.size)
|
||||||
|
Color:
|
||||||
|
rgba: 1, 0.8, 0.1, 1
|
||||||
|
Ellipse:
|
||||||
|
pos: Vector(self.pos) + Vector([0.5, 0])*Vector(self.size)
|
||||||
|
size: 0.4*Vector(self.size)
|
||||||
|
Color:
|
||||||
|
rgba: 0, 0, 0.8, 1
|
||||||
|
Line:
|
||||||
|
points:
|
||||||
|
[self.x, self.y,
|
||||||
|
self.x + self.width, self.y + 0.3*self.height,
|
||||||
|
self.x + 0.2*self.width, self.y + 0.1*self.height,
|
||||||
|
self.x + 0.85*self.width, self.y + 0.72*self.height,
|
||||||
|
self.x + 0.31*self.width, self.y + 0.6*self.height,
|
||||||
|
self.x, self.top]
|
||||||
|
width: 1
|
||||||
|
Color:
|
||||||
|
rgba: 0, 0.9, 0.1, 1
|
||||||
|
Line:
|
||||||
|
points:
|
||||||
|
[self.x + self.width, self.y + self.height,
|
||||||
|
self.x + 0.35*self.width, self.y + 0.6*self.height,
|
||||||
|
self.x + 0.7*self.width, self.y + 0.15*self.height,
|
||||||
|
self.x + 0.2*self.width, self.y + 0.22*self.height,
|
||||||
|
self.x + 0.3*self.width, self.y + 0.92*self.height]
|
||||||
|
width: 2
|
||||||
EffectWidget:
|
EffectWidget:
|
||||||
id: effect2
|
id: effect2
|
||||||
size: 100, 100
|
Widget:
|
||||||
Image:
|
canvas:
|
||||||
id: im
|
Color:
|
||||||
pos: self.parent.pos
|
rgba: 1, 0, 0, 1
|
||||||
size: self.parent.size
|
Ellipse:
|
||||||
source: 'colours2.png'
|
pos: Vector(self.pos) + 0.5*Vector(self.size)
|
||||||
allow_stretch: True
|
size: 0.4*Vector(self.size)
|
||||||
keep_ratio: False
|
Color:
|
||||||
on_touch_down: print(self.size, self.pos)
|
rgba: 0, 1, 0.3, 1
|
||||||
|
Ellipse:
|
||||||
|
pos: Vector(self.pos) + 0.1*Vector(self.size)
|
||||||
|
size: 0.6*Vector(self.size)
|
||||||
|
Color:
|
||||||
|
rgba: 0.5, 0.5, 0.2, 1
|
||||||
|
Ellipse:
|
||||||
|
pos: Vector(self.pos) + Vector([0, 0.6])*Vector(self.size)
|
||||||
|
size: 0.4*Vector(self.size)
|
||||||
|
Color:
|
||||||
|
rgba: 1, 0.8, 0.1, 1
|
||||||
|
Ellipse:
|
||||||
|
pos: Vector(self.pos) + Vector([0.5, 0])*Vector(self.size)
|
||||||
|
size: 0.4*Vector(self.size)
|
||||||
|
Color:
|
||||||
|
rgba: 0, 0, 0.8, 1
|
||||||
|
Line:
|
||||||
|
points:
|
||||||
|
[self.x, self.y,
|
||||||
|
self.x + self.width, self.y + 0.3*self.height,
|
||||||
|
self.x + 0.2*self.width, self.y + 0.1*self.height,
|
||||||
|
self.x + 0.85*self.width, self.y + 0.72*self.height,
|
||||||
|
self.x + 0.31*self.width, self.y + 0.6*self.height,
|
||||||
|
self.x, self.top]
|
||||||
|
width: 1
|
||||||
|
Color:
|
||||||
|
rgba: 0, 0.9, 0.1, 1
|
||||||
|
Line:
|
||||||
|
points:
|
||||||
|
[self.x + self.width, self.y + self.height,
|
||||||
|
self.x + 0.35*self.width, self.y + 0.6*self.height,
|
||||||
|
self.x + 0.7*self.width, self.y + 0.15*self.height,
|
||||||
|
self.x + 0.2*self.width, self.y + 0.22*self.height,
|
||||||
|
self.x + 0.3*self.width, self.y + 0.92*self.height]
|
||||||
|
width: 2
|
||||||
|
|
||||||
SpinnerRow:
|
SpinnerRow:
|
||||||
effectwidget: effect1
|
effectwidget: effect1
|
||||||
text: 'bg effects'
|
text: 'bg effects'
|
||||||
|
@ -100,7 +179,10 @@ BoxLayout:
|
||||||
|
|
||||||
<EffectSpinner>:
|
<EffectSpinner>:
|
||||||
text: 'none'
|
text: 'none'
|
||||||
values: ['none', 'monochrome', 'red', 'blue', 'green', 'invert', 'plasma']
|
values:
|
||||||
|
['none', 'monochrome',
|
||||||
|
'invert', 'plasma', 'mix',
|
||||||
|
'flash', 'blur_h', 'blur_v']
|
||||||
|
|
||||||
''')
|
''')
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ draw children to Fbo and apply custom shader to a RenderContext.
|
||||||
|
|
||||||
from kivy.clock import Clock
|
from kivy.clock import Clock
|
||||||
from kivy.lang import Builder
|
from kivy.lang import Builder
|
||||||
from kivy.uix.floatlayout import FloatLayout
|
from kivy.uix.boxlayout import BoxLayout
|
||||||
from kivy.properties import StringProperty, ObjectProperty, ListProperty
|
from kivy.properties import StringProperty, ObjectProperty, ListProperty
|
||||||
from kivy.graphics import (RenderContext, Fbo, Color, Rectangle,
|
from kivy.graphics import (RenderContext, Fbo, Color, Rectangle,
|
||||||
Translate, PushMatrix, PopMatrix,
|
Translate, PushMatrix, PopMatrix,
|
||||||
|
@ -54,7 +54,6 @@ void main (void){
|
||||||
gl_FragColor = frag_color * texture2D(texture0, tex_coord0);
|
gl_FragColor = frag_color * texture2D(texture0, tex_coord0);
|
||||||
}
|
}
|
||||||
'''
|
'''
|
||||||
# gl_FragColor = texture2D(texture0, tex_coord0);
|
|
||||||
|
|
||||||
shader_footer_effect = '''
|
shader_footer_effect = '''
|
||||||
void main (void){
|
void main (void){
|
||||||
|
@ -108,6 +107,86 @@ vec4 effect(vec4 color, sampler2D texture, vec2 tex_coords, vec2 coords)
|
||||||
}
|
}
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
effect_mix = '''
|
||||||
|
vec4 effect(vec4 color, sampler2D texture, vec2 tex_coords, vec2 coords)
|
||||||
|
{
|
||||||
|
return vec4(color.z, color.x, color.y, 1.0);
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
|
||||||
|
effect_flash = '''
|
||||||
|
vec4 effect(vec4 color, sampler2D texture, vec2 tex_coords, vec2 coords)
|
||||||
|
{
|
||||||
|
return color * abs(sin(time));
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
|
||||||
|
effect_blur_h = '''
|
||||||
|
vec4 effect(vec4 color, sampler2D texture, vec2 tex_coords, vec2 coords)
|
||||||
|
{
|
||||||
|
float dt = 0.5 * 1.0 / resolution.x;
|
||||||
|
vec4 sum = vec4(0.0);
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x - 4.0*dt, tex_coords.y)) * 0.05;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x - 3.0*dt, tex_coords.y)) * 0.09;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x - 2.0*dt, tex_coords.y)) * 0.12;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x - dt, tex_coords.y)) * 0.15;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x, tex_coords.y)) * 0.16;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x + dt, tex_coords.y)) * 0.15;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x + 2.0*dt, tex_coords.y)) * 0.12;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x + 3.0*dt, tex_coords.y)) * 0.09;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x + 4.0*dt, tex_coords.y)) * 0.05;
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
|
||||||
|
effect_blur_v = '''
|
||||||
|
vec4 effect(vec4 color, sampler2D texture, vec2 tex_coords, vec2 coords)
|
||||||
|
{
|
||||||
|
float dt = 0.5 * 1.0 / resolution.y;
|
||||||
|
vec4 sum = vec4(0.0);
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x, tex_coords.y - 4.0*dt)) * 0.05;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x, tex_coords.y - 3.0*dt)) * 0.09;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x, tex_coords.y - 2.0*dt)) * 0.12;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x, tex_coords.y - dt)) * 0.15;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x, tex_coords.y)) * 0.16;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x, tex_coords.y + dt)) * 0.15;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x, tex_coords.y + 2.0*dt)) * 0.12;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x, tex_coords.y + 3.0*dt)) * 0.09;
|
||||||
|
sum += texture2D(texture, vec2(tex_coords.x, tex_coords.y + 4.0*dt)) * 0.05;
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
|
||||||
|
effect_postprocessing = '''
|
||||||
|
vec4 effect(vec4 color, sampler2D texture, vec2 tex_coords, vec2 coords)
|
||||||
|
{
|
||||||
|
vec2 q = tex_coord0 * vec2(1, -1);
|
||||||
|
vec2 uv = 0.5 + (q-0.5);//*(0.9);// + 0.1*sin(0.2*time));
|
||||||
|
|
||||||
|
vec3 oricol = texture2D(texture0,vec2(q.x,1.0-q.y)).xyz;
|
||||||
|
vec3 col;
|
||||||
|
|
||||||
|
col.r = texture2D(texture0,vec2(uv.x+0.003,-uv.y)).x;
|
||||||
|
col.g = texture2D(texture0,vec2(uv.x+0.000,-uv.y)).y;
|
||||||
|
col.b = texture2D(texture0,vec2(uv.x-0.003,-uv.y)).z;
|
||||||
|
|
||||||
|
col = clamp(col*0.5+0.5*col*col*1.2,0.0,1.0);
|
||||||
|
|
||||||
|
//col *= 0.5 + 0.5*16.0*uv.x*uv.y*(1.0-uv.x)*(1.0-uv.y);
|
||||||
|
|
||||||
|
col *= vec3(0.8,1.0,0.7);
|
||||||
|
|
||||||
|
col *= 0.9+0.1*sin(10.0*time+uv.y*1000.0);
|
||||||
|
|
||||||
|
col *= 0.97+0.03*sin(110.0*time);
|
||||||
|
|
||||||
|
float comp = smoothstep( 0.2, 0.7, sin(time) );
|
||||||
|
//col = mix( col, oricol, clamp(-2.0+2.0*q.x+3.0*comp,0.0,1.0) );
|
||||||
|
|
||||||
|
gl_FragColor = vec4(col,1.0);
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
|
||||||
effect_plasma = '''
|
effect_plasma = '''
|
||||||
vec4 effect(vec4 color, sampler2D texture, vec2 tex_coords, vec2 coords)
|
vec4 effect(vec4 color, sampler2D texture, vec2 tex_coords, vec2 coords)
|
||||||
{
|
{
|
||||||
|
@ -141,7 +220,7 @@ class EffectFbo(Fbo):
|
||||||
raise Exception('failed')
|
raise Exception('failed')
|
||||||
|
|
||||||
|
|
||||||
class EffectWidget(FloatLayout):
|
class EffectWidget(BoxLayout):
|
||||||
|
|
||||||
fs = StringProperty(None)
|
fs = StringProperty(None)
|
||||||
|
|
||||||
|
@ -193,8 +272,13 @@ class EffectWidget(FloatLayout):
|
||||||
self.refresh_fbo_setup()
|
self.refresh_fbo_setup()
|
||||||
|
|
||||||
def update_glsl(self, *largs):
|
def update_glsl(self, *largs):
|
||||||
self.canvas['time'] = Clock.get_boottime()
|
time = Clock.get_boottime()
|
||||||
self.canvas['resolution'] = map(float, self.size)
|
resolution = [float(size) for size in self.size]
|
||||||
|
self.canvas['time'] = time
|
||||||
|
self.canvas['resolution'] = resolution
|
||||||
|
for fbo in self.fbo_list:
|
||||||
|
fbo['time'] = time
|
||||||
|
fbo['resolution'] = resolution
|
||||||
|
|
||||||
def on_effects(self, *args):
|
def on_effects(self, *args):
|
||||||
self.refresh_fbo_setup()
|
self.refresh_fbo_setup()
|
||||||
|
|
Loading…
Reference in New Issue