06-粒子系统模块(PF)

3ds Max SDK 更新于 2026-11-09 content/docs 文件
目录(26)

粒子系统模块 (ParticleFlow)

概述

ParticleFlow(PF)是 3ds Max 的事件驱动粒子系统,提供强大的粒子效果创建和模拟功能。PF 系统基于事件驱动架构,允许用户创建复杂的粒子行为逻辑。

核心接口

IPFSystem - 粒子系统接口

  • 命名空间:全局(无命名空间)
  • 头文件:ParticleFlow/IPFSystem.h
  • 接口 ID:PFSYSTEM_INTERFACE Interface_ID(0x74f93d00, 0x1eb34700)
  • 继承:FPMixinInterface
class IPFSystem : public FPMixinInterface {
public:
    // 系统参数
    virtual float GetMultiplier(TimeValue time) = 0;
    virtual int   GetBornAllowance() = 0;          // 注意返回 int
    virtual bool  HasEmitter() { return false; }   // 默认返回 false(非纯虚)

    // 发射器管理(均携带 TimeValue 参数)
    virtual int   GetEmitterType(TimeValue time) { return kEmitterType_none; }  // kEmitterType_xxx
    virtual void  GetEmitterDimensions(TimeValue time, Tab<float>& dimensions) { }  // 经 Tab 返回
    virtual Mesh* GetEmitterGeometry(TimeValue time) { return NULL; }         // 返回 Mesh*
    virtual bool  IsEmitterGeometryAnimated() { return false; }

    // 渲染状态
    virtual void  SetRenderState(bool renderState = true) = 0;
    virtual bool  IsRenderState() const = 0;

    // 积分步长
    virtual TimeValue GetIntegrationStep() const = 0;
    virtual int   GetUpdateType() const { return GetPFUpdateType(); }   // kPFUpdateType_complete / kPFUpdateType_forward

    // 选择管理
    virtual int   NumParticlesSelected() const { return 0; }
    virtual int   GetSelectedParticleID(int i) const { return 0; }
    virtual bool  IsParticleSelected(int id) const { return false; }

    // 操作列表管理(返回 INode*)
    virtual int   NumActionListsSelected() const { return 0; }
    virtual INode* GetSelectedActionList(int i) const { return NULL; }
    virtual bool  IsActionListSelected(INode* inode) const { return false; }

    // 脚本运行状态
    virtual bool  IsRunningScript() const { return false; }

    // 内部复制克隆状态(非纯虚)
    virtual bool  IsCopyClone() const { return true; }
    virtual void  ResetCopyClone() { ; }

    // 接口描述
    FPInterfaceDesc* GetDesc() { return GetDescByID(PFSYSTEM_INTERFACE); }
};

注意:IPFSystem 不提供 GetParticleChannel() 方法(旧文档中常见的该签名系杜撰,已删除)。粒子通道应通过粒子通道管理接口访问,见下文「粒子通道系统」。

发射器类型与更新类型枚举

enum { kEmitterType_none = -1,
       kEmitterType_rectangle = 0,
       kEmitterType_circle,
       kEmitterType_box,
       kEmitterType_sphere,
       kEmitterType_mesh };

enum { kPFUpdateType_complete,
       kPFUpdateType_forward };   // 完整更新 / 仅前向更新

IPFAction - 粒子操作接口

  • 头文件:ParticleFlow/IPFAction.h
  • 接口 ID:PFACTION_INTERFACE Interface_ID(0x74f93d00, 0x1eb34500)
  • 继承:FPMixinInterface
class IPFAction : public FPMixinInterface {
public:
    // 初始化 / 释构
    virtual bool Init(IObject* pCont, Object* pSystem, INode* node,
                      Tab<Object*>& actions, Tab<INode*>& actionNodes) = 0;
    virtual bool Release(IObject* pCont) = 0;

    // 通道需求 / 活动时间
    virtual const ParticleChannelMask& ChannelsUsed(const Interval& time) const = 0;
    PFExport void ChannelsUsed(const Interval& time, FPValue* channels) const;   // FnPub 备用(非纯虚)
    virtual const Interval ActivityInterval() const = 0;

    // 属性
    virtual bool IsFertile() const { return false; }
    virtual bool IsNonExecutable() const { return false; }
    virtual bool Has3dIcon() const { return false; }
    virtual int  IsColorCoordinated() const { return -1; }

    virtual bool SupportRand() const = 0;
    virtual int  GetRand() = 0;
    virtual void SetRand(int seed) = 0;
    PFExport int  NewRand();                                              // 非 virtual
    PFExport static int NewRand(IParamBlock2* pblock, ParamID randomSeedParamID);

    // 材质
    virtual bool IsMaterialHolder() const { return false; }
    virtual Mtl* GetMaterial() { return NULL; }
    virtual bool SetMaterial(Mtl* mtl) { return false; }

    // 脚本接线
    virtual bool SupportScriptWiring() const { return false; }
    virtual bool GetUseScriptWiring() const { return false; }
    virtual void SetUseScriptWiring(bool useScriptWiring) { ; }

    // 发射器依赖
    virtual bool IsEmitterTMDependent() const { return false; }
    virtual bool IsEmitterPropDependent() const { return false; }

    // 状态存取
    virtual IObject* GetCurrentState(IObject* pContainer) { return NULL; }
    virtual void SetCurrentState(IObject* actionState, IObject* pContainer) { ; }

    // 接口描述
    FPInterfaceDesc* GetDesc() { return GetDescByID(PFACTION_INTERFACE); }
};

IPFOperator - 粒子操作器接口

  • 头文件:ParticleFlow/IPFOperator.h
  • 接口 ID:PFOPERATOR_INTERFACE Interface_ID(0x74f93d01, 0x1eb34500)
  • 继承:FPMixinInterface(注意:IPFOperator 与 IPFAction 是并列接口,不继承 IPFAction)
class IPFOperator : public FPMixinInterface {
public:
    // 推进粒子(核心方法)
    virtual bool Proceed(IObject* pCont, PreciseTimeValue timeStart,
                         PreciseTimeValue& timeEnd, Object* pSystem, INode* pNode,
                         INode* actionNode, IPFIntegrator* integrator) = 0;

    // FnPub 备用签名(PreciseTimeValue 拆为 TimeValue 对,integrator 为 FPInterface*)
    PFExport bool Proceed(IObject* pCont, TimeValue timeStart, TimeValue& timeEnd,
                          Object* pSystem, INode* pNode, INode* actionNode,
                          FPInterface* integrator);

    // 预/后处理钩子(可选覆盖,默认返回 false / 空实现)
    virtual bool HasPreProceed(IObject* pCont, PreciseTimeValue timeStart,
                               PreciseTimeValue& timeEnd) { return false; }
    virtual void PreProceedBegin(IObject* pCont, PreciseTimeValue timeStart,
                                 PreciseTimeValue& timeEnd) { ; }
    virtual void PreProceedEnd(IObject* pCont, PreciseTimeValue timeStart,
                               PreciseTimeValue& timeEnd) { ; }
    virtual bool HasPostProceed(IObject* pCont, PreciseTimeValue time) { return false; }
    virtual void PostProceedBegin(IObject* pCont, PreciseTimeValue time) { ; }
    virtual void PostProceedEnd(IObject* pCont, PreciseTimeValue time) { ; }

    // 接口描述
    FPInterfaceDesc* GetDesc() { return GetDescByID(PFOPERATOR_INTERFACE); }
};

IPFTest - 粒子测试接口

  • 头文件:ParticleFlow/IPFTest.h
  • 接口 ID:PFTEST_INTERFACE Interface_ID(0x74f93d02, 0x1eb34500)
  • 继承:FPMixinInterface(与 IPFOperator 一样,不继承 IPFAction)
class IPFTest : public FPMixinInterface {
public:
    // 构造函数
    PFExport IPFTest();

    // 条件测试(核心方法):testResult 标记满足条件的粒子,testTime 记录各粒子满足条件时刻
    virtual bool Proceed(IObject* pCont, PreciseTimeValue timeStart,
                         PreciseTimeValue& timeEnd, Object* pSystem, INode* pNode,
                         INode* actionNode, IPFIntegrator* integrator,
                         BitArray& testResult, Tab<float>& testTime) = 0;

    // FnPub 两步式入口(调用顺序:先 ProceedStep1,再 ProceedStep2)
    virtual void ProceedStep1(IObject* pCont, Object* pSystem, INode* pNode,
                              INode* actionNode, FPInterface* integrator) = 0;
    virtual bool ProceedStep2(TimeValue timeStartTick, float timeStartFraction,
                              TimeValue& timeEndTick, float& timeEndFraction,
                              BitArray& testResult, Tab<float>& testTime) = 0;

    // 事件跳转(返回/设置满足条件的粒子要流向的下一个动作列表)
    PFExport INode* GetNextActionList(INode* test, bool* linkActive = NULL);
    PFExport bool  SetNextActionList(INode* nextAL, INode* test);
    PFExport bool  SetNextActionListMXS(INode* nextAL, INode* test);   // MaxScript 接口
    PFExport bool  SetLinkActive(bool active, INode* test);
    PFExport bool  ClearNextActionList(INode* test);

    // 接口描述
    FPInterfaceDesc* GetDesc() { return GetDescByID(PFTEST_INTERFACE); }

    // 内部使用(仅更新内部状态)
    inline void updateNextActionListValue(INode* nextAL) { m_nextActionList = nextAL; }
    inline void updateArrowValue(Object* arrow) { m_arrow = arrow; }
    inline void updateLinkActiveValue(bool linkActive) { m_linkActive = linkActive; }
    Object* GetArrowToNextActionList() { return m_arrow; }

protected:
    INode*  m_nextActionList; // an actionList node for the next event.
    Object* m_arrow;          // an arrow object that controls the gate to the next ActionList
    bool    m_linkActive;     // status of the link to the next ActionList
};

粒子通道系统

IParticleChannel - 基础粒子通道

  • 头文件:ParticleFlow/IParticleChannel.h
  • 接口 ID:PARTICLECHANNEL_INTERFACE Interface_ID(0x74f93b00, 0x1eb34400)
  • 继承:FPMixinInterface
class IParticleChannel : public FPMixinInterface {
public:
    // 构造函数
    PFExport IParticleChannel();
    PFExport IParticleChannel(const Interface_ID& readID, const Interface_ID& writeID);

    // 通道标识 / 克隆 / 序列化
    virtual Class_ID GetClassID() const = 0;
    virtual IObject* Clone() const = 0;
    virtual IOResult Save(ISave* isave) const = 0;
    virtual IOResult Load(ILoad* iload) = 0;

    // 相似通道判定
    PFExport bool IsSimilarChannel(IObject* channel) const;

    // 传输 / 私有归属
    PFExport bool  IsTransferable() const;
    PFExport void  SetTransferable(bool status);
    PFExport bool  IsPrivateChannel() const;
    PFExport Object* GetPrivateOwner() const;
    PFExport void  SetPrivateOwner(Object* action);

    // 创建者动作 / 通道 ID
    PFExport INode* GetCreatorAction() const;
    PFExport void   SetCreatorAction(INode* actionNode);
    PFExport DWORD  GetReadID_PartA() const;
    PFExport DWORD  GetReadID_PartB() const;
    PFExport DWORD  GetWriteID_PartA() const;
    PFExport DWORD  GetWriteID_PartB() const;
    PFExport void   SetReadID(DWORD interfaceID_PartA, DWORD interfaceID_PartB);
    PFExport void   SetWriteID(DWORD interfaceID_PartA, DWORD interfaceID_PartB);

    // 接口 ID 形式读写
    PFExport const Interface_ID& GetReadID() const;
    PFExport const Interface_ID& GetWriteID() const;
    PFExport void SetReadID(const Interface_ID& id);
    PFExport void SetWriteID(const Interface_ID& id);

    // 克隆 / 合并辅助
    PFExport void CloneChannelCore(IParticleChannel* getFrom);
    PFExport void UpdateCreatorHandle(IMergeManager* pMM);

    // 内存占用
    virtual int MemoryUsed() const = 0;

    // 接口描述
    FPInterfaceDesc* GetDesc() { return GetDescByID(PARTICLECHANNEL_INTERFACE); }

protected:
    bool m_isTransferable;
    bool m_isPrivate;
    mutable Object* m_privateOwner; // could be derived from creatorAction
    mutable INode* m_creatorAction; // could be derived from creatorHandle
    ULONG m_creatorHandle;
    Interface_ID m_readID, m_writeID;

    int  numRefs()  const { return m_numRefs; }
    int& _numRefs()       { return m_numRefs; }
private:
    int m_numRefs;
};

注意:IParticleChannel 是泛型基类,不含 GetValue()/SetValue()。这两个方法只存在于特化通道类上(见下)。

特定类型粒子通道

GetValue() / SetValue() 定义在读/写分离的特化通道类上,而非泛型 IParticleChannel。每个特化类型都提供一对 R(只读)和 W(只写)接口,二者均直接继承 FPMixinInterface。

// 位置通道 —— 头文件:ParticleFlow/IParticleChannelPoint3.h
class IParticleChannelPoint3R : public FPMixinInterface {
public:
    virtual const Point3& GetValue(int index) const = 0;   // 第 index 个粒子
    virtual bool          IsGlobal() const = 0;
    virtual const Point3& GetValue() const = 0;            // 全局值
    virtual const Box3&   GetBoundingBox() const = 0;
    PFExport void         GetBoundingBox(Point3& corner1, Point3& corner2) const;  // FnPub 备用
    virtual float         GetMaxLengthValue() const = 0;

    FPInterfaceDesc* GetDesc() { return GetDescByID(PARTICLECHANNELPOINT3R_INTERFACE); }
};

class IParticleChannelPoint3W : public FPMixinInterface {
public:
    virtual void SetValue(int index, const Point3& v) = 0;
    virtual void SetValue(const Point3& v) = 0;            // 全局写入
    virtual void BuildBoundingBox() = 0;
    virtual void UpdateMaxLengthValue() = 0;

    FPInterfaceDesc* GetDesc() { return GetDescByID(PARTICLECHANNELPOINT3W_INTERFACE); }
};

其他常用特化通道同样为 R/W 配对:

浮点通道(ParticleFlow/IParticleChannelFloat.h)

class IParticleChannelFloatR : public FPMixinInterface {
public:
    virtual float GetValue(int index) const = 0;
    virtual bool  IsGlobal() const = 0;
    virtual float GetValue() const = 0;            // 全局值

    FPInterfaceDesc* GetDesc() { return GetDescByID(PARTICLECHANNELFLOATR_INTERFACE); }
};

class IParticleChannelFloatW : public FPMixinInterface {
public:
    virtual void SetValue(int index, float value) = 0;
    virtual void SetValue(float value) = 0;        // 全局写入

    FPInterfaceDesc* GetDesc() { return GetDescByID(PARTICLECHANNELFLOATW_INTERFACE); }
};

速度通道

SDK 中没有 IParticleChannelVector;速度以 IParticleChannelSpeed 封装 IParticleChannelPoint3R/W(头文件 ParticleFlow/IParticleChannelSpeed.h)。该头文件仅定义接口 ID 与 GetParticleChannelSpeedRInterface/WInterface 宏,无独立通道类。

时间通道(ParticleFlow/IParticleChannelPTV.h)

class IParticleChannelPTVR : public FPMixinInterface {
public:
    virtual TimeValue                GetTick(int index) const = 0;
    virtual float                    GetFraction(int index) const = 0;
    virtual void                     GetTickFraction(int index, TimeValue& tick, float& fraction) const = 0;
    virtual bool                     IsSync() const = 0;
    virtual const PreciseTimeValue&  GetValue(int index) const = 0;
    virtual const PreciseTimeValue&  GetValue() const = 0;   // 全局同步值

    FPInterfaceDesc* GetDesc() { return GetDescByID(PARTICLECHANNELPTVR_INTERFACE); }
};

class IParticleChannelPTVW : public FPMixinInterface {
public:
    virtual void SetTick(int index, TimeValue time) = 0;
    virtual void SetFraction(int index, float time) = 0;
    virtual void SetTickFraction(int index, TimeValue tick, float fraction) = 0;
    virtual void SetValue(int index, const PreciseTimeValue& time) = 0;
    virtual void SetValue(const PreciseTimeValue& time) = 0;   // 全局写入

    FPInterfaceDesc* GetDesc() { return GetDescByID(PARTICLECHANNELPTVW_INTERFACE); }
};

生命周期通道

SDK 中没有独立的 IParticleChannelLifespan 类;生命周期由 IParticleChannelPTV 封装,通过宏 GetParticleChannelLifespanRInterface(obj)(返回 IParticleChannelPTVR*)访问。

通道的实际获取方式应通过 IChannelContainer 接口(头文件 ParticleFlow/IChannelContainer.h),由粒子容器提供:virtual IObject* GetChannel(Interface_ID id, Object* privateOwner=NULL, INode* creatorAction=NULL) = 0;。IParticleChannels.h 仅是一组 #include 的集合,并非管理接口。

事件驱动架构

事件流程图

发射器 → 操作器 → 测试 → 操作器 → 渲染
    ↓        ↓        ↓        ↓        ↓
生成粒子 → 修改属性 → 条件判断 → 后续操作 → 显示效果

事件处理示例(通道访问)

class MyParticleOperator : public IPFOperator {
public:
    bool Proceed(IObject* pCont, PreciseTimeValue timeStart, PreciseTimeValue& timeEnd,
                 Object* pSystem, INode* pNode, INode* actionNode,
                 IPFIntegrator* integrator) override {
        // IPFSystem 无 GetParticleChannel()。通道须通过 IChannelContainer 获取:
        IChannelContainer* chCont = GetChannelContainerInterface(pCont);
        if (!chCont) return false;
        IParticleChannelPoint3W* posW = static_cast<IParticleChannelPoint3W*>(
            chCont->GetChannel(PARTICLECHANNELPOINT3W_INTERFACE));
        if (posW) {
            const int count = ...; // 经容器取得粒子数
            for (int i = 0; i < count; i++) {
                Point3 pos = posW->GetValue(i);   // 读
                pos.z += 0.1f;
                posW->SetValue(i, pos);           // 写
            }
        }
        return true;
    }
};

关键特性

1. 事件驱动逻辑

  • 操作器 (Operators):修改粒子属性
  • 测试 (Tests):条件判断和分支
  • 发射器 (Emitters):粒子生成

2. 粒子通道系统

  • 内置通道:位置、速度、生命周期等
  • 自定义通道:扩展粒子属性
  • 数据共享:操作器间数据传递

3. 物理集成

  • PhysX 集成:物理模拟支持
  • 碰撞检测:物体碰撞处理
  • 力场影响:重力、风力等

使用示例

创建自定义粒子操作器

class GravityOperator : public IPFOperator {
public:
    bool Proceed(IObject* pCont, PreciseTimeValue timeStart, PreciseTimeValue& timeEnd,
                 Object* pSystem, INode* pNode, INode* actionNode,
                 IPFIntegrator* integrator) override {
        // 速度通道为 IParticleChannelSpeed(封装 IParticleChannelPoint3W)
        IChannelContainer* chCont = GetChannelContainerInterface(pCont);
        if (!chCont) return false;
        IParticleChannelPoint3W* velW = static_cast<IParticleChannelPoint3W*>(
            chCont->GetChannel(PARTICLECHANNELSPEEDW_INTERFACE));   // 见 IParticleChannelSpeed.h
        if (velW) {
            float gravity = -9.8f;
            float timeStep = GetTicksPerFrame() / 4800.0f;
            const int count = ...; // 经容器取得粒子数
            for (int i = 0; i < count; i++) {
                Point3 velocity = velW->GetValue(i);
                velocity.z += gravity * timeStep;
                velW->SetValue(i, velocity);
            }
        }
        return true;
    }
};

真实最小示例(接口调用)

// 通过 IPFSystem 查询系统状态
void DumpPFSystem(IPFSystem* sys) {
    if (!sys) return;
    float mult = sys->GetMultiplier(0);          // 携带 TimeValue 参数
    int etype = sys->GetEmitterType(0);          // kEmitterType_xxx
    bool running = sys->IsRunningScript();
    int nSel = sys->NumParticlesSelected();
    for (int i = 0; i < nSel; i++) {
        int pid = sys->GetSelectedParticleID(i);
        if (sys->IsParticleSelected(pid)) {
            // 处理选中粒子
        }
    }
}

开发注意事项

  1. 性能优化:粒子数量大时注意性能
  2. 内存管理:粒子通道数据量可能很大
  3. 时间同步:正确处理时间步长和插值
  4. 物理精度:物理模拟需要适当的精度控制

ParticleFlow 系统为复杂的粒子效果提供了强大的工具集,特别适合游戏特效、科学模拟和视觉特效开发。

最小可编译工程模板与 .lib 精确组合

本模块对应插件类型与产物后缀

自定义 PF 操作器/测试(挂到 PF 系统里),产物随宿主插件类型(.dlo/.dlu 等)。

需链接的 .lib(精确组合)

  • 通用核心(任何插件都链):core.lib maxutil.lib geom.lib maxscrpt.lib Paramblk2.lib
  • 本模块专用:particle.lib particleflow.lib ParticleFlowTools.lib
  • 原则:用到哪个子系统才链哪个。

最小代码骨架

#include "max.h"
#include "ParticleFlow/IPFOperator.h"
#include "ParticleFlow/IChannelContainer.h"
#include "ParticleFlow/IParticleChannelPoint3.h"

// 自定义 PF 操作器:继承 IPFOperator(与 IPFAction 并列,不继承 IPFAction)
// 核心推进方法为 Proceed()(非 OperateParticles)
class MyPushOperator : public IPFOperator {
public:
    FPInterfaceDesc* GetDesc() { return GetDescByID(PFOPERATOR_INTERFACE); }

    bool Proceed(IObject* pCont, PreciseTimeValue timeStart, PreciseTimeValue& timeEnd,
                 Object* pSystem, INode* pNode, INode* actionNode,
                 IPFIntegrator* integrator) override {
        IChannelContainer* chCont = GetChannelContainerInterface(pCont);
        if (!chCont) return false;
        IParticleChannelPoint3W* posW = static_cast<IParticleChannelPoint3W*>(
            chCont->GetChannel(PARTICLECHANNELPOINT3W_INTERFACE));
        if (posW) {
            const int count = ...; // 经容器取得粒子数
            for (int i = 0; i < count; i++) {
                Point3 p = posW->GetValue(i);
                p.z += 0.1f;
                posW->SetValue(i, p);
            }
        }
        return true;
    }
};

链接器 / CMake 配置

target_link_libraries(<Target> PRIVATE core maxutil geom maxscrpt Paramblk2 particle particleflow ParticleFlowTools)