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/Down | Move camera forward/backward |
LMB + Move Left/Right | Turn camera left/right |
RMB + Move | Turn camera |
LMB + RMB + Move | Pan 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.
diffuse | The appearance of the material as if lit by a uniform white light. Think of it as the texture. |
emissive | The colours emitted by the material irrespective of lighting. The appearance of the material as if completely unlit. |
specular | The specular power. Smaller values means a bigger specular highlight. |
shininess | The shininess of the material. The specular contribution will be multiplied by this value. |
normal | The normals to use when applying normal mapping. Usually this will be defined by a simple normal map image. |
refraction | The 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_Position | The position of the vertex in clip space. |
VertexOutput_PositionVS | The position of the vertex in view space. |
VertexOutput_PositionWS | The position of the vertex in world space. |
VertexOutput_TexCoord | The main texture coordinate for texturing and normal mapping. |
VertexOutput_Normal | The normal for lighting and normal mapping. |
VertexOutput_Tangent | The tangent for normal mapping and refraction. |
VertexOutput_Bitangent | The bitangent for normal mapping and refraction. |
VertexOutput_Colour | The 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>
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:
equation | The blending equation to use. See table below. |
sourcefactor | The property of the incoming colour to use in the chosen blending equation. |
destinationfactor | The property of the colour currently stored in the colour buffer to use in the chosen blending equation. |
colour | The colour to use when sourcefactor is set to ConstantColour, OneMinusConstantColour, ConstantAlpha or OneMinusConstantAlpha. |
The table below are the allowable values for equation.
Add | Ct = (Cs * S) + (Cd * D) |
Subtract | Ct = (Cs * S) - (Cd * D) |
ReverseSubtract | Ct = (Cd * D) - (Cs * S) |
Min | Ct = min(Cs, Cd) |
Max | Ct = max(Cs, Cd) |
In the above equations:
Allowable values of sourcefactor and destinationfactor
Zero | 0, 0, 0, 0 |
One | 1, 1, 1, 1 |
SourceColour | Srgb |
OneMinusSourceColour | 1 - Srgb |
SourceAlpha | Sa |
OneMinusSourceAlpha | 1 - Sa |
DestColour | Drgb |
OneMinusDestColour | 1 - Drgb |
DestAlpha | Da |
OneMinusDestAlpha | 1 - Da |
ConstantColour | RGB components of the value defined by colour |
OneMinusConstantColour | 1 minus RGB components of the value defined by colour |
ConstantAlpha | Alpha component of the value defined by colour |
OneMinusConstantAlpha | 1 minus alpha component of the value defined by colour |
In the above equations: