Applying Shaders to everything except UI



  • Hello everyone,
    I'm experimenting with using shaders in my HaxePunk game and I was wondering:
    Is there a way to apply a shader to the scene, but not the game UI?

    For example, imagine blurring the screen when the player opens an inventory interface, or applying effects to the game world only.

    Thanks in advance.


  • administrators

    The way I've done this is to make my UI and game content separate scenes, something like this:

    class UIScene extends Scene {
        var gameScene:GameScene;
    
        public function new(g:GameScene) {
            super();
            gameScene = g;
            bgAlpha = 0;
        }
    
        public function update() {
            super.update();
            if (!gamePaused) gameScene.update();
        }
    }
    
    class GameScene extends Scene {
        // ...
    }
    

    And to activate both:

    var gameScene = new GameScene(),
        uiScene = new UIScene(gameScene);
    HXP.engine.pushScene(gameScene);
    HXP.engine.pushScene(uiScene);
    

    You can then apply shaders to either scene.



  • @bendmorris
    Hi, is it possible to have multiple scenes rendered at once?
    i try smth like this:

    var scene1:Scene = new Scene();
    var e1:Entity = new Entity(0, 0, Image.createRect(100, 100, 0xff0000));
    scene1.add(e1);
    
    var scene2:Scene = new Scene();
    scene2.bgAlpha = 0;
    var e2:Entity = new Entity(0, 0, Image.createRect(50, 50, 0x00ff00));
    scene2.add(e2);
    
    HXP.engine.pushScene(scene1);
    HXP.engine.pushScene(scene2);
    

    and i can only see scene2, the green square



  • @kanakro Absolutely! It may not be too obvious, but it's actually being done in @bendmorris 's example! If you look closely, inside of the UIScene update the gameScene is being manually updated. Like you have found, only the last added scene will be operational.

    The trick here is to pass uiScene a reference to the game scene, and to make sure uiScene is the last scene pushed on the stack. Then both are able to render.

    There is one caveat though, as you found, if you pushed two scenes on to the stack at the same time only the last one will be rendered; but if you had pushed on your uiScene after the gameScene was running for a while then you'd see your rendered square, the scene logic wouldn't update anymore.


Log in to reply
 

Looks like your connection to NodeBB was lost, please wait while we try to reconnect.