The following script has a camera linked to it, and is trying to find the bottom right screen-coordinate in world-space. Basically I’m trying to align something to the screen edge. I encountered some weird results from camera.screenToWorld however. It seems that in Initialize and PostInitialize the camera is accessible but gives strange results.
Is there a proper way of waiting until the camera is ready? or am I doing something else wrong?
var Test = pc.createScript('test');
Test.attributes.add('cameraEntity', { type: 'entity'});
Test.prototype.initialize = function() {
var screenCoords = this.measureScreen();
console.log("initialize x:" + screenCoords.x + " y:" + screenCoords.y);
};
Test.prototype.postInitialize = function() {
var screenCoords = this.measureScreen();
console.log("postinitialize x:" + screenCoords.x + " y:" + screenCoords.y);
};
Test.prototype.update = function(dt) {
var screenCoords = this.measureScreen();
console.log("update x:" + screenCoords.x + " y:" + screenCoords.y);
};
Test.prototype.measureScreen = function() {
return this.cameraEntity.camera.screenToWorld(
this.app.graphicsDevice.width,
this.app.graphicsDevice.height,
this.cameraEntity.camera.nearClip);
};
here’s the output from my scene:
initialize x:8.25738525390625 y:-5.94846248626709
postinitialize x:8.25738525390625 y:-5.94846248626709
update x:1.6625607013702393 y:-0.9826755523681641
update x:1.6625607013702393 y:-0.9826755523681641
...
I tested it in an empty scene, with only a camera and only the ‘test’ script on the camera and got the same results: