Version

Quick search

Shader

The Shader class handles the compilation of the vertex and fragment shader as well as the creation of the program in OpenGL.

Todo

Include more complete documentation about the shader.

Header inclusion

New in version 1.0.7.

When you are creating a Shader, Kivy will always include default parameters. If you don’t want to rewrite this each time you want to customize / write a new shader, you can add the “$HEADER$” token and it will be replaced by the corresponding shader header.

Here is the header for the fragment Shader:

#ifdef GL_ES
    precision highp float;
#endif

/* Outputs from the vertex shader */
varying vec4 frag_color;
varying vec2 tex_coord0;

/* uniform texture samplers */
uniform sampler2D texture0;

uniform mat4 frag_modelview_mat;

And the header for vertex Shader:

#ifdef GL_ES
    precision highp float;
#endif

/* Outputs to the fragment shader */
varying vec4 frag_color;
varying vec2 tex_coord0;

/* vertex attributes */
attribute vec2     vPosition;
attribute vec2     vTexCoords0;

/* uniform variables */
uniform mat4       modelview_mat;
uniform mat4       projection_mat;
uniform vec4       color;
uniform float      opacity;

Single file glsl shader programs

New in version 1.6.0.

To simplify shader management, the vertex and fragment shaders can be loaded automatically from a single glsl source file (plain text). The file should contain sections identified by a line starting with ‘—vertex’ and ‘—fragment’ respectively (case insensitive), e.g.

// anything before a meaningful section such as this comment are ignored

---VERTEX SHADER--- // vertex shader starts here
void main(){
    ...
}

---FRAGMENT SHADER--- // fragment shader starts here
void main(){
    ...
}

The source property of the Shader should be set to the filename of a glsl shader file (of the above format), e.g. phong.glsl

class kivy.graphics.shader.Shader(unicode vs=None, unicode fs=None, unicode source=None)

Bases: builtins.object

Parameters:
vs: string, defaults to None

Source code for vertex shader

fs: string, defaults to None

Source code for fragment shader

fs

Fragment shader source code.

If you set a new fragment shader code source, it will be automatically compiled and will replace the current fragment shader.

source

glsl source code.

source should be the filename of a glsl shader that contains both the vertex and fragment shader sourcecode, each designated by a section header consisting of one line starting with either “–VERTEX” or “–FRAGMENT” (case insensitive).

New in version 1.6.0.

success

Indicate whether the shader loaded successfully and is ready for usage or not.

vs

Vertex shader source code.

If you set a new vertex shader code source, it will be automatically compiled and will replace the current vertex shader.