Material Editor Reference

The material editor is currently very bare bones. Plans are made to overhaul the way materials work and thus developed, so the editor referenced here will eventually be replaced.

Below is a screenshot of the material editor:

The 3D viewport is a preview view and will be updated whenever the material is saved. It has the same movement controls as the scene editor:

LMB + Move Up/DownMove camera forward/backward
LMB + Move Left/RightTurn camera left/right
RMB + MoveTurn camera
LMB + RMB + MovePan camera

The empty space to the right of the 3D viewport is reserved for a future planned feature.

Materials are currently defined as XML with manually written shaders. You currently need a programmer to write more complex materials, but they can often be made generically for copy-and-pasting, requiring only minor changes. Materials can also have mutable public variables, but support for this via the editing tools and scripting environment is not very complete right now.

A material has a set of "channels" that define it's appearance. Below is a table of the channels used by the default renderer.

diffuseThe appearance of the material as if lit by a uniform white light. Think of it as the texture.
emissiveThe colours emitted by the material irrespective of lighting. The appearance of the material as if completely unlit.
specularThe specular power. Smaller values means a bigger specular highlight.
shininessThe shininess of the material. The specular contribution will be multiplied by this value.
normalThe normals to use when applying normal mapping. Usually this will be defined by a simple normal map image.
refractionThe same as normal, except for refraction.

Renderer's are free to define their own channels. For example, a cartoon renderer might have a "line_colour" channel to define the colour edge lines. The name of the channel is set with the "name" property of the "channel" tag.

A channel is defined by a shader function that is usually run on the fragment shader. There are two ways to define the function. The first is to define the function in a shader file and then specify the ID of the shader. The other is to simply inline the shader.

You must be aware that the shader language used depends on the renderer. The default renderer uses GLSL, so examples on this page will be using that. It should be easy to translate these to other shader languages.

Below is an example for defining the diffuse channel using the first technique:

<material>
    <channel name="diffuse" shaderid="Material_WhiteDiffuse" />
</material>

Below is an example for inlining the shader code directly:

<material>
    <channel name="diffuse">
        vec4 Diffuse()
        {
            return vec4(1.0, 1.0, 1.0, 1.0);
        }
    </channel>
</material>

The entry point function and return type will depend on the renderer. For the default renderer, it must be named the same as the channel, but with a capitalized first letter.

You can specify variables that can be set externally. For you shader programmers, these are uniform variables. The next example shows how you can use one of these variables with a texture.

<material>
    <channel name="diffuse">
        uniform sampler2D DiffuseTexture;

        vec4 Diffuse()
        {
            return texture2D(DiffuseTexture, VertexOutput_TexCoord.xy);
        }
    </channel>

    <defaultproperties>
        <texture2D name="DiffuseTexture">engine/textures/default.png</texture2D>
    </defaultproperties>
</material>

There are several things that need to be explained here. First, variables will be passed to the fragment shader from the vertex shader. These are prefixed with "VertexOutput_". You will see in the "Diffuse()" function that we use one of these variables. Below is a table of every variable passed in from the vertex shader.

VertexOutput_PositionThe position of the vertex in clip space.
VertexOutput_PositionVSThe position of the vertex in view space.
VertexOutput_PositionWSThe position of the vertex in world space.
VertexOutput_TexCoordThe main texture coordinate for texturing and normal mapping.
VertexOutput_NormalThe normal for lighting and normal mapping.
VertexOutput_TangentThe tangent for normal mapping and refraction.
VertexOutput_BitangentThe bitangent for normal mapping and refraction.
VertexOutput_ColourThe colour of the vertex.

The next thing of note in the above example if the use of public variables. These are just uniform variables. A default value for uniforms should be specified in the "defaultproperties" tag. It should be easy enough to understand how to define the defaults. Below are examples for different data types.

<texture2D name="MyVariable">engine/textures/default.png</texture2D>
<float name="MyVariable">1</float>
<float2 name="MyVariable">1 2</float2>
<float3 name="MyVariable">1 2 3</float3>
<float4 name="MyVariable">1 2 3 4</float4>

Blending

To enable blending, you specify the "blending" tag inside the main "material" tag. Below is an example:

<blending>
    <equation>Add</equation>
    <sourcefactor>SourceAlpha</sourcefactor>
    <destinationfactor>OneMinusSourceAlpha</destinationfactor>
</blending>

The above example is standard alpha blending. Below is a table defining the allowable blending properties:

equationThe blending equation to use. See table below.
sourcefactorThe property of the incoming colour to use in the chosen blending equation.
destinationfactorThe property of the colour currently stored in the colour buffer to use in the chosen blending equation.
colourThe colour to use when sourcefactor is set to ConstantColour, OneMinusConstantColour, ConstantAlpha or OneMinusConstantAlpha.

The table below are the allowable values for equation.

AddCt = (Cs * S) + (Cd * D)
SubtractCt = (Cs * S) - (Cd * D)
ReverseSubtractCt = (Cd * D) - (Cs * S)
MinCt = min(Cs, Cd)
MaxCt = max(Cs, Cd)

In the above equations:

  • Cs is the incoming colour.
  • Cd is the colour currently stored in the colour buffer.
  • C is the property defined by sourcefactor.
  • D is the property defined by destinationfactor.

Allowable values of sourcefactor and destinationfactor

Zero0, 0, 0, 0
One1, 1, 1, 1
SourceColourSrgb
OneMinusSourceColour1 - Srgb
SourceAlphaSa
OneMinusSourceAlpha1 - Sa
DestColourDrgb
OneMinusDestColour1 - Drgb
DestAlphaDa
OneMinusDestAlpha1 - Da
ConstantColourRGB components of the value defined by colour
OneMinusConstantColour1 minus RGB components of the value defined by colour
ConstantAlphaAlpha component of the value defined by colour
OneMinusConstantAlpha1 minus alpha component of the value defined by colour

In the above equations:

  • Crgb is the red, green and blue components of the source colour.
  • Ca is the alpha component of the source colour.
  • Drgb is the red, green and blue components of the destination colour.
  • Da is the alpha component of the destination colour.