Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not a three.js person but I do have experience in optimization algorithms. It is a mistake to loop though all the particles each time. </p> <p>Instead, the particles need to be "sorted" and then when you have your camera position, you just find where it goes in the sort and you'll immediately have the closest points. It's very much like an index on a database. Or think the library. If someone gives you a book called "sorting" and the whole library is in strict alphabetical order, you don't have to check every single book in the library, you just walk right to S then SO, then SOR, then manually compare for the rest. So in a few seconds you could find the closest book to your book, without looking at all the books.</p> <p>The way this works in a 2d case, is you need to store a couple levels of a "grid", then just determine which grid you are in, and check particles in that grid. For example, let's say you have 1 million particles, and on each one, you store whether it's on the right, or left of the center. Now with one quick "if" you can check if your camera is on the right or left side, and with that one if, you've cut out 500,000 particles that you need to loop through. So in practice, you'd store maybe 2 levels of a 1-9 grid, on each particle, when they are created, and that will cut out 99% of the particles you have to check. once you figure out which quadrant your camera (exact center) is in, you just look in the array of particles stored in that "grid location". So your particles will be stored in particle[1-9][1-9][1-100] or something like that. </p> <p>Now if your particles are moving a lot, you'll have to do some simple mod (%) type math to figure out which grid location they're in, as they move, etc.</p> <p>futher optimization can be done by setting a threshold, ie, what is "close enough", so even if your have 1000 particles almost on top of each other, if you get it within 10 pixels, you'll get a decent result much faster.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload