Skip to content

Instantly share code, notes, and snippets.

@tito
Forked from tshirtman/radial_gradient.py
Created December 10, 2012 12:36

Revisions

  1. tito revised this gist Dec 10, 2012. 2 changed files with 67 additions and 10 deletions.
    29 changes: 19 additions & 10 deletions radial_gradient.py → cpuradialgradient.py
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,27 @@
    from kivy.graphics.texture import Texture
    from kivy.graphics import Rectangle
    from kivy.graphics import Rectangle, Color
    from kivy.uix.widget import Widget
    from kivy.graphics.opengl import glFinish
    from kivy.app import App
    from kivy.clock import Clock
    from time import time


    class RadialGradient(App):
    def build(self):
    Clock.schedule_once(self.create_tex, 1)
    self.w = Widget()
    return self.w
    root = Widget()

    glFinish()
    start = time()
    tex = self.create_tex()
    glFinish()
    end = time()
    print 'CPU RADIAL BUILD TIME: %.4fms' % ((end - start) * 100)

    with root.canvas:
    Color(1, 1, 1)
    Rectangle(texture=tex, size=(500, 500))

    return root

    def create_tex(self, *args):
    center_color = 255, 255, 0
    @@ -35,9 +47,6 @@ def create_tex(self, *args):
    int(border_color[c] * d))))

    tex.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte')
    return tex

    with self.w.canvas:
    Rectangle(texture=tex, size=(500, 500))


    RadialGradient().run()
    RadialGradient().run()
    48 changes: 48 additions & 0 deletions gpuradialgradient.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    from kivy.graphics import Fbo, Rectangle, Color
    from kivy.graphics.opengl import glFinish
    from kivy.uix.widget import Widget
    from kivy.app import App
    from time import time

    def radial_gradient(border_color=(1, 1, 0), center_color=(1, 0, 0),
    size=(64, 64)):

    fbo = Fbo(size=size)
    fbo.shader.fs = '''
    $HEADER$
    uniform vec3 border_color;
    uniform vec3 center_color;
    void main (void) {
    float d = clamp(distance(tex_coord0, vec2(0.5, 0.5)), 0., 1.);
    gl_FragColor = vec4(mix(center_color, border_color, d), 1);
    }
    '''

    # use the shader on the entire surface
    fbo['border_color'] = map(float, border_color)
    fbo['center_color'] = map(float, center_color)
    with fbo:
    Color(1, 1, 1)
    Rectangle(size=size)
    fbo.draw()

    return fbo.texture


    class GpuRadialGradient(App):
    def build(self):
    root = Widget()

    glFinish()
    start = time()
    tex = radial_gradient()
    glFinish()
    end = time()
    print 'GPU RADIAL BUILD TIME: %.4fms' % ((end - start) * 100)

    with root.canvas:
    Color(1, 1, 1)
    Rectangle(texture=tex, size=(500, 500))
    return root

    GpuRadialGradient().run()
  2. @tshirtman tshirtman revised this gist Dec 10, 2012. 1 changed file with 5 additions and 12 deletions.
    17 changes: 5 additions & 12 deletions radial_gradient.py
    Original file line number Diff line number Diff line change
    @@ -28,18 +28,11 @@ def create_tex(self, *args):
    b = y / (1.0 * sy_2)
    d = (a ** 2 + b ** 2) ** .5

    buf += chr(max(0,
    min(255,
    int(center_color[0] * (1 - d)) +
    int(border_color[0] * d))))
    buf += chr(max(0,
    min(255,
    int(center_color[1] * (1 - d)) +
    int(border_color[1] * d))))
    buf += chr(max(0,
    min(255,
    int(center_color[2] * (1 - d)) +
    int(border_color[2] * d))))
    for c in (0, 1, 2):
    buf += chr(max(0,
    min(255,
    int(center_color[c] * (1 - d)) +
    int(border_color[c] * d))))

    tex.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte')

  3. @tshirtman tshirtman created this gist Dec 10, 2012.
    50 changes: 50 additions & 0 deletions radial_gradient.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    from kivy.graphics.texture import Texture
    from kivy.graphics import Rectangle
    from kivy.uix.widget import Widget
    from kivy.app import App
    from kivy.clock import Clock


    class RadialGradient(App):
    def build(self):
    Clock.schedule_once(self.create_tex, 1)
    self.w = Widget()
    return self.w

    def create_tex(self, *args):
    center_color = 255, 255, 0
    border_color = 100, 0, 0

    size = (64, 64)
    tex = Texture.create(size=size)

    sx_2 = size[0] / 2
    sy_2 = size[1] / 2

    buf = ''
    for x in xrange(-sx_2, sx_2):
    for y in xrange(-sy_2, sy_2):
    a = x / (1.0 * sx_2)
    b = y / (1.0 * sy_2)
    d = (a ** 2 + b ** 2) ** .5

    buf += chr(max(0,
    min(255,
    int(center_color[0] * (1 - d)) +
    int(border_color[0] * d))))
    buf += chr(max(0,
    min(255,
    int(center_color[1] * (1 - d)) +
    int(border_color[1] * d))))
    buf += chr(max(0,
    min(255,
    int(center_color[2] * (1 - d)) +
    int(border_color[2] * d))))

    tex.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte')

    with self.w.canvas:
    Rectangle(texture=tex, size=(500, 500))


    RadialGradient().run()