This page has been translated automatically.
Video Tutorials
Interface
Essentials
Advanced
How To
Basics
Rendering
Professional (SIM)
UnigineEditor
Interface Overview
Assets Workflow
Version Control
Settings and Preferences
Working With Projects
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Lighting
Sandworm
Using Editor Tools for Specific Tasks
Extending Editor Functionality
Built-in Node Types
Nodes
Objects
Effects
Decals
Light Sources
Geodetics
World Nodes
Sound Objects
Pathfinding Objects
Players
Programming
Fundamentals
Setting Up Development Environment
Usage Examples
C++
C#
UnigineScript
UUSL (Unified UNIGINE Shader Language)
Plugins
File Formats
Materials and Shaders
Rebuilding the Engine Tools
GUI
Double Precision Coordinates
API
Animations-Related Classes
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
VR-Related Classes
Content Creation
Content Optimization
Materials
Material Nodes Library
Miscellaneous
Input
Math
Matrix
Textures
Art Samples
Tutorials

Unigine.Materials Class

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 (enabled by default) 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

Properties

bool PrecompileAllShaders#

The value indicating if shader precompilation is enabled.

int NumMaterials#

The number of materials loaded for the current project.

Event EventEndReload#

The event triggered after all materials are reloaded (i. e. execution of ReloadMaterials() is finished), if the materials_reload_event console variable is enabled. You can subscribe to events via Connect()  and unsubscribe via Disconnect(). You can also use EventConnection  and EventConnections  classes for convenience (see examples below).
Notice
For more details see the Event Handling article.
The event handler signature is as follows: myhandler()

Usage Example

Source code (C#)
// implement the EndReload event handler
void endreload_event_handler()
{
	Log.Message("\Handling EndReload event\n");
}

//////////////////////////////////////////////////////////////////////////////
//  1. Multiple subscriptions can be linked to an EventConnections instance
//  class that you can use later to remove all these subscriptions at once
//////////////////////////////////////////////////////////////////////////////

// create an instance of the EventConnections class
EventConnections endreload_event_connections = new EventConnections();


// link to this instance when subscribing to an event (subscription to various events can be linked)
Materials.EventEndReload.Connect(endreload_event_connections, endreload_event_handler);

// other subscriptions are also linked to this EventConnections instance 
// (e.g. you can subscribe using lambdas)
Materials.EventEndReload.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 the handler function directly
//////////////////////////////////////////////////////////////////////////////

// subscribe to the EndReload event with a handler function
Materials.EventEndReload.Connect(endreload_event_handler);


// remove subscription to the EndReload event later by the handler function
Materials.EventEndReload.Disconnect(endreload_event_handler);


//////////////////////////////////////////////////////////////////////////////
//   3. Subscribe to an event and unsubscribe later via an EventConnection instance
//////////////////////////////////////////////////////////////////////////////

// define a connection to be used to unsubscribe later
EventConnection endreload_event_connection;

// subscribe to the EndReload event with a lambda handler function and keeping the connection
endreload_event_connection = Materials.EventEndReload.Connect(() => { 
		Log.Message("Handling EndReload event lambda\n");
	}
);

// ...

// you can temporarily disable a particular event connection 
endreload_event_connection.Enabled = false;

// ... perform certain actions

// and enable it back when necessary
endreload_event_connection.Enabled = true;

// ...

// remove the subscription later using the saved connection
endreload_event_connection.Disconnect();

//////////////////////////////////////////////////////////////////////////////
//   4. Ignoring EndReload events when necessary
//////////////////////////////////////////////////////////////////////////////

// you can temporarily disable the event to perform certain actions without triggering it
Materials.EventEndReload.Enabled = false;

// ... actions to be performed

// and enable it back when necessary
Materials.EventEndReload.Enabled = true;

Event EventBeginReload#

The event triggered before reloading all loaded materials (i. e. when ReloadMaterials() is called), if the materials_reload_event console variable is enabled. You can subscribe to events via Connect()  and unsubscribe via Disconnect(). You can also use EventConnection  and EventConnections  classes for convenience (see examples below).
Notice
For more details see the Event Handling article.
The event handler signature is as follows: myhandler()

Usage Example

Source code (C#)
// implement the BeginReload event handler
void beginreload_event_handler()
{
	Log.Message("\Handling BeginReload event\n");
}

//////////////////////////////////////////////////////////////////////////////
//  1. Multiple subscriptions can be linked to an EventConnections instance
//  class that you can use later to remove all these subscriptions at once
//////////////////////////////////////////////////////////////////////////////

// create an instance of the EventConnections class
EventConnections beginreload_event_connections = new EventConnections();


// link to this instance when subscribing to an event (subscription to various events can be linked)
Materials.EventBeginReload.Connect(beginreload_event_connections, beginreload_event_handler);

// other subscriptions are also linked to this EventConnections instance 
// (e.g. you can subscribe using lambdas)
Materials.EventBeginReload.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 the handler function directly
//////////////////////////////////////////////////////////////////////////////

// subscribe to the BeginReload event with a handler function
Materials.EventBeginReload.Connect(beginreload_event_handler);


// remove subscription to the BeginReload event later by the handler function
Materials.EventBeginReload.Disconnect(beginreload_event_handler);


//////////////////////////////////////////////////////////////////////////////
//   3. Subscribe to an event and unsubscribe later via an EventConnection instance
//////////////////////////////////////////////////////////////////////////////

// define a connection to be used to unsubscribe later
EventConnection beginreload_event_connection;

// subscribe to the BeginReload event with a lambda handler function and keeping the connection
beginreload_event_connection = Materials.EventBeginReload.Connect(() => { 
		Log.Message("Handling BeginReload event lambda\n");
	}
);

// ...

// you can temporarily disable a particular event connection 
beginreload_event_connection.Enabled = false;

// ... perform certain actions

// and enable it back when necessary
beginreload_event_connection.Enabled = true;

// ...

// remove the subscription later using the saved connection
beginreload_event_connection.Disconnect();

//////////////////////////////////////////////////////////////////////////////
//   4. Ignoring BeginReload events when necessary
//////////////////////////////////////////////////////////////////////////////

// you can temporarily disable the event to perform certain actions without triggering it
Materials.EventBeginReload.Enabled = false;

// ... actions to be performed

// and enable it back when necessary
Materials.EventBeginReload.Enabled = true;

bool IsShadersCompiling#

The true if asynchronous compilation is being performed; otherwise, false.

Members


Material LoadMaterial ( string 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

  • string path - A path to the material (including its name).

Return value

A loaded material.

bool IsMaterialGUID ( UGUID guid ) #

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.

Material GetMaterial ( int num ) #

Returns the material by its number.

Arguments

  • int num - Material number.

Return value

A material.

Material FindManualMaterial ( string name ) #

Searches for a manual material by the given name.

Arguments

  • string name - A manual material name.

Return value

A manual material instance.

Material FindMaterialByGUID ( UGUID guid ) #

Searches for a material with the given GUID.

Arguments

Return value

A material instance.

Material FindMaterialByFileGUID ( UGUID guid ) #

Searches for a material with the given GUID of a material file.

Arguments

Return value

A material instance.

Material FindMaterialByPath ( string path ) #

Searches for a material stored by the specified path.

Arguments

  • string path - A loading path of the material (including a material's name).

Return value

A material instance.

void SetCachedMaterial ( 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:

Source code
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

  • Material mat - The material to be modified in runtime.

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:

Source code
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 ( string 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:

Source code
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

  • string name - The name of the state to be changed.
  • int value - The target state value.

bool RemoveMaterial ( 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

  • 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 ( Material material, Material new_material ) #

Replaces the material with the given one.

Arguments

  • Material material - A material to be replaced.
  • Material new_material - A replacing material.

Return value

1 if the material is replaced successfully; otherwise, 0.

bool SaveMaterials ( ) #

Saves changes made for all loaded materials.

Return value

true if materials are saved successfully; otherwise, false.

void ReloadMaterials ( ) #

Reloads all loaded materials.

void DestroyShaders ( ) #

Deletes all shaders used for the loaded materials.

void DestroyTextures ( ) #

Deletes all textures used by the loaded materials.

void CreateShaders ( ) #

Creates all shaders for all loaded materials.

void CreateRenderMaterials ( ) #

Creates render materials (internal materials required for rendering). For example, you can create all necessary render materials during initialization to avoid spikes that may occur later.

void CreateShaderCache ( ) #

Creates shader cache for all loaded materials.

void CreateShadersFromCache ( ) #

Compiles the shaders available in the shader cache.

bool IsShadersCompiling ( ) #

Returns a value indicating if asynchronous compilation is being performed.

Return value

true if asynchronous compilation is being performed; otherwise, false.

void FlushShadersCompiling ( ) #

Force-compiles all shaders that are queued for the asynchronous compilation.
Last update: 2024-12-13
Build: ()