Emissive meshes are equivalent to self lit meshes. Both the emissive color and texture of the material determine how the mesh will self lit. This can for instance helps create a phosphorescent watch dial. Babylon JS supports natively emissive poperties in both the standard and PBR materials.
But how could we easily make the glow around those self lit areas ?
Only one line is needed to make all the emissive parts of a scene glow:
var gl = new BABYLON.GlowLayer("glow", scene);
By default, the glow layer is filled using the material emissive parameters (emissive result = emssive color emissive texture color emissive texture level). It will also use a blur kernel of 32 which might not be adapted to your visual requirements.
To control the intensity of the color in the glow layer, you can use the dedicated property:
var gl = new BABYLON.GlowLayer("glow", scene);
gl.intensity = 0.5;
In order to control change the shape of the blur, you can rely on the creation option:
var gl = new BABYLON.GlowLayer("glow", scene, {
mainTextureFixedSize: 1024,
blurKernelSize: 64
});
By default the glow layer will use emissive texture and emissive color to generate the glow color of every active mesh. But you can override this behavior with the following callbacks:
var gl = new BABYLON.GlowLayer("glow", scene);
gl.customEmissiveColorSelector = function(mesh, subMesh, material, result) {
if (mesh.name === "lightsaber") {
result.set(1, 0, 1, 1);
} else {
result.set(0, 0, 0, 0);
}
}
Depending on your setup, some aliasing artifacts might appear in the glow. To prevent this behavior, you can specify the number of samples to use for MSAA on the main render target. Please note that it will only work on WebGL2 capable browsers.
var gl = new BABYLON.GlowLayer("glow", scene, {
mainTextureSamples: 4
});
Some helper functions have been introduced to exclude or only include some meshes from the scene.
In order to exclude meshes from the glow layer you can use the dedicated function:
var gl = new BABYLON.GlowLayer("glow", scene);
gl.addExcludedMesh(mesh)
In order to include only a subset of meshes in the glow layer you can use the dedicated function:
var gl = new BABYLON.GlowLayer("glow", scene);
gl.addIncludedOnlyMesh(mesh)
Using the function will automatically switch mode and only render the included meshes.