Assets/Shader

From Doomsday Wiki
Jump to: navigation, search

< Assets

This article is about shader programs. Shaders are defined in ScriptedInfo files named shaders.dei.

Variables

Declaring a variable

Shader variables are uniforms, i.e, constants that can be changed at runtime without recompiling the shader.

Variables must be declared separately from their declaration in the shader source code. This lets Doomsday know which uniforms are intended to be externally modifiable. Also, Doomsday can initialize the variables to a specified initial values — useful when the shader is used in a context where the value of a particular variable is not specified.

The variable declaration looks like this:

variable uVariableName { value = 0.0 }

The given initial value may either be a single number or an array:

  • value = 0 → float
  • value <0, 0> → vec2
  • value <0, 0, 0> → vec3
  • value <0, 0, 0, 0> → vec4

The corresponding uniform in the shader source code would look like:

uniform highp float uVariableName;

Built-in variables

The renderer provides built-in variables that can be used in any shader. To use one of these, the shader definition needs to declare the variable but omit any default value initializers. For example:

variable uMapTime {}
Variable Description
uMapTime Time in the current map, in seconds since the map was started. Does not increase while the game is paused.

Preprocessor definitions

A shader definition may specify a dictionary of macros that will apply to both vertex and fragment shaders in the shader program.

defines $= {'PREDEF_TRANSFORM_UV(uv)': 'uv = uv + vec2(0.5, 0.0)'}

Example

Below is an example that demonstrates the use of a shader definition:

  • Declaring variables and their initial values.
  • Defining which texture maps are expected to be present, and therefore are available in the shader via attributes.
  • Including GLSL source files.
  • The shader source code.
shader "example" {
    variable uAlphaLimit { value = 0 }
    variable uAlpha      { value = 1.0 }
    variable uColor      { value <1, 1, 1> }
    variable uOffsetUV   { value <0, 0> }
 
    # Mapping when used with ModelDrawable.
    textureMapping <diffuse, normals>
 
    include.vertex <include/skeletal.glsl>
    vertex = "/* put GLSL source code here */"
 
    include.fragment <include/texture.glsl>
    fragment = "
        uniform highp float uAlphaLimit;
        uniform highp float uAlpha;
        uniform highp vec3 uColor;       // diffuse color multiplier
        uniform highp vec2 uOffsetUV;
 
        /* put GLSL source code here */"     
}