Improving a scene with postprocessing filters

In the Creating dynamic skybox with moving sun recipe, we created a dynamic skybox that has many applications. It's possible to improve the appearance of this (and any other) scene significantly with postprocessing filters. They are called postprocessing filters because they are applied after the scene has already been rendered. This also makes them affect everything in the scene.

We also covered how to create an advanced postfilter in Chapter 1, SDK Game Development Hub.

How to do it...

The sun we have is now moving across the sky. It has very sharp edges and we can use a bloom filter to smooth it out a bit. Perform the following steps to improve a scene with the help of postprocessing filters:

  1. First of all, we need to create a new FilterPostProcessor instance called processor.
  2. Add this to the main view port, by calling viewPort.addProcessor(processor) from within the application.
  3. Then, we create a new bloom filter called bloomFilter. The default settings will produce a decent result, but it might be worth playing around a bit with the settings.
  4. Add the bloomFilter to processor.addFilter(bloomFilter) and try it again.
  5. Then, we create a new LightScatteringFilter instance called lightScatteringFilter and add it again to processor.addFilter(lightScatteringFilter).
  6. This is dependent on a position for the light to scatter, so we need to make it aware of the sun's location. We can achieve this by adding a new field for the filter in the SunControl class from the last recipe along with a setter.
  7. Then in the controlUpdate method, once we have updated position, we add the following code:
    lightScatteringFilter.setLightPosition(position.mult(1000));
  8. We still have some tweaking to do as it will now also apply the effect when the sun is below the ground. To mitigate this, we can disable the filter during nighttime:
    if(y > -2f){
      if(!lightScatteringFilter.isEnabled()){
        lightScatteringFilter.setEnabled(true);
      }
      lightScatteringFilter.setLightDensity(1.4f);
    } else if(lightScatteringFilter.isEnabled()){
      lightScatteringFilter.setEnabled(false);
    }

How it works...

The FilterPostProcessor acts as a container for the filters and applies them to the rendered result. Several filters can be added to the same processor and the order matters. If we add LightScatteringFilter before bloomFilter, we will get bloom applied to the light scattering and vice versa.

The bloomFilter works by blurring the image slightly and intensifying colors, making the result appear a bit softer. Bloom filters work best with tweaking and shouldn't just be slapped on to the scene. It's easy to be impressed by the initial effect and leave it at that but it should always be adapted to the art style of the game. A fantasy game in an enchanted forest might get away with more bloom than a hard-boiled cyberpunk shooter.

The LightScatteringFilter instance does two things. Firstly, it creates a halo of rays emanating from the direction of the light source. Secondly, if the camera is pointing towards the light source, it will whiteout the image increasingly, simulating glare.

In a normal skybox, the sun would be static but in this example the sun keeps moving. By supplying the filter to SunControl, we could keep the logic to update the position within that class. We will also get some weird effects as the glare will still show. The easy way out is to simply turn off the effect as the sun gets below the horizon.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.222.185