Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

separate the Engine from the Render #517

Closed
alpha-xdream opened this issue Nov 13, 2017 · 8 comments
Closed

separate the Engine from the Render #517

alpha-xdream opened this issue Nov 13, 2017 · 8 comments

Comments

@alpha-xdream
Copy link

I want to run the Engine in H5 Worker and run the Render in the main JS. What should I do?

@liabru
Copy link
Owner

liabru commented Nov 14, 2017

The renderer is separate from the engine already, can you be more specific? If you mean running the engine in a web worker, it should be possible but you will need to serialise the world to pass it between threads. Have a look at the serializer in matter-tools which should help you start.

@alpha-xdream
Copy link
Author

I just want to make the matter-js keep running when switching the tab. My idea is running the engine in a web worker, but it show errors when I created an engine in a web worker because a worker isn't allowed to access html dom.

need to serialise the world to pass it between threads.

Does it mean serialising the world in a worker and passing it to the main js? I don't know how to make an engine in a web worker. /(ㄒoㄒ)/~~

@alpha-xdream
Copy link
Author

I had solved my problem. Just used HackTimer that override by using a web worker. see details at this.
Thanks for your answer!

@liabru
Copy link
Owner

liabru commented Nov 15, 2017

I had solved my problem. Just used HackTimer that override by using a web worker.

Looks like a pretty wild hack, but if it works then why not!

it show errors when I created an engine in a web worker because a worker isn't allowed to access html dom

Would you mind posting those errors?

Does it mean serialising the world in a worker and passing it to the main js?

Yeah exactly, I think that's how you need to do it if you wanted to render on the main thread.
Although, there's possibly a better way using transferables where you could even render from a worker too.

@alpha-xdream
Copy link
Author

Would you mind posting those errors?

Of course!

my worker.js

importScripts('matter.js')
var engine = Matter.Engine.create();

and my js in the main thread

var w = new Worker('worker.js')

the error is

image

@liabru liabru added the bug label Nov 16, 2017
@liabru liabru closed this as completed in 18a0845 Nov 26, 2017
@Nathan-Devery
Copy link

Had this same issue and adding hacker time worked, thanks so much.
https://evolutionary-fighters.azurewebsites.net/

@koraysels
Copy link

can we find an example of this working ? Ans does it have a performance improvement?

@liabru
Copy link
Owner

liabru commented Jun 6, 2021

@koraysels do you mean running in a worker? There's no official example of this yet (it's on my list!) but it should be possible to do as discussed here and in other threads.

The main benefit of threads in this context is usually to allow rendering and physics to run at different rates rather than performance. You'd only see a boost if you can make use more than one thread, which is not straightforward for the physics side of things (on my list too, but it's a complex research topic of its own).

Usually this starts with rendering on one thread and running physics on another thread, which might give a small boost depending on how complex your rendering is more than anything else. It might also be a slight detriment depending on how you implement the data sharing between threads (sending minimal data through shared memory is probably the way to go).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants