IGame(又称 3DXI,3ds Max 可扩展交互接口)是 3ds Max 面向游戏导出的场景数据访问层。它以 IGameScene 为主入口,提供对节点、材质、纹理及变换数据的简化访问,并借助 IGameConversionManager 统一处理坐标系转换,避免导出器自行做矩阵换算。
坐标系不在 IGameScene 上。通过全局函数 GetConversionManager() 取得 IGameConversionManager,再调用 SetCoordSystem():
IGameConversionManager* cm = GetConversionManager();
cm->SetCoordSystem(IGameConversionManager::IGAME_D3D); // 亦可为 IGAME_MAX / IGAME_OGL / IGAME_USER
IGameConversionManager::CoordSystem 枚举(定义于 IConversionManager.h):
enum CoordSystem { IGAME_MAX, IGAME_D3D, IGAME_OGL, IGAME_USER };
IGAME_MAX:3ds Max 右手系,Z 轴向上,+Y 朝屏幕内(默认)。IGAME_D3D:DirectX 左手系,Y 轴向上,+Z 朝外。IGAME_OGL:OpenGL 右手系,Y 轴向上,-Z 朝外。IGAME_USER:由 SetUserCoordSystem(UserCoord) 自定义。IGame.hGetIGameInterface() 取得。class IGameScene {
public:
virtual void SetPropertyFile(const MCHAR * fileName) = 0; // 须在 InitialiseIGame 前调用
virtual bool InitialiseIGame(bool selected = false) = 0; // 整场景,或仅选中节点
virtual bool InitialiseIGame(INode * root, bool Hierarchy = true) = 0;
virtual bool InitialiseIGame(Tab<INode *> nodes, bool Hierarchy = true) = 0;
virtual const MCHAR * GetSceneFileName() = 0;
virtual void SetStaticFrame(int frameNum) = 0;
virtual TimeValue GetSceneStartTime() = 0; // 动画起点(ticks)
virtual TimeValue GetSceneEndTime() = 0; // 动画终点(ticks)
virtual int GetSceneTicks() = 0; // 每帧 ticks 数
virtual int GetTopLevelNodeCount() = 0;
virtual IGameNode * GetTopLevelNode(int index) = 0;
virtual int GetTotalNodeCount() = 0;
virtual int GetRootMaterialCount() = 0;
virtual IGameMaterial * GetRootMaterial(int index) = 0;
virtual IGameNode * GetIGameNode(INode * node) = 0;
virtual IGameNode * GetIGameNode(ULONG NodeID) = 0;
virtual Tab<IGameNode*> GetIGameNodeByType(IGameObject::ObjectTypes Type) = 0;
virtual IGameMaterial * GetIGameMaterial(Mtl * mat) = 0;
virtual IGameTextureMap * GetIGameTextureMap(Texmap * texMap) = 0;
virtual void ReleaseIGame() = 0;
};
IGame.hGetWorldTM/GetLocalTM/GetObjectTM 返回 GMatrix(非 Matrix3)。class IGameNode {
public:
virtual IGameControl * GetIGameControl() = 0;
virtual IGameObject * GetIGameObject() = 0;
virtual void ReleaseIGameObject() = 0;
virtual const MCHAR * GetName() = 0;
virtual int GetNodeID() = 0; // 对应 3ds Max 的 node handle
virtual INode * GetMaxNode() = 0; // 必要时回退到原生 INode
virtual GMatrix GetWorldTM(TimeValue t = TIME_NegInfinity) = 0;
virtual GMatrix GetLocalTM(TimeValue t = TIME_NegInfinity) = 0;
virtual GMatrix GetObjectTM(TimeValue t = TIME_NegInfinity) = 0;
virtual IGameNode * GetNodeParent() = 0;
virtual int GetChildCount() = 0;
virtual IGameNode * GetNodeChild(int index) = 0; // 注意:是 GetNodeChild,非 GetChildNode
virtual int GetMaterialIndex() = 0;
virtual IGameMaterial * GetNodeMaterial() = 0;
virtual IPoint3 & GetWireframeColor() = 0;
virtual bool IsTarget() = 0;
virtual bool IsGroupOwner() = 0;
virtual bool IsNodeHidden() = 0;
};
IGame.hIGameMesh 等具体类型由 GetIGameType() 区分;顶点/面数据在 IGameMesh(派生类)上。class IGameObject {
public:
void GetBoundingBox(Box3 & bb); // 包围盒
bool IsRenderable(); // 是否可渲染
virtual ObjectTypes GetIGameType() = 0; // 类型枚举,用于向下转型
Object * GetMaxObject(); // 底层 3ds Max 对象
MaxType GetMaxType(); // Max 对象类型
int GetNumModifiers(); // 修饰器数量
IGameModifier * GetIGameModifier(int index); // 取修饰器
bool CastShadows(); // 是否投射阴影
bool IsObjectSkinned(); // 是否绑定蒙皮
IGameSkin * GetIGameSkin(); // 蒙皮接口
bool IsObjectXRef(); // 是否 XRef 对象
GMatrix GetIGameObjectTM(); // 对象变换矩阵
virtual bool InitializeData(); // 触发数据抽取
};
IGame.hIPropertyContainer 获取。颜色/强度等以 IGameProperty* 形式返回,而非直接返回 Color。class IGameMaterial {
public:
virtual const MCHAR * GetMaterialName() = 0;
virtual const MCHAR * GetMaterialClass() = 0;
virtual bool IsMultiType() = 0;
virtual bool IsSubObjType() = 0;
virtual int GetSubMaterialCount() = 0;
virtual IGameMaterial * GetSubMaterial(int index) = 0;
virtual int GetMaterialID(int subIndex) = 0;
virtual IGameProperty * GetAmbientData() = 0;
virtual IGameProperty * GetDiffuseData() = 0;
virtual IGameProperty * GetEmissiveData() = 0;
virtual IGameProperty * GetSpecularData() = 0;
virtual IGameProperty * GetOpacityData() = 0;
virtual IGameProperty * GetGlossinessData() = 0;
virtual IGameProperty * GetSpecularLevelData() = 0;
virtual IGameProperty * GetEmissiveAmtData() = 0;
virtual int GetNumberOfTextureMaps() = 0;
virtual IGameTextureMap * GetIGameTextureMap(int index) = 0;
virtual Mtl * GetMaxMaterial() = 0;
};
IConversionManager.hGetConversionManager() 取得实例(非成员函数)。CoordSystem 枚举见上文「坐标系说明」(取值 IGAME_MAX / IGAME_D3D / IGAME_OGL / IGAME_USER)。class IGameConversionManager {
public:
virtual void SetCoordSystem(CoordSystem Coord) = 0;
virtual CoordSystem GetCoordSystem() = 0;
virtual void SetUserCoordSystem(UserCoord UC) = 0;
};
IGameConversionManager * GetConversionManager();
IGameControl.hIGameNode::GetIGameControl() 取得。提供对 Bezier / Linear / TCB 关键帧、采样关键帧及约束的统一访问。class IGameControl {
public:
virtual bool GetBezierKeys(IGameKeyTab &gameKeyTab, IGameControlType Type) = 0;
virtual bool GetLinearKeys(IGameKeyTab &gameKeyTab, IGameControlType Type) = 0;
virtual bool GetTCBKeys(IGameKeyTab &gameKeyTab, IGameControlType Type) = 0;
virtual bool GetFullSampledKeys(IGameKeyTab &sample, int frameRate, IGameControlType Type, bool Relative = true) = 0;
virtual bool GetQuickSampledKeys(IGameKeyTab &sample, IGameControlType Type) = 0;
virtual bool GetBezierIGameKey(IGameControlType Type, IGameKey &bezKey, int index) = 0;
virtual bool GetTCBIGameKey(IGameControlType Type, IGameKey &tcbKey, int index) = 0;
virtual bool GetLinearIGameKey(IGameControlType Type, IGameKey &linearKey, int index) = 0;
virtual int GetIGameKeyCount(IGameControlType Type) = 0;
virtual MaxControlType GetControlType(IGameControlType Type) = 0;
virtual void GetClassName(IGameControlType Type, MSTR &className) = 0;
virtual bool IsAnimated(IGameControlType Type) = 0;
virtual bool IsLeaf(IGameControlType Type) = 0;
virtual IGameConstraint * GetConstraint(IGameControlType Type) = 0;
virtual EulerOrder GetEulerOrder() = 0;
virtual Control * GetMaxControl(IGameControlType Type) = 0;
virtual IGameControl * GetListSubControl(int index, IGameControlType Type) = 0;
virtual int GetNumOfListSubControls(IGameControlType Type) = 0;
virtual int GetNumOfVertControls() = 0;
virtual IGameControl * GetVertexControl(int index) = 0;
};
#include <IGame.h>
void Demo() {
IGameScene* pIGame = GetIGameInterface();
IGameConversionManager* cm = GetConversionManager();
cm->SetCoordSystem(IGameConversionManager::IGAME_D3D); // 先设置坐标系
if (pIGame->InitialiseIGame()) { // 初始化整个场景
pIGame->SetStaticFrame(0);
int n = pIGame->GetTopLevelNodeCount();
for (int i = 0; i < n; ++i) {
IGameNode* node = pIGame->GetTopLevelNode(i);
if (node) {
const MCHAR* name = node->GetName();
GMatrix wm = node->GetWorldTM(); // 已按 D3D 转换
}
}
}
pIGame->ReleaseIGame();
}
GetConversionManager()->SetCoordSystem() 设置,不要在 IGameScene 上找坐标系方法。SetPropertyFile() 须在 InitialiseIGame() 之前;SetStaticFrame() 在初始化之后。IGameMesh 的顶点/面数据需先 InitializeData() 才会抽取,按需调用以省内存。ReleaseIGame();IGameObject 用 ReleaseIGameObject() 释放。IGame 为将 3ds Max 场景导出到游戏引擎提供了官方、坐标系统一的入口。
Scene Export(.dle)。
core.lib maxutil.lib geom.lib maxscrpt.lib Paramblk2.libIGame.lib#include <IGame.h>
#include <IGame/IConversionManager.h>
// 在 SceneExport::DoExport 实现中:
int ExportScene() {
IGameScene* pIGame = GetIGameInterface();
if (!pIGame) return 0;
IGameConversionManager* cm = GetConversionManager();
cm->SetCoordSystem(IGameConversionManager::IGAME_D3D); // 先定坐标系
if (!pIGame->InitialiseIGame()) { pIGame->ReleaseIGame(); return 0; }
int n = pIGame->GetTopLevelNodeCount();
for (int i = 0; i < n; ++i) {
IGameNode* node = pIGame->GetTopLevelNode(i);
if (!node) continue;
const MCHAR* name = node->GetName();
GMatrix wm = node->GetWorldTM(); // 已按 D3D 转换
IGameObject* obj = node->GetIGameObject();
IGameMaterial* mat = node->GetNodeMaterial();
// ... 导出 node / obj / mat ...
}
pIGame->ReleaseIGame();
return 1;
}
target_link_libraries(<Target> PRIVATE core maxutil geom maxscrpt Paramblk2 IGame)