Shader

Reference for the Shader class, which lets you compile and apply custom GPU shaders (vertex, fragment, geometry) in Ursina.

Shader

Shader(name='untitled_shader', language=Shader.GLSL, vertex=default_vertex_shader, fragment=default_fragment_shader, geometry='', **kwargs)

Located in ursina/shader.py

Important

Looking for shaders to add to your game? Browse community‑made shaders in the /assets folder.

👉 Browse Community Assets

Overview

The Shader class wraps Panda3D’s shader system. It lets you supply GLSL, Cg, HLSL or SPIR‑V source for vertex, fragment and optional geometry stages, compile them at runtime, then assign the shader to any Entity or Camera.

Constructor Arguments

ArgumentTypeDefaultDescription
namestr'untitled_shader'Identifier for debug or hot‑reload.
languageenumShader.GLSLOne of Shader.CG, Shader.GLSL, Shader.HLSL, Shader.SPIR_V.
vertexstrdefault_vertex_shaderSource code string for the vertex stage.
fragmentstrdefault_fragment_shaderSource code string for the fragment stage.
geometrystr''Source code for an optional geometry stage.
**kwargsanyAdditional Panda3D Shader parameters.

Class Fields

NameDescription
Shader.CGCg shading language constant.
Shader.GLSLGLSL shading language constant.
Shader.HLSLHLSL shading language constant.
Shader.SPIR_VSPIR‑V shading language constant.
.nameShader identifier.
.languageCurrent language constant.
.vertex, .fragment, .geometrySource code strings for each stage.
.default_inputUniforms set once at compile.
.continuous_inputUniforms updated every frame.
.compiledTrue once compile() has succeeded.

Properties

PropertyDescription
.nameGet/set the shader’s debug name.
.languageGet/set shading language.
.compiledTrue if the GPU program is linked.

Methods

MethodDescription
compile(shader_includes=True)Compile and link the shader. If shader_includes, inject common headers.
load(cls, language, vertex, fragment, geometry, **kwargs)Class method to create and compile a shader in one call.

Example Usage

from time import perf_counter
from ursina import *
from ursina import Ursina, Entity, held_keys, scene, EditorCamera, Shader

app = Ursina()
t0 = perf_counter()

# apply a fresh shader to a cube
cube = Entity(model='cube', shader=Shader())

# allow free camera
EditorCamera()

print('shader compile + setup time:', perf_counter() - t0)

def input(key):
    if held_keys['control'] and key == 'r':
        # recompile all shaders on the fly
        for e in scene.entities:
            if hasattr(e, '_shader'):
                e.shader.compile()
        
app.run()