Custom shaders

IntermediateProgrammer material attributes. For example, you can write a shader to add textures to materials based on the objects’ world positions, or generate noise and use it to randomize material properties. As shaders are text files, you can add comments, enable and disable lines of code, and edit them like any other code file. This makes them easy to maintain and iterate. Custom color transforms.

Create a shader

  • Xenko Visual Studio extension installed. This is necessary to convert the shader files from XSL (Xenko shading language) to .cs files.
  • (Open in IDE) to open your project in Visual Studio.
  • Solution Explorer, right-click the project (eg MyGame.Game) and select Add > New item.
  • Class.
  • Name field, specify a name with the extension .xksl (eg MyShader.xksl), and click Add. .cs file from the .xksl file. The Solution Explorer lists it as a child of the .xskl file.
  • .xksl file, remove the existing lines, and write your shader. Shading language. RGBA 0;1;0;1):
    1. namespace MyGame{ shader MyShader : ComputeColor { override float4 Compute() { return float4(0, 1, 0, 1); } };}
    Note
    MyShader above) is the same as the filename.
    Note
    ComputeColor.As ‘ComputeColor always returns a float4 value, properties that take float values (eg metalness and gloss maps) use the first component (the red component) of the value returned by ComputeColor.
  • File > Save All).
  • In Game Studio, reload the assemblies. Asset View lists the shader in the same directory as your scripts (eg MyGame.Game).
    Note
    In some situations, Game Studio incorrectly identifies the shader as a script, as in the screenshot below: File > Reload project).

    Use a custom shader

    material attribute.
  • Asset View, select the material you want to use the shader in.
  • Property Grid, next to the property you want to control with the shader, click (Change) and select Shader.
  • MyShader). The property uses the shader you specify.
    Tip
    .xksl file in Visual Studio and save it, Game Studio automatically updates the project with your changes. If this doesn’t happen, restart Game Studio (File > Reload project).
    Note
    If you delete a shader from the project assets, to prevent errors, make sure you also remove it from the properties of materials that use it.

    Arguments and parameters

    Template arguments

    Template arguments (generics) don’t change at runtime. However, different materials can use different instances of the shader with different values. When the shaders are compiled, Xenko substitutes template arguments with the value you set in the Property Grid. Frequency:
    1. shader ComputeColorWave<float Frequency> : ComputeColor, Texturing{ override float4 Compute() { return sin((Global.Time) * 2 * 3.14 * Frequency); }};

    Parameters

    Parameters can be changed at runtime. Frequency:
    1. shader ComputeColorWave: ComputeColor, Texturing{ stage float Frequency = 1.0f; override float4 Compute() { return sin(( Global.Time ) * 2 * 3.14 * Frequency); }};
    Frequency, use:
    1. myMaterial.Passes[myPassIndex].Parameters.Set(ComputeColorWaveKeys.Frequency, MyFrequency);
    Note
    ComputeColorWaveKeys.Frequency is generated by the Xenko Visual Studio extension from the shader file.

    Compositions

    composition lets you set the Frequency in the Game Studio Property Grid:
    1. shader ComputeColorWave : ComputeColor, Texturing{ compose ComputeColor Frequency; override float4 Compute() { return sin(( Global.Time ) * 2 * 3.14 * Frequency.Compute().r); }};
    Then you can set the value in the material properties:

    Custom shader sample

    custom material shader sample project included with Xenko. ComputeColorWaveNormal shader is used in the displacement map and surface material properties.

    See also

  • Shading language
  • Custom color transforms
  • Material attributes
  • Xenko Visual Studio extension