##################
Material Layersets
##################


Similarly to the shape layersets (see the **Layersets** section in the doc :doc:`/book/subjects/bk_sgo/bk_sg_shape_attributes` in the :doc:`/book/subjects/bk_scenegraph` book), HOOPS Luminate gives the ability to define several configurations in a same material associated to different ``RED::LayerSet`` layersets.

*******************************************************
Merging Materials Configurations on Different Layersets
*******************************************************

A material is composed of shaders (``RED::RenderShader`` and ``RED::StateShader``). By using different layersets, a HOOPS Luminate user can define different shader configurations for his material. The ``RED::IViewpoint::ApplyLayerSet`` function will then allow the camera to render the scene with one material layerset configuration. As an example, a material could have a layerset for real-time rendering and a layerset for high-quality rendering.

As a consequence, all the ``RED::IMaterial`` methods manipulating shaders will ask for a ``RED::LayerSet``. For example:

    * ``RED::IMaterial::AddShaderToPass``
    * ``RED::IMaterial::RemoveShaderFromPass``
    * ``RED::IMaterial::InsertShadersFrom``
    * etc.

.. figure:: bk_bm_layersets_merge_01.png 
  :align: center
  
  **An example of material using different layersets**

****************************************************
Layerset Intersection or Layerset Exact Match Option
****************************************************

For ``RED::IShape instances``, a shape is discarded from the rendering whenever its own layerset does not match any valid layer bit of the viewpoint's layerset. As explained in the **Layersets** section of the doc :doc:`/book/subjects/bk_sgo/bk_sg_shape_attributes`, an intersection test is done.

Contrary to the layersets applied on ``RED::IShape``, by default the material layerset rendering test is based on exact matching. The material is used only if the layerset of the camera is exactly the same as the material layerset. This behaviour is due to the fact that a single shader configuration (i.e. a given unique layerset) must be selected during rendering and can not coexist with another configuration.

If you take a look at the previous figure, it is clear that if the camera had the layerset #AB, one material layerset #A or #B have to be chosen. The material can not be rendered using both layerset #A and layerset #B.

A HOOPS Luminate option allows nonetheless to change this behaviour and to activate the intersection test for material layersets: ``RED::OPTIONS_MATERIAL_LAYER_INTERSECT``.

By activating this option, it is up to the user to take care of its material configurations. All of the selected shaders will have the same ``RED::LayerSet``. As there is not a unique selection solution HOOPS Luminate will select the first material layerset which intersects the layerset of the camera parsing the material from the last shader added to a rendering pass to the first.

Still by looking at the previous schema, if the last shader added to the material had been the shader #An and if the camera uses the layerset #AB then the material layerset #A will be used.
