3ds Max 的渲染系统模块是 Nitrous 视口显示系统的核心组件,提供了高性能的保留模式渲染 API。该模块允许插件在视口中进行高效绘制,支持硬件加速渲染和硬件命中测试。
显示管理器是渲染系统的核心接口,负责管理图形驱动器的状态和功能。
MaxSDK::GraphicsIDisplayManager.hInterface_ID(0x71218f68, 0x52ab4a11)FPStaticInterfaceGetIDisplayManager() 获取。namespace MaxSDK { namespace Graphics {
class IDisplayManager : public FPStaticInterface
{
public:
// 检查是否启用保留模式渲染
virtual bool IsRetainedModeEnabled() const = 0;
// 检查是否启用硬件命中测试
virtual bool IsHardwareHitTesting(ViewExp *vpt) const = 0;
// 获取设备能力
virtual bool GetDeviceCaps(DeviceCaps& caps) = 0;
};
}} // namespace MaxSDK::Graphics
关键功能:
- 保留模式检测:判断当前视口是否使用保留模式渲染
- 硬件命中测试:检查是否启用 GPU 加速的选择测试
- 设备能力查询:获取图形硬件的支持特性
相机接口定义了渲染相机的属性和变换矩阵。
MaxSDK::GraphicsGraphics/ICamera.hARefObjectnamespace MaxSDK { namespace Graphics {
class ICamera : public ARefObject
{
public:
// 工厂方法(已确认存在)
static RefPtr<ICamera> Create();
// 视图矩阵设置
virtual void SetViewMatrix(const Matrix44& viewMatrix) = 0;
virtual const Matrix44& GetViewMatrix() const = 0;
// 投影矩阵设置
virtual void SetProjectionMatrix(const Matrix44& projectionMatrix) = 0;
virtual const Matrix44& GetProjectionMatrix() const = 0;
// 目标距离管理
virtual void SetTargetDistance(float targetDistance) = 0;
virtual float GetTargetDistance() const = 0;
// 相机属性获取(返回 const 引用)
virtual const Point3& GetPosition() const = 0;
virtual const Point3& GetUp() const = 0;
virtual const Point3& GetDirection() const = 0;
// 透视检查
virtual bool IsPerspective() const = 0;
};
}} // namespace MaxSDK::Graphics
渲染几何体接口定义了可渲染的几何数据结构和绘制方法。
MaxSDK::GraphicsIRenderGeometry.hARefObject, BaseInterfaceServernamespace MaxSDK { namespace Graphics {
class IRenderGeometry : public ARefObject, public BaseInterfaceServer
{
public:
// 几何体绘制
virtual void Display(DrawContext& drawContext, int start, int count, int lod) = 0;
// 图元类型管理
virtual PrimitiveType GetPrimitiveType() = 0;
virtual void SetPrimitiveType(PrimitiveType type) = 0;
// 几何体统计信息
virtual size_t GetPrimitiveCount() = 0;
virtual size_t GetVertexCount() = 0;
virtual int GetStartPrimitive() const = 0;
// 流数据管理
virtual MaterialRequiredStreams& GetSteamRequirement() = 0;
virtual VertexBufferHandleArray& GetVertexBuffers() = 0;
virtual IndexBufferHandle& GetIndexBuffer() = 0;
};
}} // namespace MaxSDK::Graphics
管理渲染项的容器概念(概念保留),用于组织和管理场景中的可渲染对象。
MaxSDK::GraphicsGraphics/IRenderItemContainer.hMaxHeapOperators(注意:并非 ARefObject)namespace MaxSDK { namespace Graphics {
class IRenderItemContainer : public MaxHeapOperators
{
public:
virtual size_t GetNumberOfRenderItems() const = 0;
virtual RenderItemHandle GetRenderItem(size_t i) const = 0;
RenderItemHandle FindRenderItem(unsigned long descriptionBits) const;
RenderItemHandleArray FindAllRenderItems(unsigned long descriptionBits) const;
virtual void AddRenderItem(const RenderItemHandle& pRenderItem) = 0;
virtual void AddRenderItems(const IRenderItemContainer& renderItemContainer) = 0;
virtual void RemoveRenderItem(size_t i) = 0;
virtual void ClearAllRenderItems() = 0;
protected:
virtual ~IRenderItemContainer() {}
};
}} // namespace MaxSDK::Graphics
MaxSDK::GraphicsGraphics/VertexBufferHandle.h / Graphics/IndexBufferHandle.hnamespace MaxSDK { namespace Graphics {
class VertexBufferHandle : public SmartHandle
{
public:
VertexBufferHandle();
VertexBufferHandle(const VertexBufferHandle& from);
VertexBufferHandle& operator = (const VertexBufferHandle& from);
virtual ~VertexBufferHandle();
bool Initialize(size_t stride, size_t numVertices, void* initialData = nullptr,
BufferUsageType usage = BufferUsageStatic,
AccessType access = ReadWriteAcess,
BufferPoolType pool = BufferPoolTypeSystemBuffer,
VertexFieldType dataType = VertexFieldTypeUnknown);
void Clear();
size_t GetVertexStride() const;
size_t GetNumberOfVertices() const;
void SetNumberOfVertices(size_t numberOfVertices);
size_t GetCapacity() const;
unsigned char* Lock(size_t vertexIndex, size_t numberOfVertices,
AccessType accessType = WriteAcess);
void Unlock();
BufferUsageType GetBufferUsageType() const;
BufferPoolType GetBufferPoolType() const;
AccessType GetBufferAccessType() const;
VertexBufferHandle Clone();
bool CopyFrom(SmartHandle& srcBuffer);
bool CopyFrom(SmartHandle& srcBuffer, size_t start, size_t numVertices, size_t destStart);
bool RealizeToSysMemory(bool keepHWBuffer = false);
bool RealizeToHWMemory(bool keepSysBuffer = false);
};
class IndexBufferHandle : public SmartHandle
{
public:
IndexBufferHandle();
IndexBufferHandle(const IndexBufferHandle& from);
IndexBufferHandle& operator = (const IndexBufferHandle& from);
virtual ~IndexBufferHandle();
bool Initialize(IndexType type, size_t numIndices, void* initialData = nullptr,
BufferUsageType usage = BufferUsageStatic,
AccessType access = ReadWriteAcess,
BufferPoolType pool = BufferPoolTypeSystemBuffer);
IndexType GetType() const;
size_t GetNumberOfIndices() const;
unsigned char* Lock(size_t offset, size_t numberOfIndices, AccessType accessType);
void Unlock();
BufferUsageType GetBufferUsageType() const;
BufferPoolType GetBufferPoolType() const;
AccessType GetBufferAccessType() const;
IndexBufferHandle Clone();
size_t GetStartOffset() const;
void SetStartOffset(size_t offset);
bool CopyFrom(IndexBufferHandle& srcBuffer);
bool CopyFrom(IndexBufferHandle& srcBuffer, size_t start, size_t numIndices, size_t destStart);
bool RealizeToSysMemory(bool keepHWBuffer = false);
bool RealizeToHWMemory(bool keepSysBuffer = false);
};
}} // namespace MaxSDK::Graphics
IShaderManager 与 IRenderStateManager 均为 3ds Max 2022 公开 SDK 中的真实公开接口,位于 MaxSDK::Graphics 命名空间,可安全用于插件。
MaxSDK::GraphicsGraphics/IShaderManager.hISHADER_MANAGER_INTERFACE_ID Interface_ID(0x3fc568b8, 0xeea1adf)BaseInterfacenamespace MaxSDK { namespace Graphics {
class IShaderManager : public BaseInterface
{
public:
enum ShaderType { ShaderTypeMetaSL = 0, ShaderTypeAMG };
virtual Interface_ID GetID() { return ISHADER_MANAGER_INTERFACE_ID; }
virtual ShaderType GetShaderType() const =0;
virtual const MCHAR* GetShaderClassName() const =0;
virtual const MCHAR* GetShaderFile() const =0;
};
// 工厂:材质 / 贴图插件通过它创建并销毁 IShaderManager
class IShaderManagerCreator : public FPStaticInterface
{
public:
virtual IShaderManager* CreateShaderManager(IShaderManager::ShaderType type,
const MCHAR* className,
const MCHAR* shaderFileName,
ReferenceTarget * pRefTarget) = 0;
virtual void DeleteShaderManager(IShaderManager * pShaderGraphManager) = 0;
static IShaderManagerCreator* GetInstance();
};
}} // namespace MaxSDK::Graphics
MaxSDK::GraphicsGraphics/IRenderStateManager.hMaxHeapOperators(由 IPrimitiveRenderer::GetRenderStates() 取得)namespace MaxSDK { namespace Graphics {
class IRenderStateManager : public MaxHeapOperators
{
public:
// Alpha 混合
virtual void SetBlendEnable(bool bEnable) = 0;
virtual bool GetBlendEnable() = 0;
virtual void SetSourceBlend(BlendSelector value) = 0;
virtual BlendSelector GetSourceBlend() = 0;
virtual void SetDestinationBlend(BlendSelector value) = 0;
virtual BlendSelector GetDestinationBlend() = 0;
virtual void SetBlendOperation(BlendOperation value) = 0;
virtual BlendOperation GetBlendOperation() = 0;
virtual void SetBlendFactor(const Point4& factor) = 0;
virtual Point4 GetBlendFactor() = 0;
// 光栅化状态
virtual void SetCullMode(CullMode value) = 0;
virtual CullMode GetCullMode() = 0;
virtual void SetFillMode(FillMode value) = 0;
virtual FillMode GetFillMode() = 0;
virtual void SetPointSize(float value) = 0;
virtual float GetPointSize() = 0;
// 深度状态
virtual void SetDepthEnable(bool bEnable) = 0;
virtual bool GetDepthEnable() = 0;
virtual void SetDepthFunction(CompareFunction value) = 0;
virtual CompareFunction GetDepthFunction() = 0;
virtual void SetDepthWriteEnable(bool bEnable) = 0;
virtual bool GetDepthWriteEnable() = 0;
// 渲染状态栈
virtual void PushRenderStates() = 0;
virtual void PopRenderStates() = 0;
virtual void ApplyRenderStates() = 0;
protected:
virtual ~IRenderStateManager() {}
};
}} // namespace MaxSDK::Graphics
FragmentGraph 是 3ds Max 2022 公开 SDK 中的真实公开类,由若干 fragment 节点组成,可用于组合多 pass 渲染管线。
MaxSDK::GraphicsGraphics/FragmentGraph/FragmentGraph.hFRAGMENT_GRAPH_CLASS_ID Class_ID(0x32ca2b60, 0x2f2c4f4c)Fragmentnamespace MaxSDK { namespace Graphics {
class FragmentGraph : public Fragment
{
public:
FragmentGraph();
virtual ~FragmentGraph();
protected:
virtual bool DoEvaluate(void* evaluationContext);
virtual void HandleFlagChanged(FragmentFlags flag, bool oldValue);
public:
size_t GetNodeCount() const;
void AddNode(const FragmentPtr& pFragmentNode, const MSTR& nodeName);
FragmentPtr GetNode(size_t index) const;
FragmentPtr GetNode(const MSTR& nodeName) const;
const MSTR& GetNodeName(size_t index) const;
void RemoveNode(const MSTR& nodeName);
void RemoveNode(const FragmentPtr& pFragmentNode);
bool WireInput(size_t graphInputIndex, const MSTR& nodeName, size_t nodeInputIndex);
bool WireOutput(size_t graphOutputIndex, const MSTR& nodeName, size_t nodeOutputIndex);
bool WireNode(const MSTR& sourceNodeName, size_t sourceNodeOutputIndex,
const MSTR& targetNodeName, size_t targetNodeInputIndex);
bool LoadXML(const MSTR& filePathName);
bool SaveXML(const MSTR& filePathName);
virtual Class_ID GetClassID() const;
virtual Input* CreateInput(const Class_ID& typeID);
virtual Output* CreateOutput(const Class_ID& typeID);
int CheckValidity();
};
typedef RefPtr<FragmentGraph> FragmentGraphPtr;
}} // namespace MaxSDK::Graphics
已删除杜撰的
IView/IRenderView接口。3ds Max 2022 公开 SDK 中不存在这两个公开接口,相关内容系旧文档杜撰。
视口相关功能应通过公开接口(如 ViewExp、MaxSDK::Graphics::ICamera、IDisplayManager)访问,而非 IView / IRenderView。
// 初始化渲染系统
MaxSDK::Graphics::IDisplayManager* dm = GetIDisplayManager();
if (dm && dm->IsRetainedModeEnabled()) {
// 创建渲染几何体
MaxSDK::Graphics::IRenderGeometry* geometry = CreateRenderGeometry();
// 设置顶点和索引数据
geometry->GetVertexBuffers().Add(vertexBuffer);
geometry->SetIndexBuffer(indexBuffer);
// 创建渲染项并加入容器
RenderItemHandle renderItem = RenderItemHandle::Create(); // (待核实)
renderItem->SetGeometry(geometry);
renderContainer->AddRenderItem(renderItem);
}
// 设置渲染相机(Create() 工厂方法待核实)
MaxSDK::Graphics::ICamera* camera = /* 获取相机 */;
camera->SetViewMatrix(viewMatrix);
camera->SetProjectionMatrix(projMatrix);
camera->SetTargetDistance(100.0f);
bool persp = camera->IsPerspective();
IsRetainedModeEnabled() 检查当前渲染模式Mesh::render() 调用IsHardwareHitTesting() 检查硬件命中测试状态渲染系统设计考虑了与旧版本插件的兼容性:
3ds Max 的渲染系统模块提供了现代化、高性能的渲染架构,支持硬件加速的保留模式渲染。通过合理的接口设计和性能优化,该模块能够满足复杂 3D 场景的渲染需求,同时保持与旧版本插件的良好兼容性。
本模块用于 Nitrous 视口渲染相关插件(自定义对象 / Helper / 几何体显示),产物为通用插件,常见后缀为 .dlo(HelperObject 或 GeomObject 类插件)。
core.lib maxutil.lib geom.lib maxscrpt.lib Paramblk2.libgfx.lib GraphicsDriver.lib MaxGraphicsObjects.lib GraphicsUtility.lib FragmentGraph.lib DefaultRenderItems.lib#include "max.h"
#include "Graphics/IObjectDisplay2.h"
#include "Graphics/DrawContext.h"
#include "Graphics/IDisplayManager.h"
#include "Graphics/IShaderManager.h"
#include "Graphics/IRenderItemContainer.h"
#include "Graphics/ICamera.h"
using namespace MaxSDK::Graphics;
// 最小 IObjectDisplay2 实现:在保留模式下提交渲染项。
class MyObjectDisplay : public IObjectDisplay2
{
public:
MyObjectDisplay() {}
unsigned long GetObjectDisplayRequirement() const override { return 0; }
bool PrepareDisplay(const UpdateDisplayContext& prepareDisplayContext) override
{
return true;
}
bool UpdatePerNodeItems(const UpdateDisplayContext& updateDisplayContext,
UpdateNodeContext& nodeContext,
IRenderItemContainer& targetRenderItemContainer) override
{
// 取 IDisplayManager(FPStaticInterface,ID INITROUS_DISPLAY_MANAGER_INTERFACE_ID)
IDisplayManager* dm = GetIDisplayManager();
if (dm && dm->IsRetainedModeEnabled())
{
IShaderManagerCreator* smc = IShaderManagerCreator::GetInstance();
if (smc)
{
// 仅为示例:为每个着色器创建一个 IShaderManager 实例
IShaderManager* sm = smc->CreateShaderManager(
IShaderManager::ShaderTypeMetaSL, L"MyShader", L"", nullptr);
}
}
return true;
}
};
// 自定义渲染项:在 Display(DrawContext&) 中读取绘制状态(真实类 DrawContext)
class MyRenderItem : public ICustomRenderItem
{
public:
void Realize(DrawContext& drawContext) override {}
void Display(DrawContext& drawContext) override
{
const Matrix44& world = drawContext.GetWorldMatrix();
TimeValue t = drawContext.GetTime();
bool hit = drawContext.IsHitTest();
IVirtualDevice& dev = drawContext.GetVirtualDevice(); // 不长期持有
(void)world; (void)t; (void)hit; (void)dev;
}
size_t GetPrimitiveCount() const override { return 0; }
};
target_link_libraries(<Target> PRIVATE core maxutil geom maxscrpt Paramblk2 gfx GraphicsDriver MaxGraphicsObjects GraphicsUtility FragmentGraph DefaultRenderItems)