Book Image

Kivy Cookbook

By : Hugo Solis
Book Image

Kivy Cookbook

By: Hugo Solis

Overview of this book

Table of Contents (16 chapters)
Kivy Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating your own shader


We have already worked with shaders in this chapter, but now we will work with our own shaders. To show this, we will create an app with only the ShaderWidget.

Getting ready

We will use the preceding recipe Editing shaders to complete this recipe. Particularly, we will use the ShaderWidget class implemented there.

How to do it…

We will need a KV file and a Python file in this recipe. The KV file gives the size and position to the ShaderWidget, and the Python file will have our shader. Now, follow these steps:

  1. In the KV file, define the ShaderWidget rule.

  2. Add canvas for color, pos, and size:

    <ShaderWidget>:
    canvas:
        Color:
            rgb: 1, 0, 0
        Rectangle:
            pos: self.pos
            size: self.size
  3. In the Python file, import the usual Kivy packages.

  4. Also import the RenderContext and StringProperty packages.

  5. Define the header of Our shader.

  6. Define Our shader.

  7. Define ShaderWidget.

  8. Call ShaderWidget with Our shader:

    import kivy
    kivy.require('1.9.0') # Code tested in...