JavaScript 中的多线程用于游戏开发

发布于 2024-11-15 13:47:42 字数 398 浏览 1 评论 0原文

我正在考虑用纯 JavaScript 和 html5 开发游戏,而不使用任何第三方插件。我面临的问题是,我无法找到一种方法将游戏的不同“模块”分离到单独的线程中,例如渲染作业、游戏逻辑、资源加载等。

Web Workers 似乎能够将代码分离到不同的线程中,但它们的问题是我可以在它们之间传递的信息有限。例如,对于渲染作业,我需要为游戏的每次更新传递整个“世界”,以及所有实体、网格、纹理等,因为工作线程无法共享内存。它可以进行优化,例如仅在初始化时发送静态对象(网格、纹理),然后仅在更新时发送对象的状态(它是世界变换),但这仍然是不可取的。

有没有办法在它们之间发送大数据或让它们共享一些对象?或者是否有完全不同的方法来实现真正的多线程?我知道使用插件/齿轮有更简单的方法来实现这一点,但我只需要使用开放网络中可用的方法;

I am thinking of developing a game in pure JavaScript and html5, without using any third party plugins. The problem I am facing is that I cannot find a way to separate different "modules" of the game into separate threads, like the render job, game logic, asset loading and so on.

Web Workers seem to be able to separate code into different threads, but the problem with them is the limited information I can pass between them. For example, for the render job, I need to pass the whole "world", with all the entities, meshes, textures and so on for every update of the game, because worker threads cannot share memory. It can be optimized, like sending the static objects only on initialization (meshes, textures) and then send only the state of an object on update (it's world transform) but it still isn't desirable.

Is there a way to send large data between them or have them share some objects? Or is there a different method entirely of achieving true multithreading? I know there are easier ways of achieving this using plugins/ gears but I need to use only methods available in open web;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

維他命╮ 2024-11-22 13:47:42

JavaScript 的 Web Worker 在某种程度上是一种更好的并发编程模型。例如,它是事件驱动的并且没有共享对象。这意味着您不能进入网格锁(因为根本没有锁)并且对象不能通过同时被两个线程修改而进入无效状态。

问题是你不能轻易地将传统的游戏系统堆叠在这个模型之上。所以你需要以一种新的方式设计游戏系统来采用这种编程模型,我认为这可能是昂贵的。

JavaScript's Web Worker is a better concurrent programming model in some way. For example, it's event driven and there's no shared object. This means you can't get into grid lock (because there's no lock at all) and an object can't get into invalid state by being modified by two threads at the same time.

The problem is you can't easily stack traditional game system on the top of this model. So you need to design the game system in a new way to adopt this programming model, and I think this might be costly.

我最亲爱的 2024-11-22 13:47:42

您可能想研究一下 Web Workers,我没有使用它们的经验,但听说过它们。可能会引导您走向正确的方向。

http://ejohn.org/blog/web-workers/

You probably want to look into Web Workers, I have no experience with them but have heard of them. Might lead you into the right direction.

http://ejohn.org/blog/web-workers/

在巴黎塔顶看东京樱花 2024-11-22 13:47:42

web-workers 是 js 中最接近多线程的东西。
目前。
句号。

web-workers are the closest thing to multithreading in js.
At the moment.
Full stop.

只是在用心讲痛 2024-11-22 13:47:42

据我所知,Sans Web Workers 没有真正的方法可以在 javascript 中进行多线程处理。

不过,您可以使用一些逻辑来确定应该花费多少时间来完成某些任务的优先级。您可以创建在状态中存储信息的函数(因此可以退出循环,然后稍后再恢复)。每个循环只知道在保存状态并退出函数之前还有多少时间。

因此 for 循环也有一个时间条件...

//Loop over some iterator, exit if time is up
for (i = 1; i < max || timeNotUp(); i++)
{
}

//If exiting normally 
if (i == max)
{ //do somthing
}
//otherwise, save state
else
{
    this.i = i;
    this.otherStuff = otherStuff;
}

您绝对可以通过这种方式确定代码的优先级。但是,也有缺点。保存状态不会导致干净且易于遵循的代码(尤其是循环中的循环)。它也没有那么快,因为您需要不断检查时间并保存状态。

Sans Web Workers there's no real way to do multithreading in javascript that I know of.

You could use some logic to prioritize how much time should be spent doing certain tasks, though. You could make functions that store information in states (so loops could be exited and then picked back up at a later time). Each loop just knows how much time it has before it saves it's state and exits the function.

So the for loop would have a time condition, too...

//Loop over some iterator, exit if time is up
for (i = 1; i < max || timeNotUp(); i++)
{
}

//If exiting normally 
if (i == max)
{ //do somthing
}
//otherwise, save state
else
{
    this.i = i;
    this.otherStuff = otherStuff;
}

You can definitely prioritize your code this way. But, there are downsides. Saving your state doesn't lead to clean easy to follow code (especially with loops within loops). It also isn't as fast, since you're constantly checking time and saving states.

旧情别恋 2024-11-22 13:47:42

我建议使用函数式方法来构建游戏的各个方面。参见http://www.flapjax-lang.org

I recommend a functional approach for composing the aspects of your game. See e.g. http://www.flapjax-lang.org.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文