Camera coordinates math problem

This may be of help:

Also in general you can find the Unity C# implementation for each method, and attempt to convert it to Javascript/PlayCanvas.