This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
assets:shader [2017-08-26 16:58] – [Built-in variables] skyjake | assets:shader [2017-08-26 16:58] (current) – [Model renderer uniforms] skyjake | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Shader ====== | ||
+ | |||
+ | This article is about shader programs. Shaders are defined in [[modding: | ||
+ | |||
+ | |||
+ | ===== 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: | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | The corresponding uniform in the shader source code would look like: | ||
+ | |||
+ | <code glsl> | ||
+ | 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: | ||
+ | |||
+ | shader my_custom_shader { | ||
+ | variable uMapTime {} | ||
+ | ... | ||
+ | |||
+ | The GLSL source code also needs to declare a uniform with the exact same name, using the type from the table below. | ||
+ | |||
+ | ^ Variable ^ Type ^ Description | | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | |||
+ | ==== Model renderer uniforms ==== | ||
+ | |||
+ | In addition to the variables above, the model renderer automatically provides a set of uniforms for model shaders. To use these, one simply needs to declare the variable in GLSL and use it (no declarations in Doomsday' | ||
+ | |||
+ | ^ Uniform ^ Type ^ Description | | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | | '' | ||
+ | |||
+ | ===== Preprocessor definitions ===== | ||
+ | |||
+ | A shader definition may specify a dictionary of macros that will apply to both vertex and fragment shaders in the shader program. | ||
+ | |||
+ | For example, the " | ||
+ | |||
+ | defines $= {' | ||
+ | ' | ||
+ | ' | ||
+ | |||
+ | This becomes particularly useful when combined with custom GLSL functions. Additional functions can be included as separate source files. Below is an example of using Doomsday Script to modify an inherited shader. | ||
+ | |||
+ | < | ||
+ | shader model.thing.misc3.uvscroll { | ||
+ | inherits: model.skeletal.unlit.diffuse | ||
+ | script { | ||
+ | self.include.fragment += [' | ||
+ | self.defines[' | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | ===== 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. | ||
+ | |||
+ | <code glsl> | ||
+ | shader " | ||
+ | variable uAlphaLimit { value = 0 } | ||
+ | variable uAlpha | ||
+ | variable uColor | ||
+ | variable uOffsetUV | ||
+ | |||
+ | # Mapping when used with ModelDrawable. | ||
+ | textureMapping < | ||
+ | | ||
+ | include.vertex < | ||
+ | vertex = "/* put GLSL source code here */" | ||
+ | |||
+ | include.fragment < | ||
+ | fragment = " | ||
+ | uniform highp float uAlphaLimit; | ||
+ | uniform highp float uAlpha; | ||
+ | uniform highp vec3 uColor; | ||
+ | uniform highp vec2 uOffsetUV; | ||
+ | |||
+ | /* put GLSL source code here */" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||