Skip to content

Shaders

miguel edited this page May 13, 2019 · 2 revisions

Shaders are effects which manipulate a frame to achieve visual fidelity. For example, you can apply a Bloom effect which looks combined like this:

scape-example

The corresponding braingdx code looks like this:

// Within the create() method
AutoReloadPostProcessorEffect<Bloom> bloomEffect = context.getShaderManager().createBloomEffect();
bloomEffect.mutate(new Mutator<Bloom>() {
      @Override
      public void mutate(Bloom target) {
         target.setBlurAmount(10f);
         target.setBloomIntesity(1.5f);
         target.setBlurPasses(8);
         target.setThreshold(0.3f);
      }
});
context.getRenderPipeline().addEffects(RenderPipeIds.UI, bloomEffect);

Initially, we are creating a new Bloom effect (which internally consists of a compiled GLSL fragment and vertex shader). You might notice that the type is AutoReloadPostProcessorEffect. The reason for that is that we need to re-create internal frame buffers once a window is resized. This class automatically takes care of that so you do not have to worry about it.

Afterwards we can apply changes to the effect. The reason why I chose the Mutator pattern instead of simply exposing the internal Bloom implementation is simple: imagine someone could retrieve an internal reference of the shader. Once the shader gets re-created on window resize or app resume it can cause serious issues. As a result, the only way to modify shaders is via mutation.

Afterwards you can see that we simply add the effect to an existing render pipeline.

Clone this wiki locally