DirectX (13) 粒子系统

154 篇文章 8 订阅

目录(?)[+]

作者:i_dovelemon

来源:CSDN

日期:2014 / 10 / 16

主题:Point Sprite, Particle System



引言

             在游戏中,很多的绚丽,灿烂的特效,都可以使用粒子系统制作出来。那么粒子系统,到底是什么?它是如何工作的?接下来的文章,将会向大家讲述如何构建一个简单的粒子系统。



粒子系统

            所谓的粒子系统,指的是一个粒子管理器,用于在特定条件下产生特定的粒子,并且赋予粒子以运动。所以,可以将粒子系统看成是一个个粒子的集合。而不同的效果,是要我们自己来控制粒子如何产生,粒子的颜色如何变化,粒子如何进行运动。通过控制这些条件,我们就能够创造出很多很多的特效出来。而关键就在于如何控制粒子的运动和产生。

           既然,我们明白了粒子系统的概念,那么一个粒子到底是什么?它在计算机中是如何进行表示的?

           简单而言,粒子在计算机中就是使用一个结构来表达。结构中保存了粒子的基本属性,如位置,颜色,生命,以及运动状态相关的参数。不同复杂度的粒子系统,粒子所包含的属性并不相同。如果需要简单的效果,那么只需要几个基本的属性即可。如果要做出更加复杂,或者更加符合动力学的粒子系统,可以在结构中再添加很多不同的物理属性。至于如何添加这些属性,要依赖于你所需要实现的粒子系统的复杂程度,想要支持的功能来进行设计。

           当我们为粒子系统设计好了一个粒子结构之后,并且也有了粒子系统,来负责粒子的产生,以及运动等等。我们需要的当然就是显示在界面上。如果没有显示任何的内容,即使你的系统再强大,也是白费力气。

           DirectX中支持很多的基本单元类型,最常用的如三角形列表,线条列表等等。在这里,我们将会使用一个称之为Point Sprite的基本单元类型。

           Point Sprite,实际上就是一个点。我们在应用程序阶段,只需要将它当做点进行处理即可。但是要显示效果,我们自然还是需要进行纹理映射。由于Point Sprite的特殊性,DirectX内部会自动的为这些点设置纹理坐标。注意,这里的点只是逻辑意义上的。DirectX在最后处理的时候,还是会使用多边形来进行处理。所以这里说的点,存在一个大小的问题。我们能够通过程序来控制产生的点的大小。

           为了实现一些效果,我们需要开启Alpha blend。毕竟做粒子特效,如果没有进行颜色混合的话,就是一个一个的单独的纹理,这并不是粒子效果了。

            而且,粒子在界面显示的时候的先后顺序,对于我们来说并不重要。所以,将depth test以及depth buffer禁用掉,能够提高系统的效率。


基本类的设计

          在上面,说了这么多,最终要体现在代码上面,下面是粒子系统的抽象类。当我们需要创建一个新的效果的时候,只要继承这个类,并且复写虚函数即可。当然,这里的只是一个很简单的粒子系统设计,提供的粒子属性也很少。但是也能够做出很多的效果出来了。如果读者,希望更复杂的效果,就可以自己来扩展这个基本类别,然后添加你自己的功能。

          废话不多说,直接上代码:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //-----------------------------------------------------------------------  
  2. // declaration  : Copyright (c), by XJ , 2014. All right reserved .  
  3. // brief        : This file will define the Particle system  
  4. // author       : XJ  
  5. // file         : PSystem.h  
  6. // date         : 2014 / 10 / 15  
  7. // version      : 1.0  
  8. //-----------------------------------------------------------------------  
  9. #pragma once  
  10.   
  11. #include<d3dx9.h>  
  12. #include"AABB.h"  
  13. #include"Camera.h"  
  14. #include<vector>  
  15. using namespace XJCollision ;  
  16. using namespace std ;  
  17.   
  18. struct Particle  
  19. {  
  20.     D3DXVECTOR3 initPos ;  
  21.     D3DXVECTOR3 initVelocity;  
  22.     float       initSize    ;       //in pixel  
  23.     float       initTime    ;  
  24.     float       lifeTime    ;  
  25.     float       mass        ;  
  26.     D3DXCOLOR   initColor   ;  
  27.   
  28.     static IDirect3DVertexDeclaration9* decl ;  
  29. };  
  30.   
  31. class PSystem  
  32. {  
  33. public:  
  34.     PSystem(  
  35.         const char* fxName,  
  36.         const char* techName,  
  37.         const char* texName,  
  38.         const D3DXVECTOR3& accel,  
  39.         const AABB& box,  
  40.         int maxNumParticles,  
  41.         float timePerParticle,  
  42.         LPDIRECT3DDEVICE9 device,  
  43.         Camera* camera);  
  44.     virtual ~PSystem();  
  45.   
  46. public:  
  47.     float getTime();  
  48.     void setTime(float fTime);  
  49.     const AABB& getAABB() const ;  
  50.     void setWorldMtx(const D3DXMATRIX& world);  
  51.     void addParticle();  
  52.   
  53.     virtual void onLostDevice();  
  54.     virtual void onResetDevice();  
  55.   
  56.     virtual void initParticles(Particle& out) = 0;  
  57.     virtual void update(float dt);  
  58.     virtual void draw() ;  
  59.   
  60. protected:  
  61.     LPDIRECT3DDEVICE9 m_pDevice;                // Device  
  62.     ID3DXEffect     *m_FX   ;                   // Effect  
  63.     D3DXHANDLE       m_hTech;                   // Technique  
  64.     D3DXHANDLE       m_hWVP ;                   // WVP matrix  
  65.     D3DXHANDLE       m_hEyePosL;                //   
  66.     D3DXHANDLE       m_hTex;                    // Texture  
  67.     D3DXHANDLE       m_hTime;                   // Time  
  68.     D3DXHANDLE       m_hAccel;                  // Accel  
  69.     D3DXHANDLE       m_hViewportHeight;         // Viewport's height  
  70.       
  71.     IDirect3DTexture9* m_Tex;                   // Texture  
  72.     IDirect3DVertexBuffer9* m_VB    ;           // Vertex buffer  
  73.     D3DXMATRIX       m_World;                   // World matrix  
  74.     D3DXMATRIX       m_InvWorld;                // Inverse matrix  
  75.     float            m_Time ;                   // Time  
  76.     D3DXVECTOR3      m_Accel ;                  // Accelerate  
  77.     AABB             m_AABB;                    // Bounding box  
  78.     int              m_MaxNumParticles;         // Max number particles  
  79.     float            m_fTimePerParticle;        // Delay time to emit one particle  
  80.   
  81.     Camera          *m_pCamera  ;               // Camera  
  82.   
  83.     std::vector<Particle> m_Particles;        // Particles list  
  84.     std::vector<Particle*>  m_AliveParticles; // Alive particles list  
  85.     std::vector<Particle*>    m_DeadParticles;    // Dead particles list  
  86. };// end for PSystem  

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include"PSystem.h"  
  2. #include"MyUtil.h"  
  3. #include<d3dx9.h>  
  4.   
  5. IDirect3DVertexDeclaration9* Particle::decl = NULL ;  
  6. /** 
  7. * Constructor 
  8. */  
  9. PSystem::PSystem(const char* fxName,  
  10.         const char* techName,  
  11.         const char* texName,  
  12.         const D3DXVECTOR3& accel,  
  13.         const AABB& box,  
  14.         int maxNumParticles,  
  15.         float timePerParticle,  
  16.         LPDIRECT3DDEVICE9 device,  
  17.         Camera* camera)  
  18. {  
  19.     //Save the device  
  20.     m_pDevice = device ;  
  21.   
  22.     //Create error buffer  
  23.     ID3DXBuffer* _error = NULL ;  
  24.     HR(D3DXCreateBuffer(128, &_error));  
  25.   
  26.     //Create the Effect  
  27.     HR(D3DXCreateEffectFromFileA(m_pDevice, fxName,  
  28.         NULL,NULL, D3DXSHADER_DEBUG, NULL, &m_FX, &_error));  
  29.   
  30.     //If error   
  31.     if(_error)  
  32.     {  
  33.         MessageBoxA(NULL,(char*)_error->GetBufferPointer(),"Error", MB_OK);  
  34.         return ;  
  35.     }  
  36.   
  37.     //Get the technique handle  
  38.     m_hTech = m_FX->GetTechniqueByName(techName);  
  39.   
  40.     //Get all the handle  
  41.     m_hWVP = m_FX->GetParameterByName(0, "gWVP");  
  42.     m_hEyePosL = m_FX->GetParameterByName(0, "gEyePosL");  
  43.     m_hTex = m_FX->GetParameterByName(0, "gTex");  
  44.     m_hTime = m_FX->GetParameterByName(0, "gTime");  
  45.     m_hAccel = m_FX->GetParameterByName(0, "gAccel");  
  46.     m_hViewportHeight = m_FX->GetParameterByName(0, "gViewportHeight");  
  47.   
  48.     //Create the texture  
  49.     HR(D3DXCreateTextureFromFileA(m_pDevice, texName, &m_Tex));  
  50.   
  51.     //Set parameters  
  52.     HR(m_FX->SetTechnique(m_hTech));  
  53.     HR(m_FX->SetTexture(m_hTex, m_Tex));  
  54.     HR(m_FX->SetVector(m_hAccel, &D3DXVECTOR4(m_Accel,0.0f)));  
  55.   
  56.     //Save the time per particles  
  57.     m_fTimePerParticle = timePerParticle ;  
  58.   
  59.     m_Time = 0.0f ;  
  60.   
  61.     //Save the AABB  
  62.     m_AABB = box ;  
  63.   
  64.     //Save the camera  
  65.     m_pCamera = camera ;  
  66.   
  67.     //Allocate the memory for the particle  
  68.     m_MaxNumParticles = maxNumParticles ;  
  69.     m_Particles.resize(m_MaxNumParticles);  
  70.     m_AliveParticles.reserve(m_MaxNumParticles);  
  71.     m_DeadParticles.reserve(m_MaxNumParticles);  
  72.   
  73.     //They start all dead  
  74.     for(int i = 0 ; i < m_MaxNumParticles ; i ++)  
  75.     {  
  76.         m_Particles[i].initTime = 0.0f ;  
  77.         m_Particles[i].lifeTime = -1.0f ;  
  78.     }  
  79.   
  80.     //Create the vertex buffer  
  81.     HR(m_pDevice->CreateVertexBuffer(m_MaxNumParticles * sizeof(Particle), D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY|  
  82.         D3DUSAGE_POINTS,0,D3DPOOL_DEFAULT,&m_VB,NULL));  
  83. }// end for constructor  
  84.   
  85. /** 
  86. * Destructor 
  87. */  
  88. PSystem::~PSystem()  
  89. {  
  90.     m_AliveParticles.clear();  
  91.     m_DeadParticles.clear();  
  92.     m_Particles.clear();  
  93.     m_FX->Release();  
  94.     m_Tex->Release();  
  95.     m_VB->Release();  
  96. }// end for destructor  
  97.   
  98. void PSystem::setTime(float fTime)  
  99. {  
  100.     m_Time = fTime ;  
  101. }// end for setTime  
  102.   
  103. void PSystem::setWorldMtx(const D3DXMATRIX& world)  
  104. {  
  105.     m_World = world ;  
  106.     D3DXMatrixInverse(&m_World,NULL,&m_World);  
  107. }// end for setWorldMtx  
  108.   
  109. void PSystem::addParticle()  
  110. {  
  111.     if(m_DeadParticles.size() > 0)  
  112.     {  
  113.         Particle* p = m_DeadParticles.back();  
  114.         initParticles(*p);  
  115.   
  116.         m_DeadParticles.pop_back();  
  117.         m_AliveParticles.push_back(p);  
  118.     }  
  119. }// end for addParticle  
  120.   
  121. void PSystem::onLostDevice()  
  122. {  
  123.     //OnlostDevice for fx  
  124.     m_FX->OnLostDevice();  
  125.   
  126.     if(m_VB)  
  127.     {  
  128.         m_VB->Release();  
  129.         m_VB = NULL ;  
  130.     }  
  131. }// end for onLostDevice  
  132.   
  133. void PSystem::onResetDevice()  
  134. {  
  135.     //OnResetDevice for fx  
  136.     m_FX->OnResetDevice();  
  137.   
  138.     if(m_VB)  
  139.     {  
  140.         //Recreate the vertex buffer  
  141.         HR(m_pDevice->CreateVertexBuffer(m_MaxNumParticles*sizeof(Particle),  
  142.             D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY|D3DUSAGE_POINTS,0,  
  143.             D3DPOOL_DEFAULT,  
  144.             &m_VB,  
  145.             NULL));  
  146.     }  
  147. }// end for onResetDevice  
  148.   
  149. void PSystem::update(float dt)  
  150. {  
  151.     m_Time += dt ;  
  152.   
  153.     //Rebuild the dead list and alive list  
  154.     m_DeadParticles.resize(0);  
  155.     m_AliveParticles.resize(0);  
  156.   
  157.     //For each particle  
  158.     for(int i = 0 ; i < m_Particles.size() ; i ++)  
  159.     {  
  160.         if((m_Time - m_Particles[i].initTime) > m_Particles[i].lifeTime)  
  161.         {  
  162.             m_DeadParticles.push_back(&m_Particles[i]);  
  163.         }  
  164.         else  
  165.         {  
  166.             m_AliveParticles.push_back(&m_Particles[i]);  
  167.         }  
  168.     }// end for   
  169.   
  170.     //Check if it is the time to emit one another particle  
  171.     if(m_fTimePerParticle > 0.0f)  
  172.     {  
  173.         static float timeDelay = 0.0f ;  
  174.         timeDelay += dt ;  
  175.   
  176.         if(timeDelay > m_fTimePerParticle)  
  177.         {  
  178.             addParticle();  
  179.             timeDelay = 0.0f ;  
  180.         }  
  181.     }  
  182. }// end for update  
  183.   
  184. void PSystem::draw()  
  185. {  
  186.     //Get the camera's position in the world and make it relative to the particle system's local system  
  187.     D3DXVECTOR3 eyeW = m_pCamera->pos();  
  188.     D3DXVECTOR3 eyeL ;  
  189.     D3DXVec3TransformCoord(&eyeL, &eyeW, &m_InvWorld);  
  190.   
  191.     //Set the FX parameter  
  192.     HR(m_FX->SetValue(m_hEyePosL,&eyeL,sizeof(D3DXVECTOR3)));  
  193.     HR(m_FX->SetFloat(m_hTime, m_Time));  
  194.     HR(m_FX->SetMatrix(m_hWVP, &(m_World* m_pCamera->viewproj())));  
  195.     HR(m_FX->SetInt(m_hViewportHeight, 600));  
  196.   
  197.     //Draw  
  198.     //set the vertex buffer  
  199.     HR(m_pDevice->SetStreamSource(0, m_VB, 0, sizeof(Particle)));  
  200.   
  201.     //set the vertex declaration  
  202.     HR(m_pDevice->SetVertexDeclaration(Particle::decl));  
  203.   
  204.     Particle* p = 0 ;  
  205.     HR(m_VB->Lock(0, 0, (void**)&p, D3DLOCK_DISCARD));  
  206.     UINT vIndex = 0 ;  
  207.     for(int i = 0 ; i < m_AliveParticles.size(); i ++)  
  208.     {  
  209.         p[vIndex] = *m_AliveParticles[i] ;  
  210.         vIndex ++ ;  
  211.     }// end for  
  212.   
  213.     HR(m_VB->Unlock());  
  214.   
  215.     UINT numPass = 0 ;  
  216.     HR(m_FX->Begin(&numPass, 0));  
  217.     HR(m_FX->BeginPass(0));  
  218.   
  219.     if(vIndex > 0)  
  220.     {  
  221.         m_pDevice->DrawPrimitive(D3DPT_POINTLIST,0,vIndex);  
  222.     }  
  223.   
  224.     HR(m_FX->EndPass());  
  225.     HR(m_FX->End());  
  226. }// end for draw  

               我在这个类的基础上继承了一个类,用于实现自己的效果:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #pragma once  
  2.   
  3. #include"PSystem.h"  
  4.   
  5. class FireRing: public PSystem  
  6. {  
  7. public:  
  8.     FireRing(const char* fxName,  
  9.         const char* techName,  
  10.         const char* texName,  
  11.         const D3DXVECTOR3& accel,  
  12.         const AABB& box,  
  13.         int maxNumParticles,  
  14.         float timePerParticle,  
  15.         LPDIRECT3DDEVICE9 device,  
  16.         Camera* camera)  
  17.         :PSystem(fxName, techName, texName, accel,  
  18.         box, maxNumParticles, timePerParticle, device, camera)  
  19.     {  
  20.   
  21.     };  
  22.   
  23.     void initParticles(Particle& out)  
  24.     {  
  25.         //Save the init time  
  26.         out.initTime = m_Time ;  
  27.   
  28.         //Calculate the life time from 2.0s to 4.0s  
  29.         out.lifeTime = 20.0f + 2 * (rand()%10001 * 0.0001) ;  
  30.   
  31.         //Calculate the initialize size in pixel  
  32.         out.initSize = 50.0f + 10 * (rand()%10001 * 0.0001) ;  
  33.   
  34.         //Calculate the a very small velocity  
  35.         static float angle = 0.0f ;  
  36.         D3DXVECTOR3 vel(0.5f, 1.0f, 0.5f);  
  37.         D3DXMATRIX m ;  
  38.   
  39.         D3DXMatrixRotationY(&m,angle);  
  40.         D3DXVec3TransformCoord(&vel, &vel, &m);  
  41.   
  42.         out.initVelocity = vel ;  
  43.         D3DXVec3Normalize(&out.initVelocity, &out.initVelocity);  
  44.         angle += 1.0f ;  
  45.   
  46.         //Calculate the mass  
  47.         out.mass = 1.0f + (rand()%10001 * 0.0001) ;  
  48.   
  49.         //Calculate the color  
  50.         float t = (0.5f + 0.5*(rand()%10001 * 0.0001)) ;  
  51.         out.initColor = D3DXCOLOR(0.0f, 0.0f, t * 1.0f, t * 1.0f);  
  52.   
  53.         //Calculate the pos  
  54.         out.initPos.x = 0.0f;  
  55.         out.initPos.y = 0.0f ;  
  56.         out.initPos.z = 0.0f ;  
  57.   
  58.     }// end for initParticle  
  59. };  

              这个类只要复写它的粒子初始化函数就可以了。通过在初始化的里面进行设计,改变粒子的位置,状态等等,我们还是能够做出很多的效果出来。下面是这个效果配套的 Shader

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. uniform extern float4x4 gWVP ;  
  2. uniform extern texture gTex ;  
  3. uniform extern float3  gEyePosL;  
  4. uniform extern float3  gAccel ;  
  5. uniform extern float   gTime ;  
  6. uniform extern float   gViewportHeight ;  
  7.   
  8. sampler TexS = sampler_state  
  9. {  
  10.     Texture = <gTex>;  
  11.     MinFilter = LINEAR ;  
  12.     MagFilter = LINEAR ;  
  13.     MipFilter = POINT  ;  
  14.     AddressU = CLAMP ;  
  15.     AddressV = CLAMP ;  
  16. };  
  17.   
  18. struct OutputVS  
  19. {  
  20.     float4 posH : POSITION ;  
  21.     float4 color: COLOR0   ;  
  22.     float2 tex0 : TEXCOORD0 ;  
  23.     float  size : PSIZE ;  
  24. };  
  25.   
  26. //VS  
  27. OutputVS FireRingVS(  
  28.         float3 posL: POSITION,  
  29.         float3 vel : TEXCOORD0,  
  30.         float size : TEXCOORD1,  
  31.         float time : TEXCOORD2,  
  32.         float lifeTime: TEXCOORD3,  
  33.         float mass : TEXCOORD4,  
  34.         float4 color: COLOR0  
  35. )  
  36. {  
  37.     //Clear the output  
  38.     OutputVS outVS = (OutputVS)0 ;  
  39.   
  40.     float t = gTime - time ;  
  41.   
  42.     posL = posL + t * vel ;  
  43.   
  44.     outVS.posH = mul(float4(posL,1.0f), gWVP);  
  45.   
  46.     size += 0.8 * t ;  
  47.   
  48.     float d = distance(posL, gEyePosL);  
  49.     outVS.size = size ; //gViewportHeight * size / (1.0 + 8.0f*d);  
  50.     color.r = 0.0f ;  
  51.     color.g = 0.0f ;  
  52.     color.b = 1.0f * (t / lifeTime) ;   
  53.     color.a = 1.0f - 1.0f * (t / lifeTime) ;   
  54.     outVS.color = color ;//(1.0f - (t / lifeTime)) ;  
  55.   
  56.     return outVS ;  
  57. }  
  58.   
  59. //PS  
  60. float4 FireRingPS(float4 color:COLOR0,  
  61.                   float2 tex0: TEXCOORD0):COLOR  
  62. {  
  63.     return color * tex2D(TexS, tex0);  
  64. }  
  65.   
  66. technique FireRingTech  
  67. {  
  68.     pass P0  
  69.     {  
  70.         vertexShader = compile vs_2_0 FireRingVS();  
  71.         pixelShader = compile ps_2_0 FireRingPS();  
  72.   
  73.         PointSpriteEnable = true ;  
  74.         AlphaBlendEnable = true ;  
  75.         SrcBlend = One ;  
  76.         DestBlend = One ;  
  77.         ZWriteEnable = false ;  
  78.     }  
  79. }  

                这个粒子的效果如下截图:


              稍微改下,还能实现这样的效果:

                  

                粒子系统的关键就在与如何控制粒子的产生,运动等等。通过控制这些,你能够实现任何你想要的效果。

                好了,今天到这里结束了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值