Tone mapping usage on custom materials

This tutorial demonstrates how to enable toneMapping for shapes that use custom materials. ToneMapping is a very useful feature as it provides already baked shadows to your shapes.

Usually tone maps are done in any modeling application as a result of all light emitters in the scene, then the tone map is mapped in a second UVW map channel. Of cource there is an easier way to tone map your shapes. There is a certain application available for that purposes and it is called PureLight. It makes your life easier by doing everything automatically(baking and unwrapping to a second map channel) and all it needs to get started with baking the tone maps is the import of your shapes. Here is an example of a tone map:

However, even getting your shape tone mapped is not satisfactory to enable the tone map feature in the custom materials. There are some small code modifications to be done to the shader to enable the tone map feature to the custom material that uses that certain shader.

We are going to demonstrate how to modify shader number 9 (Environment DOT3 Extended) from the "Shader Pack Vol.1" collection to get it using tone maps. Look at the pictures below:

Here is the well know testshape used for the shaders' demonstation. In this picture it uses a custom material with the "Environment DOT3 Extended" shader but still not tone mapped:

And here is the same shape using the same shader but now tone mapped:

You realize the advantages of tone maps. Ok let's get started with the code modifications:

All the modifications apply to the Pixel and Vertex Shaders of "Environment DOT3 Extended" from "Shader Pack Vol.1" colleciton.

open normaleV.hlsl. At line 11 locate this:

float2 texc : TEXCOORD0;

After it in a new line add this:

float2 texc2 : TEXCOORD1;

The result code will look like this:

float2 texc : TEXCOORD0;
float2 texc2 : TEXCOORD1;

At line 27 locate this:

float4 texc1 : TEXCOORD5;

After it in a new line add this:

float4 texc2 : TEXCOORD6;

The result code will look like this:

float4 texc1 : TEXCOORD5;
float4 texc2 : TEXCOORD6;

At line 45 locate this:

Out.B = float4(In.B, 1);

After it in a new line add this:

Out.texc2 = float4(In.texc2, 0, 0);

The result code will look like this:

Out.B = float4(In.B, 1);
Out.texc2 = float4(In.texc2, 0, 0);

Save the file.

Now open normaleP.hlsl. At line 8 locate this:

uniform sampler2D diff0 : register(S2);

After it in a new line add this:

uniform sampler2D tone0 : register(S3);

The result code will look like this:

uniform sampler2D diff0 : register(S2);
uniform sampler2D tone0 : register(S3);

At line 53 locate this:

float4 texc1 : TEXCOORD5;

After it in a new line add this:

float4 texc2 : TEXCOORD6;

The result code will look like this:

float4 texc1 : TEXCOORD5;
float4 texc2 : TEXCOORD6;

At line 75 locate this:

return Out;

Just above it add this:

float4 tonecol = tex2D(tone0, In.texc2);
Out.color *= -1 * log(1 - tonecol);

The result code will look like this:

float4 tonecol = tex2D(tone0, In.texc2);
Out.color *= -1 * log(1 - tonecol);
return Out;

Save the file.

These were all the required modifications to the Pixel and Vertex Shaders.

Now let's finally modify the custom material to enable the code changes to take advantage of our tone map.

Open environment_DOT3_extended.cs and modify it to look like this:

singleton CubemapData( SkyCubeMap1 )
{
cubeFace[0] = "./liman_sky1/left";
cubeFace[1] = "./liman_sky1/right";
cubeFace[2] = "./liman_sky1/front";
cubeFace[3] = "./liman_sky1/back";
cubeFace[4] = "./liman_sky1/top";
cubeFace[5] = "./liman_sky1/bottom";
};

singleton ShaderData(NormalDot3EnvShaderData)
{
DXVertexShaderFile = "shaders/common/liman/Environment_DOT3_Extended/normaleV.hlsl";
DXPixelShaderFile = "shaders/common/liman/Environment_DOT3_Extended/normaleP.hlsl";

pixVersion = 2.0;
};

singleton CustomMaterial(NormalDot3EnvMat)
{
mapTo = "purelight_test_Module_Common_LMA_testshape-Material0";

sampler["cube0"] = "$cubemap";
sampler["bump0"] = "normal01";
sampler["diff0"] = "stone01";
sampler["tone0"] = "LMA_testshape";

cubemap = SkyCubeMap1;

shader = NormalDot3EnvShaderData;
version = 2.0;
};

Download here the lightmapped testshape with all its textures.(The "Environment_DOT3_Extended" shader is not included in the download link because it is a part of "Shader Pack vol.1")

back...