Latest  | Search | Go
Edit this page   |   Attach file 

  Home | Tutorials | Technical Reference | Runtime | API Documentation | ContentCreation > MaxShaderPlugin  

Realtime Viewport Shaders

Primary Author: TimJohnson

This document is a tutorial for the Shader portion of the Reality Max Tools plug-in for 3ds max (Figure 1), which is included in the latest files on this site. This allows you to view and configure shaders in 3dsmax, seeing the same results as you will in-game. This document will walk you through the basics and will answer some of the most common questions.

If you are not familiar with or have not installed MaxToolsPlugin, please start there!

 Figure 1 - Real-time shading in 3ds max with HLSL

Getting Started

HLSL, and the HLSL Plug-in for 3ds max 6+

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

Hardware Shading vs. Software Shading

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.


Direct 3D

The viewport manager in MAX works ONLY when MAX is configured for Direct3D. The viewport manager does not show up when running MAX in OpenGL? mode.

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.

Map Paths Management

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… .

Interface

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.

DirectX Manager HLSL rollout

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.

 

Techniques

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.

Toolbar Icons for HLSL Connection Editor

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.

Parameter Icons

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.

Icon

Function

Description

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.

                                               

Parameter Status Icons

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.

Viewing the HLSL Code

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.

Changing Your Default Text Editor

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.

Exploring the Default Shaders

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...

Working with lights

Note:        Pay attention! This part is important...

To preview the shaders in the viewport, you will have to select a light from the drop-down box under "LightPos" param. You will also need to match the technique type to the type of light you have chosen for the purposes of previewing. One shader is used for each light type, with prefixes such as _Ambient (Directional light), _Spot (Spotlight), and _Point (Omni light). Fortunately you don't need to worry about the technique light type for what appears in-game; the game will figure it all out for you. You are only setting this up for the purposes of previewing in the viewport. If you don't care about previewing (you should!) you can select whichever version you want, only paying attention to the main shader name, such as "Diffuse" or "DiffuseSpecular".

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!

You can only view one light on an object at a time in the Viewport, but again, the game will not have this limitation, and will use ALL lights touching the object, regardless of what you choose in the connections editor.

Each light has various properties that are used in the realtime shaders. Here's a table:

Info Omni Spotlight Directional
Attenuation Far Atten (Check 'Use' box!) Far Atten (Check 'Use' box!) No Atten
Color/Intensity Color and intensity Use projector map for color Color coming soon
Notes Most well-supported light Mainly intended for simple spotlighting or texture projection, nothing complex Basically a dummy light, used to tell the game to turn on ambient lighting in outdoor scenes. Very useful in conjunction with include/exclude lists for indoors areas

Light include/exclude lists

Using max's include and exclude lists can help performance on dynamic lights, or to ignore objects that are within the light radius but barely lit (such as is the case with projectors outside the projector cone.

Include lists are straightforward with static geometry and static lights. However, dynamic objects and dynamic lights have limitations:

  1. Dynamic Lights can only include Prefabs or StaticMesh? entities
  2. Dynamic Lights will auto-exclude all static geometry when using an Include list, and auto-include all static geometry when using an Exclude list
  3. Dynamic objects will be excluded if they walk into an Include-list light, unless they were on the Include list.

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.


FAQ & Known Issues

Note:        You can download DDS tools from the NVIDIA developer site.

Missing shader or texture

If the plugin complains about a missing .fx file or asks you to locate an .fx file, please navigate to your Reality Shaders folder and locate Max_Default.fx or the appropriate shader. The plugin should remember the directory for subsequent missing files. If not, you can add it via Paths Setup... Additional Directories in the Max Menu.

If the plugin complains about a missing nobump.dds or attenuation.dds, ensure these exist in your 3dsmax6\maps folder or that your max search paths includes the directory that does contain these files. If you are missing the files please download the latest install again. They should exist within the zip under the maps folder.

Mesh is inverted/invisible/black/incorrect

All meshes in viewport preview must be Editable Mesh, not Editable Poly or any other type, and any stack modifiers need to be collapsed to take effect.

If you've done this and are still having problems, check for negative scaling. There is an issue where negatively scaled (usually Mirror operation does this) meshes will have their lighting inverted. Be assured this is a preview issue and will not happen once imported into Reality Builder.

Last but not least ensure you have a light in the scene (omni is preferable) and you have the appropriate technique selected. Also be sure your graphics card supports the selected technique.

My Shader Doesn’t Seem to be Working Correctly

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.

Mesh Editing

Make sure you convert to Editable Mesh when working with HLSL meshes. Editable Poly is not supported and can cause strange rendering artifacts until converted to Editable Mesh. Alternatively you may want to disable the HLSL material while you do Editable Poly work.

Saving Changes to Parameter Settings

Changes made to any of the editable parameters will be saved with your scene file and can be exported to the game engine. 

How to to Request Features or Report Bugs

Send your feedback to tim@artificialstudios.com

MaxShaderPlugin   Edit | Attach | Ref-By | Printable | Diffs | r1.4 | > | r1.3 | > | r1.2 | More