Unigine::Materials Class
Header: | #include <UnigineMaterials.h> |
Interface for managing loaded materials via the code.
All materials existing in the project are preloaded at Engine initialization depending on the materials preload mode specified by the materials_preload console command and saved on world's saving or on calling the materials_save console command.
See Also#
- C++ API sample located in the folder <UnigineSDK>/source/samples/Api/Systems/Materials
Materials Class
Members
void setPrecompileAllShaders ( bool shaders ) #
Arguments
- bool shaders - Set true to enable shader precompilation; false - to disable it.
bool isPrecompileAllShaders() const#
Return value
true if shader precompilation is enabled; otherwise false.int getNumMaterials() const#
Return value
Current number of materials.static Event<> getEventEndReload() const#
Usage Example
// implement the EndReload event handler
void endreload_event_handler()
{
Log::message("\Handling EndReload event\n");
}
//////////////////////////////////////////////////////////////////////////////
// 1. Multiple subscriptions can be linked to an instance of the EventConnections
// class that you can use later to remove all these subscriptions at once
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnections class
EventConnections endreload_event_connections;
// link to this instance when subscribing to an event (subscription to various events can be linked)
Materials::getEventEndReload().connect(endreload_event_connections, endreload_event_handler);
// other subscriptions are also linked to this EventConnections instance
// (e.g. you can subscribe using lambdas)
Materials::getEventEndReload().connect(endreload_event_connections, []() {
Log::message("\Handling EndReload event (lambda).\n");
}
);
// ...
// later all of these linked subscriptions can be removed with a single line
endreload_event_connections.disconnectAll();
//////////////////////////////////////////////////////////////////////////////
// 2. You can subscribe and unsubscribe via an instance of the EventConnection
// class. And toggle this particular connection off and on, when necessary.
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnection class
EventConnection endreload_event_connection;
// subscribe to the EndReload event with a handler function keeping the connection
Materials::getEventEndReload().connect(endreload_event_connection, endreload_event_handler);
// ...
// you can temporarily disable a particular event connection to perform certain actions
endreload_event_connection.setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
endreload_event_connection.setEnabled(true);
// ...
// remove subscription to the EndReload event via the connection
endreload_event_connection.disconnect();
//////////////////////////////////////////////////////////////////////////////
// 3. You can add EventConnection/EventConnections instance as a member of the
// class that handles the event. In this case all linked subscriptions will be
// automatically removed when class destructor is called
//////////////////////////////////////////////////////////////////////////////
// Class handling the event
class SomeClass
{
public:
// instance of the EventConnections class as a class member
EventConnections e_connections;
// A EndReload event handler implemented as a class member
void event_handler()
{
Log::message("\Handling EndReload event\n");
// ...
}
};
SomeClass *sc = new SomeClass();
// ...
// specify a class instance in case a handler method belongs to some class
Materials::getEventEndReload().connect(sc->e_connections, sc, &SomeClass::event_handler);
// ...
// handler class instance is deleted with all its subscriptions removed automatically
delete sc;
//////////////////////////////////////////////////////////////////////////////
// 4. You can subscribe and unsubscribe via the handler function directly
//////////////////////////////////////////////////////////////////////////////
// subscribe to the EndReload event with a handler function
Materials::getEventEndReload().connect(endreload_event_handler);
// remove subscription to the EndReload event later by the handler function
Materials::getEventEndReload().disconnect(endreload_event_handler);
//////////////////////////////////////////////////////////////////////////////
// 5. Subscribe to an event saving an ID and unsubscribe later by this ID
//////////////////////////////////////////////////////////////////////////////
// define a connection ID to be used to unsubscribe later
EventConnectionId endreload_handler_id;
// subscribe to the EndReload event with a lambda handler function and keeping connection ID
endreload_handler_id = Materials::getEventEndReload().connect([]() {
Log::message("\Handling EndReload event (lambda).\n");
}
);
// remove the subscription later using the ID
Materials::getEventEndReload().disconnect(endreload_handler_id);
//////////////////////////////////////////////////////////////////////////////
// 6. Ignoring all EndReload events when necessary
//////////////////////////////////////////////////////////////////////////////
// you can temporarily disable the event to perform certain actions without triggering it
Materials::getEventEndReload().setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
Materials::getEventEndReload().setEnabled(true);
Return value
Event reference.static Event<> getEventBeginReload() const#
Usage Example
// implement the BeginReload event handler
void beginreload_event_handler()
{
Log::message("\Handling BeginReload event\n");
}
//////////////////////////////////////////////////////////////////////////////
// 1. Multiple subscriptions can be linked to an instance of the EventConnections
// class that you can use later to remove all these subscriptions at once
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnections class
EventConnections beginreload_event_connections;
// link to this instance when subscribing to an event (subscription to various events can be linked)
Materials::getEventBeginReload().connect(beginreload_event_connections, beginreload_event_handler);
// other subscriptions are also linked to this EventConnections instance
// (e.g. you can subscribe using lambdas)
Materials::getEventBeginReload().connect(beginreload_event_connections, []() {
Log::message("\Handling BeginReload event (lambda).\n");
}
);
// ...
// later all of these linked subscriptions can be removed with a single line
beginreload_event_connections.disconnectAll();
//////////////////////////////////////////////////////////////////////////////
// 2. You can subscribe and unsubscribe via an instance of the EventConnection
// class. And toggle this particular connection off and on, when necessary.
//////////////////////////////////////////////////////////////////////////////
// create an instance of the EventConnection class
EventConnection beginreload_event_connection;
// subscribe to the BeginReload event with a handler function keeping the connection
Materials::getEventBeginReload().connect(beginreload_event_connection, beginreload_event_handler);
// ...
// you can temporarily disable a particular event connection to perform certain actions
beginreload_event_connection.setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
beginreload_event_connection.setEnabled(true);
// ...
// remove subscription to the BeginReload event via the connection
beginreload_event_connection.disconnect();
//////////////////////////////////////////////////////////////////////////////
// 3. You can add EventConnection/EventConnections instance as a member of the
// class that handles the event. In this case all linked subscriptions will be
// automatically removed when class destructor is called
//////////////////////////////////////////////////////////////////////////////
// Class handling the event
class SomeClass
{
public:
// instance of the EventConnections class as a class member
EventConnections e_connections;
// A BeginReload event handler implemented as a class member
void event_handler()
{
Log::message("\Handling BeginReload event\n");
// ...
}
};
SomeClass *sc = new SomeClass();
// ...
// specify a class instance in case a handler method belongs to some class
Materials::getEventBeginReload().connect(sc->e_connections, sc, &SomeClass::event_handler);
// ...
// handler class instance is deleted with all its subscriptions removed automatically
delete sc;
//////////////////////////////////////////////////////////////////////////////
// 4. You can subscribe and unsubscribe via the handler function directly
//////////////////////////////////////////////////////////////////////////////
// subscribe to the BeginReload event with a handler function
Materials::getEventBeginReload().connect(beginreload_event_handler);
// remove subscription to the BeginReload event later by the handler function
Materials::getEventBeginReload().disconnect(beginreload_event_handler);
//////////////////////////////////////////////////////////////////////////////
// 5. Subscribe to an event saving an ID and unsubscribe later by this ID
//////////////////////////////////////////////////////////////////////////////
// define a connection ID to be used to unsubscribe later
EventConnectionId beginreload_handler_id;
// subscribe to the BeginReload event with a lambda handler function and keeping connection ID
beginreload_handler_id = Materials::getEventBeginReload().connect([]() {
Log::message("\Handling BeginReload event (lambda).\n");
}
);
// remove the subscription later using the ID
Materials::getEventBeginReload().disconnect(beginreload_handler_id);
//////////////////////////////////////////////////////////////////////////////
// 6. Ignoring all BeginReload events when necessary
//////////////////////////////////////////////////////////////////////////////
// you can temporarily disable the event to perform certain actions without triggering it
Materials::getEventBeginReload().setEnabled(false);
// ... actions to be performed
// and enable it back when necessary
Materials::getEventBeginReload().setEnabled(true);
Return value
Event reference.Ptr<Material> loadMaterial ( const char * path ) #
Loads a material stored by the given path. The function can be used to load materials created during application execution or stored outside the data directory.Arguments
- const char * path - A path to the material (including its name).
Return value
A loaded material.bool isMaterialGUID ( const UGUID & guid ) const#
Returns a value indicating if a material with the specified GUID exists.Arguments
Return value
true if the material with the specified GUID exists; otherwise, false.Ptr<Material> getMaterial ( int num ) const#
Returns the material by its number.Arguments
- int num - Material number.
Return value
A material.Ptr<Material> findManualMaterial ( const char * name ) const#
Searches for a manual material by the given name.Arguments
- const char * name - A manual material name.
Return value
A manual material smart pointer.Ptr<Material> findMaterialByGUID ( const UGUID & guid ) const#
Searches for a material with the given GUID.Arguments
Return value
A material smart pointer.Ptr<Material> findMaterialByFileGUID ( const UGUID & guid ) const#
Searches for a material with the given GUID of a material file.Arguments
Return value
A material smart pointer.Ptr<Material> findMaterialByPath ( const char * path ) const#
Searches for a material stored by the specified path.Arguments
- const char * path - A loading path of the material (including a material's name).
Return value
A material smart pointer.void setCachedMaterial ( const Ptr<Material> & mat ) #
Sets the material to be modified in runtime. This method is used together with setCachedState and getCachedMaterial() to change the material state in runtime without the necessity to recalculate the materials every frame and recompile the shaders. Using these methods is highly recommended if the material states are changed almost every frame or several times per frame.Let's review an example use case that can make use of these methods. Assume that you have a performance-consuming material and you want to reduce its quality when it's rendered in reflections. The following pseudo code demonstrates the approach to using these methods:
MaterialPtr get_effect_material(bool quality)
{
Materials::setCachedMaterial(my_effect);
Materials::setCachedState("quality", quality);
return Material::getCachedMaterial();
}
// switching quality of material depending on where it's rendered
void event()
{
bool quality = true;
if (Renderer::isReflection())
quality = false;
MaterialPtr mat = get_effect_material(quality);
}
Arguments
Ptr<Material> getCachedMaterial ( ) #
Returns the material modified in runtime. This method is used together with setCachedMaterial() and setCachedState to change the material state in runtime without the necessity to recalculate the materials every frame and recompile the shaders. Using these methods is highly recommended if the material states are changed almost every frame or several times per frame.Let's review an example use case that can make use of these methods. Assume that you have a performance-consuming material and you want to reduce its quality when it's rendered in reflections. The following pseudo code demonstrates the approach to using these methods:
MaterialPtr get_effect_material(bool quality)
{
Materials::setCachedMaterial(my_effect);
Materials::setCachedState("quality", quality);
return Material::getCachedMaterial();
}
// switching quality of material depending on where it's rendered
void event()
{
bool quality = true;
if (Renderer::isReflection())
quality = false;
MaterialPtr mat = get_effect_material(quality);
}
Return value
The material modified in runtime.void setCachedState ( const char * name, int value ) #
Sets the target state for the material to modify it in runtime. This method is used together with setCachedMaterial() and getCachedMaterial() to change the material state in runtime without the necessity to recalculate the materials every frame and recompile the shaders. Using these methods is highly recommended if the material states are changed almost every frame or several times per frame.Let's review an example use case that can make use of these methods. Assume that you have a performance-consuming material and you want to reduce its quality when it's rendered in reflections. The following pseudo code demonstrates the approach to using these methods:
MaterialPtr get_effect_material(bool quality)
{
Materials::setCachedMaterial(my_effect);
Materials::setCachedState("quality", quality);
return Material::getCachedMaterial();
}
// switching quality of material depending on where it's rendered
void event()
{
bool quality = true;
if (Renderer::isReflection())
quality = false;
MaterialPtr mat = get_effect_material(quality);
}
Arguments
- const char * name - The name of the state to be changed.
- int value - The target state value.
bool removeMaterial ( const UGUID & guid, bool remove_file = 0, bool remove_children = 1 ) #
Deletes a material. If the remove_file flag is enabled, the material file will be deleted as well. If the flag is disabled, the deleted material will be loaded again on the next application start-up. If the remove_children flag is enabled, all the children of the material will be deleted as well.Arguments
- const UGUID & guid - GUID of the material to be removed.
- bool remove_file - Flag indicating if the material file will be deleted.
- bool remove_children - Flag indicating if all the children of the material will be also deleted.
Return value
1 if the material is deleted successfully; otherwise, 0.bool replaceMaterial ( const Ptr<Material> & material, const Ptr<Material> & new_material ) #
Replaces the material with the given one.Arguments
- const Ptr<Material> & material - A material to be replaced.
- const Ptr<Material> & new_material - A replacing material.