View Markdown

This content has been deprecated

Shader Input

Including Header Files

Header files can be included in Shaders in the following two ways:

#include<common.inc>
#include "something.hlsl"

Header files enclosed in <> are built-in header files of the Mini Game framework. Custom header files defined by developers can be included using "", indexed by their relative file paths.

Defining and Using Textures

In the Mini Game framework, textures can be declared and sampled using macros when working with shaders:

// Declaration and sampling of standard textures
DECLARE_TEXTURE(_MainTex);
float4 color = SAMPLE_TEXTURE(name, TexCoord);

// Declaration and sampling of Cube Maps
DECLARE_CUBEMAP(SpeCube);
SAMPLE_CUBEMAP(SpeCube, reflectVec);

When setting textures via materials, the textures used must be declared in the .effect file. For details, refer to Effects and Materials

Defining Shader Variables

Currently, the Mini Game framework allows players to define Shader variables in a Constant Buffer for use by materials. This buffer can be named cbuffer Material. You can define it as shown below:

cbuffer Material
{
    float4 _MainTex_ST;
    float4 _Color;
}

Due to efficiency and compatibility concerns, the cbuffer definitions used in vertex shaders and pixel shaders must remain consistent. Unused uniforms will be eliminated during the optimization process.

The built-in Shader Variables in the Mini Game framework include the following:

WorldSpaceViewPosition: float3 Camera position
WorldSpaceLightDir: float3 Directional light direction
LightColor: float3 Light color
AmbientLight: float3 Ambient light color, used for multiple light sources
GameTime: float Current game time
ShadowStrength: float Shadow intensity
ShadowColor: float3 Shadow color
EnvironmentMap: Cubemap Environment map

Defining Vertex Input and Output

In the Mini Game framework, vertex inputs are defined. FEffect3DVertexInput is a built-in vertex structure, which is included in the shader via the common.inc file. Developers need to define the vertex shader outputs in the vertex shader.

#include <common.inc>

struct FVertexOutput
{
    float4 Position : SV_Position;
    float2 TexCoord : TEXCOORD0;        // world space light direction
    float3 LightDir : TEXCOORD1;        // world space view direction
    float3 ViewDir : TEXCOORD2;         // world space normal direction
    float3 WorldNormal:TEXCOORD3;
    LIGHTMAP_COORDS(4)                  //builtin lightmap coords
    FOG_COORDS(5)                       // builtin fog cooord
    SHADOW_COORDS(6)                    // builtin shadow coord
};

The vertex outputs are passed to the pixel shader as varyings in GLES2.0 for usage.

Writing a Set of Shaders

Building on the above content, let's look at a simple Shader implementation

// simple3D.vertex.hlsl

// Common header file, including built-in hlsl functions, macros and structure definitions
#include <common.inc>

// Material definition, must be consistent with the configuration in the .effect file
cbuffer material
{
    float4 _MainTex_ST;
    float4 _Color;
}
// vertex output, data passed from vertex to fragment
struct FVertexOutput
{
    float4 Position : SV_Position;
    float2 TexCoord : TEXCOORD0;
};

// vertex entry function
void Main(in FEffect3DVertexInput In, out FVertexOutput Out)
{
    // Built-in vertex processing, performing Particle/Skin/Line/Trail etc. vertex processing
	FVertexProcessOutput VPOut;
	Effect3DVertexProcess(In, VPOut);

    Out.Position = WorldToClipPosition(VPOut.WorldPosition);
    Out.TexCoord = TRANSFER_TEXCOORD(VPOut.TexCoord, _MainTex_ST);
}
// simple3D.pixel.hlsl

#include <common.inc>
cbuffer material
{
    float4 _MainTex_ST;
    float4 _Color;
}
struct FVertexOutput
{
    float4 Position : SV_Position;
    float2 TexCoord : TEXCOORD0;
};

// texture declaration, must be consistent with the declaration in the .effect file
DECLARE_TEXTURE(_MainTex);
float4 Main(in FVertexOutput In) : SV_Target0
{
    fixed4 texColor = SAMPLE_TEXTURE(_MainTex, In.TexCoord);
    return texColor * _Color;
}

Built-in Functions

  • Generic built-in functions
// Transform
// Transform local space position to world space
float3 ObjectToWorldNormal(float3 normal);
// Transform world space position to clip space
float4 ObjectToWorldPosition(float4 point);
// Calculate final UV coordinates by combining UV coordinates and scaleOffset
float2 TRANSFER_TEXCOORD(float2 TexCoord, float4 ScaleOffset);
  • Built-in functions used in vertex shaders
// Built-in vertex processing for Particle/Skin/Line/Trail, etc.
void Effect3DVertexProcess(in FEffect3DVertexInput In, out FVertexProcessOutput Out);
// Shadow parameter calculation in vertex shader
void TRANSFER_SHADOW(FVertexProcessOutput Out, float3 WorldPos);
// Fog parameter calculation in vertex shader
void TRANSFER_FOG(FVertexProcessOutput Out, float3 WorldPos);
// Light Map parameter calculation in vertex shader
void TRANSFER_LIGHTMAP(in FEffect3DVertexInput In, out FVertexProcessOutput Out);

// When Instancing is enabled, use the following built-in functions in the vertex shader for transformation
float3 ObjectToWorldDirInstancing(in FEffect3DVertexInput In, float3 dir);
float3 ObjectToWorldNormalInstancing(in FEffect3DVertexInput In, float3 normal);
float4 ObjectToWorldPositionInstancing(in FEffect3DVertexInput In, float4 point1);
  • Built-in functions used in pixel shaders
// Calculate shadow
float SHADOW_ATTENUATION(FVertexOutput In);
// Sample lightmap
float3 SAMPLE_LIGHTMAP(FVertexOutput In);
// Calculate fog value
APPLY_FOG(FVertexOutput In, out float4 Color);

Syntax Rules

  • Matrix Storage and Access Order In the runtime of the Mini Game framework, matrices are stored using column-major order by default. Similarly, in Shaders, matrices use column-major order by default. Matrix access follows the same column-major order as the runtime, using the matrix[col][row] format. The multiplication rules follow CG/GLSL conventions, i.e., mul(Matrix, Vector). For example, to perform an MVP matrix transformation, you can use the following approach:
mul(u_projection, mul(u_view, mul(u_world, position)));
  • Validity Validation The Shader tools used by developers perform HLSL syntax validation. For invalid syntax, the tool will display the erroneous shader and line number. If the common structures defined in the vertex shader and pixel shader, such as FVertexOutput and cbuffer, do not match, a Link error may occur.

Performance

The Shader compilation tool provided by the Mini Game framework performs a certain level of compilation optimization on the Shaders written by developers, including but not limited to:

  • Function inlining
  • Loop unrolling -->