Using a promise in webxr

Hi!

I’m trying to do a calculation that takes more than one frame. I’ve put this in a promise, but I still get major stutter. How do I have to structure that kind of thing for it to not affect the framerate?

The thing I’m trying to do is watch for QR codes from the camerastream, so the structure is basically:

const callMyLoop(){
new promise((resolve, reject) => {
    const result = doAThingThatTakesMoreThanOneFrame()
    if(result){
        resolve()
    } else{
        reject()
    }
})
}
navigator.mediaDevices.getUserMedia(mediaStreamConstraints).then(stream => {
    video.play().then(callMyLoop())
}

in your code here doAThingThatTakesMoreThanOneFrame is still fully synchornous. it should either return a promise or use a callback to be async.

Maybe using coroutines will help?
Coroutines | Babylon.js Documentation (babylonjs.com)

Thanks both! I’ll try that out.

Thanks for that specific feedback, I’ve found a different version of that function that seems asynchronous since it returns a promise. I’ve added my other code to the then of that promise. Unfortunately it keeps stuttering… So I suspect it has a similar implementation as to what I was doing before.

I suppose there’s not really a way to do threading with BabylonJS? :sweat_smile:

It is not a babylonjs limitation here but a javascript one and the only way to multithread is with workers.

Coroutines and Promises as Gary mentioned are an easy way to defer chunck of work on the task queue as would setTimeout or queueMicroTask do.

A cheap trick is run chunck of work within the render loop let say 5 iteration of a 1000 long loop for instance.

The patterns there might be a but tedious at first but once grasped they are pretty usable.

Hi! Thanks, I’m currently looking at workers. I’ve used coroutines with unity in the past, but the problem here is that I just have one function call that I can’t really break up.

I’m diving into Workers now though, hope that fixes it :smiley: Thanks again for all the suggestions.