Material Layers

Introduction

Material Layersets let you choose the appearance of a given object depending on the currently set viewpoint layers set. Lets take an example: you’re making an architectural visualization of an outdoor scene with lot of vegetation. Each tree or plant can have four different versions of its material: one per season. Then, based on the date of the day, the right version can be dynamically selected without modifying the scene.

Description

Each HOOPS Luminate material can have several configurations sorted in layer sets. Each configuration can be made of the assembly of any shader (built-in or custom). At render time, the configuration matching the one of the viewpoint will be used to draw, otherwise, if no configuration is suitable in the material, the default material will be used.

Here, we create two primitives and define two layer sets. The first primitive, a sphere, gets a material defined for the two layer sets (a red colour for first layer set and a blue colour for the second). The second primitive, a torus, has a green material defined only for the first layer set:

RED::Object* sphere = RED::Factory::CreateInstance( CID_REDMeshShape );
if( sphere == NULL )
    return RED_ALLOC_FAILURE;

RED::IMeshShape* isphere = sphere->As< RED::IMeshShape >();
RC_TEST( isphere->Sphere( RED::Vector3( -30.f, 0.f, 15.f ), 15.f, 64, 32, iresmgr->GetState() ) );

RED::Object* mat_sphere1, *mat_sphere2, *material;

RC_TEST( iresmgr->CreateMaterial( material, iresmgr->GetState() ) );
RED::IMaterial* imat = material->As< RED::IMaterial >();

RC_TEST( iresmgr->CreateColorMaterial( mat_sphere1, RED::Color::RED, iresmgr->GetState() ) );
RC_TEST( iresmgr->CreateColorMaterial( mat_sphere2, RED::Color::BLUE, iresmgr->GetState() ) );
RC_TEST( imat->CopyFrom( *mat_sphere1, RED::LayerSet::ALL_LAYERS, g_ls[0], iresmgr->GetState() ) );
RC_TEST( imat->CopyFrom( *mat_sphere2, RED::LayerSet::ALL_LAYERS, g_ls[1], iresmgr->GetState() ) );

RC_TEST( sphere->As< RED::IShape >()->SetMaterial( material, iresmgr->GetState() ) );

RED::Object* torus = RED::Factory::CreateInstance( CID_REDMeshShape );
if( torus == NULL )
return RED_ALLOC_FAILURE;

RED::IMeshShape* itorus = torus->As< RED::IMeshShape >();
RC_TEST( itorus->Torus( RED::Vector3( 20.f, 0.f, 15.f ), 15.f, 5.f, 64, 64, iresmgr->GetState() ) );

RED::Object* mat_torus;

RC_TEST( iresmgr->CreateMaterial( material, iresmgr->GetState() ) );
imat = material->As< RED::IMaterial >();
RC_TEST( iresmgr->CreateColorMaterial( mat_torus, RED::Color::GREEN, iresmgr->GetState() ) );
RC_TEST( imat->CopyFrom( *mat_torus, RED::LayerSet::ALL_LAYERS, g_ls[0], iresmgr->GetState() ) );

RC_TEST( torus->As< RED::IShape >()->SetMaterial( material, iresmgr->GetState() ) );

Now if we apply the first layer set on the viewpoint, we see the two primitives drawn, each with its corresponding material configuration

  • Red for the sphere, green for the torus:

../../../_images/wf_MaterialLayers_first.jpg

The scene rendered with the first layer set applied.

For the second layer set, things are a little bit different. While the sphere has a correct configuration set for that layer set, the torus hasn’t. By default with HOOPS Luminate, when no material or no material configuration exists for a given rendering setup, a shiny white default material is used instead. This is the material which is displayed by the torus when the second layer set is applied to the viewpoint.

../../../_images/wf_MaterialLayers_second.jpg

The scene rendered with the second layer set applied.