Docs says that a camera in PostProcess constructor can be null (it’s nullable) Link
But if we set a camera as null it leads to crush ( Cannot read property ‘createEffect’ of undefined).
Why the possibility to initialize with null camera is important.
Imagine that our game uses several levels, i.e. it has several createScene functions. And all of them use a player instance (that is mortal). DeathPostProcess is based on this example:
createScene1() {
var player = new Player(scene);
// other content
}
createScene2() {
var player = new Player(scene);
// other content
}
class Player {
constructor(scene) {
this.camera = new BABYLON.FreeCamera("FreeCamera", new BABYLON.Vector3(0, -6, -20), BABYLON.Vector3.Zero(), scene);
this.deathPostProcess = new BABYLON.PostProcess("deathPostProcess", "/shaders/deathPostProcess", ["time"], null, 1, this.playerCamera);
}
onPlayerDeath() {
let time = 0;
let rate = 0.016;
let v = 0;
this.scene.registerBeforeRender(function () {
time += rate;
v = Math.pow(Math.min(0.75, time), 3)*0.5;
});
this.deathPostProcess.onApply = function (effect) {
effect.setFloat('time', v);
}
}
}
What will happen here… Imagine, that player is dead and we switch the scene (probably even just reloading the same level). Will it reinitialize the PostProcess in it’s initial state? The answer is no. Babylon will apply the last state of the current PostProcess, i.e. the screen will remain the red even after the scene is reloaded and both camera and PostProcess is reinitialized. Looks like it extracts the PostProcess from GPU cache.
Solution 1: Use camera.detachPostProcess
in the constructor (right after PostProcess init). And use camera.attachPostProcess
in onPlayerDeath()
. But using camera.detachPostProcess
looks excesive, it might be more correct just to create a PostProcess without a camera and then only attach it on player’s death. Variable will be calculated to the correct value already.
Solution 2: Initialize the PostProcess inside of onPlayerDeath()
function. This solution is even worse, because it may cause unwanted delay on PostProcess load when the player is already dead.
Maybe it’s not a big deal, because workarounds are easy, but since the docs says, that initializing PostProcess with null-camera is possible, then it would be nice to restore this functionallity for more convinient usage.