使用 CPU 而不是 GPU 绘制粒子 (XNA)
我正在尝试修改以下粒子系统。 http://create.msdn.com/en-US/education/catalog /sample/article_3d
我有一个函数,当我按下空格键时,所有粒子的位置和速度都设置为 0。
for (int i = 0; i < particles.GetLength(0); i++)
{
particles[i].Position = Vector3.Zero;
particles[i].Velocity = Vector3.Zero;
}
但是,当我按下空格键时,粒子仍然在移动。如果我转到 FireParticleSystem.cs,我可以将 settings.Gravity 设置为 0 并且粒子停止移动,但粒子仍然没有移动到 (0,0,0)。
据我了解,问题在于 GPU 正在处理所有粒子位置,并且它根据粒子的初始位置、初始速度并乘以它们的年龄来计算粒子应该在哪里。因此,我所能做的就是更改粒子的初始位置和速度,但我无法即时执行此操作,因为 GPU 正在处理所有事情。
我希望CPU单独计算粒子的位置。这是因为我稍后将使用某种风来推动粒子。如何阻止 GPU 接管?我认为这与 VertexBuffers 和绘图函数有关,但我不知道如何修改它以使其工作。
I'm trying out modifications to the following particle system.
http://create.msdn.com/en-US/education/catalog/sample/particle_3d
I have a function such that when I press Space, all the particles have their positions and velocities set to 0.
for (int i = 0; i < particles.GetLength(0); i++)
{
particles[i].Position = Vector3.Zero;
particles[i].Velocity = Vector3.Zero;
}
However, when I press space, the particles are still moving. If I go to FireParticleSystem.cs I can turn settings.Gravity to 0 and the particles stop moving, but the particles are still not being shifted to (0,0,0).
As I understand it, the problem lies in the fact that the GPU is processing all the particle positions, and it's calculating where the particles should be based on their initial position, their initial velocity and multiplying by their age. Therefore, all I've been able to do is change the initial position and velocity of particles, but I'm unable to do it on the fly since the GPU is handling everything.
I want the CPU to calculate the positions of the particles individually. This is because I will be later implementing some sort of wind to push the particles around. How do I stop the GPU from taking over? I think it's something to do with VertexBuffers and the draw function, but I don't know how to modify it to make it work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您下载的示例无法满足您的要求。您对问题的诊断是正确的:粒子系统完全由 GPU 维护,因此您对位置和速度的更改只会更改起始值,而不更改实际的实时粒子值。要制作一个可由CPU更改的粒子系统,您需要制作一个粒子引擎类并自己完成。还有许多其他示例可以执行此操作。
Riemers XNA 教程非常有用。试试这个链接:
http://www.riemers.net/eng/Tutorials/XNA /Csharp/Series2D/Particle_engine.php
它教你如何制作2D粒子系统。这可以轻松转换为 3D。
或者,如果您只想下载现有引擎,请尝试 Mercury 粒子引擎:
http://mpe.codeplex.com/
The sample you downloaded is not capable of doing what you ask. You are correct in your diagnosis of the problem: the particle system is entirely maintained by the GPU, and so your changes to the position and velocity only change the start values, not the actual real-time particle values. To make a particle system that is changeable by the CPU, you need to make a particle engine class and do it yourself. There are many other samples out there that do this.
Riemers XNA tutorials are very useful. Try out this link:
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Particle_engine.php
It teaches you how to make a 2D particle system. This can be easily converted into 3D.
Or, if you want to just download an existing engine, try the Mercury particle engine:
http://mpe.codeplex.com/
这非常简单……您所要做的就是在 CPU 上进行位置/速度计算,而不是将它们卸载到着色器。我当然看不到你的代码,所以我不能真正提供任何更具体的指导......但是无论你是使用像 FarSeer 这样的物理引擎为粒子设置动画,还是自己做基本的运动方程。它会发生在CPU上。
This is quite simple ... all you have to do is do the position/velocity calculations on the CPU rather than offloading them to a shader. I of course can't see your code, so I can't really offer any more specific guidance ... but whether you animate your particles with a physics engine like FarSeer, or just do the basic equation of motion yourself. It'll happen on the CPU.
我会为此推荐DPSF(动态粒子系统框架)。它在 CPU 上进行计算,完全可定制且非常灵活,有很棒的帮助文档和教程,甚至还提供了您正在使用的 Particle3D 示例中的 FireParticleSystem 的完整源代码。您应该能够将粒子系统集成到您的游戏中,并在几分钟内完成您想要的任务。
I would recommend DPSF (Dynamic Particle System Framework) for this. It does the calculations on the CPU, is fully customizable and very flexible, has great help docs and tutorials, and it even provides the full source code for the FireParticleSystem from the Particle3D sample that you are using. You should be able to have the particle system integrated into your game and accomplishing what you want within a matter of minutes.