Figure 1 - Real-time shading in 3ds max with HLSL
HLSL stands for "High-Level Shading Language", and is a language that makes it easier to program complex pixel and vertex shaders. This HLSL Shader allows the artist to view and adjust the editable parameters of a shader right from within 3ds max. All changes made to the shader settings are displayed in real-time in the native 3ds max viewports. This affords the artist more direct control of real-time 3D shaders. HLSL files can contain multiple Techniques. A shader can be programmed for high-end systems and can render a fallback method for less capable systems.
For the best results, use a high-end GPU. By and large, most shaders we use exercise the capabilities of the DX9 cards on the market (GeForce? FX, Radeon 9500 or better). Pixel and vertex shaders depend on such hardware to run.
Many HLSL shaders will render correctly on GeForce3? class hardware through the use of fallback techniques. These fallback techniques are not automatic but are programmed into the shader and allow complex shaders to run, in a limited fashion, on less capable hardware. You must select these from the technique box. Fallback techniques end in _PS11
Because they serve different functions, there is a substantial difference between most offline shaders and most real-time shaders. Content creation tools such 3ds max use a small number of very flexible shaders which can be customized in thousands of different ways. These shaders are great for rendering offline since they offer flexibility to the artist. All of those extra features, however, are going to preclude the shader from being rendered in real-time whether these extra features are used or not.
In contrast to offline shaders, most real-time shaders are lean and specialized. This doesn’t mean that they are simplistic, just more narrowly focused. Generally, they won’t include any unused options or features. For example, if a given material is not using bump-mapping then, for fastest performance, you should use a shader that does not allow for it. In practical terms, however, some amount of flexibility to a shader decreases production time and allows for better overall results. HLSL affords you this flexibility.
Note: You can set MAX to run in Direct3D mode by doing the following: From MAX’s main menu; Customize, Preferences…, Viewport tab, Choose Driver.
The plugin needs to be able to find the textures and shaders you are using. This is most important if you are trying to load scenes made by other people with different directories, or if your directories ever change and the plugin needs to relocate the files.
Note: To add a new path to your Search Paths do the following: From MAX’s main menu; Customize, Configure Paths…, External Files tab, Add… .
Working with the HLSL Plug-in for 3ds max is easy since you are working inside of the familiar 3ds max interface. The plugin adds two new interface elements to 3ds max: The Viewport Shader HLSL Rollout in the "DirectX Manager" of the 3ds max Material Editor and the HLSL Connection Editor window which is accessed through this rollout. Both interface components are shown below in (Figure 2.)
Figure 2 Interface of HLSL Plug-in for 3ds max.
HLSL shaders are assigned to objects via the 3ds max Material Editor. Standard max materials are used. When you assign a 3ds max material to a scene object, max, by default, draws that object with the standard viewport settings. To render with hardware shading you navigate to the "DirectX Manager" rollout of that material and enable "Reality Shader Material" (Figure 3 shows an outdated image, do not look for that option.) You can later disable hardware shading by un-checking Enabled or selecting None from the popup menu.
Figure 3 Enabling HLSL shading in 3ds max.
Note: Your object display may turn black or render incorrectly. This is because some HLSL shaders do not work correctly when pertinent parameters are undefined (i.e. a missing bitmap.) Software-rendered shaders will simply ignore a missing file but real-time shaders would be too slow if they contained an endless string of conditional code. Generally, if a real-time shader requires a diffuse map to render correctly and no diffuse map has been defined then that shader will render incorrectly. It is important to understand this fundamental difference between software and hardware-rendered shaders.
Click the Connection Editor… button to launch the Connection Editor (Figure 5.) This button is at the bottom of the Viewport Shader HLSL rollout in the material properties panel of your current material.
Figure 5 Launching the Connection Editor from the Material Editor
The HLSL Connection Editor (Figure 6) is where you edit shader parameters. Changes made here affect the shaders displayed in 3ds max viewports. The HLSL Connection Editor window is divided into two columns. The left-hand column contains the editable the scene parameter labels. The right-hand column contains the parameter values. When using the Connection Editor, you are connecting the shader parameter in the left-hand column (i.e. specColor) to the value in the right-hand column (i.e. RGB 178, 178, 255).
Note: When you first launch the Connection Editor the HLSL Shader parameter may be listed as <not found>. You’ll need to assign a shader. Several example HLSL files accompany the HLSL plugin. These should be located in the media directory of your installation.
Note: Not all HLSL files contain editable parameters. These need to be coded into the shader when it is written.
Figure 6 HLSL Connection Editor
Note: The settings you make in the Connection Editor do not affect the way an object renders using the 3ds max scanline renderer. These settings affect only viewport display.
The current technique is denoted directly under the shader name (Figure 6.) Each HLSL file usually presents a certain effect that the shader author is going for—such as bump mapping, environment mapping, or anisotropic lighting. The HLSL file contains one or more Techniques (or methods) of achieving the effect. Each Technique usually targets at a certain level of GPU functionality, so a HLSL file may contain one Technique for an advanced GPU with powerful pixel shaders, and other Techniques for older video hardware supporting only fixed-function texture blending. HLSL Techniques can be used for functionality, level-of-detail, or performance fallbacks.
The first technique in the HLSL text file that works on your hardware will be chosen by default. You can click on the Technique parameter to bring up a list of other techniques defined in the HLSL file. The first technique in the file is usually the most advanced form of the technique, designed for the most recent graphics hardware.
The following are brief descriptions of the various toolbar icons and their function. Many of these functions can also be accessed through the application menu.
Icon
Function
Description
Show Read Only
Toggles the display of Read Only parameters in the viewer. This function is also available in the Attributes menu.
Expand Hierarchy
Toggle expands or collapses the currently selected hierarchy. Clicking this button with the Properties parameter selected will Expand or Collapse All.
Notice that to left of each parameter in the scene is a small icon. These icons denote what type of parameter is shown. Not all parameters are editable. Some read only parameters are show because they can provide valuable feedback as to what parameters the shader is using for calculations.
Matrix Data
Matrix Value. Numeric-entry cells are provided for editing matrix values.
Vector Data
A row of numeric-entry cells are provided for editing data values.
Float Value
Float values can be edited either directly with a numeric-entry text box or by adjusting a slider bar.
Color
Clicking on a parameter with this icon will launch the Windows color picker.
Texture
This icon indicates a texture parameter. Textures are chosen via a standard file browser window. The HLSL Plug-in supports all popular texture formats.
Boolean Value
Parameters with a value of True or False.
Some HLSL shaders will not work correctly when pertinent parameters are undefined. Software-rendered shaders will simply ignore a missing file but real-time shaders would be too slow if they contained an endless string of conditional code. Generally, if a real-time shader requires a diffuse map to render correctly and no diffuse map has been defined then that shader will render incorrectly. It is important to understand this fundamental difference between software and hardware-rendered shaders.
The HLSL Viewer will notify you if a necessary shader parameter is undefined (i.e. a missing texture map.) by marking the shader parameter icon with a red square (Figure 7) Once the problem is corrected, the red warning icon will be removed.
Figure 7 Parameter Status icon shows an undefined parameter.
Note: To streamline the interface, dialog controls for editable parameters are not displayed unless you click in the white text box to the right of each parameter name.
Note: Each HLSL file contains a description of what it does and how it works. A snippet of this is shown in the HLSL Connection Editor but the full description is available by opening the HLSL file for edit.
You can view the actual HLSL code by clicking the Edit… button to the right of the current shader (Figure 8.) Changes made to the HLSL file can be saved and the changes should be reflected in the immediately in the viewport windows. Keep in mind that changes to the HLSL file are global changes and will affect all instances of the shader.
Figure 8 Click the Edit… button to edit the HLSL shader file.
Right-click any .fx file from Windows Explorer and Select Open With… from the popup menu. Select Choose Program… and select your preferred text editor. You may also choose to enable the ‘Always use this program to open these files’ checkbox.
Figure 9 Changing the default text editor.
The best way to learn HLSL is to explore the sample files and the collection of included HLSL files included with the installation. Most of the HLSL files have names representing the effects they cover, for example, Max_Default.fx houses the standard bumped diffuse and diffuse specular material shaders, whilst EnvMapping?.fx houses a more specialized environment mapping effect. The following example is a good starting point for working with the plug-in.
1. Launch 3ds max and open a new file. Add some shapes to the scene, an omni light, and a free spotlight. Open the Material Editor and select the material in the top left of the window. We'll use this as our test material. Enable HLSL by scrolling to the bottom of the material rollout and selecting "Reality Shader Material" from the Viewport Manager Popup list. Make sure to check the Enabled button. Your object display should now render, and will probably start off completely black. This is because we haven't yet configured the textures and lighting.
2. Click the Connection Editor… button to launch the HLSL Connection Editor. You will edit shader parameters from this window. Click in the white text field that read Max_Default.fx. The dialog controls for this field will become visible. If you ever wish to change the shader file, you can click the button that says Open. This will launch a file browser from which you can load a new HLSL file (Figure 12.)
Figure 12 Loading a new HLSL file from the HLSL Connection Editor.
3. Notice that a set of editable parameters are available in the connection editor for the shader. Some read-only information is sometimes shown as it may be referenced by some shaders. You can hide the information by clicking the Show Read Only It is a good idea to get familiar with the shader. Play with the controls. Read the description text and check for additional Techniques (Figure 13.). This shader contains many techniques, representing all the different types of lighting. This tutorial continues through the next section...
Note: Pay attention! This part is important...
Note: Spotlight projection maps are supported, and can make very effective fake shadows, or similar projected texture effects. To use these, select the projection map in the spotlight properties as normal, then from the connections editor drop-down light box, select or re-select the spotlight name. You MUST do this reselection to tell the shaders to use the new projector. Don't forget to set the appropriate _Spot technique!
Figure 13 Exploring a new HLSL shader.
4. You can click on any of the other parameters present in the Max_Default.fx shader to see what types of configurations are possible. If you make changes to the default parameters. These changes will be saved with your 3ds max file. Clicking on a bitmap parameter will allow you to load a new texture file. Clicking on of the color swatches will open up a standard Windows color picker (Figure 14.)
Figure 14 Windows color picker
5. As mentioned earlier, shaders allow you to assign scene lights to be used in the shader (Figure 15.) This can be very helpful for better simulating the characteristics of your real-time 3D renderer.
Figure 15 Configuring HLSL to use scene lights.
6. Once you have assigned a scene light to your scene light to you HLSL shader you can transform the lights in your scene and watch the shader reflect the changes (Figure 16.)
Figure 16 Dynamic lighting in 3ds max.
Note: You can download DDS tools from the NVIDIA developer site.
It’s important to understand that hardware-accelerated shaders work differently from software shaders. They have more restrictions, and in general, are more finicky because hardware shaders have to work with less information. This means missing textures and other settings that would normally have defaults in 3dsmax need to be explicitly set in the plugin. This is particularly true of the diffuse and bump map texture slots.
Changes made to any of the editable parameters will be saved with your scene file and can be exported to the game engine.
Send your feedback to tim@artificialstudios.com