In this lesson you'll be using the HLSL to generate a volume texture procedurally. If you get stuck, refer to the Goal4_Solution.fx file inside the solutions directory for the suggested answer. Most of the steps can be implemented in many different ways, so don't worry if your code is different from the solution.
The procedural texture goal is divided into two tasks:
Extra Credit: Use if statements to create a volume checkerboard texture.
Preparation
Open Goal4.fx. If you have the SDK installed, the .fx file is located at:
(SDKRoot)\Samples\C++\Direct3D\Tutorials\HLSLWorkshop\
Description
The default render technique is HemisphereDiffuseSpecular, which uses the full hemispheric lighting equation developed in Goal 1 - Hemispheric Lighting Tutorial. In addition, the model shows the environment mapping developed in the previous lesson (Goal 3 - Environment Mapping):
Note that the animation calculation added in lesson 2 has been commented out to make things easier.
Preparation
Locate Goal 4A in the code and follow the instructions.
Description
Simply sample from the volume texture at the provided coordinate. Assume for now that the texture has already been procedurally generated, although this will actually be done during the next step.
Use the provided VolTexcoord and the tex3D - HLSL intrinsic function to do the texture lookup. Unlike the cubemap lookup (from lesson 2), it is easier to scale down the values of the volume map in the function that generates the volume instead of doing it here, so no scale should be required.
Solution
return tex3D(VolumeTexture, VolTexcoord);
The volume texture currently contains a solid yellow color, which yields the following render output when the sampled texture color is added to the diffuse color:
Preparation
Locate Goal 4B in the code and follow the instructions.
Description
Use the D3DXFillVolumeTextureTX function to execute this function for every texel in the volume texture. The height, width, depth, and function name are all described in the texture definition. The input is the position of the texel in the texture. In this case it is a three-dimensional coordinate that ranges from 0 to 1 on all axes (x = width, y = height, z = depth). The return value is the color stored at the texel position specified by the input position.
The noise - HLSL intrinsic is a good start for a procedural texture. Even by itself it can give a nice grainy texture. One thing to remember about the noise - HLSL function is that it is a good idea to scale up the access to it, because the range of noise on inputs of 0 to 1 is not as interesting as that of 0 to 500. You get a more random-looking set of values then. The noise - HLSL function outputs values between 0 and 1, so you might want to scale the return value down to limit the effect.
Solution
return noise(Pos * 500) * 0.5f;
The speckles added by the procedural texture give the appearance of metal shavings within a glossy paint layer:
This completes Goal 4 of the shader workshop. The tutorials are designed to make it easy to experiment with the code. Don't be afraid to experiment with different values for noise to see if you can create other interesting procedural textures.