Skip to content

Instantly share code, notes, and snippets.

@gottadiveintopython
Created April 6, 2018 16:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gottadiveintopython/d9a2935560469bd8a8d0a890180003c7 to your computer and use it in GitHub Desktop.
Save gottadiveintopython/d9a2935560469bd8a8d0a890180003c7 to your computer and use it in GitHub Desktop.
simple gradient fill (GLSL, Kivy)
from kivy.app import runTouchApp
from kivy.core.window import Window
from kivy.factory import Factory
from kivy.lang import Builder
from kivy.graphics import RenderContext
from kivy.properties import StringProperty
Builder.load_string(r'''
<ShaderWidget>:
canvas:
Rectangle:
pos: self.pos
size: self.size
''')
class ShaderWidget(Factory.Widget):
fs = StringProperty(None)
def __init__(self, **kwargs):
self.canvas = RenderContext(
use_parent_projection=True,
use_parent_modelview=True,
use_parent_frag_modelview=True)
super().__init__(**kwargs)
def on_fs(self, __, value):
shader = self.canvas.shader
old_value = shader.fs
shader.fs = value
if not shader.success:
shader.fs = old_value
raise Exception('Failed to compile GLSL.')
def on_size(self, __, size):
self.canvas['resolution'] = [float(size[0]), float(size[1]), ]
GLSL_CODE = '''
uniform vec2 resolution;
void main(void)
{
float sx = gl_FragCoord.x / resolution.x;
float sy = gl_FragCoord.y / resolution.y;
gl_FragColor = vec4( 1.0, sx, sy, 1.0);
}
'''
root = ShaderWidget(fs=GLSL_CODE)
runTouchApp(root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment