Bryan
Bryan

Reputation: 63

ThreeJS Updates Outside Loop

It's not clear if it's ok to update values outside of the animate() loop.

Would updates outside the loop affect render performance?

The only disadvantage I can see is some updates may be partially complete, having to wait for the next animate call to fully appear.

Any disadvantage I am missing?

function animate() {
    requestAnimationFrame( animate )
    updatePositions()
}

vs.

function animate() {
    requestAnimationFrame( animate )
}

function onWebSocketUpdate() {
    updatePositions()
}

Another way of looking at it:

onWebSocketUpdate(data) {

  // Option 1
  // ws directly applies the update 
  model.update(data)

  // Option 2
  // ws saves data to buffer
  buffer.push(data)
  // when animate() runs, it pops buffer data
  model.update(buffer.pop())
}

Upvotes: 1

Views: 353

Answers (1)

Aram Avetisyan
Aram Avetisyan

Reputation: 81

If your web socket sends updates to change an object position (which is the case I guess) you have two cases:

  1. If the updates come with higher frequency then your frame rate, you should run updatePositions() inside the animate(). You don't need more than one position update in between frames.
  2. If the updates come with lower frequency then your frame rate, It's better to update positions outside of the animate(). You'll save some performance by not calling updatePositions() on every frame

Upvotes: 1

Related Questions