14.07.2015 Views

Integrating DirectX Shaders in 3ds Max A Developer's ... - Visionday

Integrating DirectX Shaders in 3ds Max A Developer's ... - Visionday

Integrating DirectX Shaders in 3ds Max A Developer's ... - Visionday

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong><strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> PerspectiveNeil HazzardSoftware Eng<strong>in</strong>eerAutodesk Media & Enterta<strong>in</strong>ment Division1


Overview•Real time <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> max•Support for <strong>DirectX</strong> Effect files•<strong>DirectX</strong> Standard Material•SDK Support for DX <strong>Shaders</strong><strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 2© 2005 Autodesk


Real time <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> max<strong>3ds</strong> max 2.0 First <strong>DirectX</strong> Release <strong>DirectX</strong> 5.0 T & L <strong>in</strong> software<strong>3ds</strong> max 3.0 <strong>DirectX</strong> 6.0 Texture Improvements Very Similar to max 2.0<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 3© 2005 Autodesk


Real time <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> max<strong>3ds</strong> max 4.2 First DCC tool to support DX8 and HWshaders 1122 l<strong>in</strong>es of max code, 41 l<strong>in</strong>es of DX Cube Map sample<strong>3ds</strong> max 5.1 First <strong>DirectX</strong> 9 Support MetalBump Shader <strong>DirectX</strong> Manager 650 l<strong>in</strong>e of max code, 41 l<strong>in</strong>e of DX<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 4© 2005 Autodesk


Real time <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> max<strong>3ds</strong> max 6.0 <strong>DirectX</strong> Effect file support 0 l<strong>in</strong>es of max code, 41 l<strong>in</strong>es of DX<strong>3ds</strong> max 7.0 <strong>DirectX</strong> Standard Material First to support DXSAS 0 l<strong>in</strong>es of max code, 0 l<strong>in</strong>es of DX<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 5© 2005 Autodesk


<strong>DirectX</strong> Standard Material - Overview•Activate•Save Effect File•Dynamic Parameters•Texture Toggle•Bl<strong>in</strong>n & Oren-Nayar Bl<strong>in</strong>nlight<strong>in</strong>g models<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 8© 2005 Autodesk


<strong>DirectX</strong> Standard Material - DetailsUses ATI’s Ashli Technology A GPU compiler Can convert between HLSL, GLSL and RenderMan Compiles to the target platform and language supported In <strong>3ds</strong> max’s case compiles to PS2.0 and PS2.X Queries the card to f<strong>in</strong>d the <strong>in</strong>struction and texture counts supported Will use Render To Texture when needed.Builds an HLSL representation of the Standard Material Supports dynamic b<strong>in</strong>d<strong>in</strong>gs for the most common Params Recompile needed for anyth<strong>in</strong>g else – e.g Chang<strong>in</strong>g the shader modelHLSL is compiled by ASHLI <strong>in</strong>to an DX effect Effect is then parsed by the default max parser Displayed us<strong>in</strong>g similar techniques as the <strong>DirectX</strong> MaterialCode is available <strong>in</strong> the maxsdk<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 9© 2005 Autodesk


SDK Support forDX <strong>Shaders</strong><strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 10© 2005 Autodesk


Extend<strong>in</strong>g the <strong>DirectX</strong> StandardMaterialAny texture can “<strong>in</strong>ject” HLSL codeThis allows custom code to be used to represent non standard orprocedural textureAn example from the SDK is marble.Texture Maps need to support a new <strong>in</strong>terface called IHLSLTexmapbool DoesSupportHLSL();void GetTexmapHLSLFunction(TCHAR * code, TCHAR * entryPo<strong>in</strong>t);void GetTextureData(TexDataList * list);<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 11© 2005 Autodesk


Extend<strong>in</strong>g the <strong>DirectX</strong> StandardMaterialBasic Implementation of GetTexmapHLSLFunction() – fullcode <strong>in</strong> marble.cppif(mapOn[0] && subTex[0]){_tcscat(HLSL, _T("uniform sampler2D MarbleCol1;\n"));}//… lots of HLSL// then copy it <strong>in</strong>to the buffer_tcscpy(code,HLSL);_tcscpy(entryPo<strong>in</strong>t,_T("marblefunc")); // used by compilerNote the use of “uniform”. This is required by the compiler forglobal variables.<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 12© 2005 Autodesk


Extend<strong>in</strong>g the <strong>DirectX</strong> StandardMaterialBasic Implementation of GetTextureData () – full code <strong>in</strong>marble.cppif(mapOn[0]&& subTex[0]){TextureData sample1;_tcscpy(sample1.UIName, _T("MarbleCol1"));_tcscpy(sample1.SamplerName, _T("MarbleCol1"));sample1.SubMapNum = 0;list->Append(1, &sample1);}This <strong>in</strong>formation is used to create the UI for the effect file<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 13© 2005 Autodesk


<strong>DirectX</strong> Effect System OverviewGFX Render<strong>DirectX</strong> MaterialGet ParserLoad Effect FileIParserLoaderRenderLoad TexturesSet ParametersParse FileIEffectParserRenderIRenderMesh<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 14© 2005 Autodesk


Extend<strong>in</strong>g the <strong>DirectX</strong> Effect SupportNew Effect Parsers can be written by Developers.New Interfaces IEffectParser and EffectDescriptor are availablefor use. EffectDescriptor:: CreateParser() EffectDescriptor::GetParserID() IEffectParser::ParseEffectFile() IEffectParser::PreRender() IEffectParser::Render()Each parser is a DLL, max ships with 2 The default max parser and DXSAS 0.8Effects conta<strong>in</strong>s <strong>in</strong>fo on what parser to load str<strong>in</strong>g ParamID = "0x0001"; // DXSAS<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 15© 2005 Autodesk


Extend<strong>in</strong>g the <strong>DirectX</strong> Effect SupportIEffectManager Used to store parameters that need to be set from <strong>3ds</strong> max UI Parameters – <strong>in</strong>t, float, Boolean, po<strong>in</strong>t4, texture and colour Transform data Provides access to the parameters via Trackview Used to <strong>in</strong>tegrate the effect <strong>in</strong>to <strong>3ds</strong> max Render to texture Vertex Pa<strong>in</strong>t Modifier Access to light<strong>in</strong>g data Position and Direction Colour Hotspot and Falloff<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 16© 2005 Autodesk


Writ<strong>in</strong>g your own <strong>DirectX</strong> shadersWhen you need more than max can deliver, youwill need to write your own code.Two choices, a Material/TextureMap or a <strong>DirectX</strong>Manager plug<strong>in</strong>Cubemap, LightMap and MetalBump are examplesof a <strong>DirectX</strong> Manager plug<strong>in</strong>DXStdMtl2 is an example of a full materialThere is little between them, so its really personalchoice. A <strong>DirectX</strong> Manager plug<strong>in</strong> requires theleast amount of actual <strong>3ds</strong> max code<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 17© 2005 Autodesk


Writ<strong>in</strong>g a <strong>DirectX</strong> Manager Plug<strong>in</strong>Create a basic plug<strong>in</strong> derived from class ReferenceTargetIDX9DataBridge & IDX9VertexShader Interfaces Provides the hooks <strong>in</strong>to <strong>3ds</strong> max No need to use IDX9PixelShader anymore Only needed if <strong>3ds</strong> max is draw<strong>in</strong>g the object, which limits theshader you can use. Use IRenderMesh & IRenderMeshCache helper classes Creates and renders a D3D compliant mesh Creates Normals, UVs andTangent Vectors IDX9VertexShader::DrawWireMesh() simply, return true or false Tell <strong>3ds</strong> max whether you have drawn the mesh or not. IDX9DataBridge::GetDXVersion() – return 9.0Look at MetalBump and Membrane samples found at<strong>Max</strong>sdk\samples\hardwareshaders<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 18© 2005 Autodesk


Writ<strong>in</strong>g a <strong>DirectX</strong> Hardware MaterialUse all the same <strong>in</strong>terfaces as the <strong>DirectX</strong> Manager plug<strong>in</strong>, butsimply derive from class Mtl <strong>in</strong>stead SetMtlFlag(MTL_HW_MAT_ENABLED) IMtlRender_Compatibility_MtlBase Allows you to supply a nice colour icon for material browser! Specifies what Software Renderer is supported.<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 19© 2005 Autodesk


Tips and TricksUse IDirect3DStateBlock9 cautiously <strong>Max</strong> can ask an object to be drawn various times, and this canbe a resource hog.Do the actual draw<strong>in</strong>g <strong>in</strong> the IDX9VerterShader::Initialize()methods Simplifies the multi material supportKeep DrawMeshStrips simple – just return True/FalseOnly support Mesh objects – keeps code simple. Use MNMesh::OutToTri()Use GetCOREInterface()->GetTime(), not TimeValue t = 0 Prevents unwanted evaluations of the modifier stackManage your resources, and RenderStates !! <strong>3ds</strong> max is easy to upset – typically you loose the viewportnames or transparency sett<strong>in</strong>gs. Typically DESTBLEND and SRCBLEND<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 20© 2005 Autodesk


<strong>3ds</strong> max resourcesSparks Support Program http://sparks.discreet.com Debug builds of max Advanced beta exposure Knowledge baseMe!! neil.hazzard@autodesk.com<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 21© 2005 Autodesk


Questions?<strong>Integrat<strong>in</strong>g</strong> <strong>DirectX</strong> <strong>Shaders</strong> <strong>in</strong> <strong>3ds</strong> <strong>Max</strong>A <strong>Developer's</strong> Perspective 22© 2005 Autodesk

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!