I have a bit of a weird on here.
In my project, I am using an of screen camera to render a 3D model to a texture and then applying it to UI element. This is so that I can rely on a ButtonComponent ‘click’ event when using a touch screen where multiple touches are being processed simultaneously.
This project contains two separate games in two scenes that can be chosen from a menu scene. When launching this particular scene, directly from the editor, the texture renders as expected:
However, when launching the menu scene first and then switching to this game, the texture becomes distorted/stretched:
The 3D objects are already existing in the scene in front of the required camera and are enabled when required. A screen space ParticleSystemComponent with a child ButtonComponent are instantiated and assigned the texture provided by the correct camera like this:
PowerUpManager3D.prototype.createPowerUp = function() {
let rand = (Math.random() > 0.5);
const choice = rand ? 'ball' : 'paddle';
const other = (!rand) ? 'paddle' : 'ball';
if(!this.activePowerUps[choice]) {
const templates = this.templates[choice];
const particles = this.particles[choice];
const randomTemplate = Math.floor(pc.math.random(0, templates.length));
particles[randomTemplate].enabled = true;
let leftEntity = templates[randomTemplate].resource.instantiate();
this.screen.addChild(leftEntity);
leftEntity.script.powerUp3d.renderCamera = this.cams[choice];
leftEntity.script.powerUp3d.dir = true;
leftEntity.script.powerUp3d.partner = this.paddles[0]
let rightEntity = templates[randomTemplate].resource.instantiate();
this.screen.addChild(rightEntity);
rightEntity.script.powerUp3d.renderCamera = this.cams[choice];
rightEntity.script.powerUp3d.partner = this.paddles[1]
rightEntity.enabled = true;
leftEntity.enabled = true;
this.activePowerUps[choice] = {
entities: [leftEntity, rightEntity],
particles: particles[randomTemplate]
};
}
else if(!this.activePowerUps[other]) {
const templates = this.templates[other];
const particles = this.particles[other];
const randomTemplate = Math.floor(pc.math.random(0, templates.length));
particles[randomTemplate].enabled = true;
let leftEntity = templates[randomTemplate].resource.instantiate();
this.screen.addChild(leftEntity);
leftEntity.script.powerUp3d.renderCamera = this.cams[other];
leftEntity.script.powerUp3d.dir = true;
leftEntity.script.powerUp3d.partner = this.paddles[0]
let rightEntity = templates[randomTemplate].resource.instantiate();
this.screen.addChild(rightEntity);
rightEntity.script.powerUp3d.renderCamera = this.cams[other];
rightEntity.script.powerUp3d.partner = this.paddles[1]
rightEntity.enabled = true;
leftEntity.enabled = true;
this.activePowerUps[other] = {
entities: [leftEntity, rightEntity],
particles: particles[randomTemplate]
};
}
this.startPowerUps(pc.math.random(5, 15));
};
As you can see, the properties are assigned to object after instantiation but before being enabled. When enabled, here is the code that the instantiated object runs:
PowerUp3d.prototype.initialize = function() {
this.powerUpMgr = this.app.root.children[0];
this.app.on('newCameraRender', this.onNewTexture, this);
this.on('destroy', this.onDestroy, this);
};
PowerUp3d.prototype.postInitialize = function() {
this.onNewTexture();
this.button.button.on('click', this.onClick, this);
};
PowerUp3d.prototype.onNewTexture = function() {
if(!this.renderCamera) return;
this.renderTexture = this.renderCamera.script.renderCameraTotexture.renderedTexture;
this.button = this.entity.findByName('PowerupButton');
this.button.element.texture = this.renderTexture;
};
PowerUp3d.prototype.onDestroy = function() {
this.floatTween.stop();
this.app.off('newCameraRender', this.onNewTexture, this);
};
At first, when testing the switching of scenes, I was met with an error that indicated that this.renderCamera.script
was undefined. This was strange because the referenced entity already exists in the scene for at least 10 seconds before instantiating the template. Despite the error, the distorted texture would appear a second or two after. I assumed that this was potentially a hierarchy order problem and moved things around. The project is not longer providing this error, but the distortion remains.
The most confusing part of this for me is that these issues (including errors) are/were not issues when launching the scene directly from the editor.
This is a somewhat mission critical problem for me to get solved, and I would really appreciate any guidance on why this is occurring and any workarounds that may address it.